Here is a class I use to split triangles with a 3D plane. This can be used as a building block for creating spatial acceleration structures, such as Bounding Volume Hierarchies, from a list of triangles, where triangles can have large extents and should be split at the plane to reduce overlap when subdividing a tree node.
The class itself uses an abstract interface to define necessary operations of a triangle in terms of abstract methods, allowing the user application to use any custom triangle data structure it wants. It also uses functional interfaces / callbacks to let the user application decide how to store the generated split triangles, for all cases (triangle lies behind, on or infront of the split plane). I’ve tried to make it as efficient as possible, but improvements are welcome!
import java.util.*;
/**
* Methods to split triangles with a plane.
*
* @author Kai Burjack
*/
public class TriangleSplit {
/**
* Describes a vertex of the triangle with its position.
*/
public static class Vertex {
public float x, y, z;
Vertex() {
}
Vertex(Vertex v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
Vertex lerp(float x, float y, float z, float t) {
this.x = this.x + (x - this.x) * t;
this.y = this.y + (y - this.y) * t;
this.z = this.z + (z - this.z) * t;
return this;
}
Vertex set(Vertex v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
}
}
/**
* General contract of a triangle to be processed by methods in the
* {@link TriangleSplit} class.
* <p>
* User applications can define own triangle data structures but must
* provide methods to obtain the three vertices of the triangle and to
* generate a new triangle from a given triangle. The latter is to ensure
* that any auxiliary information such as material associated with the
* original triangle is retained in a splitted triangles.
*/
public static interface ITriangle {
/**
* Get the first vertex of `this` triangle and store it into the given
* vector `v`.
*/
void v0(Vertex v);
/**
* Get the second vertex of `this` triangle and store it into the given
* vector `v`.
*/
void v1(Vertex v);
/**
* Get the third vertex of `this` triangle and store it into the given
* vector `v`.
*/
void v2(Vertex v);
/**
* Generate a new triangle from `this` triangle using the given
* vertices.
* <p>
* This method exists to ensure that any auxiliary information such as
* material associated with `this` triangle is retained in a splitted
* triangles.
*
* @param v0 the first vertex. The data of the provided {@link Vertex}
* instance MUST be copied during this method and the method
* MUST NOT store a reference to this vector
* @param v1 the second vertex. The data of the provided {@link Vertex}
* instance MUST be copied during this method and the method
* MUST NOT store a reference to this vector
* @param v2 the third vertex. The data of the provided {@link Vertex}
* instance MUST be copied during this method and the method
* MUST NOT store a reference to this vector
* @return the new triangle
*/
ITriangle split(Vertex v0, Vertex v1, Vertex v2);
}
/**
* Functional interface for triangle callbacks. See the methods in the
* {@link TriangleSplit} class.
*
* @param <T> the type of the triangle class used
*/
@FunctionalInterface
public static interface ITriangleCallback<T extends ITriangle> {
/**
* Will be called when processing the given triangle, providing the
* triangle instance as well as the index of that triangle.
*
* @param t the triangle
* @param index the triangle's index
*/
void onTriangle(T t, int index);
}
/**
* Will hold the number of triangles behind and infront of the split plane.
* See the methods in the {@link TriangleSplit} class.
*/
public static class SplitInfo {
public int behind;
public int infront;
@Override
public String toString() {
return "SplitInfo [behind=" + behind + ", infront=" + infront + "]";
}
}
private static byte encodeSplitCase(byte a, byte b, byte c) {
return (byte) (a + 1 << 4 | b + 1 << 2 | c + 1);
}
private static float fraction(float a, float b) {
return java.lang.Math.abs(a)
/ (java.lang.Math.abs(a) + java.lang.Math.abs(b));
}
private static float dist(float a, float b, float c, float d,
float invDenom, float x, float y, float z) {
return (a * x + b * y + c * z + d) * invDenom;
}
private static byte signd(float a, float b, float c, float d, float x,
float y, float z) {
return (byte) java.lang.Math.signum(a * x + b * y + c * z + d);
}
/**
* Determine the number of split triangles behind and infront of the split
* plane and write those numbers to the provided {@link SplitInfo}
* parameter.
*
* @param a the `a` factor in the plane equation `a*x + b*y + c*z +
* d = 0`
* @param b the `b` factor in the plane equation `a*x + b*y + c*z +
* d = 0`
* @param c the `c` factor in the plane equation `a*x + b*y + c*z +
* d = 0`
* @param d the `d` factor in the plane equation `a*x + b*y + c*z +
* d = 0`
* @param triangles the list of triangles to test for splits
* @param out will hold the number of split triangles behind and
* infront of the split plane
*/
public static <T extends ITriangle, I extends Iterable<T>> void testSplit(
float a, float b, float c, float d, I triangles, SplitInfo out) {
int behind = 0, infront = 0;
Vertex v0 = new Vertex(), v1 = new Vertex(), v2 = new Vertex();
for (T t : triangles) {
t.v0(v0);
t.v1(v1);
t.v2(v2);
byte sa = signd(a, b, c, d, v0.x, v0.y, v0.z);
byte sb = signd(a, b, c, d, v1.x, v1.y, v1.z);
byte sc = signd(a, b, c, d, v2.x, v2.y, v2.z);
switch (encodeSplitCase(sa, sb, sc)) {
case 0 /* (-1,-1,-1) */:
case 1 /* (-1,-1,0) */:
case 4 /* (-1,0,-1) */:
case 5 /* (-1,0,0) */:
case 16 /* (0,-1,-1) */:
case 17 /* (0,-1,0) */:
case 20 /* (0,0,-1) */:
behind++;
break;
case 22 /* (0,0,1) */:
case 25 /* (0,1,0) */:
case 26 /* (0,1,1) */:
case 37 /* (1,0,0) */:
case 38 /* (1,0,1) */:
case 41 /* (1,1,0) */:
case 42 /* (1,1,1) */:
infront++;
break;
case 21 /* (0,0,0) */:
break;
case 2 /* (-1,-1,1) */:
case 8 /* (-1,1,-1) */:
case 32 /* (1,-1,-1) */:
infront++;
behind += 2;
break;
case 6 /* (-1,0,1) */:
case 9 /* (-1,1,0) */:
case 18 /* (0,-1,1) */:
case 24 /* (0,1,-1) */:
case 33 /* (1,-1,0) */:
case 36 /* (1,0,-1) */:
behind++;
infront++;
break;
case 10 /* (-1,1,1) */:
case 34 /* (1,-1,1) */:
case 40 /* (1,1,-1) */:
behind++;
infront += 2;
break;
}
}
out.behind = behind;
out.infront = infront;
}
/**
* Split the given triangle using the plane specified as the coefficients in
* the general plane equation `a*x + b*y + c*z + d = 0` and call the
* respective {@link ITriangleCallback} when that triangle (or a potentially
* generated splitted triangle) lies behind, on or infront of the split
* plane, allowing the caller to process the triangle in each case.
*
* @param a the `a` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param b the `b` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param c the `c` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param d the `d` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param t the triangle to split
* @param index any caller-defined index of the triangle reported to
* any {@link ITriangleCallback}
* @param behind callback to invoke for each triangle (original or
* splitted) that lies behind the split plane
* @param on callback to invoke when the triangle lies on the
* split plane
* @param infront callback to invoke for each triangle (original or
* splitted) that lies infront of the split plane
* @param newTriangles callback to invoke for each newly generated splitted
* triangle. This callback will always be invoked before
* calling any of the behind, on or infront callbacks
* for a generated splitted triangle
*/
@SuppressWarnings("unchecked")
public static <T extends ITriangle> void split(float a, float b, float c,
float d, T t, int index, ITriangleCallback<T> behind,
ITriangleCallback<T> on, ITriangleCallback<T> infront,
ITriangleCallback<T> newTriangles) {
float invDenom = (float) (1.0
/ java.lang.Math.sqrt(a * a + b * b + c * c));
Vertex v0 = new Vertex(), v1 = new Vertex(), v2 = new Vertex();
t.v0(v0);
t.v1(v1);
t.v2(v2);
float dA = dist(a, b, c, d, invDenom, v0.x, v0.y, v0.z);
float dB = dist(a, b, c, d, invDenom, v1.x, v1.y, v1.z);
float dC = dist(a, b, c, d, invDenom, v2.x, v2.y, v2.z);
byte sa = (byte) java.lang.Math.signum(dA);
byte sb = (byte) java.lang.Math.signum(dB);
byte sc = (byte) java.lang.Math.signum(dC);
Vertex a0 = null, a1 = null, a2 = null;
T t0, t1, t2;
if (sa * sb == -1)
a0 = new Vertex(v0).lerp(v1.x, v1.y, v1.z, fraction(dA, dB));
if (sb * sc == -1)
a1 = new Vertex(v1).lerp(v2.x, v2.y, v2.z, fraction(dB, dC));
if (sc * sa == -1)
a2 = new Vertex(v2).lerp(v0.x, v0.y, v0.z, fraction(dC, dA));
switch (encodeSplitCase(sa, sb, sc)) {
case 0 /* (-1,-1,-1) */:
case 1 /* (-1,-1,0) */:
case 4 /* (-1,0,-1) */:
case 5 /* (-1,0,0) */:
case 16 /* (0,-1,-1) */:
case 17 /* (0,-1,0) */:
case 20 /* (0,0,-1) */:
behind.onTriangle(t, index);
break;
case 22 /* (0,0,1) */:
case 25 /* (0,1,0) */:
case 26 /* (0,1,1) */:
case 37 /* (1,0,0) */:
case 38 /* (1,0,1) */:
case 41 /* (1,1,0) */:
case 42 /* (1,1,1) */:
infront.onTriangle(t, index);
break;
case 2 /* (-1,-1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1), index + 1);
infront.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2), index + 2);
behind.onTriangle(t1, index + 2);
newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1), index + 3);
behind.onTriangle(t2, index + 3);
break;
case 6 /* (-1,0,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a2), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a2), index + 2);
infront.onTriangle(t1, index + 2);
break;
case 8 /* (-1,1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0), index + 1);
infront.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v2, a0, a1), index + 2);
behind.onTriangle(t1, index + 2);
newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a0), index + 3);
behind.onTriangle(t2, index + 3);
break;
case 9 /* (-1,1,0) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a0), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a0), index + 2);
infront.onTriangle(t1, index + 2);
break;
case 10 /* (-1,1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0), index + 2);
infront.onTriangle(t1, index + 2);
newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2), index + 3);
infront.onTriangle(t2, index + 3);
break;
case 18 /* (0,-1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a1), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a1), index + 2);
infront.onTriangle(t1, index + 2);
break;
case 21 /* (0,0,0) */:
on.onTriangle(t, index);
break;
case 24 /* (0,1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a1), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a1), index + 1);
infront.onTriangle(t1, index + 2);
break;
case 32 /* (1,-1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2), index + 1);
infront.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0), index + 2);
behind.onTriangle(t1, index + 2);
newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2), index + 3);
behind.onTriangle(t2, index + 3);
break;
case 33 /* (1,-1,0) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a0), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a0), index + 2);
infront.onTriangle(t1, index + 3);
break;
case 34 /* (1,-1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v0, a0, a1), index + 2);
infront.onTriangle(t1, index + 2);
newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a1), index + 3);
infront.onTriangle(t2, index + 3);
break;
case 36 /* (1,0,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a2), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a2), index + 2);
infront.onTriangle(t1, index + 2);
break;
case 40 /* (1,1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1), index + 1);
behind.onTriangle(t0, index + 1);
newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2), index + 2);
infront.onTriangle(t1, index + 2);
newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1), index + 3);
infront.onTriangle(t2, index + 3);
break;
}
}
/**
* Split the given list of triangles using the plane specified as the
* coefficients in the general plane equation `a*x + b*y + c*z + d = 0` and
* call the respective {@link ITriangleCallback} when a triangle (or any
* generated splitted triangle) lies behind, on or infront of the split
* plane, allowing the caller to process the triangle in each case.
*
* @param a the `a` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param b the `b` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param c the `c` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param d the `d` factor in the plane equation `a*x + b*y + c*z
* + d = 0`
* @param triangles the list of triangles to split
* @param behind callback to invoke for each triangle (original or
* splitted) that lies behind the split plane
* @param on callback to invoke for each original triangle that
* lies on the split plane
* @param infront callback to invoke for each triangle (original or
* splitted) that lies infront of the split plane
* @param newTriangles callback to invoke for each newly generated splitted
* triangle. This callback will always be invoked before
* calling any of the behind, on or infront callbacks
* for a generated splitted triangle
*/
@SuppressWarnings("unchecked")
public static <T extends ITriangle, I extends Collection<T>> void split(
float a, float b, float c, float d, I triangles,
ITriangleCallback<T> behind, ITriangleCallback<T> on,
ITriangleCallback<T> infront, ITriangleCallback<T> newTriangles) {
float invDenom = (float) (1.0
/ java.lang.Math.sqrt(a * a + b * b + c * c));
int lastIndex = triangles.size() - 1;
Vertex v0 = new Vertex(), v1 = new Vertex(), v2 = new Vertex();
Vertex a0 = new Vertex(), a1 = new Vertex(), a2 = new Vertex();
int i = 0;
for (T t : triangles) {
t.v0(v0);
t.v1(v1);
t.v2(v2);
float dA = dist(a, b, c, d, invDenom, v0.x, v0.y, v0.z);
float dB = dist(a, b, c, d, invDenom, v1.x, v1.y, v1.z);
float dC = dist(a, b, c, d, invDenom, v2.x, v2.y, v2.z);
byte sa = (byte) java.lang.Math.signum(dA);
byte sb = (byte) java.lang.Math.signum(dB);
byte sc = (byte) java.lang.Math.signum(dC);
T t0, t1, t2;
if (sa * sb == -1)
a0.set(v0).lerp(v1.x, v1.y, v1.z, fraction(dA, dB));
if (sb * sc == -1)
a1.set(v1).lerp(v2.x, v2.y, v2.z, fraction(dB, dC));
if (sc * sa == -1)
a2.set(v2).lerp(v0.x, v0.y, v0.z, fraction(dC, dA));
switch (encodeSplitCase(sa, sb, sc)) {
case 0 /* (-1,-1,-1) */:
case 1 /* (-1,-1,0) */:
case 4 /* (-1,0,-1) */:
case 5 /* (-1,0,0) */:
case 16 /* (0,-1,-1) */:
case 17 /* (0,-1,0) */:
case 20 /* (0,0,-1) */:
behind.onTriangle(t, i);
break;
case 22 /* (0,0,1) */:
case 25 /* (0,1,0) */:
case 26 /* (0,1,1) */:
case 37 /* (1,0,0) */:
case 38 /* (1,0,1) */:
case 41 /* (1,1,0) */:
case 42 /* (1,1,1) */:
infront.onTriangle(t, i);
break;
case 2 /* (-1,-1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1),
++lastIndex);
infront.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2),
++lastIndex);
behind.onTriangle(t1, lastIndex);
newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1),
++lastIndex);
behind.onTriangle(t2, lastIndex);
break;
case 6 /* (-1,0,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a2),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a2),
++lastIndex);
infront.onTriangle(t1, lastIndex);
break;
case 8 /* (-1,1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0),
++lastIndex);
infront.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v2, a0, a1),
++lastIndex);
behind.onTriangle(t1, lastIndex);
newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a0),
++lastIndex);
behind.onTriangle(t2, lastIndex);
break;
case 9 /* (-1,1,0) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a0),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a0),
++lastIndex);
infront.onTriangle(t1, lastIndex);
break;
case 10 /* (-1,1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0),
++lastIndex);
infront.onTriangle(t1, lastIndex);
newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2),
++lastIndex);
infront.onTriangle(t2, lastIndex);
break;
case 18 /* (0,-1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a1),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a1),
++lastIndex);
infront.onTriangle(t1, lastIndex);
break;
case 21 /* (0,0,0) */:
on.onTriangle(t, i);
break;
case 24 /* (0,1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a1),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a1),
++lastIndex);
infront.onTriangle(t1, lastIndex);
break;
case 32 /* (1,-1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2),
++lastIndex);
infront.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0),
++lastIndex);
behind.onTriangle(t1, lastIndex);
newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2),
++lastIndex);
behind.onTriangle(t2, lastIndex);
break;
case 33 /* (1,-1,0) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a0),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a0),
++lastIndex);
infront.onTriangle(t1, lastIndex);
break;
case 34 /* (1,-1,1) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v0, a0, a1),
++lastIndex);
infront.onTriangle(t1, lastIndex);
newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a1),
++lastIndex);
infront.onTriangle(t2, lastIndex);
break;
case 36 /* (1,0,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a2),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a2),
++lastIndex);
infront.onTriangle(t1, lastIndex);
break;
case 40 /* (1,1,-1) */:
newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1),
++lastIndex);
behind.onTriangle(t0, lastIndex);
newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2),
++lastIndex);
infront.onTriangle(t1, lastIndex);
newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1),
++lastIndex);
infront.onTriangle(t2, lastIndex);
break;
}
i++;
}
}
}