Example #1
0
        public void SerializationXml()
        {
            CurveKey2F curveKey1 = new CurveKey2F
            {
                Interpolation = SplineInterpolation.Bezier,
                Point         = new Vector2F(1.2f, 3.4f),
                TangentIn     = new Vector2F(0.7f, 2.6f),
                TangentOut    = new Vector2F(1.9f, 3.3f)
            };
            CurveKey2F curveKey2;

            const string fileName = "SerializationCurve2FKey.xml";

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

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

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

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

            curveKey2 = (CurveKey2F)serializer.Deserialize(fileStream);
            MathAssert.AreEqual(curveKey1, curveKey2);
        }
Example #2
0
        public void SerializationBinary()
        {
            CurveKey2F curveKey1 = new CurveKey2F
            {
                Interpolation = SplineInterpolation.Bezier,
                Point         = new Vector2F(1.2f, 3.4f),
                TangentIn     = new Vector2F(0.7f, 2.6f),
                TangentOut    = new Vector2F(1.9f, 3.3f)
            };
            CurveKey2F curveKey2;

            const string fileName = "SerializationCurve2FKey.bin";

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

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

            BinaryFormatter formatter = new BinaryFormatter();

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

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

            MathAssert.AreEqual(curveKey1, curveKey2);
        }
Example #3
0
        public void SerializationXml()
        {
            CurveKey2F curveKey1 = new CurveKey2F
              {
               Interpolation = SplineInterpolation.Bezier,
               Point = new Vector2F(1.2f, 3.4f),
               TangentIn = new Vector2F(0.7f, 2.6f),
               TangentOut = new Vector2F(1.9f, 3.3f)
              };
              CurveKey2F curveKey2;

              const string fileName = "SerializationCurve2FKey.xml";

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

              XmlSerializer serializer = new XmlSerializer(typeof(CurveKey2F));
              StreamWriter writer = new StreamWriter(fileName);
              serializer.Serialize(writer, curveKey1);
              writer.Close();

              serializer = new XmlSerializer(typeof(CurveKey2F));
              FileStream fileStream = new FileStream(fileName, FileMode.Open);
              curveKey2 = (CurveKey2F)serializer.Deserialize(fileStream);
              MathAssert.AreEqual(curveKey1, curveKey2);
        }
Example #4
0
        public void SerializationBinary()
        {
            CurveKey2F curveKey1 = new CurveKey2F
              {
            Interpolation = SplineInterpolation.Bezier,
            Point = new Vector2F(1.2f, 3.4f),
            TangentIn = new Vector2F(0.7f, 2.6f),
            TangentOut = new Vector2F(1.9f, 3.3f)
              };
              CurveKey2F curveKey2;

              const string fileName = "SerializationCurve2FKey.bin";

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

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

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

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

              MathAssert.AreEqual(curveKey1, curveKey2);
        }
Example #5
0
        public void ParameterTest()
        {
            CurveKey2F curveKey = new CurveKey2F
            {
                Interpolation = SplineInterpolation.Bezier,
                Point         = new Vector2F(1.2f, 3.4f),
                TangentIn     = new Vector2F(0.7f, 2.6f),
                TangentOut    = new Vector2F(1.9f, 3.3f)
            };

            Assert.AreEqual(1.2f, curveKey.Parameter);

            curveKey.Parameter = 0.2f;
            Assert.AreEqual(0.2f, curveKey.Point.X);

            curveKey.Point = new Vector2F(1, 2);
            Assert.AreEqual(1, curveKey.Parameter);
            Assert.AreEqual(1, curveKey.Point.X);
            Assert.AreEqual(2, curveKey.Point.Y);
        }
Example #6
0
        public void ParameterTest()
        {
            CurveKey2F curveKey = new CurveKey2F
              {
            Interpolation = SplineInterpolation.Bezier,
            Point = new Vector2F(1.2f, 3.4f),
            TangentIn = new Vector2F(0.7f, 2.6f),
            TangentOut = new Vector2F(1.9f, 3.3f)
              };

              Assert.AreEqual(1.2f, curveKey.Parameter);

              curveKey.Parameter = 0.2f;
              Assert.AreEqual(0.2f, curveKey.Point.X);

              curveKey.Point = new Vector2F(1, 2);
              Assert.AreEqual(1, curveKey.Parameter);
              Assert.AreEqual(1, curveKey.Point.X);
              Assert.AreEqual(2, curveKey.Point.Y);
        }
