a.k.a. Polygon clipping algorithm in 3D
a.k.a. Finding the collision manifold between 2 colliding polygons
Most algorithms for polygon clipping are described in detail for 2D and described as being extendable to 3D but without details. For example the sutherland-hodgman clipping algorithm
Having been unable to find any 3D implementations or pseudo code on the internet I am now asking here (and attempting to answer my own question)
The algorithm would take two shapes such as those shown below:
And would output the intersection of the two shapes as shown below:
Note that although the Sutherland-Hodgman algorithm finds the intersection of two polygons it (and most others) make a distinction between the clipped polygon and the clipping polygon; the clipped polygon may be concave or convex, but the clipping shape must be convex. However, my implementation in extending to 3D requires that both shapes be convex, this suggests that it is not a true 3D sutherland-hodgman algorithm and other answers (using any algorithm) lifting this requirement would be very much appreciated
The 2D sutherland-hodgman clipping algorithm clips each edge of the clipped shape by each edge of the clipping shape. The 3D algorithm, however, clips each edge of each face of the clipped shape by each face of the clipping shape (NOT each edge of each face of the clipping shape).
My algorithm is "inspired" by this approach but I had to add an extra step to get all the edges to come out correctly. Basically I clipped both ways; clipping one shape by the other then doing the reverse and adding the two; this causes the requirement that both shapes be convex. Failing to include this "both ways" misses some of the faces as shown below
The pseudo code for this algorithm is as follows
This algorithm is implimented in java as shown below: (Note JMonkey engine is used for 3D visualisation but is demarked so it can be easily stripped out)
The following main class creates the images shown and renders them using the JMonkey engine library