mPlaneTest.cpp 3.0 KB

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