Example #7
0
        public override void Update(GameTime gameTime)
        {
            bool updateCurve = false;

            // When the sample is started or when <Space> is pressed, we create a random curve.
            if (_curve == null || InputService.IsPressed(Keys.Space, true))
            {
                _curve = new Curve2F
                {
                    PreLoop    = _loopType,
                    PostLoop   = _loopType,
                    SmoothEnds = _smoothEnds
                };

                // Create random key points.
                for (float x = 0; x <= 1; x += 0.1f)
                {
                    float y = RandomHelper.Random.NextFloat(0, 1);

                    _curve.Add(new CurveKey2F
                    {
                        Point = new Vector2F(x, y),

                        // We use the same interpolation type (spline type) for all curve keys. But
                        // it is possible to use a different interpolation type for each curve key.
                        Interpolation = _interpolationType,
                    });
                }

                updateCurve = true;
            }

            // If <1> is pressed, we change the loop type.
            if (InputService.IsPressed(Keys.D1, true))
            {
                // Choose next enumeration value for _loopType.
                if ((int)_loopType + 1 >= EnumHelper.GetValues(typeof(CurveLoopType)).Length)
                {
                    _loopType = 0;
                }
                else
                {
                    _loopType++;
                }

                _curve.PreLoop  = _loopType;
                _curve.PostLoop = _loopType;
                updateCurve     = true;
            }

            // If <2> is pressed, we change the spline type.
            if (InputService.IsPressed(Keys.D2, true))
            {
                // Choose next enumeration value for _interpolationType.
                if ((int)_interpolationType + 1 >= EnumHelper.GetValues(typeof(SplineInterpolation)).Length)
                {
                    _interpolationType = 0;
                }
                else
                {
                    _interpolationType++;
                }

                // We skip Bezier and Hermite splines because both need additional information per
                // curve key (control points or tangents), which we did not specify in the curve creation.
                while (_interpolationType == SplineInterpolation.Bezier ||
                       _interpolationType == SplineInterpolation.Hermite)
                {
                    _interpolationType++;
                }

                _curve.ForEach(key => key.Interpolation = _interpolationType);
                updateCurve = true;
            }

            // If <3> is pressed, we toggle "SmoothEnds".
            // If SmoothEnds is enabled the curve is smoother at the first and at the last
            // key point. The effect of SmoothEnds is visible, for example, if the loop type
            // is "Oscillate" and the spline type is "CatmullRom".
            if (InputService.IsPressed(Keys.D3, true))
            {
                _smoothEnds       = !_smoothEnds;
                _curve.SmoothEnds = _smoothEnds;
                updateCurve       = true;
            }

            if (updateCurve)
            {
                var debugRenderer = GraphicsScreen.DebugRenderer2D;
                debugRenderer.Clear();
                debugRenderer.DrawText(
                    string.Format("\n\nLoop type = {0}\nInterpolation = {1}\nSmoothEnds = {2}",
                                  _curve.PreLoop, _curve[0].Interpolation, _curve.SmoothEnds));

                // Draw two lines to create chart axes.
                debugRenderer.DrawLine(new Vector3F(100, 300, 0), new Vector3F(1000, 300, 0), Color.Black, true);
                debugRenderer.DrawLine(new Vector3F(300, 100, 0), new Vector3F(300, 320, 0), Color.Black, true);

                Vector2F scale  = new Vector2F(400, 200);
                Vector2F offset = new Vector2F(300, 100);

                // Draw a small cross for all curve key points.
                for (int index = 0; index < _curve.Count; index++)
                {
                    CurveKey2F key   = _curve[index];
                    Vector2F   point = scale * key.Point + offset;
                    debugRenderer.DrawLine(new Vector3F(point.X - 5, point.Y - 5, 0), new Vector3F(point.X + 5, point.Y + 5, 0), Color.Black, true);
                    debugRenderer.DrawLine(new Vector3F(point.X + 5, point.Y - 5, 0), new Vector3F(point.X - 5, point.Y + 5, 0), Color.Black, true);
                }

                // Draw the curve.
                const float stepSize = 0.02f;
                for (float x = -0.5f; x < 1.7f; x += stepSize)
                {
                    Vector2F point0 = scale * _curve.GetPoint(x) + offset;
                    Vector2F point1 = scale * _curve.GetPoint(x + stepSize) + offset;
                    debugRenderer.DrawLine(new Vector3F(point0.X, point0.Y, 0), new Vector3F(point1.X, point1.Y, 0), Color.Black, true);
                }
            }

            base.Update(gameTime);
        }
