Point Clouds
Houdini point cloud operations are very efficient ways of processing
data associated with points in space. These operations take advantage of, I imagine, algorithms such as k-d
trees to increase speed. For example, a nearest neighbour search using a k-d tree is an O(log N),
rather than O(N^2) as you would expect from a brute force approach.
pcopen()
Open a point cloud from geometry connected to input 1.
float mxrad = 1000;
int mxpts = 10;
int pch = pcopen(1, 'P', @P, mxrad, mxpts);
float psep;
int pnum;
vector r;
while (pciterate(pch))
{
pcimport(pch,'point.distance',&psep);
pcimport(pch,'point.number',&pnum);
pcimport(pch,'P',r);
}
Note that pcopen() is used in conjunction with pciterate() and pcimport(), as illustrated above.
nearpoint() and nearpoints()
These two functions are the simplest way to access the power of point clouds. These open a point cloud
from geometry connected to input 1.
int pt = nearpoint(1,@P);
vector r = point(1, 'P', pt);
float mxrad = 1000.0;
int pts[] = nearpoints(1,@P,mxrad);
vector r;
foreach( int pt; pts)
{
r = point(1,'P',pt);
}
Miscellaneous
An unstructured gathering of useful snippets of VEX code.
random orthogonal vectors
#include <math.h>
vector nml = normalize(@N);
vector v1 = set(nml.y,-nml.x,0);
vector v2 = cross(nml,v1);
v1 = normalize(v1);
v2 = normalize(v2);
float theta = fit01(rand(@ptnum), 0, 2*PI);
vector vo = cos(theta)*v1 + sin(theta)*v2;
Note that the first orthogonal vector is the cross product of the vector with [0,0,1].
Therefore, this snippet will fail if nml is colinear with [0,0,1]. In this case,
choose another vector to construct the first cross product.
Instancing custom attributes to geometry
While certain standard attributes are instanced automatically (pscale, orient, ...), custom
attributes must be transferred to the instanced geometry explicitly. Create a "For-Each Point"
loop with a "copy to points" node inside, and then on the instanced geometry, use the
following expression:
point("../foreach_begin1", 0, "custom_attribute", 0)
The first argument is the path to the For-Each begin node, the second is the point number
(which is always zero), the third the the attribute name, and the fourth is the index number.
VEX toggle expression
Instead of key framing a variable through an on-off cycle, here is code where you simply enter the
toggle frames.
int initialState = 0;
int toggles[] = {10,20,30,40,50};
int toggle, interval = 0;
foreach (int tog; toggles)
{
if (@Frame < tog) break;
interval += 1;
}
toggle = (interval + initialState) % 2;
Set the 'initialState' variable to zero or one, and enter the toggle frames into the
array 'toggles'. The toggle frames must be in ascending order.