triangle_mesh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*************************************************************************/
  2. /* triangle_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "triangle_mesh.h"
  31. #include "sort.h"
  32. int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc) {
  33. if (p_depth > max_depth) {
  34. max_depth = p_depth;
  35. }
  36. if (p_size == 1) {
  37. return p_bb[p_from] - p_bvh;
  38. } else if (p_size == 0) {
  39. return -1;
  40. }
  41. AABB aabb;
  42. aabb = p_bb[p_from]->aabb;
  43. for (int i = 1; i < p_size; i++) {
  44. aabb.merge_with(p_bb[p_from + i]->aabb);
  45. }
  46. int li = aabb.get_longest_axis_index();
  47. switch (li) {
  48. case Vector3::AXIS_X: {
  49. SortArray<BVH *, BVHCmpX> sort_x;
  50. sort_x.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
  51. //sort_x.sort(&p_bb[p_from],p_size);
  52. } break;
  53. case Vector3::AXIS_Y: {
  54. SortArray<BVH *, BVHCmpY> sort_y;
  55. sort_y.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
  56. //sort_y.sort(&p_bb[p_from],p_size);
  57. } break;
  58. case Vector3::AXIS_Z: {
  59. SortArray<BVH *, BVHCmpZ> sort_z;
  60. sort_z.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
  61. //sort_z.sort(&p_bb[p_from],p_size);
  62. } break;
  63. }
  64. int left = _create_bvh(p_bvh, p_bb, p_from, p_size / 2, p_depth + 1, max_depth, max_alloc);
  65. int right = _create_bvh(p_bvh, p_bb, p_from + p_size / 2, p_size - p_size / 2, p_depth + 1, max_depth, max_alloc);
  66. int index = max_alloc++;
  67. BVH *_new = &p_bvh[index];
  68. _new->aabb = aabb;
  69. _new->center = aabb.pos + aabb.size * 0.5;
  70. _new->face_index = -1;
  71. _new->left = left;
  72. _new->right = right;
  73. return index;
  74. }
  75. void TriangleMesh::create(const DVector<Vector3> &p_faces) {
  76. valid = false;
  77. int fc = p_faces.size();
  78. ERR_FAIL_COND(!fc || ((fc % 3) != 0));
  79. fc /= 3;
  80. triangles.resize(fc);
  81. bvh.resize(fc * 3); //will never be larger than this (todo make better)
  82. DVector<BVH>::Write bw = bvh.write();
  83. {
  84. //create faces and indices and base bvh
  85. //except for the Set for repeated triangles, everything
  86. //goes in-place.
  87. DVector<Vector3>::Read r = p_faces.read();
  88. DVector<Triangle>::Write w = triangles.write();
  89. Map<Vector3, int> db;
  90. for (int i = 0; i < fc; i++) {
  91. Triangle &f = w[i];
  92. const Vector3 *v = &r[i * 3];
  93. for (int j = 0; j < 3; j++) {
  94. int vidx = -1;
  95. Vector3 vs = v[j].snapped(0.0001);
  96. Map<Vector3, int>::Element *E = db.find(vs);
  97. if (E) {
  98. vidx = E->get();
  99. } else {
  100. vidx = db.size();
  101. db[vs] = vidx;
  102. }
  103. f.indices[j] = vidx;
  104. if (j == 0)
  105. bw[i].aabb.pos = vs;
  106. else
  107. bw[i].aabb.expand_to(vs);
  108. }
  109. f.normal = Face3(r[i * 3 + 0], r[i * 3 + 1], r[i * 3 + 2]).get_plane().get_normal();
  110. bw[i].left = -1;
  111. bw[i].right = -1;
  112. bw[i].face_index = i;
  113. bw[i].center = bw[i].aabb.pos + bw[i].aabb.size * 0.5;
  114. }
  115. vertices.resize(db.size());
  116. DVector<Vector3>::Write vw = vertices.write();
  117. for (Map<Vector3, int>::Element *E = db.front(); E; E = E->next()) {
  118. vw[E->get()] = E->key();
  119. }
  120. }
  121. DVector<BVH *> bwptrs;
  122. bwptrs.resize(fc);
  123. DVector<BVH *>::Write bwp = bwptrs.write();
  124. for (int i = 0; i < fc; i++) {
  125. bwp[i] = &bw[i];
  126. }
  127. max_depth = 0;
  128. int max_alloc = fc;
  129. int max = _create_bvh(bw.ptr(), bwp.ptr(), 0, fc, 1, max_depth, max_alloc);
  130. bw = DVector<BVH>::Write(); //clearup
  131. bvh.resize(max_alloc); //resize back
  132. valid = true;
  133. }
  134. Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const {
  135. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  136. enum {
  137. TEST_AABB_BIT = 0,
  138. VISIT_LEFT_BIT = 1,
  139. VISIT_RIGHT_BIT = 2,
  140. VISIT_DONE_BIT = 3,
  141. VISITED_BIT_SHIFT = 29,
  142. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  143. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  144. };
  145. int n_count = 0;
  146. Vector3 n;
  147. int level = 0;
  148. DVector<Triangle>::Read trianglesr = triangles.read();
  149. DVector<Vector3>::Read verticesr = vertices.read();
  150. DVector<BVH>::Read bvhr = bvh.read();
  151. const Triangle *triangleptr = trianglesr.ptr();
  152. int pos = bvh.size() - 1;
  153. const BVH *bvhptr = bvhr.ptr();
  154. stack[0] = pos;
  155. while (true) {
  156. uint32_t node = stack[level] & NODE_IDX_MASK;
  157. const BVH &b = bvhptr[node];
  158. bool done = false;
  159. switch (stack[level] >> VISITED_BIT_SHIFT) {
  160. case TEST_AABB_BIT: {
  161. bool valid = b.aabb.intersects(p_aabb);
  162. if (!valid) {
  163. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  164. } else {
  165. if (b.face_index >= 0) {
  166. const Triangle &s = triangleptr[b.face_index];
  167. n += s.normal;
  168. n_count++;
  169. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  170. } else {
  171. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  172. }
  173. }
  174. continue;
  175. }
  176. case VISIT_LEFT_BIT: {
  177. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  178. stack[level + 1] = b.left | TEST_AABB_BIT;
  179. level++;
  180. continue;
  181. }
  182. case VISIT_RIGHT_BIT: {
  183. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  184. stack[level + 1] = b.right | TEST_AABB_BIT;
  185. level++;
  186. continue;
  187. }
  188. case VISIT_DONE_BIT: {
  189. if (level == 0) {
  190. done = true;
  191. break;
  192. } else
  193. level--;
  194. continue;
  195. }
  196. }
  197. if (done)
  198. break;
  199. }
  200. if (n_count > 0)
  201. n /= n_count;
  202. return n;
  203. }
  204. bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const {
  205. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  206. enum {
  207. TEST_AABB_BIT = 0,
  208. VISIT_LEFT_BIT = 1,
  209. VISIT_RIGHT_BIT = 2,
  210. VISIT_DONE_BIT = 3,
  211. VISITED_BIT_SHIFT = 29,
  212. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  213. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  214. };
  215. Vector3 n = (p_end - p_begin).normalized();
  216. real_t d = 1e10;
  217. bool inters = false;
  218. int level = 0;
  219. DVector<Triangle>::Read trianglesr = triangles.read();
  220. DVector<Vector3>::Read verticesr = vertices.read();
  221. DVector<BVH>::Read bvhr = bvh.read();
  222. const Triangle *triangleptr = trianglesr.ptr();
  223. const Vector3 *vertexptr = verticesr.ptr();
  224. int pos = bvh.size() - 1;
  225. const BVH *bvhptr = bvhr.ptr();
  226. stack[0] = pos;
  227. while (true) {
  228. uint32_t node = stack[level] & NODE_IDX_MASK;
  229. const BVH &b = bvhptr[node];
  230. bool done = false;
  231. switch (stack[level] >> VISITED_BIT_SHIFT) {
  232. case TEST_AABB_BIT: {
  233. bool valid = b.aabb.intersects_segment(p_begin, p_end);
  234. // bool valid = b.aabb.intersects(ray_aabb);
  235. if (!valid) {
  236. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  237. } else {
  238. if (b.face_index >= 0) {
  239. const Triangle &s = triangleptr[b.face_index];
  240. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  241. Vector3 res;
  242. if (f3.intersects_segment(p_begin, p_end, &res)) {
  243. float nd = n.dot(res);
  244. if (nd < d) {
  245. d = nd;
  246. r_point = res;
  247. r_normal = f3.get_plane().get_normal();
  248. inters = true;
  249. }
  250. }
  251. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  252. } else {
  253. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  254. }
  255. }
  256. continue;
  257. }
  258. case VISIT_LEFT_BIT: {
  259. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  260. stack[level + 1] = b.left | TEST_AABB_BIT;
  261. level++;
  262. continue;
  263. }
  264. case VISIT_RIGHT_BIT: {
  265. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  266. stack[level + 1] = b.right | TEST_AABB_BIT;
  267. level++;
  268. continue;
  269. }
  270. case VISIT_DONE_BIT: {
  271. if (level == 0) {
  272. done = true;
  273. break;
  274. } else
  275. level--;
  276. continue;
  277. }
  278. }
  279. if (done)
  280. break;
  281. }
  282. if (inters) {
  283. if (n.dot(r_normal) > 0)
  284. r_normal = -r_normal;
  285. }
  286. return inters;
  287. }
  288. bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const {
  289. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  290. enum {
  291. TEST_AABB_BIT = 0,
  292. VISIT_LEFT_BIT = 1,
  293. VISIT_RIGHT_BIT = 2,
  294. VISIT_DONE_BIT = 3,
  295. VISITED_BIT_SHIFT = 29,
  296. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  297. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  298. };
  299. Vector3 n = p_dir;
  300. real_t d = 1e20;
  301. bool inters = false;
  302. int level = 0;
  303. DVector<Triangle>::Read trianglesr = triangles.read();
  304. DVector<Vector3>::Read verticesr = vertices.read();
  305. DVector<BVH>::Read bvhr = bvh.read();
  306. const Triangle *triangleptr = trianglesr.ptr();
  307. const Vector3 *vertexptr = verticesr.ptr();
  308. int pos = bvh.size() - 1;
  309. const BVH *bvhptr = bvhr.ptr();
  310. stack[0] = pos;
  311. while (true) {
  312. uint32_t node = stack[level] & NODE_IDX_MASK;
  313. const BVH &b = bvhptr[node];
  314. bool done = false;
  315. switch (stack[level] >> VISITED_BIT_SHIFT) {
  316. case TEST_AABB_BIT: {
  317. bool valid = b.aabb.intersects_ray(p_begin, p_dir);
  318. if (!valid) {
  319. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  320. } else {
  321. if (b.face_index >= 0) {
  322. const Triangle &s = triangleptr[b.face_index];
  323. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  324. Vector3 res;
  325. if (f3.intersects_ray(p_begin, p_dir, &res)) {
  326. float nd = n.dot(res);
  327. if (nd < d) {
  328. d = nd;
  329. r_point = res;
  330. r_normal = f3.get_plane().get_normal();
  331. inters = true;
  332. }
  333. }
  334. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  335. } else {
  336. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  337. }
  338. }
  339. continue;
  340. }
  341. case VISIT_LEFT_BIT: {
  342. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  343. stack[level + 1] = b.left | TEST_AABB_BIT;
  344. level++;
  345. continue;
  346. }
  347. case VISIT_RIGHT_BIT: {
  348. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  349. stack[level + 1] = b.right | TEST_AABB_BIT;
  350. level++;
  351. continue;
  352. }
  353. case VISIT_DONE_BIT: {
  354. if (level == 0) {
  355. done = true;
  356. break;
  357. } else
  358. level--;
  359. continue;
  360. }
  361. }
  362. if (done)
  363. break;
  364. }
  365. if (inters) {
  366. if (n.dot(r_normal) > 0)
  367. r_normal = -r_normal;
  368. }
  369. return inters;
  370. }
  371. bool TriangleMesh::is_valid() const {
  372. return valid;
  373. }
  374. DVector<Face3> TriangleMesh::get_faces() const {
  375. if (!valid)
  376. return DVector<Face3>();
  377. DVector<Face3> faces;
  378. int ts = triangles.size();
  379. faces.resize(triangles.size());
  380. DVector<Face3>::Write w = faces.write();
  381. DVector<Triangle>::Read r = triangles.read();
  382. DVector<Vector3>::Read rv = vertices.read();
  383. for (int i = 0; i < ts; i++) {
  384. for (int j = 0; j < 3; j++) {
  385. w[i].vertex[j] = rv[r[i].indices[j]];
  386. }
  387. }
  388. w = DVector<Face3>::Write();
  389. return faces;
  390. }
  391. TriangleMesh::TriangleMesh() {
  392. valid = false;
  393. max_depth = 0;
  394. }