Debian Codegraph @

core/math/quick_hull.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* quick_hull.cpp */
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 #include "quick_hull.h"
32
33 #include "core/templates/hash_map.h"
34 #include "core/templates/hash_set.h"
35
36 uint32_t QuickHull::debug_stop_after = 0xFFFFFFFF;
37
38 Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_mesh) {
39 /* CREATE AABB VOLUME */
40
41 AABB aabb;
42 for (int i = 0; i < p_points.size(); i++) {
43 if (i == 0) {
44 aabb.position = p_points[i];
45 } else {
46 aabb.expand_to(p_points[i]);
47 }
48 }
49
50 if (aabb.size == Vector3()) {
51 return ERR_CANT_CREATE;
52 }
53
54 Vector<bool> valid_points;
55 valid_points.resize(p_points.size());
56 HashSet<Vector3> valid_cache;
57
58 for (int i = 0; i < p_points.size(); i++) {
59 Vector3 sp = p_points[i].snappedf(0.0001);
60 if (valid_cache.has(sp)) {
61 valid_points.write[i] = false;
62 } else {
63 valid_points.write[i] = true;
64 valid_cache.insert(sp);
65 }
66 }
67
68 /* CREATE INITIAL SIMPLEX */
69
70 int longest_axis = aabb.get_longest_axis_index();
71
72 //first two vertices are the most distant
73 int simplex[4] = { 0 };
74
75 {
76 real_t max = 0, min = 0;
77
78 for (int i = 0; i < p_points.size(); i++) {
79 if (!valid_points[i]) {
80 continue;
81 }
82 real_t d = p_points[i][longest_axis];
83 if (i == 0 || d < min) {
84 simplex[0] = i;
85 min = d;
86 }
87
88 if (i == 0 || d > max) {
89 simplex[1] = i;
90 max = d;
91 }
92 }
93 }
94
95 //third vertex is one most further away from the line
96
97 {
98 real_t maxd = 0;
99 Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]];
100
101 for (int i = 0; i < p_points.size(); i++) {
102 if (!valid_points[i]) {
103 continue;
104 }
105
106 Vector3 n = rel12.cross(p_points[simplex[0]] - p_points[i]).cross(rel12).normalized();
107 real_t d = Math::abs(n.dot(p_points[simplex[0]]) - n.dot(p_points[i]));
108
109 if (i == 0 || d > maxd) {
110 maxd = d;
111 simplex[2] = i;
112 }
113 }
114 }
115
116 //fourth vertex is the one most further away from the plane
117
118 {
119 real_t maxd = 0;
120 Plane p(p_points[simplex[0]], p_points[simplex[1]], p_points[simplex[2]]);
121
122 for (int i = 0; i < p_points.size(); i++) {
123 if (!valid_points[i]) {
124 continue;
125 }
126
127 real_t d = Math::abs(p.distance_to(p_points[i]));
128
129 if (i == 0 || d > maxd) {
130 maxd = d;
131 simplex[3] = i;
132 }
133 }
134 }
135
136 //compute center of simplex, this is a point always warranted to be inside
137 Vector3 center;
138
139 for (int i = 0; i < 4; i++) {
140 center += p_points[simplex[i]];
141 }
142
143 center /= 4.0;
144
145 //add faces
146
147 List<Face> faces;
148
149 for (int i = 0; i < 4; i++) {
150 static const int face_order[4][3] = {
151 { 0, 1, 2 },
152 { 0, 1, 3 },
153 { 0, 2, 3 },
154 { 1, 2, 3 }
155 };
156
157 Face f;
158 for (int j = 0; j < 3; j++) {
159 f.vertices[j] = simplex[face_order[i][j]];
160 }
161
162 Plane p(p_points[f.vertices[0]], p_points[f.vertices[1]], p_points[f.vertices[2]]);
163
164 if (p.is_point_over(center)) {
165 //flip face to clockwise if facing inwards
166 SWAP(f.vertices[0], f.vertices[1]);
167 p = -p;
168 }
169
170 f.plane = p;
171
172 faces.push_back(f);
173 }
174
175 real_t over_tolerance = 3 * UNIT_EPSILON * (aabb.size.x + aabb.size.y + aabb.size.z);
176
177 /* COMPUTE AVAILABLE VERTICES */
178
179 for (int i = 0; i < p_points.size(); i++) {
180 if (i == simplex[0]) {
181 continue;
182 }
183 if (i == simplex[1]) {
184 continue;
185 }
186 if (i == simplex[2]) {
187 continue;
188 }
189 if (i == simplex[3]) {
190 continue;
191 }
192 if (!valid_points[i]) {
193 continue;
194 }
195
196 for (Face &E : faces) {
197 if (E.plane.distance_to(p_points[i]) > over_tolerance) {
198 E.points_over.push_back(i);
199 break;
200 }
201 }
202 }
203
204 faces.sort(); // sort them, so the ones with points are in the back
205
206 /* BUILD HULL */
207
208 //poop face (while still remain)
209 //find further away point
210 //find lit faces
211 //determine horizon edges
212 //build new faces with horizon edges, them assign points side from all lit faces
213 //remove lit faces
214
215 uint32_t debug_stop = debug_stop_after;
216
217 while (debug_stop > 0 && faces.back()->get().points_over.size()) {
218 debug_stop--;
219 Face &f = faces.back()->get();
220
221 //find vertex most outside
222 int next = -1;
223 real_t next_d = 0;
224
225 for (int i = 0; i < f.points_over.size(); i++) {
226 real_t d = f.plane.distance_to(p_points[f.points_over[i]]);
227
228 if (d > next_d) {
229 next_d = d;
230 next = i;
231 }
232 }
233
234 ERR_FAIL_COND_V(next == -1, ERR_BUG);
235
236 Vector3 v = p_points[f.points_over[next]];
237
238 //find lit faces and lit edges
239 List<List<Face>::Element *> lit_faces; //lit face is a death sentence
240
241 HashMap<Edge, FaceConnect, Edge> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot
242
243 for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
244 if (E->get().plane.distance_to(v) > 0) {
245 lit_faces.push_back(E);
246
247 for (int i = 0; i < 3; i++) {
248 uint32_t a = E->get().vertices[i];
249 uint32_t b = E->get().vertices[(i + 1) % 3];
250 Edge e(a, b);
251
252 HashMap<Edge, FaceConnect, Edge>::Iterator F = lit_edges.find(e);
253 if (!F) {
254 F = lit_edges.insert(e, FaceConnect());
255 }
256 if (e.vertices[0] == a) {
257 //left
258 F->value.left = E;
259 } else {
260 F->value.right = E;
261 }
262 }
263 }
264 }
265
266 //create new faces from horizon edges
267 List<List<Face>::Element *> new_faces; //new faces
268
269 for (KeyValue<Edge, FaceConnect> &E : lit_edges) {
270 FaceConnect &fc = E.value;
271 if (fc.left && fc.right) {
272 continue; //edge is uninteresting, not on horizon
273 }
274
275 //create new face!
276
277 Face face;
278 face.vertices[0] = f.points_over[next];
279 face.vertices[1] = E.key.vertices[0];
280 face.vertices[2] = E.key.vertices[1];
281
282 Plane p(p_points[face.vertices[0]], p_points[face.vertices[1]], p_points[face.vertices[2]]);
283
284 if (p.is_point_over(center)) {
285 //flip face to clockwise if facing inwards
286 SWAP(face.vertices[0], face.vertices[1]);
287 p = -p;
288 }
289
290 face.plane = p;
291 new_faces.push_back(faces.push_back(face));
292 }
293
294 //distribute points into new faces
295
296 for (List<Face>::Element *&F : lit_faces) {
297 Face &lf = F->get();
298
299 for (int i = 0; i < lf.points_over.size(); i++) {
300 if (lf.points_over[i] == f.points_over[next]) { //do not add current one
301 continue;
302 }
303
304 Vector3 p = p_points[lf.points_over[i]];
305 for (List<Face>::Element *&E : new_faces) {
306 Face &f2 = E->get();
307 if (f2.plane.distance_to(p) > over_tolerance) {
308 f2.points_over.push_back(lf.points_over[i]);
309 break;
310 }
311 }
312 }
313 }
314
315 //erase lit faces
316
317 while (lit_faces.size()) {
318 faces.erase(lit_faces.front()->get());
319 lit_faces.pop_front();
320 }
321
322 //put faces that contain no points on the front
323
324 for (List<Face>::Element *&E : new_faces) {
325 Face &f2 = E->get();
326 if (f2.points_over.is_empty()) {
327 faces.move_to_front(E);
328 }
329 }
330
331 //whew, done with iteration, go next
332 }
333
334 /* CREATE MESHDATA */
335
336 //make a map of edges again
337 HashMap<Edge, RetFaceConnect, Edge> ret_edges;
338 List<Geometry3D::MeshData::Face> ret_faces;
339
340 for (const Face &E : faces) {
341 Geometry3D::MeshData::Face f;
342 f.plane = E.plane;
343
344 for (int i = 0; i < 3; i++) {
345 f.indices.push_back(E.vertices[i]);
346 }
347
348 List<Geometry3D::MeshData::Face>::Element *F = ret_faces.push_back(f);
349
350 for (int i = 0; i < 3; i++) {
351 uint32_t a = E.vertices[i];
352 uint32_t b = E.vertices[(i + 1) % 3];
353 Edge e(a, b);
354
355 HashMap<Edge, RetFaceConnect, Edge>::Iterator G = ret_edges.find(e);
356 if (!G) {
357 G = ret_edges.insert(e, RetFaceConnect());
358 }
359 if (e.vertices[0] == a) {
360 //left
361 G->value.left = F;
362 } else {
363 G->value.right = F;
364 }
365 }
366 }
367
368 //fill faces
369
370 for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
371 Geometry3D::MeshData::Face &f = E->get();
372
373 for (uint32_t i = 0; i < f.indices.size(); i++) {
374 int a = E->get().indices[i];
375 int b = E->get().indices[(i + 1) % f.indices.size()];
376 Edge e(a, b);
377
378 HashMap<Edge, RetFaceConnect, Edge>::Iterator F = ret_edges.find(e);
379
380 ERR_CONTINUE(!F);
381 List<Geometry3D::MeshData::Face>::Element *O = F->value.left == E ? F->value.right : F->value.left;
382 ERR_CONTINUE(O == E);
383 ERR_CONTINUE(O == nullptr);
384
385 if (O->get().plane.is_equal_approx(f.plane)) {
386 //merge and delete edge and contiguous face, while repointing edges (uuugh!)
387 int o_index_size = O->get().indices.size();
388
389 for (int j = 0; j < o_index_size; j++) {
390 //search a
391 if (O->get().indices[j] == a) {
392 //append the rest
393 for (int k = 0; k < o_index_size; k++) {
394 int idx = O->get().indices[(k + j) % o_index_size];
395 int idxn = O->get().indices[(k + j + 1) % o_index_size];
396 if (idx == b && idxn == a) { //already have b!
397 break;
398 }
399 if (idx != a) {
400 f.indices.insert(i + 1, idx);
401 i++;
402 }
403 Edge e2(idx, idxn);
404
405 HashMap<Edge, RetFaceConnect, Edge>::Iterator F2 = ret_edges.find(e2);
406 ERR_CONTINUE(!F2);
407 //change faceconnect, point to this face instead
408 if (F2->value.left == O) {
409 F2->value.left = E;
410 } else if (F2->value.right == O) {
411 F2->value.right = E;
412 }
413 }
414
415 break;
416 }
417 }
418
419 // remove all edge connections to this face
420 for (KeyValue<Edge, RetFaceConnect> &G : ret_edges) {
421 if (G.value.left == O) {
422 G.value.left = nullptr;
423 }
424
425 if (G.value.right == O) {
426 G.value.right = nullptr;
427 }
428 }
429
430 ret_edges.remove(F); //remove the edge
431 ret_faces.erase(O); //remove the face
432 }
433 }
434 }
435
436 //fill mesh
437 r_mesh.faces.clear();
438 r_mesh.faces.resize(ret_faces.size());
439
440 HashMap<List<Geometry3D::MeshData::Face>::Element *, int> face_indices;
441
442 int idx = 0;
443 for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
444 face_indices[E] = idx;
445 r_mesh.faces[idx++] = E->get();
446 }
447 r_mesh.edges.resize(ret_edges.size());
448 idx = 0;
449 for (const KeyValue<Edge, RetFaceConnect> &E : ret_edges) {
450 Geometry3D::MeshData::Edge e;
451 e.vertex_a = E.key.vertices[0];
452 e.vertex_b = E.key.vertices[1];
453 ERR_CONTINUE(!face_indices.has(E.value.left));
454 ERR_CONTINUE(!face_indices.has(E.value.right));
455 e.face_a = face_indices[E.value.left];
456 e.face_b = face_indices[E.value.right];
457 r_mesh.edges[idx++] = e;
458 }
459
460 r_mesh.vertices = p_points;
461
462 return OK;
463 }