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