Пример #1
0
        public void GetLength()
        {
            Path2F empty = new Path2F();
              empty.Sort();
              Assert.AreEqual(0, empty.GetLength(0, 1, 100, 0.0001f));

              Path2F path = CreatePath();
              path.PreLoop = CurveLoopType.Constant;
              path.PostLoop = CurveLoopType.Oscillate;
              Assert.IsTrue(Numeric.AreEqual((new Vector2F(0, 1) - new Vector2F(1, 2)).Length, path.GetLength(-1, 12, 100, 0.0001f), 0.001f));
              Assert.IsTrue(Numeric.AreEqual((new Vector2F(0, 1) - new Vector2F(1, 2)).Length, path.GetLength(-1, 20, 100, 0.0001f), 0.001f));

              CatmullRomSegment2F catmullOscillate = new CatmullRomSegment2F()
              {
            Point1 = new Vector2F(10, 12),
            Point2 = new Vector2F(10, 14),
            Point3 = new Vector2F(20, 14),
            Point4 = new Vector2F(30, 14),
              };
              float desiredLength = catmullOscillate.GetLength(0, 1, 20, 0.0001f);
              float actualLength = path.GetLength(40, 50, 20, 0.0001f);
              Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.001f));
              desiredLength = catmullOscillate.GetLength(1, 0.8f, 20, 0.0001f);
              actualLength = path.GetLength(52, 50, 20, 0.0001f);
              Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.001f));
              desiredLength = catmullOscillate.GetLength(1, 0.8f, 20, 0.0001f) * 2;
              actualLength = path.GetLength(52, 48, 20, 0.0001f);
              Assert.IsTrue(Numeric.AreEqual(desiredLength, actualLength, 0.001f));

              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;
        }
Пример #2
0
        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;
        }
Пример #3
0
        public void GetTangent()
        {
            Path2F path = CreatePath();
              path.PreLoop = CurveLoopType.Constant;
              path.PostLoop = CurveLoopType.Oscillate;
              Assert.IsTrue(Vector2F.AreNumericallyEqual(new Vector2F(0, 0), path.GetTangent(-10)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual((new Vector2F(1, 2) - new Vector2F(0, 1)) / 2, path.GetTangent(10)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual((new Vector2F(1, 2) - new Vector2F(0, 1)) / 2, path.GetTangent(11.5f)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual(new Vector2F(0, 0), path.GetTangent(16)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual(new Vector2F(0, 0), path.GetTangent(85)));

              CatmullRomSegment2F catmullOscillate = new CatmullRomSegment2F()
              {
            Point1 = new Vector2F(10, 12),
            Point2 = new Vector2F(10, 14),
            Point3 = new Vector2F(20, 14),
            Point4 = new Vector2F(30, 14),
              };
              Assert.IsTrue(Vector2F.AreNumericallyEqual(catmullOscillate.GetTangent(0.3f) / 10.0f, path.GetTangent(43)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual(-catmullOscillate.GetTangent(0.7f) / 10.0f, path.GetTangent(53)));

              path.PreLoop = CurveLoopType.Linear;
              path.PostLoop = CurveLoopType.Cycle;
              Assert.IsTrue(Vector2F.AreNumericallyEqual((new Vector2F(1, 2) - new Vector2F(0, 1)) / 2, path.GetTangent(0)));

              path.PreLoop = CurveLoopType.Cycle;
              path.PostLoop = CurveLoopType.CycleOffset;
              Assert.IsTrue(Vector2F.AreNumericallyEqual(catmullOscillate.GetTangent(0.4f) / 10.0f, path.GetTangent(-36)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual(catmullOscillate.GetTangent(0.4f) / 10.0f, path.GetTangent(4)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual(catmullOscillate.GetTangent(0.3f) / 10.0f, path.GetTangent(83)));

              path.PreLoop = CurveLoopType.CycleOffset;
              path.PostLoop = CurveLoopType.Linear;
              Assert.IsTrue(Vector2F.AreNumericallyEqual(catmullOscillate.GetTangent(1f) / 10.0f, path.GetTangent(434)));

              path.PreLoop = CurveLoopType.Oscillate;
              path.PostLoop = CurveLoopType.Constant;

              path = new Path2F();
              path.Add(new PathKey2F()
              {
            Parameter = 25,
            Point = new Vector2F(6, 7),
            Interpolation = SplineInterpolation.Bezier,
            TangentIn = new Vector2F(5, 6),
            TangentOut = new Vector2F(7, 8),
              });
              path.Add(new PathKey2F()
              {
            Parameter = 35,
            Point = new Vector2F(10, 12),
            Interpolation = SplineInterpolation.Hermite,
            TangentIn = new Vector2F(1, 0),
            TangentOut = new Vector2F(1, 0),
              });
              path.PreLoop = CurveLoopType.Linear;
              path.PostLoop = CurveLoopType.Linear;
              float Δu = path[1].Parameter - path[0].Parameter;
              Assert.IsTrue(Vector2F.AreNumericallyEqual((new Vector2F(6, 7) - new Vector2F(5, 6)) * 3 / Δu, path.GetTangent(0)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual(new Vector2F(1, 0) / Δu, path.GetTangent(100)));

              path[1].Parameter = 25;
              path[0].Parameter = 35;
              path.Sort();
              Δu = path[1].Parameter - path[0].Parameter;
              Assert.IsTrue(Vector2F.AreNumericallyEqual(new Vector2F(1, 0) / Δu, path.GetTangent(0)));
              Assert.IsTrue(Vector2F.AreNumericallyEqual((new Vector2F(7, 8) - new Vector2F(6, 7)) * 3 / Δu, path.GetTangent(100)));

              path.Add(new PathKey2F()
              {
            Parameter = 15,
            Point = new Vector2F(0, 0),
            Interpolation = SplineInterpolation.BSpline,
              });
              path.Sort();
        }
Пример #4
0
 public void GetTangentShouldReturnZeroIfPathIsEmpty()
 {
     Path2F empty = new Path2F();
       empty.Sort();
       Assert.AreEqual(Vector2F.Zero, empty.GetTangent(-0.5f));
       Assert.AreEqual(Vector2F.Zero, empty.GetTangent(0));
       Assert.AreEqual(Vector2F.Zero, empty.GetTangent(0.5f));
 }
Пример #5
0
        public void GetPointShouldReturnNanIfPathIsEmpty()
        {
            Path2F empty = new Path2F();
              empty.Sort();

              Vector2F p = empty.GetPoint(-0.5f);
              Assert.IsNaN(p.X);
              Assert.IsNaN(p.Y);

              p = empty.GetPoint(0);
              Assert.IsNaN(p.X);
              Assert.IsNaN(p.Y);

              p = empty.GetPoint(0.5f);
              Assert.IsNaN(p.X);
              Assert.IsNaN(p.Y);
        }