getT method

double getT(
  1. double x
)

Implementation

double getT(double x) {
  double intervalStart = 0.0;
  int currentSample = 1;
  int lastSample = splineTableSize - 1;

  for (;
      currentSample != lastSample && _values[currentSample] <= x;
      ++currentSample) {
    intervalStart += sampleStepSize;
  }
  --currentSample;

  // Interpolate to provide an initial guess for t
  var dist = (x - _values[currentSample]) /
      (_values[currentSample + 1] - _values[currentSample]);
  var guessForT = intervalStart + dist * sampleStepSize;

  var initialSlope = getSlope(guessForT, x1, x2);
  if (initialSlope >= newtonMinSlope) {
    for (int i = 0; i < newtonIterations; ++i) {
      double currentSlope = getSlope(guessForT, x1, x2);
      if (currentSlope == 0.0) {
        return guessForT;
      }
      double currentX = calcBezier(guessForT, x1, x2) - x;
      guessForT -= currentX / currentSlope;
    }
    return guessForT;
  } else if (initialSlope == 0.0) {
    return guessForT;
  } else {
    double aB = intervalStart + sampleStepSize;
    double currentX, currentT;
    int i = 0;
    do {
      currentT = intervalStart + (aB - intervalStart) / 2.0;
      currentX = calcBezier(currentT, x1, x2) - x;
      if (currentX > 0.0) {
        aB = currentT;
      } else {
        intervalStart = currentT;
      }
    } while (currentX.abs() > subdivisionPrecision &&
        ++i < subdivisionMaxIterations);
    return currentT;
  }
}