Aligning curve start point of multiple curves

   111   0   0
User Avatar
Member
1 posts
Joined: Sept. 2021
Offline
Hello,
I'm working on a custom g-code slicer and one problem I encounter is that I don't know how to align the curve start/end points of vertically stacked closed curves. I want the start points to be close together, to minimize the travel between layer changes and to move the seams to the back of the print. I've attached a simplified example with two curves, one of them rotated. I want to check and rotate each of them so that the start point is the point that has an x-coordinate closest to x=-1. Just for demo purposes here.
I am doing the point sorting in an attribute wrangle which has multiple problems I'm afraid. I hope someone can help me out. I checked these solutions [www.sidefx.com] but they all work with simple shifts, I need to shift each curve differently.

Here's the vex:

for(int c = 0; c < nprimitives(0); ++c) {
    float minX = -1;
    int minIndex = -1;
    float minDistance = 1e30;
    
    int cPoints[] = primpoints(0, c);
    int numPoints = len(cPoints);

    for (int i = 0; i < numPoints; ++i)
    {
        vector P = cPoints[i]; 
        float distance = abs(P.x - minX);
    
        if (distance < minDistance)
        {
            minDistance = distance;
            minIndex = i;
        }
    }
    
    int newIndexArray[] = array(numPoints, 0);
    
    for (int i = 0; i < numPoints; ++i)
    {
        newIndexArray[i] = (minIndex + i) % numPoints;
        // that would break for curves with different point counts:
        setpointattrib(geoself(), "newindex", i + (c * numPoints), newIndexArray[i], "set");
    }
}

I know one problem is mixing absolute point index and index within primpoints but I don't know how to best solve it.

Attachments:
align_curve_starts.hipnc (83.9 KB)

  • Quick Links