**Contents**

We can mathematically combine and manipulate implicit surfaces in all sort of ways to create shapes that would be extremely hard to produce with more traditional modeling methods (especially polygons). Generally the method of assembling shapes to create more complex shapes is called **constructive solid geometry** or **CSG**. CSG is a method of what we call procedural modelling. You don't model shapes by moving vertices by hand, but by combining simple shapes using mathematics and automated processes.

Let's take a few basic examples. One of the most basic operations is to add two objects together something that is best described in mathematics with the *union* operator. You can achieve this effect by just returning the minimum distance to the two objects you wish to combine as shown in figure 1.

You can also subtract the volume of a shape from another, something that again in mathematics might be best described with a *set difference* operator (in plain English a subtraction). To do so, you first need to invert the sign of the distance estimator of the first shape. If you look at figure 2, you can see that the inside of the sphere now returns positive signed distance to the surface, and any point outside of the sphere will return a negative signed distance. Then take the maximum of the two distances. With this effect, you can create a hole in the second object that has the shape of the first object.

Finally our last basic example will consist of computing the surface resulting of the *intersection* of two surfaces. To do so (as illustrated in figure 3) just use the largest distance (using the *max* operator).

What these operations have in common is that that they use what we call **boolean operators**.

But these are just a few examples.

Since we just mentioned the modulo operator, take a moment to think about the type of effect you will get if you apply a modulo operator to the distance to an object? Something like distance % 10 for example. Well this will have for effect to duplicate the object in every direction to infinity. You operate on the object space. You can also deform that space to create all sort of effects like twisting and bending (using sine and cosine).

You can combine all these effects but more importantly when it comes to CSG, you are not limited to two objects. You can for example compute the intersection of two objects and combine the resulting shape with a third object, etc. People working on solid geometry, call this a CSG tree. Computing or evaluating these trees efficiently is a topic of research.

The two effect will study in this chapter is morphing or mixing and blending. The idea of morphing (mixing) is to create a shape that is somewhere in between the first and the second shape. For example you can morph between a sphere and cube (figure 4). The process to do so is very simple, you just (linearly) mix the distances to the two objects:

Where mixValue is a float contained in the range [0,1].

The effect of blending is more interesting. The idea is that when you compute the union between two objects you get sharp edges where the two objects intersect. If you want to create like a welding effect, this is not desirable. What you want is some sort of smooth transition between one surface to the other, in areas where the two surfaces are close to each other. Interesting enough many of the equations you will find in papers on implicit surfaces that relate to blending equation "just don't work". We don't know if these are just typos that kept being repeated from paper to paper but that's bad and frustrating. Let's give credit to Inigo Quilez for sharing on his website a few blending functions that just work. In the code below we implemented the exponential version. We might explain this function in a future revision of this lesson; for now we will only provide the code (see below the implementation chapter).

## Implementation

The code we provide to implement some of these ideas is rather basic and straightforward. Though from a programming standing note that we use a pattern called a *functor* in programming (this method is also a form of what we call generic programming). All the functions to operate on the two distances are actually defined into classes (or more exactly structure in this case). This allows us to eventually store within these classes member variables such as a blending factor as in the case of the blendFunc structure for instance. But more importantly because these are classes or structures, they define a type and as such they can be used in template class (note that the CSG class is a template and that the first template parameter is one of these structures in which we implement the function that operates on the distance via the () operator) . If you are unfamiliar with this patter, we recommend you to read about it on the web. This code also uses some nice features from the more advanced C++ standards such as the variadic arguments Args&& ... args and the using directive that can be used instead of typedef. Time to progress with your C++ coding skills!