Exemple #1
0
        public void SerializationBinary()
        {
            PathKey3F pathKey1 = new PathKey3F
            {
                Interpolation = SplineInterpolation.Bezier,
                Parameter     = 56.7f,
                Point         = new Vector3(1.2f, 3.4f, 5.6f),
                TangentIn     = new Vector3(0.7f, 2.6f, 5.1f),
                TangentOut    = new Vector3(1.9f, 3.3f, 5.9f)
            };
            PathKey3F pathKey2;

            const string fileName = "SerializationPath3FKey.bin";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            FileStream fs = new FileStream(fileName, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(fs, pathKey1);
            fs.Close();

            fs        = new FileStream(fileName, FileMode.Open);
            formatter = new BinaryFormatter();
            pathKey2  = (PathKey3F)formatter.Deserialize(fs);
            fs.Close();

            MathAssert.AreEqual(pathKey1, pathKey2);
        }
Exemple #2
0
        public void SerializationXml()
        {
            PathKey3F pathKey1 = new PathKey3F
            {
                Interpolation = SplineInterpolation.Bezier,
                Parameter     = 56.7f,
                Point         = new Vector3(1.2f, 3.4f, 5.6f),
                TangentIn     = new Vector3(0.7f, 2.6f, 5.1f),
                TangentOut    = new Vector3(1.9f, 3.3f, 5.9f)
            };
            PathKey3F pathKey2;

            const string fileName = "SerializationPath3FKey.xml";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            XmlSerializer serializer = new XmlSerializer(typeof(PathKey3F));
            StreamWriter  writer     = new StreamWriter(fileName);

            serializer.Serialize(writer, pathKey1);
            writer.Close();

            serializer = new XmlSerializer(typeof(PathKey3F));
            FileStream fileStream = new FileStream(fileName, FileMode.Open);

            pathKey2 = (PathKey3F)serializer.Deserialize(fileStream);
            MathAssert.AreEqual(pathKey1, pathKey2);
        }
Exemple #3
0
        public void SerializationXml()
        {
            PathKey3F pathKey1 = new PathKey3F
              {
            Interpolation = SplineInterpolation.Bezier,
            Parameter = 56.7f,
            Point = new Vector3F(1.2f, 3.4f, 5.6f),
            TangentIn = new Vector3F(0.7f, 2.6f, 5.1f),
            TangentOut = new Vector3F(1.9f, 3.3f, 5.9f)
              };
              PathKey3F pathKey2;

              const string fileName = "SerializationPath3FKey.xml";

              if (File.Exists(fileName))
            File.Delete(fileName);

              XmlSerializer serializer = new XmlSerializer(typeof(PathKey3F));
              StreamWriter writer = new StreamWriter(fileName);
              serializer.Serialize(writer, pathKey1);
              writer.Close();

              serializer = new XmlSerializer(typeof(PathKey3F));
              FileStream fileStream = new FileStream(fileName, FileMode.Open);
              pathKey2 = (PathKey3F)serializer.Deserialize(fileStream);
              MathAssert.AreEqual(pathKey1, pathKey2);
        }
Exemple #4
0
        public void SerializationBinary()
        {
            PathKey3F pathKey1 = new PathKey3F
              {
            Interpolation = SplineInterpolation.Bezier,
            Parameter = 56.7f,
            Point = new Vector3F(1.2f, 3.4f, 5.6f),
            TangentIn = new Vector3F(0.7f, 2.6f, 5.1f),
            TangentOut = new Vector3F(1.9f, 3.3f, 5.9f)
              };
              PathKey3F pathKey2;

              const string fileName = "SerializationPath3FKey.bin";

              if (File.Exists(fileName))
            File.Delete(fileName);

              FileStream fs = new FileStream(fileName, FileMode.Create);

              BinaryFormatter formatter = new BinaryFormatter();
              formatter.Serialize(fs, pathKey1);
              fs.Close();

              fs = new FileStream(fileName, FileMode.Open);
              formatter = new BinaryFormatter();
              pathKey2 = (PathKey3F)formatter.Deserialize(fs);
              fs.Close();

              MathAssert.AreEqual(pathKey1, pathKey2);
        }
Exemple #5
0
        private void CreatePath()
        {
            // Create a cyclic path. (More information on paths can be found in the DigitalRune
            // Mathematics documentation and related samples.)
            _path = new Path3F
            {
                SmoothEnds = true,
                PreLoop    = CurveLoopType.Cycle,
                PostLoop   = CurveLoopType.Cycle
            };

            // The curvature of the path is defined by a number of path keys.
            _path.Add(new PathKey3F
            {
                Parameter     = 0,                              // The path parameter defines position of the path key on the curve.
                Point         = new Vector3(-4, 0.5f, -3),      // The world space position of the path key.
                Interpolation = SplineInterpolation.CatmullRom, // The type of interpolation that is used between this path key and the next.
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 1,
                Point         = new Vector3(-1, 0.5f, -5),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 2,
                Point         = new Vector3(3, 0.5f, -4),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 3,
                Point         = new Vector3(0, 0.5f, 0),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 4,
                Point         = new Vector3(-3, 0.5f, 3),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 5,
                Point         = new Vector3(-1, 0.5f, 5),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 6,
                Point         = new Vector3(0, 0.5f, 0),
                Interpolation = SplineInterpolation.CatmullRom,
            });

            // The last key uses the same position as the first key to create a closed path.
            PathKey3F lastKey = new PathKey3F
            {
                Parameter     = _path.Count,
                Point         = _path[0].Point,
                Interpolation = SplineInterpolation.CatmullRom,
            };

            _path.Add(lastKey);

            // The current path parameter goes from 0 to 7. This path parameter is not linearly
            // proportional to the path length. This is not suitable for animations.
            // To move an object with constant speed along a path, the path parameter should
            // be linearly proportional to the length of the path.
            // ParameterizeByLength() changes the path parameter so that the path parameter
            // at the each key is equal to the length of path (measured from the first key position
            // to the current key position).
            // ParameterizeByLength() uses an iterative process, we end the process after 10
            // iterations or when the error is less than 0.01f.
            _path.ParameterizeByLength(10, 0.01f);

            // Sample the path for rendering.
            int   numberOfSamples = _pointList.Length - 1;
            float pathLength      = _path.Last().Parameter;

            for (int i = 0; i <= numberOfSamples; i++)
            {
                Vector3 pointOnPath = _path.GetPoint(pathLength / numberOfSamples * i);
                _pointList[i] = pointOnPath;
            }
        }
Exemple #6
0
        public void SerializationXml()
        {
            PathKey3F pathKey1 = new PathKey3F
              {
            Interpolation = SplineInterpolation.Bezier,
            Parameter = 56.7f,
            Point = new Vector3F(1.2f, 3.4f, 5.6f),
            TangentIn = new Vector3F(0.7f, 2.6f, 5.1f),
            TangentOut = new Vector3F(1.9f, 3.3f, 5.9f)
              };
              PathKey3F pathKey2 = new PathKey3F
              {
            Interpolation = SplineInterpolation.Hermite,
            Parameter = 66.7f,
            Point = new Vector3F(2.2f, 1.4f, 6.6f),
            TangentIn = new Vector3F(1.7f, 3.6f, 4.1f),
            TangentOut = new Vector3F(2.9f, 2.3f, 6.9f)
              };
              Path3F path = new Path3F { pathKey1, pathKey2 };
              path.PreLoop = CurveLoopType.Cycle;
              path.PostLoop = CurveLoopType.CycleOffset;
              path.SmoothEnds = true;

              const string fileName = "SerializationPath3F.xml";

              if (File.Exists(fileName))
            File.Delete(fileName);

              XmlSerializer serializer = new XmlSerializer(typeof(Path3F));
              StreamWriter writer = new StreamWriter(fileName);
              serializer.Serialize(writer, path);
              writer.Close();

              serializer = new XmlSerializer(typeof(Path3F));
              FileStream fileStream = new FileStream(fileName, FileMode.Open);
              path = (Path3F)serializer.Deserialize(fileStream);

              Assert.AreEqual(2, path.Count);
              MathAssert.AreEqual(pathKey1, path[0]);
              MathAssert.AreEqual(pathKey2, path[1]);
              Assert.AreEqual(CurveLoopType.Cycle, path.PreLoop);
              Assert.AreEqual(CurveLoopType.CycleOffset, path.PostLoop);
              Assert.AreEqual(true, path.SmoothEnds);
        }
Exemple #7
0
        // Creates a random 3D path.
        private void CreatePath()
        {
            // Create a cyclic path.
            _path = new Path3F
            {
                PreLoop    = CurveLoopType.Cycle,
                PostLoop   = CurveLoopType.Cycle,
                SmoothEnds = true
            };

            // Add random path key points.
            for (int i = 0; i < 5; i++)
            {
                float x   = RandomHelper.Random.NextFloat(-3, 3);
                float y   = RandomHelper.Random.NextFloat(1, 3);
                float z   = RandomHelper.Random.NextFloat(-3, 0);
                var   key = new PathKey3F
                {
                    Parameter     = i,
                    Point         = new Vector3F(x, y, z),
                    Interpolation = SplineInterpolation.CatmullRom
                };
                _path.Add(key);
            }

            // The last key uses the same position as the first key to create a closed path.
            var lastKey = new PathKey3F
            {
                Parameter     = _path.Count,
                Point         = _path[0].Point,
                Interpolation = SplineInterpolation.CatmullRom,
            };

            _path.Add(lastKey);

            // The current path parameter goes from 0 to 5. This path parameter is not linearly
            // proportional to the path length. This is not suitable for animations.
            // To move an object with constant speed along the path, the path parameter should
            // be linearly proportional to the length of the path.
            // ParameterizeByLength() changes the path parameter so that the path parameter
            // at the each key is equal to the length of path (measured from the first key position
            // to the current key position).
            // ParameterizeByLength() uses and iterative process, we end the process after 10
            // iterations or when the error is less than 0.001f.
            _path.ParameterizeByLength(10, 0.001f);

            // Now, the parameter of the first key (_path[0]) is unchanged.
            // The parameter of the second key (_path[1]) is equal to the length of the path
            // from the first key to the second key.
            // The parameter of the third key (_path[2]) is equal to the length of the path
            // from the first key to the third key.
            // And so on.

            // The parameter of the last key is equal to the length of the whole path:
            //   float pathLength = _path[_path.Count - 1].Parameter;

            // Important: The path parameter is now equal to the path length at the path keys.
            // But in general between path keys the path parameter is not linearly proportional
            // to the path length. This is due to the nature of splines.
            //
            // Example:
            // Lets assume the second path key is at path length 100 and the third key is
            // at path length 200.
            // If we call _path.GetPoint(100), we get the position of the second key.
            // If we call _path.GetPoint(200), we get the position ot the third key.
            // We can call _path.GetPoint(130) to get a position on the path between the second and
            // third key. But it is not guaranteed that the path is exactly 130 long at this position.
            // We only know that the point is somewhere between 100 and 200 path length.
            //
            // To get the path point at exactly the distance 130 from the path start, we have to call
            //   float parameter = _path.GetParameterFromLength(130, 10, 0.01f);
            // This uses an iterative root finding process to find the path parameter where the
            // path length is 130.
            // Then we can get the path position with
            //   Vector3F pathPointAt130Length = _path.GetPoint(parameter).
        }
Exemple #8
0
    // Creates a random 3D path.
    private void CreatePath()
    {
      // Create a cyclic path.
      _path = new Path3F
      {
        PreLoop = CurveLoopType.Cycle,
        PostLoop = CurveLoopType.Cycle,
        SmoothEnds = true
      };

      // Add random path key points.
      for (int i = 0; i < 5; i++)
      {
        float x = RandomHelper.Random.NextFloat(-3, 3);
        float y = RandomHelper.Random.NextFloat(1, 3);
        float z = RandomHelper.Random.NextFloat(-3, 0);
        var key = new PathKey3F
        {
          Parameter = i,
          Point = new Vector3F(x, y, z),
          Interpolation = SplineInterpolation.CatmullRom
        };
        _path.Add(key);
      }

      // The last key uses the same position as the first key to create a closed path.
      var lastKey = new PathKey3F
      {
        Parameter = _path.Count,
        Point = _path[0].Point,
        Interpolation = SplineInterpolation.CatmullRom,
      };
      _path.Add(lastKey);

      // The current path parameter goes from 0 to 5. This path parameter is not linearly
      // proportional to the path length. This is not suitable for animations.
      // To move an object with constant speed along the path, the path parameter should
      // be linearly proportional to the length of the path.
      // ParameterizeByLength() changes the path parameter so that the path parameter
      // at the each key is equal to the length of path (measured from the first key position
      // to the current key position).
      // ParameterizeByLength() uses and iterative process, we end the process after 10 
      // iterations or when the error is less than 0.001f.
      _path.ParameterizeByLength(10, 0.001f);

      // Now, the parameter of the first key (_path[0]) is unchanged.
      // The parameter of the second key (_path[1]) is equal to the length of the path
      // from the first key to the second key.
      // The parameter of the third key (_path[2]) is equal to the length of the path
      // from the first key to the third key.
      // And so on. 

      // The parameter of the last key is equal to the length of the whole path:
      //   float pathLength = _path[_path.Count - 1].Parameter;

      // Important: The path parameter is now equal to the path length at the path keys.
      // But in general between path keys the path parameter is not linearly proportional
      // to the path length. This is due to the nature of splines.
      //
      // Example: 
      // Lets assume the second path key is at path length 100 and the third key is 
      // at path length 200.
      // If we call _path.GetPoint(100), we get the position of the second key.
      // If we call _path.GetPoint(200), we get the position ot the third key.
      // We can call _path.GetPoint(130) to get a position on the path between the second and
      // third key. But it is not guaranteed that the path is exactly 130 long at this position.
      // We only know that the point is somewhere between 100 and 200 path length.
      //
      // To get the path point at exactly the distance 130 from the path start, we have to call
      //   float parameter = _path.GetParameterFromLength(130, 10, 0.01f);
      // This uses an iterative root finding process to find the path parameter where the
      // path length is 130.
      // Then we can get the path position with 
      //   Vector3F pathPointAt130Length = _path.GetPoint(parameter).
    }
Exemple #9
0
    private void CreatePath()
    {
      // Create a cyclic path. (More information on paths can be found in the DigitalRune
      // Mathematics documentation and related samples.)
      _path = new Path3F
      {
        SmoothEnds = true,
        PreLoop = CurveLoopType.Cycle,
        PostLoop = CurveLoopType.Cycle
      };

      // The curvature of the path is defined by a number of path keys.
      _path.Add(new PathKey3F
      {
        Parameter = 0,                                  // The path parameter defines position of the path key on the curve.
        Point = new Vector3F(-4, 0.5f, -3),             // The world space position of the path key.
        Interpolation = SplineInterpolation.CatmullRom, // The type of interpolation that is used between this path key and the next.
      });
      _path.Add(new PathKey3F
      {
        Parameter = 1,
        Point = new Vector3F(-1, 0.5f, -5),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 2,
        Point = new Vector3F(3, 0.5f, -4),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 3,
        Point = new Vector3F(0, 0.5f, 0),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 4,
        Point = new Vector3F(-3, 0.5f, 3),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 5,
        Point = new Vector3F(-1, 0.5f, 5),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 6,
        Point = new Vector3F(0, 0.5f, 0),
        Interpolation = SplineInterpolation.CatmullRom,
      });

      // The last key uses the same position as the first key to create a closed path.
      PathKey3F lastKey = new PathKey3F
      {
        Parameter = _path.Count,
        Point = _path[0].Point,
        Interpolation = SplineInterpolation.CatmullRom,
      };
      _path.Add(lastKey);

      // The current path parameter goes from 0 to 7. This path parameter is not linearly
      // proportional to the path length. This is not suitable for animations.
      // To move an object with constant speed along a path, the path parameter should
      // be linearly proportional to the length of the path.
      // ParameterizeByLength() changes the path parameter so that the path parameter
      // at the each key is equal to the length of path (measured from the first key position
      // to the current key position).
      // ParameterizeByLength() uses an iterative process, we end the process after 10 
      // iterations or when the error is less than 0.01f.
      _path.ParameterizeByLength(10, 0.01f);

      // Sample the path for rendering.
      int numberOfSamples = _pointList.Length - 1;
      float pathLength = _path.Last().Parameter;
      for (int i = 0; i <= numberOfSamples; i++)
      {
        Vector3F pointOnPath = _path.GetPoint(pathLength / numberOfSamples * i);
        _pointList[i] = pointOnPath;
      }
    }