test_plane.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #pragma once
  31. #include "core/math/plane.h"
  32. #include "thirdparty/doctest/doctest.h"
  33. namespace TestPlane {
  34. // Plane
  35. TEST_CASE("[Plane] Constructor methods") {
  36. constexpr Plane plane = Plane(32, 22, 16, 3);
  37. constexpr Plane plane_vector = Plane(Vector3(32, 22, 16), 3);
  38. constexpr Plane plane_copy_plane = Plane(plane);
  39. static_assert(
  40. plane == plane_vector,
  41. "Planes created with same values but different methods should be equal.");
  42. static_assert(
  43. plane == plane_copy_plane,
  44. "Planes created with same values but different methods should be equal.");
  45. }
  46. TEST_CASE("[Plane] Basic getters") {
  47. constexpr Plane plane = Plane(32, 22, 16, 3);
  48. constexpr Plane plane_normalized = Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42);
  49. CHECK_MESSAGE(
  50. plane.get_normal().is_equal_approx(Vector3(32, 22, 16)),
  51. "get_normal() should return the expected value.");
  52. CHECK_MESSAGE(
  53. plane.normalized().is_equal_approx(plane_normalized),
  54. "normalized() should return a copy of the normalized value.");
  55. }
  56. TEST_CASE("[Plane] Basic setters") {
  57. Plane plane = Plane(32, 22, 16, 3);
  58. plane.set_normal(Vector3(4, 2, 3));
  59. CHECK_MESSAGE(
  60. plane.is_equal_approx(Plane(4, 2, 3, 3)),
  61. "set_normal() should result in the expected plane.");
  62. plane = Plane(32, 22, 16, 3);
  63. plane.normalize();
  64. CHECK_MESSAGE(
  65. plane.is_equal_approx(Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42)),
  66. "normalize() should result in the expected plane.");
  67. }
  68. TEST_CASE("[Plane] Plane-point operations") {
  69. constexpr Plane plane = Plane(32, 22, 16, 3);
  70. constexpr Plane y_facing_plane = Plane(0, 1, 0, 4);
  71. CHECK_MESSAGE(
  72. plane.get_center().is_equal_approx(Vector3(32 * 3, 22 * 3, 16 * 3)),
  73. "get_center() should return a vector pointing to the center of the plane.");
  74. CHECK_MESSAGE(
  75. y_facing_plane.is_point_over(Vector3(0, 5, 0)),
  76. "is_point_over() should return the expected result.");
  77. CHECK_MESSAGE(
  78. y_facing_plane.get_any_perpendicular_normal().is_equal_approx(Vector3(1, 0, 0)),
  79. "get_any_perpendicular_normal() should return the expected result.");
  80. // TODO distance_to()
  81. }
  82. TEST_CASE("[Plane] Has point") {
  83. constexpr Plane x_facing_plane = Plane(1, 0, 0, 0);
  84. constexpr Plane y_facing_plane = Plane(0, 1, 0, 0);
  85. constexpr Plane z_facing_plane = Plane(0, 0, 1, 0);
  86. constexpr Vector3 x_axis_point = Vector3(10, 0, 0);
  87. constexpr Vector3 y_axis_point = Vector3(0, 10, 0);
  88. constexpr Vector3 z_axis_point = Vector3(0, 0, 10);
  89. constexpr Plane x_facing_plane_with_d_offset = Plane(1, 0, 0, 1);
  90. constexpr Vector3 y_axis_point_with_d_offset = Vector3(1, 10, 0);
  91. CHECK_MESSAGE(
  92. x_facing_plane.has_point(y_axis_point),
  93. "has_point() with contained Vector3 should return the expected result.");
  94. CHECK_MESSAGE(
  95. x_facing_plane.has_point(z_axis_point),
  96. "has_point() with contained Vector3 should return the expected result.");
  97. CHECK_MESSAGE(
  98. y_facing_plane.has_point(x_axis_point),
  99. "has_point() with contained Vector3 should return the expected result.");
  100. CHECK_MESSAGE(
  101. y_facing_plane.has_point(z_axis_point),
  102. "has_point() with contained Vector3 should return the expected result.");
  103. CHECK_MESSAGE(
  104. z_facing_plane.has_point(y_axis_point),
  105. "has_point() with contained Vector3 should return the expected result.");
  106. CHECK_MESSAGE(
  107. z_facing_plane.has_point(x_axis_point),
  108. "has_point() with contained Vector3 should return the expected result.");
  109. CHECK_MESSAGE(
  110. x_facing_plane_with_d_offset.has_point(y_axis_point_with_d_offset),
  111. "has_point() with passed Vector3 should return the expected result.");
  112. }
  113. TEST_CASE("[Plane] Intersection") {
  114. constexpr Plane x_facing_plane = Plane(1, 0, 0, 1);
  115. constexpr Plane y_facing_plane = Plane(0, 1, 0, 2);
  116. constexpr Plane z_facing_plane = Plane(0, 0, 1, 3);
  117. Vector3 vec_out;
  118. CHECK_MESSAGE(
  119. x_facing_plane.intersect_3(y_facing_plane, z_facing_plane, &vec_out),
  120. "intersect_3() should return the expected result.");
  121. CHECK_MESSAGE(
  122. vec_out.is_equal_approx(Vector3(1, 2, 3)),
  123. "intersect_3() should modify vec_out to the expected result.");
  124. CHECK_MESSAGE(
  125. x_facing_plane.intersects_ray(Vector3(0, 1, 1), Vector3(2, 0, 0), &vec_out),
  126. "intersects_ray() should return the expected result.");
  127. CHECK_MESSAGE(
  128. vec_out.is_equal_approx(Vector3(1, 1, 1)),
  129. "intersects_ray() should modify vec_out to the expected result.");
  130. CHECK_MESSAGE(
  131. x_facing_plane.intersects_segment(Vector3(0, 1, 1), Vector3(2, 1, 1), &vec_out),
  132. "intersects_segment() should return the expected result.");
  133. CHECK_MESSAGE(
  134. vec_out.is_equal_approx(Vector3(1, 1, 1)),
  135. "intersects_segment() should modify vec_out to the expected result.");
  136. }
  137. TEST_CASE("[Plane] Finite number checks") {
  138. constexpr Vector3 x(0, 1, 2);
  139. constexpr Vector3 infinite_vec(Math::NaN, Math::NaN, Math::NaN);
  140. constexpr real_t y = 0;
  141. constexpr real_t infinite_y = Math::NaN;
  142. CHECK_MESSAGE(
  143. Plane(x, y).is_finite(),
  144. "Plane with all components finite should be finite");
  145. CHECK_FALSE_MESSAGE(
  146. Plane(x, infinite_y).is_finite(),
  147. "Plane with one component infinite should not be finite.");
  148. CHECK_FALSE_MESSAGE(
  149. Plane(infinite_vec, y).is_finite(),
  150. "Plane with one component infinite should not be finite.");
  151. CHECK_FALSE_MESSAGE(
  152. Plane(infinite_vec, infinite_y).is_finite(),
  153. "Plane with two components infinite should not be finite.");
  154. }
  155. } // namespace TestPlane