testPolyhedron.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "unit/test.h"
  23. #include "math/mPolyhedron.h"
  24. #ifndef TORQUE_SHIPPING
  25. using namespace UnitTesting;
  26. #define TEST( x ) test( ( x ), "FAIL: " #x )
  27. #define XTEST( t, x ) t->test( ( x ), "FAIL: " #x )
  28. CreateUnitTest( TestMathPolyhedronBuildFromPlanes, "Math/Polyhedron/BuildFromPlanes" )
  29. {
  30. void test_unitCube()
  31. {
  32. Vector< PlaneF > planes;
  33. // Build planes for a unit cube centered at the origin.
  34. // Note that the normals must be facing inwards.
  35. planes.push_back( PlaneF( Point3F( -0.5f, 0.f, 0.f ), Point3F( 1.f, 0.f, 0.f ) ) );
  36. planes.push_back( PlaneF( Point3F( 0.5f, 0.f, 0.f ), Point3F( -1.f, 0.f, 0.f ) ) );
  37. planes.push_back( PlaneF( Point3F( 0.f, -0.5f, 0.f ), Point3F( 0.f, 1.f, 0.f ) ) );
  38. planes.push_back( PlaneF( Point3F( 0.f, 0.5f, 0.f ), Point3F( 0.f, -1.f, 0.f ) ) );
  39. planes.push_back( PlaneF( Point3F( 0.f, 0.f, -0.5f ), Point3F( 0.f, 0.f, 1.f ) ) );
  40. planes.push_back( PlaneF( Point3F( 0.f, 0.f, 0.5f ), Point3F( 0.f, 0.f, -1.f ) ) );
  41. // Turn it into a polyhedron.
  42. Polyhedron polyhedron;
  43. polyhedron.buildFromPlanes(
  44. PlaneSetF( planes.address(), planes.size() )
  45. );
  46. // Check if we got a cube back.
  47. TEST( polyhedron.getNumPoints() == 8 );
  48. TEST( polyhedron.getNumPlanes() == 6 );
  49. TEST( polyhedron.getNumEdges() == 12 );
  50. }
  51. void test_extraPlane()
  52. {
  53. Vector< PlaneF > planes;
  54. // Build planes for a unit cube centered at the origin.
  55. // Note that the normals must be facing inwards.
  56. planes.push_back( PlaneF( Point3F( -0.5f, 0.f, 0.f ), Point3F( 1.f, 0.f, 0.f ) ) );
  57. planes.push_back( PlaneF( Point3F( 0.5f, 0.f, 0.f ), Point3F( -1.f, 0.f, 0.f ) ) );
  58. planes.push_back( PlaneF( Point3F( 0.f, -0.5f, 0.f ), Point3F( 0.f, 1.f, 0.f ) ) );
  59. planes.push_back( PlaneF( Point3F( 0.f, 0.5f, 0.f ), Point3F( 0.f, -1.f, 0.f ) ) );
  60. planes.push_back( PlaneF( Point3F( 0.f, 0.f, -0.5f ), Point3F( 0.f, 0.f, 1.f ) ) );
  61. planes.push_back( PlaneF( Point3F( 0.f, 0.f, 0.5f ), Point3F( 0.f, 0.f, -1.f ) ) );
  62. // Add extra plane that doesn't contribute a new edge.
  63. planes.push_back( PlaneF( Point3F( 0.5f, 0.5f, 0.5f ), Point3F( -1.f, -1.f, -1.f ) ) );
  64. // Turn it into a polyhedron.
  65. Polyhedron polyhedron;
  66. polyhedron.buildFromPlanes(
  67. PlaneSetF( planes.address(), planes.size() )
  68. );
  69. // Check if we got a cube back.
  70. TEST( polyhedron.getNumPoints() == 8 );
  71. TEST( polyhedron.getNumPlanes() == 6 );
  72. TEST( polyhedron.getNumEdges() == 12 );
  73. }
  74. void run()
  75. {
  76. test_unitCube();
  77. //test_extraPlane();
  78. }
  79. };
  80. #endif // !TORQUE_SHIPPING