intersectsWithQuad method

double intersectsWithQuad (Quad other)

Return the distance from the origin of this to the intersection with other if this intersects with other, or null if the don't intersect.

Implementation

double intersectsWithQuad(Quad other) {
  const double EPSILON = 10e-6;

  // First triangle
  Vector3 point0 = other._point0;
  Vector3 point1 = other._point1;
  Vector3 point2 = other._point2;

  _e1
    ..setFrom(point1)
    ..sub(point0);
  _e2
    ..setFrom(point2)
    ..sub(point0);

  _direction.crossInto(_e2, _q);
  final double a0 = _e1.dot(_q);

  if (!(a0 > -EPSILON && a0 < EPSILON)) {
    final double f = 1 / a0;
    _s
      ..setFrom(_origin)
      ..sub(point0);
    final double u = f * (_s.dot(_q));

    if (u >= 0.0) {
      _s.crossInto(_e1, _r);
      final double v = f * (_direction.dot(_r));

      if (!(v < -EPSILON || u + v > 1.0 + EPSILON)) {
        final double t = f * (_e2.dot(_r));

        return t;
      }
    }
  }

  // Second triangle
  point0 = other._point3;
  point1 = other._point0;
  point2 = other._point2;

  _e1
    ..setFrom(point1)
    ..sub(point0);
  _e2
    ..setFrom(point2)
    ..sub(point0);

  _direction.crossInto(_e2, _q);
  final double a1 = _e1.dot(_q);

  if (!(a1 > -EPSILON && a1 < EPSILON)) {
    final double f = 1 / a1;
    _s
      ..setFrom(_origin)
      ..sub(point0);
    final double u = f * (_s.dot(_q));

    if (u >= 0.0) {
      _s.crossInto(_e1, _r);
      final double v = f * (_direction.dot(_r));

      if (!(v < -EPSILON || u + v > 1.0 + EPSILON)) {
        final double t = f * (_e2.dot(_r));

        return t;
      }
    }
  }

  return null;
}