Get the highes prim number with the same attribute value

   704   2   0
User Avatar
Member
3 posts
Joined: Dec. 2016
Offline
I wanna get the largest primitive number which has the same attribute value as the "first one" (in this case this attribute is the 'class'). I came up with this snippet which kinda works, but it has two major issues:
Somehow it offset the results and the values by 1
It works on a small scale (around 1000-2000 primitives), but with 100,000 prim it always crashes Houdini

//loop until got the largest primnumber with the same class attrib
void performLoop(int class_attrib; int current_prim; int target_class; int result_array[]; int max_value) {
    int new_class_attrib = 0;
    do {
        current_prim += 1;
        push(result_array, current_prim);
        new_class_attrib = prim(0, 'class', current_prim);
    } while (new_class_attrib == target_class);
    max_value = current_prim;
}

int target_class = 0;
int class_attrib = 0;

for (int prim = 0; prim < @numprim; ++prim){
    class_attrib = prim(0, 'class', prim);

    if (class_attrib == target_class) {
        int result_array[] = {};
        int max_value = 0;
        performLoop(class_attrib, prim, target_class, result_array, max_value);
        foreach (int primnums; result_array){
            setattrib(geoself(), 'prim', 'max_value', primnums, 0, max_value, 'set');
        }
        prim = max_value;
    }
    else {
        target_class += 1;
        int max_value = 0;
        int result_array[] = {};
        performLoop(class_attrib, prim, target_class, result_array, max_value);
        foreach (int primnums; result_array){
            setattrib(geoself(), 'prim', 'max_value', primnums, 0, max_value, 'set');
        }
        prim = max_value;
    }
}

Attachments:
get_largest_value.png (33.6 KB)

User Avatar
Member
480 posts
Joined: July 2005
Offline
Here is one way,
if you have an int attribute you can sort by this attribute -> extract all classes -> create dummy points for each class -> foreach class/point put all prim numbers in an array -> sort all arrays -> get highest value.

Attachments:
get_highest_prim_number_for_target_int_attr.hipnc (116.4 KB)

User Avatar
Member
8599 posts
Joined: July 2007
Offline
you should be able to do it like this in a single prim wrangle:
int prims[] = findattribval(0, "prim", "class", i@class);
i@largest_primnum_in_class = prims[-1];
Edited by tamte - June 18, 2023 17:38:09
Tomas Slancik
FX Supervisor
Method Studios, NY
  • Quick Links