facesAwayFrom method

bool facesAwayFrom(
  1. int cluster,
  2. double x,
  3. double y,
  4. double z,
)

Whether every triangle of cluster faces away from an eye at (x, y, z) in the mesh's space.

True is a promise the rasteriser would cull all of them; anything short of certain is false. The box is taken as its bounding sphere (centre c, radius r) and the normals as the cone (axis a, half-angle θ), and with d = c - eye every triangle faces away when the angle between a and d, plus θ, is less than acos(r / |d|): then each normal is less than that far from d, its dot with d exceeds r, and so its dot with p - eye is positive for every point p of the sphere. In cosines: r < |d| cos θ and a · d > r cos θ + sqrt(|d|² - r²) sin θ.

Implementation

bool facesAwayFrom(int cluster, double x, double y, double z) {
  final o = cluster * floatsPerCluster;
  final cosine = data[o + 9];
  if (!(cosine > 0.0)) return false;
  final sine = data[o + 10];
  final hx = (data[o + 3] - data[o]) * 0.5;
  final hy = (data[o + 4] - data[o + 1]) * 0.5;
  final hz = (data[o + 5] - data[o + 2]) * 0.5;
  final r = math.sqrt(hx * hx + hy * hy + hz * hz);
  final dx = data[o] + hx - x;
  final dy = data[o + 1] + hy - y;
  final dz = data[o + 2] + hz - z;
  final distanceSquared = dx * dx + dy * dy + dz * dz;
  if (!(r * r < distanceSquared * cosine * cosine)) return false;
  final along = data[o + 6] * dx + data[o + 7] * dy + data[o + 8] * dz;
  return along > r * cosine + math.sqrt(distanceSquared - r * r) * sine;
}