First you need a voxel / triangles intersection test.
To achieve this, the natural approach is to re-apply the polygon cropping algorithm (for example, Sutherland-Hodgman in 3D) on a triangle using half-planes of six faces of the cube and check what remains after.
The best approach is described in Graphics Gems III, Triangle-Cube Intersection, pp. 236-239 ( implementation available).
Then you need to list all the voxels intersected by the triangle.
The first possible approach:
This can be optimized in at least several ways:
- The values ββinvolved in the voxel / triangle intersection test can be calculated incrementally in different
for loops. - The range of the last
for loop can be reduced by considering only voxels crossing the supporting plane of the triangle. - The order of the cycles can be changed to prioritize some axis over another (to take into account the orientation of the triangle).
Second possible approach:
- Select one of the vertices of the triangle and find a voxel in it. This voxel will be used as a seed.
- Starting with this seed voxel, do a breadth-first search (BFS) or depth-based search (DFS) of voxels crossing the triangle, i.e.:
- Track which voxels have been tested to intersect with a triangle,
- Process all unverified adjacent voxels of intersecting voxel, but
- Queue (BFS) or push (DFS) are only voxels crossing a triangle.
source share