Example #8
0
        public void SerializationXml()
        {
            CurveKey2F curveKey1 = new CurveKey2F
              {
            Interpolation = SplineInterpolation.Bezier,
            Point = new Vector2F(1.2f, 3.4f),
            TangentIn = new Vector2F(0.7f, 2.6f),
            TangentOut = new Vector2F(1.9f, 3.3f)
              };
              CurveKey2F curveKey2 = new CurveKey2F
              {
            Interpolation = SplineInterpolation.Hermite,
            Point = new Vector2F(2.2f, 4.4f),
            TangentIn = new Vector2F(1.7f, 3.6f),
            TangentOut = new Vector2F(2.9f, 4.3f)
              };
              Curve2F curve = new Curve2F { curveKey1, curveKey2 };
              curve.PreLoop = CurveLoopType.Cycle;
              curve.PostLoop = CurveLoopType.CycleOffset;
              curve.SmoothEnds = true;

              const string fileName = "SerializationCurve2F.xml";

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

              XmlSerializer serializer = new XmlSerializer(typeof(Curve2F));
              StreamWriter writer = new StreamWriter(fileName);
              serializer.Serialize(writer, curve);
              writer.Close();

              serializer = new XmlSerializer(typeof(Curve2F));
              FileStream fileStream = new FileStream(fileName, FileMode.Open);
              curve = (Curve2F)serializer.Deserialize(fileStream);
              Assert.AreEqual(2, curve.Count);
              MathAssert.AreEqual(curveKey1, curve[0]);
              MathAssert.AreEqual(curveKey2, curve[1]);
              Assert.AreEqual(CurveLoopType.Cycle, curve.PreLoop);
              Assert.AreEqual(CurveLoopType.CycleOffset, curve.PostLoop);
              Assert.AreEqual(true, curve.SmoothEnds);
        }
Example #9
0
        public void SerializationBinary()
        {
            CurveKey2F curveKey1 = new CurveKey2F
              {
            Interpolation = SplineInterpolation.Bezier,
            Point = new Vector2F(1.2f, 3.4f),
            TangentIn = new Vector2F(0.7f, 2.6f),
            TangentOut = new Vector2F(1.9f, 3.3f)
              };
              CurveKey2F curveKey2 = new CurveKey2F
              {
            Interpolation = SplineInterpolation.Hermite,
            Point = new Vector2F(2.2f, 4.4f),
            TangentIn = new Vector2F(1.7f, 3.6f),
            TangentOut = new Vector2F(2.9f, 4.3f)
              };
              Curve2F curve = new Curve2F { curveKey1, curveKey2 };
              curve.PreLoop = CurveLoopType.Cycle;
              curve.PostLoop = CurveLoopType.CycleOffset;
              curve.SmoothEnds = true;

              const string fileName = "SerializationCurve2F.bin";

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

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

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

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

              Assert.AreEqual(2, curve.Count);
              MathAssert.AreEqual(curveKey1, curve[0]);
              MathAssert.AreEqual(curveKey2, curve[1]);
              Assert.AreEqual(CurveLoopType.Cycle, curve.PreLoop);
              Assert.AreEqual(CurveLoopType.CycleOffset, curve.PostLoop);
              Assert.AreEqual(true, curve.SmoothEnds);
        }