/// <summary> /// Gets the time at which the internal curve would be evaluated at the given time. /// </summary> /// <param name="time">Time to evaluate the speed-controlled curve.</param> /// <returns>Time at which the internal curve would be evaluated.</returns> public double GetInnerTime(double time) { if (Curve == null) { throw new InvalidOperationException("SpeedControlledCurve's internal curve is null; ensure that its curve property is set prior to evaluation."); } float firstTime, lastTime; GetPathBoundsInformation(out firstTime, out lastTime); time = Curve <TValue> .ModifyTime(time, firstTime, lastTime, Curve.PreLoop, Curve.PostLoop); int indexMin = 0; int indexMax = samples.Count; if (indexMax == 0) { return(0); } //If time < controlpoints.mintime, should be... 0 or -1? while (indexMax - indexMin > 1) //if time belongs to min { int midIndex = (indexMin + indexMax) / 2; if (time > samples[midIndex].X) { //Use midIndex as the minimum. indexMin = midIndex; } else if (time < samples[midIndex].X) { //Use midindex as the max. indexMax = midIndex; } else { //Equal; use it. indexMin = midIndex; break; } } if (samples[indexMin].X > time) { indexMin -= 1; } double curveTime = (time - samples[indexMin].X) / (samples[indexMin + 1].X - samples[indexMin].X); return((1 - curveTime) * samples[indexMin].Y + (curveTime) * samples[indexMin + 1].Y); }
/// <summary> /// Gets the time at which the internal curve would be evaluated at the given time. /// </summary> /// <param name="time">Time to evaluate the speed-controlled curve.</param> /// <returns>Time at which the internal curve would be evaluated.</returns> public Fix64 GetInnerTime(Fix64 time) { if (Curve == null) { throw new InvalidOperationException("SpeedControlledCurve's internal curve is null; ensure that its curve property is set prior to evaluation."); } Fix64 firstTime, lastTime; GetPathBoundsInformation(out firstTime, out lastTime); time = Curve <TValue> .ModifyTime(time, firstTime, lastTime, Curve.PreLoop, Curve.PostLoop); int indexMin = 0; int indexMax = samples.Count; if (indexMax == 1) { //1-length curve; asking the system to evaluate //this will be a waste of time AND //crash since +1 will be outside scope return(samples[0].SpeedControlled); } if (indexMax == 0) { return(F64.C0); } //If time < controlpoints.mintime, should be... 0 or -1? while (indexMax - indexMin > 1) //if time belongs to min { int midIndex = (indexMin + indexMax) / 2; if (time > samples[midIndex].Wrapped) { indexMin = midIndex; } else { indexMax = midIndex; } } Fix64 curveTime = (time - samples[indexMin].Wrapped) / (samples[indexMin + 1].Wrapped - samples[indexMin].Wrapped); return((F64.C1 - curveTime) * samples[indexMin].SpeedControlled + (curveTime) * samples[indexMin + 1].SpeedControlled); }