Example #1
0
        [TestCase(8, -8, -8, 3, -4, -5, 0, 0, 0)]    // +x-y-z vert
        public void TestClosestPointOnBox(
            float px, float py, float pz,
            float spx, float spy, float spz,
            float nx, float ny, float nz)
        {
            var pos                 = new Vector3(px, py, pz);
            var halfWidth           = new Vector3(3, 4, 5);
            var expectedSurfacePos  = new Vector3(spx, spy, spz);
            var expectedSurfaceNorm = new Vector3(nx, ny, nz);

            Vector3 surfacePos, surfaceNorm;

            CubeStencil.FindClosestPointOnBoxSurface(pos, halfWidth,
                                                     out surfacePos, out surfaceNorm);
            MathTestUtils.AssertAlmostEqual(expectedSurfacePos, surfacePos);
            if (expectedSurfaceNorm != Vector3.zero)
            {
                MathTestUtils.AssertAlmostEqual(expectedSurfaceNorm, surfaceNorm);
            }
            else
            {
                // Test case wants us to calculate it from scratch
                Vector3 diff = pos - expectedSurfacePos;
                if (diff != Vector3.zero)
                {
                    MathTestUtils.AssertAlmostEqual(diff.normalized, surfaceNorm);
                }
            }
        }
Example #2
0
        public void TestClosestPointOnBoxEdgeCases()
        {
            var halfWidth = new Vector3(3, 4, 5);

            // Try all permutations of points directly on verts, faces, edges
            for (int xsign = -1; xsign <= 1; ++xsign)
            {
                for (int ysign = -1; ysign <= 1; ++ysign)
                {
                    for (int zsign = -1; zsign <= 1; ++zsign)
                    {
                        int numFaces = Mathf.Abs(xsign) + Mathf.Abs(ysign) + Mathf.Abs(zsign);
                        // Only care about running tests where the point is on 2 or 3 faces (ie edge, or vert)
                        if (numFaces <= 1)
                        {
                            continue;
                        }
                        var pos = new Vector3(xsign * halfWidth.x,
                                              ysign * halfWidth.y,
                                              zsign * halfWidth.z);

                        Vector3 surfacePos, surfaceNorm;
                        CubeStencil.FindClosestPointOnBoxSurface(pos, halfWidth,
                                                                 out surfacePos, out surfaceNorm);
                        MathTestUtils.AssertAlmostEqual(pos, surfacePos);
                        MathTestUtils.AssertAlmostEqual(1, surfaceNorm.magnitude);
                        for (int axis = 0; axis < 3; ++axis)
                        {
                            float p = pos[axis];
                            float h = halfWidth[axis];
                            float n = surfaceNorm[axis];
                            // The normal is not well defined, but it should at least point away from the box
                            if (p == h)
                            {
                                Assert.GreaterOrEqual(n, 0, "Axis {0}", axis);
                            }
                            else if (p == -h)
                            {
                                Assert.LessOrEqual(n, 0, "Axis {0}", axis);
                            }
                            else if (-h < p & p < h)
                            {
                                // Should have no component parallel to the edge we're on
                                Assert.AreEqual(n, 0, "Axis {0}", axis);
                            }
                            else
                            {
                                Assert.Fail("Bad test");
                            }
                        }
                    }
                }
            }
        }