quick_hull.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #include "quick_hull.h"
  31. #include "core/templates/rb_map.h"
  32. uint32_t QuickHull::debug_stop_after = 0xFFFFFFFF;
  33. Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::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. HashSet<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. }
  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. }
  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. }
  103. real_t d = Math::abs(p.distance_to(p_points[i]));
  104. if (i == 0 || d > maxd) {
  105. maxd = d;
  106. simplex[3] = i;
  107. }
  108. }
  109. }
  110. //compute center of simplex, this is a point always warranted to be inside
  111. Vector3 center;
  112. for (int i = 0; i < 4; i++) {
  113. center += p_points[simplex[i]];
  114. }
  115. center /= 4.0;
  116. //add faces
  117. List<Face> faces;
  118. for (int i = 0; i < 4; i++) {
  119. static const int face_order[4][3] = {
  120. { 0, 1, 2 },
  121. { 0, 1, 3 },
  122. { 0, 2, 3 },
  123. { 1, 2, 3 }
  124. };
  125. Face f;
  126. for (int j = 0; j < 3; j++) {
  127. f.vertices[j] = simplex[face_order[i][j]];
  128. }
  129. Plane p(p_points[f.vertices[0]], p_points[f.vertices[1]], p_points[f.vertices[2]]);
  130. if (p.is_point_over(center)) {
  131. //flip face to clockwise if facing inwards
  132. SWAP(f.vertices[0], f.vertices[1]);
  133. p = -p;
  134. }
  135. f.plane = p;
  136. faces.push_back(f);
  137. }
  138. real_t over_tolerance = 3 * UNIT_EPSILON * (aabb.size.x + aabb.size.y + aabb.size.z);
  139. /* COMPUTE AVAILABLE VERTICES */
  140. for (int i = 0; i < p_points.size(); i++) {
  141. if (i == simplex[0]) {
  142. continue;
  143. }
  144. if (i == simplex[1]) {
  145. continue;
  146. }
  147. if (i == simplex[2]) {
  148. continue;
  149. }
  150. if (i == simplex[3]) {
  151. continue;
  152. }
  153. if (!valid_points[i]) {
  154. continue;
  155. }
  156. for (Face &E : faces) {
  157. if (E.plane.distance_to(p_points[i]) > over_tolerance) {
  158. E.points_over.push_back(i);
  159. break;
  160. }
  161. }
  162. }
  163. faces.sort(); // sort them, so the ones with points are in the back
  164. /* BUILD HULL */
  165. //poop face (while still remain)
  166. //find further away point
  167. //find lit faces
  168. //determine horizon edges
  169. //build new faces with horizon edges, them assign points side from all lit faces
  170. //remove lit faces
  171. uint32_t debug_stop = debug_stop_after;
  172. while (debug_stop > 0 && faces.back()->get().points_over.size()) {
  173. debug_stop--;
  174. Face &f = faces.back()->get();
  175. //find vertex most outside
  176. int next = -1;
  177. real_t next_d = 0;
  178. for (int i = 0; i < f.points_over.size(); i++) {
  179. real_t d = f.plane.distance_to(p_points[f.points_over[i]]);
  180. if (d > next_d) {
  181. next_d = d;
  182. next = i;
  183. }
  184. }
  185. ERR_FAIL_COND_V(next == -1, ERR_BUG);
  186. Vector3 v = p_points[f.points_over[next]];
  187. //find lit faces and lit edges
  188. List<List<Face>::Element *> lit_faces; //lit face is a death sentence
  189. HashMap<Edge, FaceConnect, Edge> lit_edges; //create this on the flight, should not be that bad for performance and simplifies code a lot
  190. for (List<Face>::Element *E = faces.front(); E; E = E->next()) {
  191. if (E->get().plane.distance_to(v) > 0) {
  192. lit_faces.push_back(E);
  193. for (int i = 0; i < 3; i++) {
  194. uint32_t a = E->get().vertices[i];
  195. uint32_t b = E->get().vertices[(i + 1) % 3];
  196. Edge e(a, b);
  197. HashMap<Edge, FaceConnect, Edge>::Iterator F = lit_edges.find(e);
  198. if (!F) {
  199. F = lit_edges.insert(e, FaceConnect());
  200. }
  201. if (e.vertices[0] == a) {
  202. //left
  203. F->value.left = E;
  204. } else {
  205. F->value.right = E;
  206. }
  207. }
  208. }
  209. }
  210. //create new faces from horizon edges
  211. List<List<Face>::Element *> new_faces; //new faces
  212. for (KeyValue<Edge, FaceConnect> &E : lit_edges) {
  213. FaceConnect &fc = E.value;
  214. if (fc.left && fc.right) {
  215. continue; //edge is uninteresting, not on horizon
  216. }
  217. //create new face!
  218. Face face;
  219. face.vertices[0] = f.points_over[next];
  220. face.vertices[1] = E.key.vertices[0];
  221. face.vertices[2] = E.key.vertices[1];
  222. Plane p(p_points[face.vertices[0]], p_points[face.vertices[1]], p_points[face.vertices[2]]);
  223. if (p.is_point_over(center)) {
  224. //flip face to clockwise if facing inwards
  225. SWAP(face.vertices[0], face.vertices[1]);
  226. p = -p;
  227. }
  228. face.plane = p;
  229. new_faces.push_back(faces.push_back(face));
  230. }
  231. //distribute points into new faces
  232. for (List<Face>::Element *&F : lit_faces) {
  233. Face &lf = F->get();
  234. for (int i = 0; i < lf.points_over.size(); i++) {
  235. if (lf.points_over[i] == f.points_over[next]) { //do not add current one
  236. continue;
  237. }
  238. Vector3 p = p_points[lf.points_over[i]];
  239. for (List<Face>::Element *&E : new_faces) {
  240. Face &f2 = E->get();
  241. if (f2.plane.distance_to(p) > over_tolerance) {
  242. f2.points_over.push_back(lf.points_over[i]);
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. //erase lit faces
  249. while (lit_faces.size()) {
  250. faces.erase(lit_faces.front()->get());
  251. lit_faces.pop_front();
  252. }
  253. //put faces that contain no points on the front
  254. for (List<Face>::Element *&E : new_faces) {
  255. Face &f2 = E->get();
  256. if (f2.points_over.size() == 0) {
  257. faces.move_to_front(E);
  258. }
  259. }
  260. //whew, done with iteration, go next
  261. }
  262. /* CREATE MESHDATA */
  263. //make a map of edges again
  264. HashMap<Edge, RetFaceConnect, Edge> ret_edges;
  265. List<Geometry3D::MeshData::Face> ret_faces;
  266. for (const Face &E : faces) {
  267. Geometry3D::MeshData::Face f;
  268. f.plane = E.plane;
  269. for (int i = 0; i < 3; i++) {
  270. f.indices.push_back(E.vertices[i]);
  271. }
  272. List<Geometry3D::MeshData::Face>::Element *F = ret_faces.push_back(f);
  273. for (int i = 0; i < 3; i++) {
  274. uint32_t a = E.vertices[i];
  275. uint32_t b = E.vertices[(i + 1) % 3];
  276. Edge e(a, b);
  277. HashMap<Edge, RetFaceConnect, Edge>::Iterator G = ret_edges.find(e);
  278. if (!G) {
  279. G = ret_edges.insert(e, RetFaceConnect());
  280. }
  281. if (e.vertices[0] == a) {
  282. //left
  283. G->value.left = F;
  284. } else {
  285. G->value.right = F;
  286. }
  287. }
  288. }
  289. //fill faces
  290. for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
  291. Geometry3D::MeshData::Face &f = E->get();
  292. for (uint32_t i = 0; i < f.indices.size(); i++) {
  293. int a = E->get().indices[i];
  294. int b = E->get().indices[(i + 1) % f.indices.size()];
  295. Edge e(a, b);
  296. HashMap<Edge, RetFaceConnect, Edge>::Iterator F = ret_edges.find(e);
  297. ERR_CONTINUE(!F);
  298. List<Geometry3D::MeshData::Face>::Element *O = F->value.left == E ? F->value.right : F->value.left;
  299. ERR_CONTINUE(O == E);
  300. ERR_CONTINUE(O == nullptr);
  301. if (O->get().plane.is_equal_approx(f.plane)) {
  302. //merge and delete edge and contiguous face, while repointing edges (uuugh!)
  303. int o_index_size = O->get().indices.size();
  304. for (int j = 0; j < o_index_size; j++) {
  305. //search a
  306. if (O->get().indices[j] == a) {
  307. //append the rest
  308. for (int k = 0; k < o_index_size; k++) {
  309. int idx = O->get().indices[(k + j) % o_index_size];
  310. int idxn = O->get().indices[(k + j + 1) % o_index_size];
  311. if (idx == b && idxn == a) { //already have b!
  312. break;
  313. }
  314. if (idx != a) {
  315. f.indices.insert(i + 1, idx);
  316. i++;
  317. }
  318. Edge e2(idx, idxn);
  319. HashMap<Edge, RetFaceConnect, Edge>::Iterator F2 = ret_edges.find(e2);
  320. ERR_CONTINUE(!F2);
  321. //change faceconnect, point to this face instead
  322. if (F2->value.left == O) {
  323. F2->value.left = E;
  324. } else if (F2->value.right == O) {
  325. F2->value.right = E;
  326. }
  327. }
  328. break;
  329. }
  330. }
  331. // remove all edge connections to this face
  332. for (KeyValue<Edge, RetFaceConnect> &G : ret_edges) {
  333. if (G.value.left == O) {
  334. G.value.left = nullptr;
  335. }
  336. if (G.value.right == O) {
  337. G.value.right = nullptr;
  338. }
  339. }
  340. ret_edges.remove(F); //remove the edge
  341. ret_faces.erase(O); //remove the face
  342. }
  343. }
  344. }
  345. //fill mesh
  346. r_mesh.faces.clear();
  347. r_mesh.faces.resize(ret_faces.size());
  348. HashMap<List<Geometry3D::MeshData::Face>::Element *, int> face_indices;
  349. int idx = 0;
  350. for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) {
  351. face_indices[E] = idx;
  352. r_mesh.faces[idx++] = E->get();
  353. }
  354. r_mesh.edges.resize(ret_edges.size());
  355. idx = 0;
  356. for (const KeyValue<Edge, RetFaceConnect> &E : ret_edges) {
  357. Geometry3D::MeshData::Edge e;
  358. e.vertex_a = E.key.vertices[0];
  359. e.vertex_b = E.key.vertices[1];
  360. ERR_CONTINUE(!face_indices.has(E.value.left));
  361. ERR_CONTINUE(!face_indices.has(E.value.right));
  362. e.face_a = face_indices[E.value.left];
  363. e.face_b = face_indices[E.value.right];
  364. r_mesh.edges[idx++] = e;
  365. }
  366. r_mesh.vertices = p_points;
  367. return OK;
  368. }