getPoint method

  1. @override
Vector3? getPoint(
  1. double t, [
  2. Vector? optionalTarget
])
override

Implementation

@override
Vector3? getPoint(double t, [Vector? optionalTarget]) {
  optionalTarget ??= Vector3();
  if(optionalTarget is! Vector3){
    if(optionalTarget is Vector4){
      optionalTarget = Vector3(optionalTarget.x, optionalTarget.y, optionalTarget.z);
    }
    else{
      optionalTarget = Vector3(optionalTarget.x, optionalTarget.y);
    }
  }
  final point = optionalTarget;

  final points = this.points;
  final l = points.length;

  final p = (l - (closed ? 0 : 1)) * t;
  int intPoint = p.floor();
  double weight = p - intPoint;

  if (closed) {
    intPoint += intPoint > 0 ? 0 : ((intPoint.abs() / l).floor() + 1) * l;
  } else if (weight == 0 && intPoint == l - 1) {
    intPoint = l - 2;
    weight = 1;
  }

  Vector3 p0;
  Vector3 p3; // 4 points (p1 & p2 defined below)

  if (closed || intPoint > 0) {
    p0 = points[(intPoint - 1) % l] as Vector3;
  }
  else {
    // extrapolate first point
    tmp.sub2(points[0], points[1]).add(points[0]);
    p0 = tmp;
  }

  final Vector3 p1 = points[intPoint % l] as Vector3;
  final Vector3 p2 = points[(intPoint + 1) % l] as Vector3;

  if (closed || intPoint + 2 < l) {
    p3 = points[(intPoint + 2) % l] as Vector3;
  }
  else {
    // extrapolate last point
    tmp.sub2(points[l - 1], points[l - 2]).add(points[l - 1]);
    p3 = tmp;
  }

  if (curveType == 'centripetal' || curveType == 'chordal') {
    // init Centripetal / Chordal Catmull-Rom
    final pow = curveType == 'chordal' ? 0.5 : 0.25;
    double dt0 = math.pow(p0.distanceToSquared(p1), pow).toDouble();
    double dt1 = math.pow(p1.distanceToSquared(p2), pow).toDouble();
    double dt2 = math.pow(p2.distanceToSquared(p3), pow).toDouble();

    // safety check for repeated points
    if (dt1 < 1e-4) dt1 = 1.0;
    if (dt0 < 1e-4) dt0 = dt1;
    if (dt2 < 1e-4) dt2 = dt1;

    px.initNonuniformCatmullRom(p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2);
    py.initNonuniformCatmullRom(p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2);
    pz.initNonuniformCatmullRom(p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2);
  }
  else if (curveType == 'catmullrom') {
    px.initCatmullRom(p0.x, p1.x, p2.x, p3.x, tension);
    py.initCatmullRom(p0.y, p1.y, p2.y, p3.y, tension);
    pz.initCatmullRom(p0.z, p1.z, p2.z, p3.z, tension);
  }

  point.setValues(px.calc(weight), py.calc(weight), pz.calc(weight));

  return point;
}