quick_hull.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*************************************************************************/
  2. /* quick_hull.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. static const real_t over_tolerance = 0.0001;
  35. /* CREATE AABB VOLUME */
  36. AABB aabb;
  37. for (int i = 0; i < p_points.size(); i++) {
  38. if (i == 0) {
  39. aabb.position = p_points[i];
  40. } else {
  41. aabb.expand_to(p_points[i]);
  42. }
  43. }
  44. if (aabb.size == Vector3()) {
  45. return ERR_CANT_CREATE;
  46. }
  47. Vector<bool> valid_points;
  48. valid_points.resize(p_points.size());
  49. Set<Vector3> valid_cache;
  50. for (int i = 0; i < p_points.size(); i++) {
  51. Vector3 sp = p_points[i].snapped(Vector3(0.0001, 0.0001, 0.0001));
  52. if (valid_cache.has(sp)) {
  53. valid_points.write[i] = false;
  54. } else {
  55. valid_points.write[i] = true;
  56. valid_cache.insert(sp);
  57. }
  58. }
  59. /* CREATE INITIAL SIMPLEX */
  60. int longest_axis = aabb.get_longest_axis_index();
  61. //first two vertices are the most distant
  62. int simplex[4] = { 0 };
  63. {
  64. real_t max = 0, min = 0;
  65. for (int i = 0; i < p_points.size(); i++) {
  66. if (!valid_points[i])
  67. continue;
  68. real_t d = p_points[i][longest_axis];
  69. if (i == 0 || d < min) {
  70. simplex[0] = i;
  71. min = d;
  72. }
  73. if (i == 0 || d > max) {
  74. simplex[1] = i;
  75. max = d;
  76. }
  77. }
  78. }
  79. //third vertex is one most further away from the line
  80. {
  81. real_t maxd = 0;
  82. Vector3 rel12 = p_points[simplex[0]] - p_points[simplex[1]];
  83. for (int i = 0; i < p_points.size(); i++) {
  84. if (!valid_points[i])
  85. continue;
  86. Vector3 n = rel12.cross(p_points[simplex[0]] - p_points[i]).cross(rel12).normalized();
  87. real_t d = Math::abs(n.dot(p_points[simplex[0]]) - n.dot(p_points[i]));
  88. if (i == 0 || d > maxd) {
  89. maxd = d;
  90. simplex[2] = i;
  91. }
  92. }
  93. }
  94. //fourth vertex is the one most further away from the plane
  95. {
  96. real_t maxd = 0;
  97. Plane p(p_points[simplex[0]], p_points[simplex[1]], p_points[simplex[2]]);
  98. for (int i = 0; i < p_points.size(); i++) {
  99. if (!valid_points[i])
  100. continue;
  101. real_t d = Math::abs(p.distance_to(p_points[i]));
  102. if (i == 0 || d > maxd) {
  103. maxd = d;
  104. simplex[3] = i;
  105. }
  106. }
  107. }
  108. //compute center of simplex, this is a point always warranted to be inside
  109. Vector3 center;
  110. for (int i = 0; i < 4; i++) {
  111. center += p_points[simplex[i]];
  112. }
  113. center /= 4.0;
  114. //add faces
  115. List<Face> faces;
  116. for (int i = 0; i < 4; i++) {
  117. static const int face_order[4][3] = {
  118. { 0, 1, 2 },
  119. { 0, 1, 3 },
  120. { 0, 2, 3 },
  121. { 1, 2, 3 }
  122. };
  123. Face f;
  124. for (int j = 0; j < 3; j++) {
  125. f.vertices[j] = simplex[face_order[i][j]];
  126. }
  127. Plane p(p_points[f.vertices[0]], p_points[f.vertices[1]], p_points[f.vertices[2]]);
  128. if (p.is_point_over(center)) {
  129. //flip face to clockwise if facing inwards
  130. SWAP(f.vertices[0], f.vertices[1]);
  131. p = -p;
  132. }
  133. f.plane = p;
  134. faces.push_back(f);
  135. }
  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_almost_like(f.plane)) {
  293. //merge and delete edge and contiguous face, while repointing edges (uuugh!)
  294. int ois = O->get().indices.size();
  295. int merged = 0;
  296. for (int j = 0; j < ois; j++) {
  297. //search a
  298. if (O->get().indices[j] == a) {
  299. //append the rest
  300. for (int k = 0; k < ois; k++) {
  301. int idx = O->get().indices[(k + j) % ois];
  302. int idxn = O->get().indices[(k + j + 1) % ois];
  303. if (idx == b && idxn == a) { //already have b!
  304. break;
  305. }
  306. if (idx != a) {
  307. f.indices.insert(i + 1, idx);
  308. i++;
  309. merged++;
  310. }
  311. Edge e2(idx, idxn);
  312. Map<Edge, RetFaceConnect>::Element *F2 = ret_edges.find(e2);
  313. ERR_CONTINUE(!F2);
  314. //change faceconnect, point to this face instead
  315. if (F2->get().left == O)
  316. F2->get().left = E;
  317. else if (F2->get().right == O)
  318. F2->get().right = E;
  319. }
  320. break;
  321. }
  322. }
  323. // remove all edge connections to this face
  324. for (Map<Edge, RetFaceConnect>::Element *E = ret_edges.front(); E; E = E->next()) {
  325. if (E->get().left == O)
  326. E->get().left = NULL;
  327. if (E->get().right == O)
  328. E->get().right = NULL;
  329. }
  330. ret_edges.erase(F); //remove the edge
  331. ret_faces.erase(O); //remove the face
  332. }
  333. }
  334. }
  335. //fill mesh
  336. r_mesh.faces.clear();
  337. r_mesh.faces.resize(ret_faces.size());
  338. int idx = 0;
  339. for (List<Geometry::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
  340. r_mesh.faces.write[idx++] = E->get();
  341. }
  342. r_mesh.edges.resize(ret_edges.size());
  343. idx = 0;
  344. for (Map<Edge, RetFaceConnect>::Element *E = ret_edges.front(); E; E = E->next()) {
  345. Geometry::MeshData::Edge e;
  346. e.a = E->key().vertices[0];
  347. e.b = E->key().vertices[1];
  348. r_mesh.edges.write[idx++] = e;
  349. }
  350. r_mesh.vertices = p_points;
  351. return OK;
  352. }