Group nearby points with VEX

   1346   4   1
User Avatar
Member
69 posts
Joined: 9月 2018
Offline
I'm trying to group nearby points into different groups using VEX.

I have an attribcreate (i) initialized to 1 plugged into a point wrangle with the following code:

float dist = ch("dist");
int nearestPoints[] = nearpoints(0, point(0, "P", @ptnum), dist);

string groupName = concat(group_, itoa(i@i));

foreach (int point; nearestPoints) {
    if(point(0, "grouped", point) != 1)
    {
        setpointgroup(0, groupName, point, 1, "set");
        setpointattrib(0, "grouped", point, 1, "set");
    }
}

if(len(nearestPoints) > 0)
{
    nearestPoints = int[](array());
    i@i++;
}

Why doesn't this work?

My behaviour: All points gets the “grouped” attibute set to 1 and gets added to the group “group_1”, and “i” only gets to 2.
Edited by albinioni - 2020年6月22日 06:54:45
User Avatar
Member
69 posts
Joined: 9月 2018
Offline
I was able to get closer to what I want to achieve by by wrapping all the points of the geometry into a foreach. Contrary to my belief, it seems like a point wrangle doesn't work as a for each loop from the start?

Can someone then please educate me on the behaviour of a point wrangle? Does every line of code loop over every point before jumping onto the next one?

What I changed my code to:

vector allPos[];
resize(allPos, npoints(0));

for(int i = 0; i < npoints(0); i++) {
    allPos[i] = point(0, "P", i);
}

float dist = ch("dist");

foreach(vector p; allPos)
{
    int nearestPoints[] = nearpoints(0, p, dist);
    string groupName = concat(group_, itoa(i@i));
    
    foreach (int point; nearestPoints) {
        if(point(0, "grouped", point) != 1) {
          setpointgroup(0, groupName, point, 1, "set");
          setpointattrib(0, "grouped", point, 1, "set");
        }
    }

    if(len(nearestPoints) > 1) {
    nearestPoints = int[](array());
    i@i++;
    }
}
Edited by albinioni - 2020年6月22日 10:33:56
User Avatar
Member
670 posts
Joined: 9月 2013
Offline
Just a post a file please. Saves time = Makes more people help you.
https://procegen.konstantinmagnus.de/ [procegen.konstantinmagnus.de]
User Avatar
Member
69 posts
Joined: 9月 2018
Offline
Here is the file
Edited by albinioni - 2020年6月22日 08:11:16

Attachments:
Group points with Vex.hipnc (82.3 KB)

User Avatar
Member
24 posts
Joined: 1月 2018
Offline
Here's what I did with your original code.
Hopefully that helps.

Attachments:
Group points with Vex_Edited.hipnc (87.0 KB)

  • Quick Links