views:

41

answers:

2

In my program I have some cubes (simple, xyz location, xyz size). I want bo be able to take one of these cubes and 'Subtract' another cube from it.

So my question, what is a good generic data structure to represent the resulting 3d object, and what kind of algorithm is used to subtract a 3d solid from another?

A: 

I doubt there is a standard way of representing geometric objects at that level.

I recall that Povray, an open source raytracer, that has a nice textual language for representing 3D scenes, and which includes a complete set of geometric operations (union, intersection, etc) "Constructive Solid Geometry"; it's quite flexible but I doubt it's what your are looking for. Bear in mind, besides, that a raytracer has some more concepts to deal with, apart from pure geometry: textures, lights, etc.

leonbloy
+1  A: 

This is a pretty general question and depends on what you want to know about the solid and how fast you want to know it. Assuming that you only want membership tests, this might work (psuedocode):

class Solid {
    Solid solids = [] // each Solid has a list of solids that  
                      // have been subtracted from it.                      

    abstract method containedInSelf(point) {
        // this will obviously vary from one type of solid to another
    } 

    method contains(point) {
        if !containedInSelf(point) return False;
        else {
            for solid in solids {  // loop over all contained solids
                if solid.contains(point) return False; 
                // point is contained in a solid that has been subtracted from it
            }
            // Now we know that point is contained but not contained in anything
            // that's been subtracted
            return True;
        }
    }

    method subtract(solid) {
        solids.append(solid)
    } 

}

This has the advantage of allowing composite subtractions. For example, you can subtract solid A from solid B and then solid B from solid C and it will work as expected. For example with three spheres centered at the origin and radius(A) < radius(B) < radius(C), you'll get points that are contained in A or contained in C but not B.

You can also, for example, subtract a two dodecahedrons from a sphere and then subtract that to a cube. Which is of course the same as subtracting the sphere from a cube and adding two dodecahedrons back in.

aaronasterling
I think this will work for what I am doing, and its pretty close to the data I already have, a sheet of plywood with variouse cuts and holes applied. I guess I just assumed it would get more complex than that.
Kratz