Resampling curves using VEX part 2: Maximum segment length

Continuing from the previous post, I'll show you how to turn the resample script into one that uses maximum segment length instead of constant segment count. This means we end up with a curve with an adaptive segment count based on the curve's length. To do this, we need to measure the length of the curves first. This can be done using the Measure SOP or a script like this: Measure Hair Length (VEX). I like to work with scripts as much as possible, so I can combine some processes when optimizing.

To know the value to be used as the resample count, we need to divide the input curve length by the desired maximum segment length. This will likely result in a decimal value, and we need an integer value for accurate resample. If we round this decimal value to an integer, the result will be a value that is the closest approximate resample segment count that fits our rule of maximum segment length. Once we have figured out this value, we can use it as the resample count in our script.

Before, using constant segment count:

//primitive wrangle

int count = chi("count");

int new_prim = addprim(0,"polyline"); 
vector uv = {0,0};

for ( int i=0; i<=count; i++ ){

    uv.x = i/float(count);

    vector pos = primuv(0,"P",i@primnum,uv);
    int new_pnt = addpoint(0,pos);
    
    addvertex(0,new_prim,new_pnt);
}
removeprim(0,i@primnum,1);

After, using maximum segment length:

//primitive wrangle

float max_seg_length = chf("maximum_segment_length");

float length = f@length;
float count = rint(length/max_seg_length);

int new_prim = addprim(0,"polyline");
vector uv = {0,0};

for ( int i=0; i<=count+1; i++ ){
    
    uv.x = i/count;

    vector pos = primuv(0,"P",i@primnum,uv);

    int new_pnt = addpoint(0,pos);
    addvertex(0,new_prim,new_pnt);
}
removeprim(0,i@primnum,1);

Again, same as last time. When things are built into a script, it's easy to randomize the results. We can randomize the resample count by primitive like this:

//primitive wrangle

float rand = rand(i@primnum);
rand = fit01(rand,chf("rand_min"),chf("rand_max"));

float max_segs = chf("maximum_segment_length");
max_segs *= rand;

float length = f@length;
float count = rint(length/max_segs);

int new_prim = addprim(0,"polyline");
vector uv = {0,0};

for ( int i=0; i<=count+1; i++ ){
    
    uv.x = i/count;

    vector pos = primuv(0,"P",i@primnum,uv);

    int new_pnt = addpoint(0,pos);
    addvertex(0,new_prim,new_pnt);
}
removeprim(0,i@primnum,1);
Previous
Previous

Resampling curves using VEX part 3: Adaptive root to tip

Next
Next

Resampling curves using VEX part 1: Constant segment count