Basic Shapes

Here, the shapes sphere, cube and plane are described. See figure 1 shows them from left to right.

1: Sphere, cube and plane as basic CSG shapes

1: Sphere, cube and plane as basic CSG shapes

Pay attention to the rounded edges of the cube. They are due to the fact, that in this dual marching cubes implementation, the preserving of sharp features is skipped in order to gain performance.


The volume model of the sphere is calculated like this:

f(x, y, z) = r - \sqrt{(x_c - x)^2 + (y_c - y)^2 + (z_c - z)^2}

With this gradient:

\nabla f(x, y, z) = \begin{pmatrix} x \\ y \\ z \end{pmatrix} - \begin{pmatrix} x_c \\ y_c \\ z_c \end{pmatrix}

x_c, y_c and z_c are the center of the sphere, r the radius.


The cube is described with his left, lower, back corner and his right, upper, front one. This way, position and size can be described, but not the orientation. The orientation just stays the same. A CSG rotation operation could fill this gap.

For determining the density, it is first checked whether the given coordinate is within the cube. This is easily done with six comparsions to the cubes planes.

If the coordinate is within the cube, the lowest distance to the planes is calculated. For the left cube plane, this would be $x – min_x$ with $\vec{min}$ as the left, lower back corner of the cube.

In case of the coordinate being outside of the cube, the specific function of Real Ogre::AxisAlignedBox::distance (const Vector3 & v) const. This function takes care whether an edge, a corner or a plane is the next element to the coordinate.

In order to get the gradient, the same method as in the discrete grid is used, the basic version of marching cubes.


The plane is defined by the distance d to the origin and a normal \vec{n}. The following density function is derived from the formula of Farin and Hansford by determining the distance of the coordinate to the plane [FH03, S. 180 – 181].

f(x, y, z) = d - \vec{n} \cdot \begin{pmatrix} x \\ y \\ z \end{pmatrix}

The gradient of the plane is its normale \vec{n} .

If you like this work and want to support it, feel free to donate something or click on any Flattr button!