Can anbody suggest me the way to voxelize 3d model?

515 Views Asked by At

This website explains on how to do it. http://drububu.com/miscellaneous/voxelizer/index.html But I can't understand how to implement it in code.

1

There are 1 best solutions below

0
AudioBubble On

Procedure:

1) Specify two perpendicular directions which determine the orientation of your cubes. For convenience in this context I will choose them to be the X and Z axes respectively. X will be the normal of the intersection planes, and Y, Z will be the 2D coordinate axes within the plane.

enter image description here

2) Find the smallest axially-oriented cuboid (AABB) that encloses your mesh. I'm sure that would be quite easy. In this case that would give you 6 numbers, [X1, X2, Y1, Y2, Z1, Z2].

3) Say you choose your cubes to have dimension S; let H = S / 2. Starting from X X = X1 + H, construct a plane having normal (1, 0, 0) and center (X, 0, 0).

enter image description here

4) Intersect the mesh with it. See here for details on the math & implementation in C++: http://www.geometrictools.com/Documentation/ClipMesh.pdf

5) Take the edges which result from the intersection. Join them up by finding pairs which share a common end point, and inserting them into some doubly-linked list. This way they can be ordered into a path.

enter image description here

6) Find the smallest enclosing rectangle for this shape, giving you [Y1, Y2, Z1, Z2]

7) Starting from Z = Z1 + H, create a line which goes from (X, Y1, Z) to (X, Y2, Z). Intersect this line with all the edges in the path to get the points. Walk along the link list to avoid duplicate testing.

enter image description here

8) Sort the points by to their Y-coordinates. Insertion sort will do: https://en.wikipedia.org/wiki/Insertion_sort

9) For adjacent pairs of points, with Y-coordinates A and B, starting from Y = A + H, place a cube of size S at (X, Y, Z) where X and Z are from the previous steps.

10) Repeat from step (9) for each pair of points (as shown), while incrementing Y by S each time, until Y > B - H.

enter image description here

11) Repeat from step (7), while incrementing Z by S each time, until Z > Z2 - H.

12) Repeat from step (3), while incrementing X by S each time, until X > X2 - H.

And you're done. Disclaimer: this is probably NOT an efficient way, but it's probably the easiest and simplest way to implement.