public void GetParameterByLength() { Path2F empty = new Path2F(); empty.ParameterizeByLength(20, 0.001f); // No exception, do nothing. Assert.IsTrue(float.IsNaN(empty.GetParameterFromLength(10, 20, 0.1f))); Path2F path = CreatePath(); path.ParameterizeByLength(20, 0.001f); Assert.AreEqual(0, path.GetParameterFromLength(0, 20, 0.001f)); Assert.AreEqual(path[2].Parameter, path.GetParameterFromLength(path[2].Parameter, 20, 0.01f)); Assert.AreEqual(path[4].Parameter, path.GetParameterFromLength(path[4].Parameter, 20, 0.01f)); Assert.AreEqual(path[5].Parameter, path.GetParameterFromLength(path[5].Parameter, 20, 0.01f)); Assert.AreEqual(path[6].Parameter, path.GetParameterFromLength(path[6].Parameter, 20, 0.01f)); Assert.AreEqual(path[7].Parameter, path.GetParameterFromLength(path[7].Parameter, 20, 0.01f)); Assert.AreEqual(path[8].Parameter, path.GetParameterFromLength(path[8].Parameter, 20, 0.01f)); Assert.AreEqual(path[9].Parameter, path.GetParameterFromLength(path[9].Parameter, 20, 0.01f)); float pathLength = path[9].Parameter; float desiredLength = 11; float actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.01f), 20, 0.01f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.01f)); desiredLength = 20; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.01f)); desiredLength = 26f; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.01f)); path.PreLoop = CurveLoopType.Linear; path.PostLoop = CurveLoopType.Linear; desiredLength = 60; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -10f; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.01f)); path.PreLoop = CurveLoopType.CycleOffset; path.PostLoop = CurveLoopType.CycleOffset; path.ParameterizeByLength(20, 0.001f); desiredLength = -90; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -50; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -30; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 50; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 100; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 130; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 200; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); path.PreLoop = CurveLoopType.Oscillate; path.PostLoop = CurveLoopType.Cycle; path.ParameterizeByLength(20, 0.001f); desiredLength = -66; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -30; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -20; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 40; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 70; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 100; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 190; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); path.PreLoop = CurveLoopType.Cycle; path.PostLoop = CurveLoopType.Oscillate; path.ParameterizeByLength(20, 0.001f); desiredLength = -90; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -50; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = -30; actualLength = -path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 50; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 110; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 130; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); desiredLength = 210; actualLength = path.GetLength(0, path.GetParameterFromLength(desiredLength, 20, 0.001f), 20, 0.001f); Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.1f)); // Test path with zero length. path = new Path2F(); path.Add(new PathKey2F() { Parameter = 10, Point = new Vector2F(0, 1), Interpolation = SplineInterpolation.Linear, TangentIn = new Vector2F(1, 0), TangentOut = new Vector2F(1, 0), }); path.ParameterizeByLength(20, 0.001f); Assert.AreEqual(0, path.GetParameterFromLength(0, 20, 0.1f)); path.Add(new PathKey2F() { Parameter = 20, Point = new Vector2F(0, 1), Interpolation = SplineInterpolation.Linear, TangentIn = new Vector2F(1, 0), TangentOut = new Vector2F(1, 0), }); path.ParameterizeByLength(20, 0.001f); Assert.AreEqual(0, path.GetParameterFromLength(0, 20, 0.1f)); }
public void ParameterizeByLength() { Path2F empty = new Path2F(); empty.Sort(); empty.ParameterizeByLength(20, 0.001f); // No exception, do nothing. Path2F path = CreatePath(); Path2F lengthPath = CreatePath(); lengthPath.ParameterizeByLength(20, 0.001f); float length = (lengthPath[1].Point - lengthPath[0].Point).Length; Assert.AreEqual(0, lengthPath[0].Parameter); Assert.AreEqual(length, lengthPath[1].Parameter); Assert.AreEqual(length, lengthPath[2].Parameter); Assert.AreEqual(length, lengthPath[3].Parameter); float step = 0.001f; int i = 4; float u = 20; Vector2F oldPoint = path.GetPoint(u); for (; u < 51 && i<10; u += step) { if (Numeric.AreEqual(u, path[i].Parameter)) { Assert.IsTrue(Numeric.AreEqual(length, lengthPath[i].Parameter, 0.01f)); // Set explicit values against numerical problems. length = lengthPath[i].Parameter; u = path[i].Parameter; oldPoint = path.GetPoint(u); i++; } Vector2F newPoint = path.GetPoint(u + step); length += (newPoint - oldPoint).Length; oldPoint = newPoint; } Assert.AreEqual(10, i); // Have we checked all keys? path.PreLoop = CurveLoopType.Constant; path.PostLoop = CurveLoopType.Oscillate; path.PreLoop = CurveLoopType.Linear; path.PostLoop = CurveLoopType.Cycle; path.PreLoop = CurveLoopType.Cycle; path.PostLoop = CurveLoopType.CycleOffset; path.PreLoop = CurveLoopType.CycleOffset; path.PostLoop = CurveLoopType.Linear; path.PreLoop = CurveLoopType.Oscillate; path.PostLoop = CurveLoopType.Constant; }