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.