Skip to content

Instantly share code, notes, and snippets.

@vvzen
Last active September 12, 2020 21:14
Show Gist options
  • Save vvzen/b3f24ec2736843cfb003daa25c6fd803 to your computer and use it in GitHub Desktop.
Save vvzen/b3f24ec2736843cfb003daa25c6fd803 to your computer and use it in GitHub Desktop.
VEX code for finding centroids of clusters made with Houdini cluster node
// The cluster node in houdini creates 2 groups:
// attributepoints and clusterpoints
// but I need an attribute to store, for each cluster, the centroid
// So what I've come up is to...
// 1. Create an attribute on each point, called centroid
// and formatted like this: "{index_of_the_point}_{index_of_the_cluster}"
// See set_centroid_attribute.cpp
// 2. Use it so that each point will loop in all other points
// until he finds a centroid attribute the other point that is not 1_1
// and where the {index_of_the_cluster} matches its own cluster
// See the while loop below
// Split the centroid attribute so that we know what we're looking for
string splitted[] = split(s@centroid, "_");
int point_index = atoi(splitted[0]);
int cluster_index = atoi(splitted[1]);
// Only if the current point is not the average point
// (this could also be done with the inpointgroup)
if (point_index == 1 && cluster_index == 1){
int p = 0;
int maximum_iter = @numpt;
while (p < maximum_iter){
// Skip this point
if (p == @ptnum){
p++;
continue;
}
// Split the centroid attr on the other point
int s;
string centroid_attr = pointattrib(0, "centroid", p, s);
string other_splitted[] = split(centroid_attr, "_");
int other_point_index = atoi(other_splitted[0]);
int other_cluster_index = atoi(other_splitted[1]);
// Look for the point with a similar cluster
if (other_point_index != 1 && other_cluster_index == @cluster){
int success;
vector centroid_pos = pointattrib(0, "P", other_point_index, success);
v@centroid_pos = centroid_pos;
// For debugging, use the normals to visualize if things are correct
@N = normalize(@P - centroid_pos);
break;
}
p++;
}
}
if (inpointgroup(0, "averagepoints", @ptnum)){
s@centroid = sprintf("%s_%s", i@ptnum, i@cluster);
}
else {
s@centroid = "1_1";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment