quick_hull.cpp 12 KB

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