vesaw55345 Posted April 20, 2024 Share Posted April 20, 2024 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 Quote Link to comment Share on other sites More sharing options...
Aizatulin Posted April 20, 2024 Share Posted April 20, 2024 (edited) 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 April 20, 2024 by Aizatulin Quote Link to comment Share on other sites More sharing options...
vesaw55345 Posted April 20, 2024 Author Share Posted April 20, 2024 Thanks a lot. I just checked it - however, it seems to break if I feed curves with different point counts. For instance, if I resample one of the circles it doesn't line up anymore. Quote Link to comment Share on other sites More sharing options...
Aizatulin Posted April 20, 2024 Share Posted April 20, 2024 (edited) If the point number per prim is different, you can workaround it by resampling all primitives with same count. After this you can capture each point from the original curve using xyzdist() and reorder the vertices based on their u-param. It may be a bit less accurate. align_curve_starts_modA.hipnc Edited April 20, 2024 by Aizatulin Quote Link to comment Share on other sites More sharing options...
Alain2131 Posted April 20, 2024 Share Posted April 20, 2024 Could you use the Sort by Proximity feature in the Sort node ? This "breaks" the order, but gives a starting point for each curves, which can be leveraged with some logic. Here's my take on it align_curve_starts.hipnc Quote Link to comment Share on other sites More sharing options...
Aizatulin Posted April 20, 2024 Share Posted April 20, 2024 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). Quote Link to comment Share on other sites More sharing options...
vesaw55345 Posted April 22, 2024 Author Share Posted April 22, 2024 Thanks so much to both of you @Aizatulin & @Alain2131. The last solution works best for my scenario. I did run into some issues with the winding order part once I put it in my bigger project, so I'm using the prim_normal now to check for that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.