Grasshopper

algorithmic modeling for Rhino

Hey,

I am trying to write a simple script to remove duplicated polylines.

the polylines are closed edges of trangular surfaces. each surface has more than 2 duplicates. what i am trying to do is to find the contre of each polyline. if the centres are the same, polyline will be removed from the list.

Here is my script:

    Dim i As Integer
    Dim j As Integer

    Dim threshold As Integer:threshold = 1

    Dim cen As New Point3d
    Dim pLine As New Polyline


    Dim pList As New list(Of Polyline):pList = x
 
    
    Dim cenT As New Point3d
    Dim pLineT As New polyline



    For i = 0 To x.Count - 1
      pLine = x(i)
      cen = pLine.CenterPoint
 
      For j = 0 To x.Count - 1

        If Not j = i Then
          pLineT = x(j)
          cenT = pLineT.CenterPoint

          If cen.DistanceTo(cenT) <= threshold Then

            pList.removeat(j)

          End If

        End If

      Next

    Next

    A = indexList

I think I have found the problem which is: duplicated polylines are being removed more than one time. but I can't find the right syntax to correct it....

Help!!!!

many thanks in advance!!

Kwo

Views: 1055

Replies to This Discussion

hey guys,

i think i have solved the problem by myself. In stead of removing objects from a list, now i just create a new empty list to add non-repeated objects in.

cheers

Hi Kwo,

if you're removing items from a collection, you should iterate over it backwards. If you iterate forwards you have to adjust your iteration variable on every remove. There were other weird things in your code, a lot of pointless New keywords. Below is how I would write it.

Dim tolerance As Double = 1.0

'First collect all center points.

Dim c As New List(Of Point3d)

For Each polyline As Polyline in x

  c.Add(polyline.CenterPoint())

Next

For i As Int32 = x.Count - 1 To 1 Step -1

  For j As Int32 = 0 To i-1

    If (c(i).DistanceTo(c(j)) < tolerance) Then

      x.RemoveAt(i)

      c.RemoveAt(i)

      Exit For

    End If

  Next

Next

A = x

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David,

well..... i have to say, this is really cool. much more simplified. best solution is always simplest.


thanks a lot! really appreciate it!

Best,

Kwo

using a center point tolerance is not going to work all the time. What if you have two polylines whose center points are in the same place? i.e. if they are crossing one another. You pribably want to use a boundary box inclusion test for this

 

 

hey Steve, can you be more specific about boundary box inclusion test?

It all depends on whether you want to find identical polylines, identical polylines within a tolerance x or polylines that have similar shape.

You can test all sorts of properties depending on which of the above cases you're working with:

  • Is the point count the same for both
  • Are they both closed or open
  • Do they have the same center point (within tolerance)
  • Do they have the same bounding box (within tolerance)
  • Is the distance between respective vertices zero (or less than the tolerance)
  • Is the distance between the vertices in each polyline to the segments of the other polyline zero (or less than the tolerance)

These cases are sorted by performance, but only the last one will allow you to find similar looking polylines.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

See this script which implements all of the above comparisons. I hope it opens up correctly on older versions though.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Attachments:

sorry for late reply, but thanks lot David! It is super helpful!

Attached is my GH definition which is a geodesic Dome solution.
What I did was to create geodesic dome from an icosahedron. once the icosahedron is created, all the vertices remain undifferentiated. If I draw polyline between each vertice and its closest two vertices, all plygons will be drawn twice.

and this is when removing duplicate polylines starts to work.

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service