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