getBarycoord static method

Vector3 getBarycoord(
  1. Vector3 point,
  2. Vector3 a,
  3. Vector3 b,
  4. Vector3 c,
  5. Vector3 target,
)

Implementation

static Vector3 getBarycoord(Vector3 point, Vector3 a, Vector3 b, Vector3 c, Vector3 target) {
  _v0.sub2(c, a);
  _v1.sub2(b, a);
  _v2.sub2(point, a);

  final dot00 = _v0.dot(_v0);
  final dot01 = _v0.dot(_v1);
  final dot02 = _v0.dot(_v2);
  final dot11 = _v1.dot(_v1);
  final dot12 = _v1.dot(_v2);

  final denom = (dot00 * dot11 - dot01 * dot01);

  // collinear or singular triangle
  if (denom == 0) {
    // arbitrary location outside of triangle?
    // not sure if this is the best idea, maybe should be returning null
    target.setValues(-2, -1, -1);
    return target;
  }

  final invDenom = 1 / denom;
  final u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  final v = (dot00 * dot12 - dot01 * dot02) * invDenom;

  // barycentric coordinates must always sum to 1
  target.setValues(1 - u - v, v, u);
  return target;
}