A consistent CSG model is one which is made up of solid objects with no dangling surfaces. In rayshade, it is quite easy to construct inconsistent models, which will usually appear incorrect in the final images. In rayshade, CSG is implemented by maintaining the tree structure of the CSG operations. This tree is traversed, and the operators therein applied, on a per-ray basis. It is therefore difficult to verify the consistency of the model ``on the fly.''
One class of CSG problems occur when surfaces of objects being operated upon coincide. For example, when subtracting a box from another box to make a square cup, the result will be wrong if the tops of the two boxes coincide. To correct this, the inner box should be made slightly taller than the outer box. A related problem that must be avoided occurs when two coincident surfaces are assigned different surface properties.
It may seem that the union operator is unnecessary, since listing two objects together in an aggregate results in an image that appears to be the same. While the result of such a short-cut may appear the same on the exterior, the interior of the resulting object will contain extraneous surfaces. The following example show this quite clearly.
difference box -2 0 -3 2 3 3 union /* change to list; note bad internal surfaces */ sphere 2 1 0 0 sphere 2 -1 0 0 end end rotate 1 0 0 -40 rotate 0 0 1 50
The visual evidence of an inconsistent CSG object varies depending upon the operator being used. When subtracting a consistent object from and inconsistent one, the resulting object will appear to be the union of the two objects, but the shading will be incorrect. It will appear to be inside-out in places, while correct in other places. The inside-out sections indicate the areas where the problems occur. Such problems are often caused by polygons with incorrectly specified normals, or by surfaces that exactly coincide (which appear as partial ``Swiss cheese'' objects).
The following example illustrates an attempt to subtract a sphere from a pyramid defined using an incorrectly facing triangle. Note that the resulting image obviously points to which triangle is reversed.
name pyramid list triangle 1 0 0 0 1 0 0 0 1 triangle 1 0 0 0 0 0 0 1 0 triangle 0 1 0 0 0 0 0 0 1 triangle 0 0 1 1 0 0 0 0 0 /* wrong order */ end difference object pyramid scale 3 3 3 rotate 0 0 1 45 rotate 1 0 0 -30 translate 0 -3.5 0 sphere 2.4 0 0 0 end
By default, cylinders and cones do not have end caps, and thus are not consistent primitives. One must usually add endcaps by listing the cylinder or cone with (correctly-oriented) endcap discs in an aggregate.