Recursion In Vex

Recursion In Vex

in

So many times I’ve googled “VEX recursive function” that even chrome added a bookmark to my homepage. I’ve always ended up on an odforce page that master edward advices to use stack based workflow.

Until recently I wasn’t sure how can I handle it but decided to give it a go for a project I was working on about polygon folding –more on that later-. The project included creating a spanning tree and I was in need of a lot of recursive functions to find children and parents of primitives in that tree.

I will now show you one example regarding finding the children primitives (in connected sense) of a primitive but the ideas apply to other areas as well.


Take a look at the image below. You will see that every primitive is connected by one point. And each point has a maximum of two primitives.

Every primitive is connected by one point Finding the connected primitives to a primitive should be trivial. You can easily find the primitive list a point has with:

pointprims(geometry, ptnum); 

And the points a primitive has with:

primpoints(geometry, primnum); 

But when you try to find the prim list a point has, and the point lists of that primitives, and the primitive lists of said points and so on and so on.. Things get complicated easily.

What you need is an algorithm that helps you track the connected primitives of a point and stack them in a separate place to visit them later. This is what is called stack based recursion. Actually what you do is not so different then a C++ code in the sense that, instead of compiler, you are responsible for creating a stack and adding or removing elements to it.

Now take a look at the code below. I will try to walk you through it step by step.

i[]@children; 

int stack[]; // Our own stack!!

push(stack, @primnum); 

while(len(stack) > 0){
    
    int last = pop(stack); 
    
    int pts[] = primpoints(0, last);
    int last_pt = pop(pts); 
 
    int n_pts = neighbourcount(0,last_pt);      
    if(n_pts>2){
        int prims[] = pointprims(0, last_pt);
        foreach(int prim; prims){
            if(prim != last){   
                push(stack, prim);
                push(i[]@children, prim); 
            } 
        }
    }
} 

The first thing we do is create an array to hold whatever we want. We can fill it with points numbers, primitive offsets, attributes etc. For now we will just fill it with connected primitives.

Then we create our own stack to process elements. First thing we should do is to put the first element to be processed. Adding @primnum puts the primitive we are working on as the first element.

We use a while loop to process elements one by one. Inside the loop first thing we sould do always be to remove the last element in the stack (pop(array[]) function removes the last). But don’t just pop it and store it in “int last” variable, it will come handy later.

Then we find the points of the last variable (for primitive “0” this is now 0 and our stack is empty). And we will need the points of this primitive.

A big assumption here is that every primitive’s last point is the one that it connects to other primitives. That’s why we use the last point in our primitive’s point list (int last_pt = pop(pts)). We will use this point to look up it primitives list.

A good check always is to use a little bit of logic. If a point has less than 3 neighbours we probably don’t need to process it because it is not a branching point.

Now that we have the primitive list of the last point, we can query its primitives and if it is not equal to the primitive we currently processing (“last” variable now comes handy), we add it to our stack and also to our children list.

What you get is a beautiful children array!

This completes this tutorial on recursive functions in VEX. If you would like a challenge you may wish to do it on points instead of primitives (hint: it requires you to hold a global “last_point” variable).

If you would like to know more you can visit forums on sidefx.com and odforce. I wasn’t able to find an example on the subject, and decided to make one. I hope you find this tutorial useful.

A file is worth thousand words!!! If you would like to dive into an example directly you can purchase the Houdini file below.

Vex Recursion on

Thanks for reading! Have a great day.