summaryrefslogtreecommitdiff
path: root/tests/core/math/test_plane.h
blob: b2b857ca6920a3807dd4ef834945ac379e305626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**************************************************************************/
/*  test_plane.h                                                          */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#ifndef TEST_PLANE_H
#define TEST_PLANE_H

#include "core/math/plane.h"

#include "thirdparty/doctest/doctest.h"

namespace TestPlane {

// Plane

TEST_CASE("[Plane] Constructor methods") {
	const Plane plane = Plane(32, 22, 16, 3);
	const Plane plane_vector = Plane(Vector3(32, 22, 16), 3);
	const Plane plane_copy_plane = Plane(plane);

	CHECK_MESSAGE(
			plane == plane_vector,
			"Planes created with same values but different methods should be equal.");

	CHECK_MESSAGE(
			plane == plane_copy_plane,
			"Planes created with same values but different methods should be equal.");
}

TEST_CASE("[Plane] Basic getters") {
	const Plane plane = Plane(32, 22, 16, 3);
	const Plane plane_normalized = Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42);

	CHECK_MESSAGE(
			plane.get_normal().is_equal_approx(Vector3(32, 22, 16)),
			"get_normal() should return the expected value.");

	CHECK_MESSAGE(
			plane.normalized().is_equal_approx(plane_normalized),
			"normalized() should return a copy of the normalized value.");
}

TEST_CASE("[Plane] Basic setters") {
	Plane plane = Plane(32, 22, 16, 3);
	plane.set_normal(Vector3(4, 2, 3));

	CHECK_MESSAGE(
			plane.is_equal_approx(Plane(4, 2, 3, 3)),
			"set_normal() should result in the expected plane.");

	plane = Plane(32, 22, 16, 3);
	plane.normalize();

	CHECK_MESSAGE(
			plane.is_equal_approx(Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42)),
			"normalize() should result in the expected plane.");
}

TEST_CASE("[Plane] Plane-point operations") {
	const Plane plane = Plane(32, 22, 16, 3);
	const Plane y_facing_plane = Plane(0, 1, 0, 4);

	CHECK_MESSAGE(
			plane.center().is_equal_approx(Vector3(32 * 3, 22 * 3, 16 * 3)),
			"center() should return a vector pointing to the center of the plane.");

	CHECK_MESSAGE(
			y_facing_plane.is_point_over(Vector3(0, 5, 0)),
			"is_point_over() should return the expected result.");

	CHECK_MESSAGE(
			y_facing_plane.get_any_perpendicular_normal().is_equal_approx(Vector3(1, 0, 0)),
			"get_any_perpindicular_normal() should return the expected result.");

	// TODO distance_to()
}

TEST_CASE("[Plane] Has point") {
	const Plane x_facing_plane = Plane(1, 0, 0, 0);
	const Plane y_facing_plane = Plane(0, 1, 0, 0);
	const Plane z_facing_plane = Plane(0, 0, 1, 0);

	const Vector3 x_axis_point = Vector3(10, 0, 0);
	const Vector3 y_axis_point = Vector3(0, 10, 0);
	const Vector3 z_axis_point = Vector3(0, 0, 10);

	const Plane x_facing_plane_with_d_offset = Plane(1, 0, 0, 1);
	const Vector3 y_axis_point_with_d_offset = Vector3(1, 10, 0);

	CHECK_MESSAGE(
			x_facing_plane.has_point(y_axis_point),
			"has_point() with contained Vector3 should return the expected result.");
	CHECK_MESSAGE(
			x_facing_plane.has_point(z_axis_point),
			"has_point() with contained Vector3 should return the expected result.");

	CHECK_MESSAGE(
			y_facing_plane.has_point(x_axis_point),
			"has_point() with contained Vector3 should return the expected result.");
	CHECK_MESSAGE(
			y_facing_plane.has_point(z_axis_point),
			"has_point() with contained Vector3 should return the expected result.");

	CHECK_MESSAGE(
			z_facing_plane.has_point(y_axis_point),
			"has_point() with contained Vector3 should return the expected result.");
	CHECK_MESSAGE(
			z_facing_plane.has_point(x_axis_point),
			"has_point() with contained Vector3 should return the expected result.");

	CHECK_MESSAGE(
			x_facing_plane_with_d_offset.has_point(y_axis_point_with_d_offset),
			"has_point() with passed Vector3 should return the expected result.");
}

TEST_CASE("[Plane] Intersection") {
	const Plane x_facing_plane = Plane(1, 0, 0, 1);
	const Plane y_facing_plane = Plane(0, 1, 0, 2);
	const Plane z_facing_plane = Plane(0, 0, 1, 3);

	Vector3 vec_out;

	CHECK_MESSAGE(
			x_facing_plane.intersect_3(y_facing_plane, z_facing_plane, &vec_out),
			"intersect_3() should return the expected result.");
	CHECK_MESSAGE(
			vec_out.is_equal_approx(Vector3(1, 2, 3)),
			"intersect_3() should modify vec_out to the expected result.");

	CHECK_MESSAGE(
			x_facing_plane.intersects_ray(Vector3(0, 1, 1), Vector3(2, 0, 0), &vec_out),
			"intersects_ray() should return the expected result.");
	CHECK_MESSAGE(
			vec_out.is_equal_approx(Vector3(1, 1, 1)),
			"intersects_ray() should modify vec_out to the expected result.");

	CHECK_MESSAGE(
			x_facing_plane.intersects_segment(Vector3(0, 1, 1), Vector3(2, 1, 1), &vec_out),
			"intersects_segment() should return the expected result.");
	CHECK_MESSAGE(
			vec_out.is_equal_approx(Vector3(1, 1, 1)),
			"intersects_segment() should modify vec_out to the expected result.");
}

TEST_CASE("[Plane] Finite number checks") {
	const Vector3 x(0, 1, 2);
	const Vector3 infinite_vec(NAN, NAN, NAN);
	const real_t y = 0;
	const real_t infinite_y = NAN;

	CHECK_MESSAGE(
			Plane(x, y).is_finite(),
			"Plane with all components finite should be finite");

	CHECK_FALSE_MESSAGE(
			Plane(x, infinite_y).is_finite(),
			"Plane with one component infinite should not be finite.");
	CHECK_FALSE_MESSAGE(
			Plane(infinite_vec, y).is_finite(),
			"Plane with one component infinite should not be finite.");

	CHECK_FALSE_MESSAGE(
			Plane(infinite_vec, infinite_y).is_finite(),
			"Plane with two components infinite should not be finite.");
}

} // namespace TestPlane

#endif // TEST_PLANE_H