test_plane.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************/
  2. /* test_plane.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_PLANE_H
  31. #define TEST_PLANE_H
  32. #include "core/math/plane.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestPlane {
  35. // Plane
  36. TEST_CASE("[Plane] Constructor methods") {
  37. const Plane plane = Plane(32, 22, 16, 3);
  38. const Plane plane_vector = Plane(Vector3(32, 22, 16), 3);
  39. const Plane plane_copy_plane = Plane(plane);
  40. CHECK_MESSAGE(
  41. plane == plane_vector,
  42. "Planes created with same values but different methods should be equal.");
  43. CHECK_MESSAGE(
  44. plane == plane_copy_plane,
  45. "Planes created with same values but different methods should be equal.");
  46. }
  47. TEST_CASE("[Plane] Basic getters") {
  48. const Plane plane = Plane(32, 22, 16, 3);
  49. const Plane plane_normalized = Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42);
  50. CHECK_MESSAGE(
  51. plane.get_normal().is_equal_approx(Vector3(32, 22, 16)),
  52. "get_normal() should return the expected value.");
  53. CHECK_MESSAGE(
  54. plane.normalized().is_equal_approx(plane_normalized),
  55. "normalized() should return a copy of the normalized value.");
  56. }
  57. TEST_CASE("[Plane] Basic setters") {
  58. Plane plane = Plane(32, 22, 16, 3);
  59. plane.set_normal(Vector3(4, 2, 3));
  60. CHECK_MESSAGE(
  61. plane.is_equal_approx(Plane(4, 2, 3, 3)),
  62. "set_normal() should result in the expected plane.");
  63. plane = Plane(32, 22, 16, 3);
  64. plane.normalize();
  65. CHECK_MESSAGE(
  66. plane.is_equal_approx(Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42)),
  67. "normalize() should result in the expected plane.");
  68. }
  69. TEST_CASE("[Plane] Plane-point operations") {
  70. const Plane plane = Plane(32, 22, 16, 3);
  71. const Plane y_facing_plane = Plane(0, 1, 0, 4);
  72. CHECK_MESSAGE(
  73. plane.get_center().is_equal_approx(Vector3(32 * 3, 22 * 3, 16 * 3)),
  74. "get_center() should return a vector pointing to the center of the plane.");
  75. CHECK_MESSAGE(
  76. y_facing_plane.is_point_over(Vector3(0, 5, 0)),
  77. "is_point_over() should return the expected result.");
  78. CHECK_MESSAGE(
  79. y_facing_plane.get_any_perpendicular_normal().is_equal_approx(Vector3(1, 0, 0)),
  80. "get_any_perpindicular_normal() should return the expected result.");
  81. // TODO distance_to()
  82. }
  83. TEST_CASE("[Plane] Has point") {
  84. const Plane x_facing_plane = Plane(1, 0, 0, 0);
  85. const Plane y_facing_plane = Plane(0, 1, 0, 0);
  86. const Plane z_facing_plane = Plane(0, 0, 1, 0);
  87. const Vector3 x_axis_point = Vector3(10, 0, 0);
  88. const Vector3 y_axis_point = Vector3(0, 10, 0);
  89. const Vector3 z_axis_point = Vector3(0, 0, 10);
  90. const Plane x_facing_plane_with_d_offset = Plane(1, 0, 0, 1);
  91. const Vector3 y_axis_point_with_d_offset = Vector3(1, 10, 0);
  92. CHECK_MESSAGE(
  93. x_facing_plane.has_point(y_axis_point),
  94. "has_point() with contained Vector3 should return the expected result.");
  95. CHECK_MESSAGE(
  96. x_facing_plane.has_point(z_axis_point),
  97. "has_point() with contained Vector3 should return the expected result.");
  98. CHECK_MESSAGE(
  99. y_facing_plane.has_point(x_axis_point),
  100. "has_point() with contained Vector3 should return the expected result.");
  101. CHECK_MESSAGE(
  102. y_facing_plane.has_point(z_axis_point),
  103. "has_point() with contained Vector3 should return the expected result.");
  104. CHECK_MESSAGE(
  105. z_facing_plane.has_point(y_axis_point),
  106. "has_point() with contained Vector3 should return the expected result.");
  107. CHECK_MESSAGE(
  108. z_facing_plane.has_point(x_axis_point),
  109. "has_point() with contained Vector3 should return the expected result.");
  110. CHECK_MESSAGE(
  111. x_facing_plane_with_d_offset.has_point(y_axis_point_with_d_offset),
  112. "has_point() with passed Vector3 should return the expected result.");
  113. }
  114. TEST_CASE("[Plane] Intersection") {
  115. const Plane x_facing_plane = Plane(1, 0, 0, 1);
  116. const Plane y_facing_plane = Plane(0, 1, 0, 2);
  117. const Plane z_facing_plane = Plane(0, 0, 1, 3);
  118. Vector3 vec_out;
  119. CHECK_MESSAGE(
  120. x_facing_plane.intersect_3(y_facing_plane, z_facing_plane, &vec_out),
  121. "intersect_3() should return the expected result.");
  122. CHECK_MESSAGE(
  123. vec_out.is_equal_approx(Vector3(1, 2, 3)),
  124. "intersect_3() should modify vec_out to the expected result.");
  125. CHECK_MESSAGE(
  126. x_facing_plane.intersects_ray(Vector3(0, 1, 1), Vector3(2, 0, 0), &vec_out),
  127. "intersects_ray() should return the expected result.");
  128. CHECK_MESSAGE(
  129. vec_out.is_equal_approx(Vector3(1, 1, 1)),
  130. "intersects_ray() should modify vec_out to the expected result.");
  131. CHECK_MESSAGE(
  132. x_facing_plane.intersects_segment(Vector3(0, 1, 1), Vector3(2, 1, 1), &vec_out),
  133. "intersects_segment() should return the expected result.");
  134. CHECK_MESSAGE(
  135. vec_out.is_equal_approx(Vector3(1, 1, 1)),
  136. "intersects_segment() should modify vec_out to the expected result.");
  137. }
  138. TEST_CASE("[Plane] Finite number checks") {
  139. const Vector3 x(0, 1, 2);
  140. const Vector3 infinite_vec(NAN, NAN, NAN);
  141. const real_t y = 0;
  142. const real_t infinite_y = NAN;
  143. CHECK_MESSAGE(
  144. Plane(x, y).is_finite(),
  145. "Plane with all components finite should be finite");
  146. CHECK_FALSE_MESSAGE(
  147. Plane(x, infinite_y).is_finite(),
  148. "Plane with one component infinite should not be finite.");
  149. CHECK_FALSE_MESSAGE(
  150. Plane(infinite_vec, y).is_finite(),
  151. "Plane with one component infinite should not be finite.");
  152. CHECK_FALSE_MESSAGE(
  153. Plane(infinite_vec, infinite_y).is_finite(),
  154. "Plane with two components infinite should not be finite.");
  155. }
  156. } // namespace TestPlane
  157. #endif // TEST_PLANE_H