Wednesday, May 12, 2010

CSG operations

Lately I've been posting a lot of random thoughts, and this is one of them.
Today I read a post at filmic games about CSG, and for one reason or another a thought popped up in my head:

CSG operations can be expressed more intuitively as logical operations!

Technically, bitwise operations would work too, at least from a programming pov, but would make less sense (we're not working with bits here, aren't we).

  • A CSG complement operation would behave like a logical NOT operation (! in C languages), where the shape becomes "everything outside of the shape" (which is only useful as an intermediate step, not so much as an actual operation).
  • A CSG common (also known as 'intersection') operation behaves like a logical AND operation (&& in C languages) because only the parts that are kept are the ones shared between the two shapes.
  • A CSG addition (also known as 'union') operation behaves like a logical OR operation (|| in C languages) because all the parts of both shapes in this operation are kept afterwards.
  • A CSG subtraction operation would actually be a combination of AND and NOT (A&&!B).
The pictures have shamelessly been taken from wikipedia on the topic of logical operators which interestingly enough showed CSG like pictures!
(They're actually Venn diagrams)

Which shows that these things are, in fact, the very same thing!
(Hmm ... I wonder why I never noticed that before)

Note that logical OR (CSG union) operations can also be expressed as !(!A && !B), and that I've already shown that CSG subtraction can be expressed as (A && !B), meaning that all popular CSG operations can be implemented using only NOT and AND operations.

Now consider that a NOT operation would effectively not change the mesh at all, but would only invert the classification of triangles being inside or outside, there would effectively only be one kind of mesh modifying operation left..

Interesting!