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.
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);
}
// 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);
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.