Hello all, I'm starting out with VEX been at it for about 2 weeks.
I've been trying to organize/group my point cloud by assigning a new attribute to each point (mesh_index of int type) based on an existing attribute in my points (mesh_name of string type).
--------------------------------
Here's my code :
string uniqueNames() *square brackets are being hidden by the forum, not sure why;
int total_points = npoints(0);
for (int i = 0; i < total_points; i++)
{
string currentString = point(0, "mesh_name", i);
int index = find(uniqueNames, currentString);
if (index == -1)
{
append(uniqueNames, currentString);
int index = len(uniqueNames) - 1;
setpointattrib(0, "mesh_index", i, index, "set");
} else {
setpointattrib(0, "mesh_index", i, index, "set");
}
}
int arraylen = len(uniqueNames);
i@arraylength = arraylen;
In my test scene, I have 10 points with 3 unique mesh_names.
My end goal is to get each point assigned with a relevant mesh_index based on the local mesh_name array that's being generated when I iterate over each point. I'm perplexed as to why my array length is not at the expected value of 3, and my points' mesh_index value are wrong except for pt 0 & 1.
I hope someone can help me understand what I'm doing wrong here, I've been reading the code over and over and can't tell why it's not giving me the expected results.
VEX for loop logic
878 2 2-
- LearningPointCloud
- Member
- 7 posts
- Joined: July 2024
- Offline
-
- RGaal
- Member
- 143 posts
- Joined: June 2024
- Offline
For a practical solution to this problem, you should use the enumerate node and enable chunk numbering. Then you will immediately get indexes 0,1,2 for 3 names.
As for your code - 1) you have syntax errors, fix them, they are written in the information panel. Duplicate variable declarations. A string variable must be initialized = "".
2) The main problem is that you misunderstood how the find function works and assumed that it returns -1 in case of failure. The help says that it returns a negative number, i.e. any negative. Therefore, the condition should be "<0", and not "==-1".
3) The meaning of the lines "append(uniqueNames, currentString); index = len(uniqueNames) - 1;" eludes me.
You take the first point of the first group, there is no match for it and assign it a word length number (len(uniqueNames) - 1), for example 7 (I don't remember what names you had). The second point of the same group - there is a match, so you assign index = find(uniqueNames, currentString); which is 0 (this is the number of the first character of the match), all the following points will get 0. For the second group, the situation is repeated - the first point of the second group will get the number of the end of the word, the other points - the number of the beginning of the word.
The algorithm is strange and meaningless in my opinion.
As for your code - 1) you have syntax errors, fix them, they are written in the information panel. Duplicate variable declarations. A string variable must be initialized = "".
2) The main problem is that you misunderstood how the find function works and assumed that it returns -1 in case of failure. The help says that it returns a negative number, i.e. any negative. Therefore, the condition should be "<0", and not "==-1".
3) The meaning of the lines "append(uniqueNames, currentString); index = len(uniqueNames) - 1;" eludes me.
You take the first point of the first group, there is no match for it and assign it a word length number (len(uniqueNames) - 1), for example 7 (I don't remember what names you had). The second point of the same group - there is a match, so you assign index = find(uniqueNames, currentString); which is 0 (this is the number of the first character of the match), all the following points will get 0. For the second group, the situation is repeated - the first point of the second group will get the number of the end of the word, the other points - the number of the beginning of the word.
The algorithm is strange and meaningless in my opinion.
-
- LearningPointCloud
- Member
- 7 posts
- Joined: July 2024
- Offline
RGaal
For a practical solution to this problem, you should use the enumerate node and enable chunk numbering. Then you will immediately get indexes 0,1,2 for 3 names.
As for your code - 1) you have syntax errors, fix them, they are written in the information panel. Duplicate variable declarations. A string variable must be initialized = "".
2) The main problem is that you misunderstood how the find function works and assumed that it returns -1 in case of failure. The help says that it returns a negative number, i.e. any negative. Therefore, the condition should be "<0", and not "==-1".
3) The meaning of the lines "append(uniqueNames, currentString); index = len(uniqueNames) - 1;" eludes me.
You take the first point of the first group, there is no match for it and assign it a word length number (len(uniqueNames) - 1), for example 7 (I don't remember what names you had). The second point of the same group - there is a match, so you assign index = find(uniqueNames, currentString); which is 0 (this is the number of the first character of the match), all the following points will get 0. For the second group, the situation is repeated - the first point of the second group will get the number of the end of the word, the other points - the number of the beginning of the word.
The algorithm is strange and meaningless in my opinion.
Hey RGaal, thanks for the feedback.
I just tested with the enumerate node, SO MUCH SIMPLER.
---------------------------------------------------------
As for your feedback on my code;
1)I opened my file again and looked at the vex code and I don't see any errors showing up. Not sure why in the screenshot attached it shows a yellow error symbol...
but the results i'm getting is still the same as my initial post.
2)I will admit I'm using chatGPT to help explain to me and suggest code. I've read the vex find function documentation online, and I don't quite understand what it's saying as syntax is still quite difficult to understand for me.
I see there's 3 types of find function for arrays...one returns an empty array (means 0?), the other a negative number (why any negative number? or how does it decide if its -2 vs -99?), and lastly if its a "scalar version" it returns -1?
What does scalar version of an array mean?
I can understand -1, since 0 is a valid index number it makes sense to me to return -1.
So I assumed if the find function returned negative, then it would be -1 which I used as an if condition to append a new entry into my local array - string uniqueNames
3)This part -
if (index == -1)
{
append(uniqueNames, currentString);
index = len(uniqueNames) - 1;
is for when the condition is met (i.e. no matching entry was found in the array uniqueNames) append the current string value to the array. Then find the new index immediately after adding a new entry which should return me with the new length of the array - 1.
That's the logic I had going on.
Am I understanding the append array function wrongly? I thought because I declared it as a string array, new string entries that get added would be like adding a new index per string entry.
e.g.
string uniqueNames() *square brackets are being hidden by the forum..; (to initialize an empty array)
new entry 1 - Stanag.mag (this becomes index 0 of my array)
new entry 2 - handle (this becomes index 1 of my array)
new entry 2 - Acog.scope (this becomes index 2 of my array)
// even though I have my solution, that is to use the Enum node. I would still want to solve this if possible so I can learn from my mistakes and get better at VEX.
Edited by LearningPointCloud - Aug. 7, 2024 10:46:22
-
- Quick Links