VEXercise 4 – Breaking up a curve

This is part of a series of small exercises and experiments I am doing in Houdinis VEX language. There might be better ways to solve this and if I discover a better way I will update the post.

While building a city in Houidini I needed to place some electrical wires along the roads. In order for the wire placements to look a bit more chaotic I needed to break up the curve into smaller sections.

This is done by looping through the curve vertices and create a new prim (poly line) with a random ratio. If a prim only have 1 vertice then it is deleted. All vertices are deleted from the curve and new ones created for the new curves in the process.

//break the primitives into shorter lines
//each prim will only contain 2 vertices (option)  limitVerts

for (int i = 0; i<@numprim; i++) {
    int numVerticies = primvertexcount(0, i);
    int vtxPrim = i;
    int lastPrimVtxCount = 0;
    int lastPointNum = 0;
    for (int j = 0; j <numVerticies; j++) {
        //store vertex point & prim
        int vtxNum = primvertices(0, i)[j];
        int vtxPoint = vertexpoint(0, vtxNum);
          
        //remove the vertex
        removevertex(0, vtxNum);

        //check if we should start a new prim
        if(random(vtxNum) > ch('../breakRatio')) {
            vtxPrim = addprim(0, "polyline");
            if(lastPrimVtxCount == 1) removepoint(0, lastPointNum); // if the prim only has 1 vert then delete the point
            lastPrimVtxCount = 0;
        }
        
        //create new vertex for current prim with current point     
        addvertex(0, vtxPrim, vtxPoint);
        lastPointNum = vtxPoint;
        lastPrimVtxCount += 1;
    }
}

I added a second piece of code that breaks up each segment into a separate primitive, this has nothing to do with the first piece as such but the electrical wires was being exported to Unreal and I need to limit the size of each actor created. Anyway, here’s the code for that part.

//loopp through verts and break up the shit
if(ch('../limitVerts') == 1) {
    int numVerts = primvertexcount(0, @primnum);
    if(numVerts > 2) {
        for(int i = 2; i<numVerts; i++) {
            //store vertex points
            int vtxNum1 = primvertices(0, @primnum)[i];
            int vtxPoint1 = vertexpoint(0, vtxNum1);       
            int vtxNum2 = primvertices(0, @primnum)[i-1];
            int vtxPoint2 = vertexpoint(0, vtxNum2);
            
            //create a new prim and add
            int newPrim = addprim(0, "polyline");
            addvertex(0, newPrim, vtxPoint1);
            addvertex(0, newPrim, vtxPoint2);
        }
        
        for(int i = 2; i<numVerts; i++) {
            //delete the old extra vertices in a separate loop
            int vtxNum = primvertices(0, @primnum)[i];
            removevertex(0, vtxNum);
       }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *