3.4.6 Constructive Solid Geometry

In addition to all of the primitive shapes POV-Ray supports, you can also combine multiple simple shapes into complex shapes using Constructive Solid Geometry (CSG). There are four basic types of CSG operations: union, intersection, difference, and merge. CSG objects can be composed of primitives or other CSG objects to create more, and more complex shapes.

3.4.6.1 Inside and Outside

Most shape primitives, like spheres, boxes and blobs divide the world into two regions. One region is inside the object and one is outside. Given any point in space you can say it is either inside or outside any particular primitive object. Well, it could be exactly on the surface but this case is rather hard to determine due to numerical problems.

Even planes have an inside and an outside. By definition, the surface normal of the plane points towards the outside of the plane. You should note that triangles cannot be used as solid objects in CSG since they have no well defined inside and outside. Triangle-based shapes (mesh, mesh2) can only be used in CSG when they are closed objects and have an inside vector specified.

Note:: Although triangles, bicubic_patches and some other shapes have no well defined inside and outside, they have a front- and backside which makes it possible to use a texture on the front side and an interior_texture on the back side.

CSG uses the concepts of inside and outside to combine shapes together as explained in the following sections.

Imagine you have two objects that partially overlap like shown in the figure below. Four different areas of points can be distinguished: points that are neither in object A nor in object B, points that are in object A but not in object B, points that are not in object A but in object B and last not least points that are in object A and object B.


Two overlapping objects.

Keeping this in mind it will be quite easy to understand how the CSG operations work.

When using CSG it is often useful to invert an object so that it will be inside-out. The appearance of the object is not changed, just the way that POV-Ray perceives it. When the inverse keyword is used the inside of the shape is flipped to become the outside and vice versa.

The inside/outside distinction is not important for a union, but is important for intersection, difference, and merge.Therefore any objects may be combined using union but only solid objects, i.e. objects that have a well-defined interior can be used in the other kinds of CSG. The objects described in "Finite Patch Primitives" have no well defined inside/outside. All objects described in the sections "Finite Solid Primitives" and "Infinite Solid Primitives".

3.4.6.2 Union


The union of two objects.

The simplest kind of CSG is the union. The syntax is:

UNION:
    union
    {
        OBJECTS...
        [OBJECT_MODIFIERS...]
    }

Unions are simply glue used to bind two or more shapes into a single entity that can be manipulated as a single object. The image above shows the union of A and B. The new object created by the union operation can be scaled, translated and rotated as a single shape. The entire union can share a single texture but each object contained in the union may also have its own texture, which will override any texture statements in the parent object.

You should be aware that the surfaces inside the union will not be removed. As you can see from the figure this may be a problem for transparent unions. If you want those surfaces to be removed you will have to use the merge operations explained in a later section.

The following union will contain a box and a sphere.

  union {
    box { <-1.5, -1, -1>, <0.5, 1, 1> }
    cylinder { <0.5, 0, -1>, <0.5, 0, 1>, 1 }
  }

Earlier versions of POV-Ray placed restrictions on unions so you often had to combine objects with composite statements. Those earlier restrictions have been lifted so composite is no longer needed. It is still supported for backwards compatibility.

3.4.6.2.1 Split_Union

split_union is a boolean keyword that can be added to a union. It has two states on/off, its default is on.

split_union is used when photons are shot at the CSG-object. The object is split up in its compound parts, photons are shot at each part separately. This is to prevent photons from being shot at 'empty spaces' in the object, for example the holes in a grid. With compact objects, without 'empty spaces' split_union off can improve photon gathering.

  union {
    object {...}
    object {...}
    split_union off
  }

3.4.6.3 Intersection

The intersection object creates a shape containing only those areas where all components overlap. A point is part of an intersection if it is inside both objects, A and B, as show in the figure below.


The intersection of two objects.

The syntax is:

INTERSECTION:
    intersection
    {
        SOLID_OBJECTS...
        [OBJECT_MODIFIERS...]
    }

The component objects must have well defined inside/outside properties. Patch objects are not allowed.

Note: if all components do not overlap, the intersection object disappears.

Here is an example that overlaps:

  intersection {
    box { <-1.5, -1, -1>, <0.5, 1, 1> }
    cylinder { <0.5, 0, -1>, <0.5, 0, 1>, 1 }
  }

3.4.6.4 Difference

The CSG difference operation takes the intersection between the first object and the inverse of all subsequent objects. Thus only points inside object A and outside object B belong to the difference of both objects.

The result is a subtraction of the 2nd shape from the first shape as shown in the figure below.


The difference between two objects.

The syntax is:

DIFFERENCE:
    difference
    {
        SOLID_OBJECTS...
        [OBJECT_MODIFIERS...]
    }

The component objects must have well defined inside/outside properties. Patch objects are not allowed.

Note: if the first object is entirely inside the subtracted objects, the difference object disappears.

Here is an example of a properly formed difference:

  difference {
    box { <-1.5, -1, -1>, <0.5, 1, 1> }
    cylinder { <0.5, 0, -1>, <0.5, 0, 1>, 1 }
  }

Note: internally, POV-Ray simply adds the inverse keyword to the second (and subsequent) objects and then performs an intersection.

The example above is equivalent to:

  intersection {
    box { <-1.5, -1, -1>, <0.5, 1, 1> }
    cylinder { <0.5, 0, -1>, <0.5, 0, 1>, 1 inverse }
  }

3.4.6.5 Merge

The union operation just glues objects together, it does not remove the objects' surfaces inside the union. Under most circumstances this does not matter. However if a transparent union is used, those interior surfaces will be visible. The merge operations can be used to avoid this problem. It works just like union but it eliminates the inner surfaces like shown in the figure below.


Merge removes inner surfaces.

The syntax is:

MERGE:
    merge
    {
        SOLID_OBJECTS...
        [OBJECT_MODIFIERS...]
    }

The component objects must have well defined inside/outside properties. Patch objects are not allowed.

Note: that in general merge is slower rendering than union when used with non transparent objects. A small test may be needed to determine what is the optimal solution regarding speed and visual result.