mPlaneTest.cpp

Engine/source/math/test/mPlaneTest.cpp

More...

Detailed Description

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2014 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#ifdef TORQUE_TESTS_ENABLED
25#include "testing/unitTesting.h"
26#include "math/mPlane.h"
27
28// Static test data. All combinations of position and normal are tested in each
29// test case. This allows a large number of tests without introducing non-
30// deterministic test behavior.
31
32static const Point3F positions[] = {Point3F(0, 0, 0), Point3F(1, -2, 3), Point3F(1e-2, -2e-2, 1)};
33static const U32 numPositions = sizeof(positions) / sizeof(Point3F);
34
35static const Point3F normals[] = {Point3F(1, 0, 0), Point3F(-4, -2, 6)};
36static const U32 numNormals = sizeof(normals) / sizeof(Point3F);
37
38/// Tests that points in the direction of the normal are in 'Front' of the
39/// plane, while points in the reverse direction of the normal are in
40/// 'Back' of the plane.
41TEST(Plane, WhichSide)
42{
43   for(U32 i = 0; i < numPositions; i++) {
44      for(U32 j = 0; j < numNormals; j++) {
45         Point3F position = positions[i];
46         Point3F normal = normals[j];
47
48         PlaneF p(position, normal);
49
50         EXPECT_EQ(p.whichSide(position + normal), PlaneF::Front );
51         EXPECT_EQ(p.whichSide(position - normal), PlaneF::Back );
52         EXPECT_EQ(p.whichSide(position), PlaneF::On );
53      }
54   }
55}
56
57/// Tests that the distToPlane method returns the exact length that the test
58/// point is offset by in the direction of the normal.
59TEST(Plane, DistToPlane)
60{
61   for(U32 i = 0; i < numPositions; i++) {
62      for(U32 j = 0; j < numNormals; j++) {
63         Point3F position = positions[i];
64         Point3F normal = normals[j];
65
66         PlaneF p(position, normal);
67
68         EXPECT_FLOAT_EQ(p.distToPlane(position + normal), normal.len());
69         EXPECT_FLOAT_EQ(p.distToPlane(position - normal), -normal.len());
70         EXPECT_FLOAT_EQ(p.distToPlane(position), 0);
71      }
72   }
73}
74
75#endif
76