clipFaceAgainstPlane method
Clip a face in a hull against the back of a plane. @param planeConstant The constant in the mathematical plane equation
Implementation
List<Vector3> clipFaceAgainstPlane(List<Vector3> inVertices, List<Vector3> outVertices, Vector3 planeNormal, double planeConstant){
double nDotFirst;
double nDotLast;
final numVerts = inVertices.length;
if (numVerts < 2) {
return outVertices;
}
Vector3 firstVertex = inVertices[inVertices.length - 1];
Vector3 lastVertex = inVertices[0];
nDotFirst = planeNormal.dot(firstVertex) + planeConstant;
for (int vi = 0; vi < numVerts; vi++) {
lastVertex = inVertices[vi];
nDotLast = planeNormal.dot(lastVertex) + planeConstant;
if (nDotFirst < 0) {
if (nDotLast < 0) {
// Start < 0, end < 0, so output lastVertex
final newv = Vector3.zero();
newv.setFrom(lastVertex);
outVertices.add(newv);
} else {
// Start < 0, end >= 0, so output intersection
final newv = Vector3.zero();
firstVertex.lerp(lastVertex, nDotFirst / (nDotFirst - nDotLast), newv);
outVertices.add(newv);
}
} else {
if (nDotLast < 0) {
// Start >= 0, end < 0 so output intersection and end
final newv = Vector3.zero();
firstVertex.lerp(lastVertex, nDotFirst / (nDotFirst - nDotLast), newv);
outVertices.add(newv);
outVertices.add(lastVertex);
}
}
firstVertex = lastVertex;
nDotFirst = nDotLast;
}
return outVertices;
}