Jsmidlow Posted August 8, 2021 Share Posted August 8, 2021 Hey hey. I'm trying to do a very simple thing - connect opposite points within vex... Normalas are given. I've tried with dot product but failed((( Any advices? Quote Link to comment Share on other sites More sharing options...
Alain2131 Posted August 8, 2021 Share Posted August 8, 2021 (edited) Hello, In a Point Wrangle, make a for loop going over every points, and compute the direction it has with the "current one". Points that faces each other will match the Normal, and others won't. So, if some point match with the normal of the current point, then you know you need to connect those points together. As for the connecting, we can't really connect all points when they found a match, because you'd have two lines for each pair. What I propose is to only make the connection when the match has an id higher than the current point. So, say the match is 9 and 15. 9 matches with 15, 9 is lower than 15, so a line is created. 15 also match with 9, but 15 is higher than 9, so we don't connect. (In the example below, I dodge this issue altogether by not including points with a lower id than the current point in the loop) Here's untested code, to "visually" explain what I mean. This should go in an Attribute Wrangle, running over Points. // We loop over the other points. // We start at the current point +1, // so we know we will never have "double-match" for(int i=@ptnum+1, i<npoints(0); i++) { vector P1 = point(0, "P", i); // Get the position of the other point vector dir = normalize(P1 - @P); // Get the direction of the other point relative to the current one if(dir == v@N) // If the direction match with the normal { addprim(0, "polyline", @ptnum, i); // Add the polyline break; // Since we have a match, we know that we need to stop looking for other points. } } That's the idea anyways. Maybe the computed "dir" won't match perfectly with the Normal, and you'll need some threshold to compare dir and N (if equal within a margin of 0.001 or whatever). A quick way to do that is abs( a - b ) <= threshold. (I think that should work for vectors, but it might not be the ideal way to do it. Although it should be fine for your case) EDIT : I quickly recreated the scene, and it seems to work I had to use the "almost equal" trick, because it had connected only 2 out of the 9 pairs. Next time, please share a scene file. connect_by_dir.hipnc Edited August 8, 2021 by Alain2131 1 Quote Link to comment Share on other sites More sharing options...
Fenolis Posted August 10, 2021 Share Posted August 10, 2021 (edited) Removed my comment, @Alain2131's is more effective. Edited August 10, 2021 by Fenolis 1 Quote Link to comment Share on other sites More sharing options...
Jsmidlow Posted August 16, 2021 Author Share Posted August 16, 2021 On 09/08/2021 at 12:58 AM, Alain2131 said: Hello, In a Point Wrangle, make a for loop going over every points, and compute the direction it has with the "current one". Points that faces each other will match the Normal, and others won't. So, if some point match with the normal of the current point, then you know you need to connect those points together. As for the connecting, we can't really connect all points when they found a match, because you'd have two lines for each pair. What I propose is to only make the connection when the match has an id higher than the current point. So, say the match is 9 and 15. 9 matches with 15, 9 is lower than 15, so a line is created. 15 also match with 9, but 15 is higher than 9, so we don't connect. (In the example below, I dodge this issue altogether by not including points with a lower id than the current point in the loop) Here's untested code, to "visually" explain what I mean. This should go in an Attribute Wrangle, running over Points. // We loop over the other points. // We start at the current point +1, // so we know we will never have "double-match" for(int i=@ptnum+1, i<npoints(0); i++) { vector P1 = point(0, "P", i); // Get the position of the other point vector dir = normalize(P1 - @P); // Get the direction of the other point relative to the current one if(dir == v@N) // If the direction match with the normal { addprim(0, "polyline", @ptnum, i); // Add the polyline break; // Since we have a match, we know that we need to stop looking for other points. } } That's the idea anyways. Maybe the computed "dir" won't match perfectly with the Normal, and you'll need some threshold to compare dir and N (if equal within a margin of 0.001 or whatever). A quick way to do that is abs( a - b ) <= threshold. (I think that should work for vectors, but it might not be the ideal way to do it. Although it should be fine for your case) EDIT : I quickly recreated the scene, and it seems to work I had to use the "almost equal" trick, because it had connected only 2 out of the 9 pairs. Next time, please share a scene file. connect_by_dir.hipnc Thank you so much!!! It really helped me)) (Sorry for late answer) 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.