public void MultiplyOperator() { Pose p1 = new Pose(new Vector3(1, 2, 3), Quaternion.CreateRotationY(0.3f)); Pose p2 = new Pose(new Vector3(-4, 5, -6), Quaternion.CreateRotationZ(-0.1f)); Assert.IsTrue(Vector4.AreNumericallyEqual( p1.ToMatrix() * p2.ToMatrix() * new Vector4(1, 2, 3, 1), p1 * p2 * new Vector4(1, 2, 3, 1))); }
public void InterpolationTest() { var traits = Vector4Traits.Instance; var value0 = new Vector4(1, 2, 3, 1); var value1 = new Vector4(-4, 5, -6, 5); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)value0, (Vector4)traits.Interpolate(value0, value1, 0.0f))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)value1, (Vector4)traits.Interpolate(value0, value1, 1.0f))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(0.25f * value0 + 0.75f * value1), (Vector4)traits.Interpolate(value0, value1, 0.75f))); }
public void TestIsZero() { Vector4 nonZero = new Vector4(0.001f, 0.001f, 0.0f, 0.001f); Assert.IsFalse(Vector4.AreNumericallyEqual(nonZero, Vector4.Zero)); Vector4 zero = new Vector4(0.0000001f, 0.0000001f, 0.0f, 0.0000001f); Assert.IsTrue(Vector4.AreNumericallyEqual(zero, Vector4.Zero)); }
public void AreEqualWithEpsilon() { float epsilon = 0.001f; Vector4 u = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 v = new Vector4(1.002f, 2.002f, 3.002f, 4.002f); Vector4 w = new Vector4(1.0001f, 2.0001f, 3.0001f, 4.0001f); Assert.IsTrue(Vector4.AreNumericallyEqual(u, u, epsilon)); Assert.IsFalse(Vector4.AreNumericallyEqual(u, v, epsilon)); Assert.IsTrue(Vector4.AreNumericallyEqual(u, w, epsilon)); }
public void MultiplyTest() { var traits = Vector4Traits.Instance; var value = new Vector4(-1, -2, 3, 1); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)Vector4.Zero, (Vector4)traits.Multiply(value, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)value, (Vector4)traits.Multiply(value, 1))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(value + value), (Vector4)traits.Multiply(value, 2))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(value + value + value), (Vector4)traits.Multiply(value, 3))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(-value), (Vector4)traits.Multiply(value, -1))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(-value - value), (Vector4)traits.Multiply(value, -2))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(-value - value - value), (Vector4)traits.Multiply(value, -3))); }
public void MultiplyVector4() { var a = new SrtTransform(new Vector3(1, 2, 7), new Quaternion(1, 2, 3, 4).Normalized, new Vector3(4, -5, 6)); var v = new Vector4(7, 9, -12, -2); var result1 = a * v; var result2 = a.ToMatrix() * v; Assert.IsTrue(Vector4.AreNumericallyEqual(result1, result2)); result1 = SrtTransform.Multiply(a, v); result2 = a.ToMatrix() * v; Assert.IsTrue(Vector4.AreNumericallyEqual(result1, result2)); }
public void AnimateFromBy() { var animation = new Vector4FromToByAnimation(); animation.From = new Vector4(0.2f, 2.0f, 20.0f, 200.0f); animation.To = null; animation.By = new Vector4(1.0f, 10.0f, 100.0f, 1000.0f); Assert.AreEqual(new Vector4(0.2f, 2.0f, 20.0f, 200.0f), animation.GetValue(TimeSpan.FromSeconds(0.0), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), new Vector4(1.0f, 10.0f, 100.0f, 1000.0f))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(0.7f, 7.0f, 70.0f, 700.0f), animation.GetValue(TimeSpan.FromSeconds(0.5), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), new Vector4(1.0f, 10.0f, 100.0f, 1000.0f)))); Assert.AreEqual(new Vector4(1.2f, 12.0f, 120.0f, 1200.0f), animation.GetValue(TimeSpan.FromSeconds(1.0), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), new Vector4(1.0f, 10.0f, 100.0f, 1000.0f))); animation.By = new Vector4(-0.1f, -1.0f, -10.0f, -100.0f); Assert.AreEqual(new Vector4(0.2f, 2.0f, 20.0f, 200.0f), animation.GetValue(TimeSpan.FromSeconds(0.0), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), new Vector4(1.0f, 10.0f, 100.0f, 1000.0f))); Assert.AreEqual(new Vector4(0.15f, 1.5f, 15.0f, 150.0f), animation.GetValue(TimeSpan.FromSeconds(0.5), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), new Vector4(1.0f, 10.0f, 100.0f, 1000.0f))); Assert.AreEqual(new Vector4(0.1f, 1.0f, 10.0f, 100.0f), animation.GetValue(TimeSpan.FromSeconds(1.0), new Vector4(0.0f, 0.0f, 0.0f, 0.0f), new Vector4(1.0f, 10.0f, 100.0f, 1000.0f))); }
public void FromByTest() { // IAnimationValueTraits<T> is used in a from-by animation to a add a relative offset to // the start value. var traits = Vector4Traits.Instance; var from = new Vector4(-1, -2, 3, 1); var by = new Vector4(4, -5, 6, 1); var to = traits.Add(from, by); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(by + from), (Vector4)to)); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)from, (Vector4)traits.Add(to, traits.Inverse(by)))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)by, (Vector4)traits.Add(traits.Inverse(from), to))); }
public void AreEqual() { float originalEpsilon = Numeric.EpsilonF; Numeric.EpsilonF = 1e-8f; Vector4 u = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 v = new Vector4(1.000001f, 2.000001f, 3.000001f, 4.000001f); Vector4 w = new Vector4(1.00000001f, 2.00000001f, 3.00000001f, 4.00000001f); Assert.IsTrue(Vector4.AreNumericallyEqual(u, u)); Assert.IsFalse(Vector4.AreNumericallyEqual(u, v)); Assert.IsTrue(Vector4.AreNumericallyEqual(u, w)); Numeric.EpsilonF = originalEpsilon; }
public void Test1() { Pose p = Pose.Identity; Assert.AreEqual(Matrix.Identity, p.ToMatrix()); Assert.AreEqual(Matrix.Identity, p.Orientation); Assert.AreEqual(Vector3.Zero, p.Position); p.Position = new Vector3(1, 2, 3); p.Orientation = Matrix.CreateRotation(new Vector3(3, -4, 9), 0.49f); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToWorldDirection(Vector3.UnitX), 0), p * new Vector4(1, 0, 0, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToWorldDirection(Vector3.UnitY), 0), p * new Vector4(0, 1, 0, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToWorldDirection(Vector3.UnitZ), 0), p * new Vector4(0, 0, 1, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToWorldPosition(Vector3.UnitX), 1), p * new Vector4(1, 0, 0, 1))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToWorldPosition(Vector3.UnitY), 1), p * new Vector4(0, 1, 0, 1))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToWorldPosition(Vector3.UnitZ), 1), p * new Vector4(0, 0, 1, 1))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToLocalDirection(Vector3.UnitX), 0), p.Inverse * new Vector4(1, 0, 0, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToLocalDirection(Vector3.UnitY), 0), p.Inverse * new Vector4(0, 1, 0, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToLocalDirection(Vector3.UnitZ), 0), p.Inverse * new Vector4(0, 0, 1, 0))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToLocalPosition(Vector3.UnitX), 1), p.Inverse * new Vector4(1, 0, 0, 1))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToLocalPosition(Vector3.UnitY), 1), p.Inverse * new Vector4(0, 1, 0, 1))); Assert.IsTrue(Vector4.AreNumericallyEqual(new Vector4(p.ToLocalPosition(Vector3.UnitZ), 1), p.Inverse * new Vector4(0, 0, 1, 1))); Pose p2 = Pose.FromMatrix(new Matrix(p.Orientation, Vector3.Zero)); Assert.IsTrue(Matrix.AreNumericallyEqual(p.Orientation, p2.Orientation)); Assert.IsTrue(Vector3.AreNumericallyEqual(p2.Position, Vector3.Zero)); Matrix m = p2; m.SetColumn(3, new Vector4(p.Position, 1)); p2 = Pose.FromMatrix(m); Assert.IsTrue(Matrix.AreNumericallyEqual(p.Orientation, p2.Orientation)); Assert.AreEqual(p.Position, p2.Position); //Assert.IsTrue(Vector3.AreNumericallyEqual(p.Position, p2.Position)); // Test other constructors. Assert.AreEqual(Vector3.Zero, new Pose(Quaternion.CreateRotationX(0.3f)).Position); Assert.AreEqual(Matrix.CreateRotationX(0.3f), new Pose(Matrix.CreateRotationX(0.3f)).Orientation); Assert.AreEqual(new Vector3(1, 2, 3), new Pose(new Vector3(1, 2, 3)).Position); Assert.AreEqual(Matrix.Identity, new Pose(new Vector3(1, 2, 3)).Orientation); }
public void ViewTest() { CameraInstance cameraInstance = new CameraInstance(new Camera(new PerspectiveProjection())); Assert.AreEqual(Matrix.Identity, cameraInstance.View); Assert.AreEqual(Matrix.Identity, cameraInstance.ViewInverse); Vector3 position = new Vector3(1, 2, 3); Vector3 target = new Vector3(2, 5, 4); Vector3 upVector = new Vector3(1, 1, 1); Matrix view = Matrix.CreateLookAt(position, target, upVector); cameraInstance.View = view; Assert.AreEqual(view, cameraInstance.View); Assert.AreEqual(view.Inverse, cameraInstance.ViewInverse); Vector3 originOfCamera = cameraInstance.PoseWorld.Position; originOfCamera = cameraInstance.View.TransformPosition(originOfCamera); Assert.IsTrue(Vector3.AreNumericallyEqual(Vector3.Zero, originOfCamera)); Vector4 positionView = new Vector4(0, 0, -1, 1); Vector4 positionView2; // Transform a point from view space to world space. Vector4 positionWorld = cameraInstance.PoseWorld * positionView; Vector4 positionWorld2 = cameraInstance.ViewInverse * positionView; Assert.IsTrue(Vector4.AreNumericallyEqual(positionWorld, positionWorld2)); // Transform a point from world space to view space. positionView = cameraInstance.PoseWorld.Inverse * positionWorld; positionView2 = cameraInstance.View * positionWorld; Assert.IsTrue(Vector4.AreNumericallyEqual(positionView, positionView2)); cameraInstance.View = Matrix.Identity; Assert.AreEqual(Vector3.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix.Identity, cameraInstance.PoseWorld.Orientation); }
public void CycleOffsetTest() { // IAnimationValueTraits<T> is used in a cyclic animation to a add the cycle offset in // each iteration. var traits = Vector4Traits.Instance; var first = new Vector4(1, 2, 3, 1); // Animation value of first key frame. var last = new Vector4(-4, 5, -6, 5); // Animation value of last key frame. var cycleOffset = traits.Add(traits.Inverse(first), last); // Cycle offset should be the difference between last and first key frame. Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)last, (Vector4)traits.Add(first, cycleOffset))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)last, (Vector4)(cycleOffset + first))); // Check multiple cycles (post-loop). Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)last, (Vector4)traits.Add(first, traits.Multiply(cycleOffset, 1)))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(cycleOffset + cycleOffset + last), (Vector4)traits.Add(first, traits.Multiply(cycleOffset, 3)))); // Check multiple cycles (pre-loop). Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)first, (Vector4)traits.Add(last, traits.Multiply(cycleOffset, -1)))); Assert.IsTrue(Vector4.AreNumericallyEqual((Vector4)(first - cycleOffset - cycleOffset), (Vector4)traits.Add(last, traits.Multiply(cycleOffset, -3)))); }