surfaceNormal static method

Sp3dV3D surfaceNormal(
  1. List<Sp3dV3D> face
)

(en)Calculates and returns the surface normal. The order of the vertices must be counterclockwise. Note that this cannot be used for degenerate polygons.

(ja)面法線を計算して返します。 頂点の順は逆時計回りである必要があります。 これは縮退ポリゴンには使えないので注意が必要です。

  • face : face vertices.

Returns surface normal.

Implementation

static Sp3dV3D surfaceNormal(List<Sp3dV3D> face) {
  final int vLen = face.length;
  if (vLen == 3) {
    return Sp3dV3D.cross(face[1] - face[0], face[1] - face[2]);
  } else {
    // Newellの方法の反転版
    Sp3dV3D r = Sp3dV3D(0, 0, 0);
    for (int i = 0; i < vLen; i++) {
      Sp3dV3D cv = face[i];
      Sp3dV3D nv = face[(i + 1) % vLen];
      r.x -= (cv.y - nv.y) * (cv.z + nv.z);
      r.y -= (cv.z - nv.z) * (cv.x + nv.x);
      r.z -= (cv.x - nv.x) * (cv.y + nv.y);
    }
    return r;
  }
}