1

I'm trying to build an octree representing a volume which is originally described by a CSG (Constructive Solid Geometry) tree.

My original plan was to start with a big cube that contains the entire object and then test, for each of the eight sub-cubies, which ones are completely outside, which are completely inside the object, and which are both inside and outside. These "middle" cubies would then get recursively subdivided.

My problem may be stupid, but I can't devise a way to find the intersection of a cube and the CSG object, to be able to classify the cube as above.

My CSG structure is built from primitives such as cubes, spheres and cylinders (and maybe toruses in the future), with the boolean operations of union, intersection and subtraction.

Beside the explicit tree structure of the CSG, from its representation I also have a sort of distance function d(x,y,z) that would tell me if the point (x,y,z) is outside (>0) or inside (<0) the object.

How can I find if a cube intersect the object described by the CSG structure?

gigabytes
  • 3,104
  • 19
  • 35

1 Answers1

1

Sounds a bit like you are trying to re-invent sparse voxel octrees.

My idea would be to take samples at the finest cube resolution you wish to consider (you will have to have a resolution cutoff anyway).

Sample your distance function on points that lie on a Z-Order curve (also known as Morton order) and take note of the coordinates every time your distance function changes sign. Note that based on the properties of the Z-order curve, the storage space required for this should scale asymptotically with respect to surface-area rather than by volume.

This structure is functionally equivalent to a discrete octree (so you could conceivably use it as-is) since the Morton ordering is equivalent to depth first traversal of an octree.

But the point is, with this structure you can test octree-cube intersections efficiently using a binary search of the Mortonized coordinates of the cube corners. Note when doing this that an octree aligned cube can be described using 2 coordinates. We will have a "lower" and an "upper" cube corner with respect to the Morton-order, and a cube is "interesting" at the sampling resolution (not completely empty or full) if and only if there are any sign changes on the interval (lower,upper]. As long as you choose your original sampling resolution to be fine enough such that individual CSG primitives can't fit completely inside your smallest gridlines, you won't miss any features.

Explicitly finding the intersections with the border of the cubes is slightly more involved, but fairly doable. To my memory you have to consider sign changes that occur at a cube boundary and consider the special case that a surface boundary coincides with the cube boundary (which may happen on completely full cubes also). I'll leave it as an exercise for the reader, because I haven't worked out the math in a while. I just wanted to try to help you more forward.

Tim Seguine
  • 2,887
  • 25
  • 38