clipFaceAgainstPlane method

List<Vec3> clipFaceAgainstPlane(
  1. List<Vec3> inVertices,
  2. List<Vec3> outVertices,
  3. Vec3 planeNormal,
  4. double planeConstant,
)

Clip a face in a hull against the back of a plane. @param planeConstant The constant in the mathematical plane equation

Implementation

List<Vec3> clipFaceAgainstPlane(List<Vec3> inVertices, List<Vec3> outVertices, Vec3 planeNormal, double planeConstant){
  double nDotFirst;
  double nDotLast;
  final numVerts = inVertices.length;

  if (numVerts < 2) {
    return outVertices;
  }

  Vec3 firstVertex = inVertices[inVertices.length - 1];
  Vec3 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 = Vec3();
        newv.copy(lastVertex);
        outVertices.add(newv);
      } else {
        // Start < 0, end >= 0, so output intersection
        final newv = Vec3();
        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 = Vec3();
        firstVertex.lerp(lastVertex, nDotFirst / (nDotFirst - nDotLast), newv);
        outVertices.add(newv);
        outVertices.add(lastVertex);
      }
    }
    firstVertex = lastVertex;
    nDotFirst = nDotLast;
  }
  return outVertices;
}