Ben Traje
← Back to houdini

Houdini VEX: Deleting Points Inside Another Object's Radius

31 Mar 26 (Today)

The Problem: You have two sets of scattered points—for example, leaves (Input 0) and rocks (Input 1). You want to draw a radius around each rock point, and if a leaf point falls inside that radius, you want to delete it so they don't instantiate inside the rocks.

The Solution: To achieve this efficiently in SideFX Houdini, you should use the pcfind function in a Point Wrangle. Instead of using a heavy, nested loop to compare every point against every other point, pcfind uses a spatial accelerator (a point cloud) to only look for points within your specified radius.

The VEX Code (Point Wrangle)

Connect your leaf points to the first input (Input 0) and your rock points to the second input (Input 1).

// 1. Define the radius of the "exclusion zone" around each rock
float radius = chf("rock_radius"); 

// 2. Search the second input (Input 1) for rock points near the current leaf.
// The '1' at the end tells it to stop searching after finding just 1 point.
int close_rocks[] = pcfind(1, "P", @P, radius, 1);

// 3. If the array found anything, the leaf is inside a rock's radius. Delete it.
if (len(close_rocks) > 0) {
    removepoint(0, @ptnum);
}

Note: Click the "Spare Parameters" icon next to the VEX editor to generate the rock_radius slider.

Advanced: Using Variable Radius (@pscale)

If your rocks aren't all the same size and you want the "kill zone" to match a specific @pscale attribute on the rocks, you can iterate over the found points and check their exact scale:

float search_radius = chf("max_search_radius"); // Set this large enough to catch your biggest rocks
int close_rocks[] = pcfind(1, "P", @P, search_radius, 1);

foreach (int rock_pt; close_rocks) {
    float r_scale = point(1, "pscale", rock_pt);
    
    // Check if the distance is smaller than the specific rock's pscale
    if (distance(@P, point(1, "P", rock_pt)) < r_scale) {
        removepoint(0, @ptnum);
        break; // Stop checking other rocks once deleted
    }
}