Jump to content

Aligning curve start point of multiple curves


Recommended Posts

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 solution 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.

align_curve_starts.hipnc

Link to comment
Share on other sites

Hello,

if you have a set of curve (I would prefer always closed ones, so no you don't have repeating points), you can measure the the distance between all point pairs and sum them up and doing this for all offsets (0, 1, 2, ... number of prim). After this there should be an optimal offset (with minimum distant sum). You can do this for all curve pairs and get the relative offset from curve i to curve i-1 etc... .

 

align_curve_starts_mod.hipnc

Edited by Aizatulin
Link to comment
Share on other sites

I would say this will work, if your curves are aligned in a straight way along an axis. If not you can probably extend this using a distance to an object (like a curve or something if you have it already).

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...