Example #1
0
        public static void TestUnwrappedMeshAreas()
        {
            var mesh = Unwrapper.MakeUnwrappedMeshBFS(MeshUnwrapper.Common.Icosahedron.Points, MeshUnwrapper.Common.Icosahedron.Triangles);

            HashSet <int> usedTris = new HashSet <int>();

            int N = 0;

            foreach (var tri in mesh.Triangles)
            {
                var  p0    = mesh.Vertices[tri.p0];
                var  p1    = mesh.Vertices[tri.p1];
                var  p2    = mesh.Vertices[tri.p2];
                var  e0    = p1 - p0;
                var  e1    = p2 - p0;
                var  A0    = Vec3HighPrecision.Cross(e0, e1).Length / 2.0;
                bool match = false;
                for (int i = 0; i < MeshUnwrapper.Common.Icosahedron.Triangles.Length; i++)
                {
                    if (usedTris.Contains(i))
                    {
                        continue;
                    }
                    var tri0 = MeshUnwrapper.Common.Icosahedron.Triangles[i];
                    var q0   = MeshUnwrapper.Common.Icosahedron.Points[tri0.p0];
                    var q1   = MeshUnwrapper.Common.Icosahedron.Points[tri0.p1];
                    var q2   = MeshUnwrapper.Common.Icosahedron.Points[tri0.p2];
                    var f0   = q1 - q0;
                    var f1   = q2 - q0;
                    var A1   = Vec3HighPrecision.Cross(f0, f1).Length / 2.0;
                    var err  = Math.Abs(A0 - A1);
                    if (err < 0.001)
                    {
                        usedTris.Add(i);
                        match = true;
                        break;
                    }
                }
                Assert.True(match, "Area of triangles is distorted, N=" + N + ", A0=" + A0);
                N++;
            }
        }
Example #2
0
        public static void TestUnwrappedMeshInXZPlane()
        {
            var mesh = Unwrapper.MakeUnwrappedMeshBFS(MeshUnwrapper.Common.Icosahedron.Points, MeshUnwrapper.Common.Icosahedron.Triangles);

            Assert.AreEqual(MeshUnwrapper.Common.Icosahedron.Triangles.Length, mesh.Triangles.Count, "Triangle counts don't match!");

            int N = 0;

            foreach (var tri in mesh.Triangles)
            {
                var p0 = mesh.Vertices[tri.p0];
                var p1 = mesh.Vertices[tri.p1];
                var p2 = mesh.Vertices[tri.p2];
                var e0 = p1 - p0;
                var e1 = p2 - p0;
                var n  = Vec3HighPrecision.Cross(e0, e1).Normalized;
                Assert.That(n.Y, Is.EqualTo(1.0).Within(0.01), string.Format("Normal {0} of tri #{1} ({2}-{3}-{4}) not facing up.", n, N, p0, p1, p2));
                N++;
            }
        }
Example #3
0
        public void TestAngleAxis()
        {
            Assert.AreEqual(
                new Vec3HighPrecision(0, 0, 1),
                (new Vec4(0, 0, -1, 0) * Mat4HighPrecision.AngleAxisRot(new Vec3HighPrecision(0, 1, 0), -1.0, 0.0)).ToVec3() // PI = -1
                );

            Vec3HighPrecision.AssertAreEqual(
                new Vec3HighPrecision(-1, 0, 0),
                (new Vec4(0, 0, -1, 0) * Mat4HighPrecision.AngleAxisRot(new Vec3HighPrecision(0, 1, 0), 0.0, 1.0)).ToVec3(), // PI/2 = 0
                "Rotation by 90 degrees along Y axis failed");

            Vec3HighPrecision.AssertAreEqual(
                new Vec3HighPrecision(1, 0, 0),
                (new Vec4(0, 0, -1, 0) * Mat4HighPrecision.AngleAxisRot(new Vec3HighPrecision(0, 1, 0), 0.0, -1.0)).ToVec3(), // -PI/2 = 0
                "Rotation by -90 degrees along Y axis failed");

            Vec3HighPrecision.AssertAreEqual(
                new Vec3HighPrecision(0, -1, 0),
                (new Vec4(0, 0, -1, 0) * Mat4HighPrecision.AngleAxisRot(new Vec3HighPrecision(1, 0, 0), 0.0, -1.0)).ToVec3(), // -PI/2 = 0
                "Rotation by 90 degrees along X axis failed");

            var rnd = new System.Random();

            for (int i = 0; i < 100; i++)
            {
                var k = (new Vec3HighPrecision(rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble()) -
                         new Vec3HighPrecision(0.5, 0.5, 0.5)) * 100.0;
                var v = (new Vec3HighPrecision(rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble()) -
                         new Vec3HighPrecision(0.5, 0.5, 0.5)) * 100.0;
                var a     = rnd.NextDouble() * Math.PI * 2.0 - Math.PI;
                var vr    = (new Vec4(v, 1.0) * Mat4HighPrecision.AngleAxisRot(k.Normalized, Math.Cos(a), Math.Sin(a))).ToVec3();
                var vproj = k.Normalized * Vec3HighPrecision.Dot(v, k.Normalized);
                var ang   = Math.Acos(Vec3HighPrecision.Dot((vproj - v).Normalized, (vproj - vr).Normalized));
                Assert.That(Math.Abs(a), Is.EqualTo(ang).Within(0.00001), "Random rotation around random axis failed.");
            }
        }