Beispiel #1
0
        public void IdentityIsUnpackedAsIdentity()
        {
            var packer = new QuaternionPacker(10);

            packer.Pack(writer, Quaternion.identity);
            Quaternion unpack = packer.Unpack(GetReader());

            Assert.That(unpack, Is.EqualTo(Quaternion.identity));
        }
Beispiel #2
0
        private void RunPackAndUnpack(int bits, Quaternion inValueNotNormalized)
        {
            Quaternion inValue = inValueNotNormalized.normalized;

            // precision for 1 element
            float max       = (1 / Mathf.Sqrt(2));
            float precision = 2 * max / ((1 << bits) - 1);

            // allow extra precision because largest is caculated using the other 3 values so may be out side of precision
            precision *= 2;

            var packer = new QuaternionPacker(bits);

            packer.Pack(writer, inValueNotNormalized);

            Quaternion outValue = packer.Unpack(GetReader());

            //Debug.Log($"Packed: ({inValue.x:0.000},{inValue.y:0.000},{inValue.z:0.000},{inValue.w:0.000}) " +
            //          $"UnPacked: ({outValue.x:0.000},{outValue.y:0.000},{outValue.z:0.000},{outValue.w:0.000})");

            Assert.That(outValue.x, Is.Not.NaN, "x was NaN");
            Assert.That(outValue.y, Is.Not.NaN, "y was NaN");
            Assert.That(outValue.z, Is.Not.NaN, "z was NaN");
            Assert.That(outValue.w, Is.Not.NaN, "w was NaN");

            float assertSign = getAssertSign(inValue, outValue);

            Assert.That(outValue.x, IsUnSignedEqualWithIn(inValue.x), $"x off by {Mathf.Abs(assertSign * inValue.x - outValue.x)}");
            Assert.That(outValue.y, IsUnSignedEqualWithIn(inValue.y), $"y off by {Mathf.Abs(assertSign * inValue.y - outValue.y)}");
            Assert.That(outValue.z, IsUnSignedEqualWithIn(inValue.z), $"z off by {Mathf.Abs(assertSign * inValue.z - outValue.z)}");
            Assert.That(outValue.w, IsUnSignedEqualWithIn(inValue.w), $"w off by {Mathf.Abs(assertSign * inValue.w - outValue.w)}");

            Vector3 inVec  = inValue * Vector3.forward;
            Vector3 outVec = outValue * Vector3.forward;

            // allow for extra precision when rotating vector
            Assert.AreEqual(inVec.x, outVec.x, precision * 2, $"vx off by {Mathf.Abs(inVec.x - outVec.x)}");
            Assert.AreEqual(inVec.y, outVec.y, precision * 2, $"vy off by {Mathf.Abs(inVec.y - outVec.y)}");
            Assert.AreEqual(inVec.z, outVec.z, precision * 2, $"vz off by {Mathf.Abs(inVec.z - outVec.z)}");


            EqualConstraint IsUnSignedEqualWithIn(float v)
            {
                return(Is.EqualTo(v).Within(precision).Or.EqualTo(assertSign * v).Within(precision));
            }
        }