1 | /**************************************************************************/ |
2 | /* face3.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 | #ifndef FACE3_H |
32 | #define FACE3_H |
33 | |
34 | #include "core/math/aabb.h" |
35 | #include "core/math/plane.h" |
36 | #include "core/math/transform_3d.h" |
37 | #include "core/math/vector3.h" |
38 | |
39 | struct _NO_DISCARD_ Face3 { |
40 | enum Side { |
41 | SIDE_OVER, |
42 | SIDE_UNDER, |
43 | SIDE_SPANNING, |
44 | SIDE_COPLANAR |
45 | }; |
46 | |
47 | Vector3 vertex[3]; |
48 | |
49 | /** |
50 | * @param p_plane plane used to split the face |
51 | * @param p_res array of at least 3 faces, amount used in function return |
52 | * @param p_is_point_over array of at least 3 booleans, determining which face is over the plane, amount used in function return |
53 | * @return amount of faces generated by the split, either 0 (means no split possible), 2 or 3 |
54 | */ |
55 | int split_by_plane(const Plane &p_plane, Face3 *p_res, bool *p_is_point_over) const; |
56 | |
57 | Plane get_plane(ClockDirection p_dir = CLOCKWISE) const; |
58 | Vector3 get_random_point_inside() const; |
59 | |
60 | bool is_degenerate() const; |
61 | real_t get_area() const; |
62 | |
63 | Vector3 get_closest_point_to(const Vector3 &p_point) const; |
64 | |
65 | bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const; |
66 | bool intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const; |
67 | |
68 | void get_support(const Vector3 &p_normal, const Transform3D &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const; |
69 | void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const; |
70 | |
71 | AABB get_aabb() const { |
72 | AABB aabb(vertex[0], Vector3()); |
73 | aabb.expand_to(vertex[1]); |
74 | aabb.expand_to(vertex[2]); |
75 | return aabb; |
76 | } |
77 | |
78 | bool intersects_aabb(const AABB &p_aabb) const; |
79 | _FORCE_INLINE_ bool intersects_aabb2(const AABB &p_aabb) const; |
80 | operator String() const; |
81 | |
82 | inline Face3() {} |
83 | inline Face3(const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) { |
84 | vertex[0] = p_v1; |
85 | vertex[1] = p_v2; |
86 | vertex[2] = p_v3; |
87 | } |
88 | }; |
89 | |
90 | bool Face3::intersects_aabb2(const AABB &p_aabb) const { |
91 | Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]); |
92 | |
93 | Vector3 half_extents = p_aabb.size * 0.5f; |
94 | Vector3 ofs = p_aabb.position + half_extents; |
95 | |
96 | Vector3 sup = Vector3( |
97 | (perp.x > 0) ? -half_extents.x : half_extents.x, |
98 | (perp.y > 0) ? -half_extents.y : half_extents.y, |
99 | (perp.z > 0) ? -half_extents.z : half_extents.z); |
100 | |
101 | real_t d = perp.dot(vertex[0]); |
102 | real_t dist_a = perp.dot(ofs + sup) - d; |
103 | real_t dist_b = perp.dot(ofs - sup) - d; |
104 | |
105 | if (dist_a * dist_b > 0) { |
106 | return false; //does not intersect the plane |
107 | } |
108 | |
109 | #define TEST_AXIS(m_ax) \ |
110 | { \ |
111 | real_t aabb_min = p_aabb.position.m_ax; \ |
112 | real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \ |
113 | real_t tri_min, tri_max; \ |
114 | for (int i = 0; i < 3; i++) { \ |
115 | if (i == 0 || vertex[i].m_ax > tri_max) \ |
116 | tri_max = vertex[i].m_ax; \ |
117 | if (i == 0 || vertex[i].m_ax < tri_min) \ |
118 | tri_min = vertex[i].m_ax; \ |
119 | } \ |
120 | \ |
121 | if (tri_max < aabb_min || aabb_max < tri_min) \ |
122 | return false; \ |
123 | } |
124 | |
125 | TEST_AXIS(x); |
126 | TEST_AXIS(y); |
127 | TEST_AXIS(z); |
128 | |
129 | #undef TEST_AXIS |
130 | |
131 | const Vector3 edge_norms[3] = { |
132 | vertex[0] - vertex[1], |
133 | vertex[1] - vertex[2], |
134 | vertex[2] - vertex[0], |
135 | }; |
136 | |
137 | for (int i = 0; i < 12; i++) { |
138 | Vector3 from, to; |
139 | switch (i) { |
140 | case 0: { |
141 | from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); |
142 | to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); |
143 | } break; |
144 | case 1: { |
145 | from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); |
146 | to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); |
147 | } break; |
148 | case 2: { |
149 | from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); |
150 | to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); |
151 | |
152 | } break; |
153 | case 3: { |
154 | from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); |
155 | to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); |
156 | |
157 | } break; |
158 | case 4: { |
159 | from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); |
160 | to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); |
161 | } break; |
162 | case 5: { |
163 | from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); |
164 | to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); |
165 | } break; |
166 | case 6: { |
167 | from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); |
168 | to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); |
169 | |
170 | } break; |
171 | case 7: { |
172 | from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); |
173 | to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); |
174 | |
175 | } break; |
176 | case 8: { |
177 | from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); |
178 | to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); |
179 | |
180 | } break; |
181 | case 9: { |
182 | from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z); |
183 | to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); |
184 | |
185 | } break; |
186 | case 10: { |
187 | from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z); |
188 | to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z); |
189 | |
190 | } break; |
191 | case 11: { |
192 | from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z); |
193 | to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z); |
194 | |
195 | } break; |
196 | } |
197 | |
198 | Vector3 e1 = from - to; |
199 | for (int j = 0; j < 3; j++) { |
200 | Vector3 e2 = edge_norms[j]; |
201 | |
202 | Vector3 axis = vec3_cross(e1, e2); |
203 | |
204 | if (axis.length_squared() < 0.0001f) { |
205 | continue; // coplanar |
206 | } |
207 | //axis.normalize(); |
208 | |
209 | Vector3 sup2 = Vector3( |
210 | (axis.x > 0) ? -half_extents.x : half_extents.x, |
211 | (axis.y > 0) ? -half_extents.y : half_extents.y, |
212 | (axis.z > 0) ? -half_extents.z : half_extents.z); |
213 | |
214 | real_t maxB = axis.dot(ofs + sup2); |
215 | real_t minB = axis.dot(ofs - sup2); |
216 | if (minB > maxB) { |
217 | SWAP(maxB, minB); |
218 | } |
219 | |
220 | real_t minT = 1e20, maxT = -1e20; |
221 | for (int k = 0; k < 3; k++) { |
222 | real_t vert_d = axis.dot(vertex[k]); |
223 | |
224 | if (vert_d > maxT) { |
225 | maxT = vert_d; |
226 | } |
227 | |
228 | if (vert_d < minT) { |
229 | minT = vert_d; |
230 | } |
231 | } |
232 | |
233 | if (maxB < minT || maxT < minB) { |
234 | return false; |
235 | } |
236 | } |
237 | } |
238 | return true; |
239 | } |
240 | |
241 | #endif // FACE3_H |
242 | |