Jump to content

Sorting Points That Are Not In Order


Recommended Posts

Hi guys and gals,

I have been sitting with the problem for quite some time and thought it was time to ask some smarter people than myself.

I have a geo that I am wanting to run a curve through the middle of.

What I have managed to do so far is create points running through the center of the geo but the points are not in order. I need them to run in order to create a line out of the points as I am wanting to generate a gradient along the line from point 0 to the last point.

I have attached some screenshots. Any advice would be greatly appreciated!

Thanks :)

01.jpg

02.jpg

Link to comment
Share on other sites

I hope to one day be the proud owner of a... "_Blender Library/Scans/Donut/Sprinkled_Donut_01_C.OBJ"!

Until then you could use this maybe? It acts wonky unless you flatten the points against one axis, but your thingie looks circular enough for it to work.

I tried to copy your geometry and it sorted it alright.

 

sortish.hiplc

Link to comment
Share on other sites

Thank you so much for that hip file! 

I have been through it using the F1 key on each expression to try and understand but it's going over my head a little.

I'm sure I'll understand it in the future but if you have some time and if you wouldn't mind, would it be possible to comment on each line of code to explain what it's doing?

If not I totally understand and this is already awesome! Will just take a while to sink in. Thanks for the help!

Link to comment
Share on other sites

// Relative Neighbourhood Graph

// https://houdininote.tumblr.com/post/162427874300/connecting-points

// This wrangle runs thru all points.
// Current point = point being ran thru
// 'nearpoints' gives you a list of point numbers near the current point

foreach(int pt; nearpoints(0, @P, 1.0, 10)) // For every point 'nearpoints' finds in the given radius..
{
        if(pt == @ptnum) continue; // Skip itself, go to the next iteration 

        vector p = point(0, 'P', pt); // Store position of the near point
        float dist = distance(p, @P); // Store distance (mydist) from current point to near point      

        // Building 2 new arrays...
        // We will search for any points within the radii (mydist) of both the current and near points
        // to check if they are closer to each other.

        int ptsA[] = nearpoints(0, @P, dist); // Look for any more points around the current point within mydist...
        int ptsB[] = nearpoints(0, p, dist); // Then, look for any more points around its near point within mydist.

        int chk = 0;  // Initialize a flag

        foreach(int cpt; ptsB) // For every point (cpt) found in our 'ptsB' array..
        {

                if(cpt == @ptnum || cpt == pt) continue; // if cpt is the current point or near point, skip it.

                chk = removevalue(ptsA, cpt); // Remove cpt from our 'ptsB' array. If successful, set our flag to TRUE.
                if(chk) break; // If we successfuly removed cpt, break out of this nested loop.
        } 

        // If our list 'ptsA' is empty, lets draw a line between the current point and the near point.
        // If there were points left in the list, no line will be drawn.
  
        if(!chk) // If we failed to remove a value previously...
        {
                int pr = addprim(0, 'polyline'); // create a new empty polyline
                addvertex(0, pr, @ptnum);  // Add a new vertex to that polyline at the position of the current point
                addvertex(0, pr, pt); // Add another vertex to that polyline at the position of the near point
        }
}

Im not the original author of this code. Its an example I pointed to in that link.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...