Skip to content

Fix vertical slice artifacts in mesh voxelization#323

Open
meet-brad-ch wants to merge 1 commit intoProjectPhysX:masterfrom
meet-brad-ch:fix-verticial-slice-arteract
Open

Fix vertical slice artifacts in mesh voxelization#323
meet-brad-ch wants to merge 1 commit intoProjectPhysX:masterfrom
meet-brad-ch:fix-verticial-slice-arteract

Conversation

@meet-brad-ch
Copy link
Copy Markdown

Fix: STL Voxelization Artifacts (Vertical Slices)

Problem

When voxelizing STL meshes with regular grid structures (e.g., terrain generated from height maps), vertical slice artifacts appear in the voxelized output. These manifest as missing voxels forming vertical gaps through the geometry.

Root Cause

The voxelize_mesh kernel uses ray-triangle intersection (Moeller-Trumbore algorithm) to determine if voxels are inside or outside the mesh. The algorithm becomes numerically unstable when rays align exactly with mesh vertices or edges.

For meshes with regular grid topology (common in terrain/heightmap data), ray origins often fall exactly on vertex planes, causing:

  • Undefined or inconsistent intersection results
  • Missed intersections due to floating-point edge cases
  • Visible vertical slices in the final voxelization

Solution

Add a small jitter (0.1% of voxel size) to ray origins perpendicular to the ray direction. This ensures rays never start exactly on mesh vertex planes while maintaining voxelization accuracy.

Code Change

File: src/kernel.cpp (in voxelize_mesh kernel)

// Before
const float3 r_origin = position(xyz)+offset;

// After
float3 r_origin = position(xyz)+offset;

const float jitter = 0.001f; // 0.1% of voxel size
r_origin = r_origin + (float3)(jitter*(float)(direction!=0u),
                            jitter*(float)(direction!=1u),
                            jitter*(float)(direction!=2u));

How It Works

  1. Jitter magnitude: 0.001 (0.1% of voxel size) is small enough to not affect accuracy but large enough to avoid exact vertex alignment
  2. Direction-aware offset: Jitter is applied only perpendicular to the ray direction using (direction!=N) masks
  3. Result: Ray-triangle intersections produce stable, well-defined results

Testing

Tested with hill.stl terrain mesh (421 Y-aligned planes, regular grid structure):

  • Before: Visible vertical slice artifacts
  • After: Clean voxelization without gaps

Notes

  • This fix addresses the coplanarity problem inherent in ray-mesh intersection algorithms
  • The 0.1% offset is imperceptible in the final simulation
  • Alternative approach: Use SDF (Signed Distance Field) files which bypass ray-tracing entirely

Add small jitter (0.1%) to ray origins to avoid coplanarity
@hweifluids
Copy link
Copy Markdown

I also faced this problem. Thank you for your solution, Brad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants