quick_hull.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*************************************************************************/
  2. /* quick_hull.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "quick_hull.h"
  31. #include "core/map.h"
  32. uint32_t QuickHull::debug_stop_after = 0xFFFFFFFF;
  33. Error QuickHull::build(const Vector<Vector3> &p_points, Geometry::MeshData &r_mesh) {
  34. /* CREATE AABB VOLUME */
  35. AABB aabb;
  36. for (int i = 0; i < p_points.size(); i++) {
  37. if (i == 0) {
  38. aabb.position = p_points[i];
  39. } else {
  40. aabb.expand_to(p_points[i]);
  41. }
  42. }
  43. if (aabb.size == Vector3()) {
  44. return ERR_CANT_CREATE;
  45. }
  46. Vector<bool> valid_points;
  47. valid_points.resize(p_points.size());
  48. Set<Vector3> valid_cache;
  49. for (int i = 0; i < p_points.size(); i++) {
  50. Vector3 sp = p_points[i].snapped(Vector3(0.0001, 0.0001, 0.0001));
  51. if (valid_cache.has(sp)) {
  52. valid_points.write[i] = false;
  53. } else {
  54. valid_points.write[i] = true;
  55. valid_cache.insert(sp);
  56. }
  57. }
  58. /* CREATE INITIAL SIMPLEX */
  59. int longest_axis = aabb.get_longest_axis_index();
  60. //first two vertices are the most distant
  61. int simplex[4] = { 0 };
  62. {
  63. real_t max = 0, min = 0;
  64. for (int i = 0; i < p_points.size(); i++) {
  65. if (!valid_points[i])
  66. continue;
  67. real_t d = p_points[i][longest_axis];
  68. if (i == 0 || d < min) {
  69. simplex[0] = i;
  70. min = d;
  71. }
  72. if (i == 0 || d > max) {
  73. simplex[1] = i;
  74. max = d;
  75. }
  76. }
  77. }
  78. //third vertex is one most further away from the line
  79. {
  80. real_t maxd = 0;
  81. Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]];
  82. for (int i = 0; i < p_points.size(); i++) {
  83. if (!valid_points[i])
  84. continue;
  85. Vector3 n = rel12.cross(p_points[simplex[0]] - p_points[i]).cross(rel12).normalized();
  86. real_t d = Math::abs(n.dot(p_points[simplex[0]]) - n.dot(p_points[i]));
  87. if (i == 0 || d > maxd) {
  88. maxd = d;
  89. simplex[2] = i;
  90. }
  91. }
  92. }
  93. //fourth vertex is the one most further away from the plane
  94. {
  95. real_t maxd = 0;
  96. Plane p(p_points[simplex[0]], p_points[simplex[1]], p_points[simplex[2]]);
  97. for (int i = 0; i < p_points.size(); i++) {
  98. if (!valid_points[i])
  99. continue;
  100. real_t d = Math::abs(p.distance_to(p_points[i]));
  101. if (i == 0 || d > maxd) {
  102. maxd = d;
  103. simplex[3] = i;
  104. }
  105. }
  106. }
  107. //compute center of simplex, this is a point always warranted to be inside
  108. Vector3 center;
  109. for (int i = 0; i < 4; i++) {
  110. center += p_points[simplex[i]];
  111. }
  112. center /= 4.0;
  113. //add faces
  114. List<Face> faces;
  115. for (int i = 0; i < 4; i++) {
  116. static const int face_order[4][3] = {
  117. { 0, 1, 2 },
  118. { 0, 1, 3 },
  119. { 0, 2, 3 },
  120. { 1, 2, 3 }
  121. };
  122. Face f;
  123. for (int j = 0; j < 3; j++) {
  124. f.vertices[j] = simplex[face_order[i][j]];
  125. }
  126. Plane p(p_points[f.vertices[0]], p_points[f.vertices[1]], p_points[f.vertices[2]]);
  127. if (p.is_point_over(center)) {
  128. //flip face to clockwise if facing inwards
  129. SWAP(f.vertices[0], f.vertices[1]);
  130. p = -p;
  131. }
  132. f.plane = p;
  133. faces.push_back(f);
  134. }
  135. real_t over_tolerance = 3 * UNIT_EPSILON * (aabb.size.x + aabb.size.y + aabb.size.z);
  136. /* COMPUTE AVAILABLE VERTICES */
  137. for (int i = 0; i < p_points.size(); i++) {
  138. if (i == simplex[0])
  139. continue;
  140. if (i == simplex[1])
  141. continue;
  142. if (i == simplex[2])
  143. continue;
  144. if (i == simplex[3])
  145. continue;
  146. if (!valid_points[i])
  147. continue;
  148. for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
  149. if (E->get().plane.distance_to(p_points[i]) > over_tolerance) {
  150. E->get().points_over.push_back(i);
  151. break;
  152. }
  153. }
  154. }
  155. faces.sort(); // sort them, so the ones with points are in the back
  156. /* BUILD HULL */
  157. //poop face (while still remain)
  158. //find further away point
  159. //find lit faces
  160. //determine horizon edges
  161. //build new faces with horizon edges, them assign points side from all lit faces
  162. //remove lit faces
  163. uint32_t debug_stop = debug_stop_after;
  164. while (debug_stop > 0 && faces.back()->get().points_over.size()) {
  165. debug_stop--;
  166. Face &f = faces.back()->get();
  167. //find vertex most outside
  168. int next = -1;
  169. real_t next_d = 0;
  170. for (int i = 0; i < f.points_over.size(); i++) {
  171. real_t d = f.plane.distance_to(p_points[f.points_over[i]]);
  172. if (d > next_d) {
  173. next_d = d;
  174. next = i;
  175. }
  176. }
  177. ERR_FAIL_COND_V(next == -1, ERR_BUG);
  178. Vector3 v = p_points[f.points_over[next]];
  179. //find lit faces and lit edges
  180. List<List<Face>::Element *> lit_faces; //lit face is a death sentence
  181. Map<Edge, FaceConnect> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot
  182. for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
  183. if (E->get().plane.distance_to(v) > 0) {
  184. lit_faces.push_back(E);
  185. for (int i = 0; i < 3; i++) {
  186. uint32_t a = E->get().vertices[i];
  187. uint32_t b = E->get().vertices[(i + 1) % 3];
  188. Edge e(a, b);
  189. Map<Edge, FaceConnect>::Element *F = lit_edges.find(e);
  190. if (!F) {
  191. F = lit_edges.insert(e, FaceConnect());
  192. }
  193. if (e.vertices[0] == a) {
  194. //left
  195. F->get().left = E;
  196. } else {
  197. F->get().right = E;
  198. }
  199. }
  200. }
  201. }
  202. //create new faces from horizon edges
  203. List<List<Face>::Element *> new_faces; //new faces
  204. for (Map<Edge, FaceConnect>::Element *E = lit_edges.front(); E; E = E->next()) {
  205. FaceConnect &fc = E->get();
  206. if (fc.left && fc.right) {
  207. continue; //edge is uninteresting, not on horizont
  208. }
  209. //create new face!
  210. Face face;
  211. face.vertices[0] = f.points_over[next];
  212. face.vertices[1] = E->key().vertices[0];
  213. face.vertices[2] = E->key().vertices[1];
  214. Plane p(p_points[face.vertices[0]], p_points[face.vertices[1]], p_points[face.vertices[2]]);
  215. if (p.is_point_over(center)) {
  216. //flip face to clockwise if facing inwards
  217. SWAP(face.vertices[0], face.vertices[1]);
  218. p = -p;
  219. }
  220. face.plane = p;
  221. new_faces.push_back(faces.push_back(face));
  222. }
  223. //distribute points into new faces
  224. for (List<List<Face>::Element *>::Element *F = lit_faces.front(); F; F = F->next()) {
  225. Face &lf = F->get()->get();
  226. for (int i = 0; i < lf.points_over.size(); i++) {
  227. if (lf.points_over[i] == f.points_over[next]) //do not add current one
  228. continue;
  229. Vector3 p = p_points[lf.points_over[i]];
  230. for (List<List<Face>::Element *>::Element *E = new_faces.front(); E; E = E->next()) {
  231. Face &f2 = E->get()->get();
  232. if (f2.plane.distance_to(p) > over_tolerance) {
  233. f2.points_over.push_back(lf.points_over[i]);
  234. break;
  235. }
  236. }
  237. }
  238. }
  239. //erase lit faces
  240. while (lit_faces.size()) {
  241. faces.erase(lit_faces.front()->get());
  242. lit_faces.pop_front();
  243. }
  244. //put faces that contain no points on the front
  245. for (List<List<Face>::Element *>::Element *E = new_faces.front(); E; E = E->next()) {
  246. Face &f2 = E->get()->get();
  247. if (f2.points_over.size() == 0) {
  248. faces.move_to_front(E->get());
  249. }
  250. }
  251. //whew, done with iteration, go next
  252. }
  253. /* CREATE MESHDATA */
  254. //make a map of edges again
  255. Map<Edge, RetFaceConnect> ret_edges;
  256. List<Geometry::MeshData::Face> ret_faces;
  257. for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
  258. Geometry::MeshData::Face f;
  259. f.plane = E->get().plane;
  260. for (int i = 0; i < 3; i++) {
  261. f.indices.push_back(E->get().vertices[i]);
  262. }
  263. List<Geometry::MeshData::Face>::Element *F = ret_faces.push_back(f);
  264. for (int i = 0; i < 3; i++) {
  265. uint32_t a = E->get().vertices[i];
  266. uint32_t b = E->get().vertices[(i + 1) % 3];
  267. Edge e(a, b);
  268. Map<Edge, RetFaceConnect>::Element *G = ret_edges.find(e);
  269. if (!G) {
  270. G = ret_edges.insert(e, RetFaceConnect());
  271. }
  272. if (e.vertices[0] == a) {
  273. //left
  274. G->get().left = F;
  275. } else {
  276. G->get().right = F;
  277. }
  278. }
  279. }
  280. //fill faces
  281. for (List<Geometry::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
  282. Geometry::MeshData::Face &f = E->get();
  283. for (int i = 0; i < f.indices.size(); i++) {
  284. int a = E->get().indices[i];
  285. int b = E->get().indices[(i + 1) % f.indices.size()];
  286. Edge e(a, b);
  287. Map<Edge, RetFaceConnect>::Element *F = ret_edges.find(e);
  288. ERR_CONTINUE(!F);
  289. List<Geometry::MeshData::Face>::Element *O = F->get().left == E ? F->get().right : F->get().left;
  290. ERR_CONTINUE(O == E);
  291. ERR_CONTINUE(O == NULL);
  292. if (O->get().plane.is_equal_approx(f.plane)) {
  293. //merge and delete edge and contiguous face, while repointing edges (uuugh!)
  294. int ois = O->get().indices.size();
  295. for (int j = 0; j < ois; j++) {
  296. //search a
  297. if (O->get().indices[j] == a) {
  298. //append the rest
  299. for (int k = 0; k < ois; k++) {
  300. int idx = O->get().indices[(k + j) % ois];
  301. int idxn = O->get().indices[(k + j + 1) % ois];
  302. if (idx == b && idxn == a) { //already have b!
  303. break;
  304. }
  305. if (idx != a) {
  306. f.indices.insert(i + 1, idx);
  307. i++;
  308. }
  309. Edge e2(idx, idxn);
  310. Map<Edge, RetFaceConnect>::Element *F2 = ret_edges.find(e2);
  311. ERR_CONTINUE(!F2);
  312. //change faceconnect, point to this face instead
  313. if (F2->get().left == O)
  314. F2->get().left = E;
  315. else if (F2->get().right == O)
  316. F2->get().right = E;
  317. }
  318. break;
  319. }
  320. }
  321. // remove all edge connections to this face
  322. for (Map<Edge, RetFaceConnect>::Element *G = ret_edges.front(); G; G = G->next()) {
  323. if (G->get().left == O)
  324. G->get().left = NULL;
  325. if (G->get().right == O)
  326. G->get().right = NULL;
  327. }
  328. ret_edges.erase(F); //remove the edge
  329. ret_faces.erase(O); //remove the face
  330. }
  331. }
  332. }
  333. //fill mesh
  334. r_mesh.faces.clear();
  335. r_mesh.faces.resize(ret_faces.size());
  336. int idx = 0;
  337. for (List<Geometry::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
  338. r_mesh.faces.write[idx++] = E->get();
  339. }
  340. r_mesh.edges.resize(ret_edges.size());
  341. idx = 0;
  342. for (Map<Edge, RetFaceConnect>::Element *E = ret_edges.front(); E; E = E->next()) {
  343. Geometry::MeshData::Edge e;
  344. e.a = E->key().vertices[0];
  345. e.b = E->key().vertices[1];
  346. r_mesh.edges.write[idx++] = e;
  347. }
  348. r_mesh.vertices = p_points;
  349. return OK;
  350. }