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.
-
you can try to "rasterize" the object in 3D, by intersecting it with planes and filling the planes with cubes (much like rasterizing a triangle) – Nov 02 '15 at 14:16
-
@willywonka_dailyblah is it like slicing object with different plane and draw ing a section at a time and storing pixels values to calculate where cubes should be made? – spuemaacne Nov 05 '15 at 04:42
-
kind of, yeah. will conjure up an answer in a mo – Nov 05 '15 at 11:43
1 Answers
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.
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).
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.
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.
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.
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.