Debian Codegraph @

tests/core/math/test_plane.h

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