Cubic bézier curves in VEX

calendar_today

24.05.2022

label

VEX, Modeling

mouse

17.0

Description

Various methods to create smoothly interpolated curves in VEX.

1 Drawing a spline along four points

A cubic bezier curve linearly interpolates between 4 point positions. The first interpolation between point 0 and 1 and the second between 2 and 3. Then it interpolates between the first and the second interpolation.

Thus resulting in a smooth curve that starts at point 0, ends at point 3 and bends towards point 1 and 2. In this code the linear interpolations are driven by u. A for loop is adding a new point at the calculated positions.

CPP
        int steps = chi('steps');

vector pos_0 = point(1, "P", 0);
vector pos_1 = point(1, "P", 1);
vector pos_2 = point(1, "P", 2);
vector pos_3 = point(1, "P", 3);

int prim = addprim(0, "polyline");

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

    float u = i / float(steps - 1);
    vector pos_a = lerp(pos_0, pos_1, u);
    vector pos_b = lerp(pos_2, pos_3, u);
    vector pos = lerp(pos_a, pos_b, u);

    int pt = addpoint(0, pos);
    addvertex(0, prim, pt);
}
    

2 Bezier function on numberous points

CPP
        // BEZIER FUNCTION
function vector bezier(const vector pos_array[]; const float t){
    vector p[] = pos_array;
    int n = len(pos_array);
    while(n--){
        for(int i = 0; i < n; i++){
            p[i] = p[i] * (1.0 - t) + p[i + 1] * t;
        }
    }
    return p[0];
}

// POSITION ARRAY
int pts[] = expandpointgroup(1, '*');
vector pos[] = {};
resize(pos, len(pts));
foreach(int i; int pt; pts){
    pos[i] = point(1, 'P', pt);
}

// INTERPOLATE
float u = vertexcurveparam(0, i@vtxnum);
v@P = bezier(pos, u);
v@Cd = set(u, 1.0 - u, 0.0);
    

3 spline() VEX function

Houdini also comes with a built-in spline VEX function which offers various interpolation types:
https://www.sidefx.com/docs/houdini/vex/functions/spline.html

See splines_VEX.hip as an example.

download

Downloads

smart_display

Videos

In-depth explanation by Freya Holmér

The Continuity of Splines

link

Related articles

favorite

350

label

VEX

Attribute to match across inputs

favorite

236

label

VEX

Sphere Packing / Dart Throwing Algorithm

favorite

262

label

VEX

Deintersecting spheres with Voronoi

favorite

293

label

VEX

Fitting Planes to Point Clouds

favorite

302

label

VEX

How to analyze VEX code

favorite

142

label

VEX

Monte Carlo Geometry Processing