triangle_mesh.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*************************************************************************/
  2. /* triangle_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "core/templates/sort_array.h"
  32. int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &r_max_depth, int &r_max_alloc) {
  33. if (p_depth > r_max_depth) {
  34. r_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, r_max_depth, r_max_alloc);
  65. int right = _create_bvh(p_bvh, p_bb, p_from + p_size / 2, p_size - p_size / 2, p_depth + 1, r_max_depth, r_max_alloc);
  66. int index = r_max_alloc++;
  67. BVH *_new = &p_bvh[index];
  68. _new->aabb = aabb;
  69. _new->center = aabb.get_center();
  70. _new->face_index = -1;
  71. _new->left = left;
  72. _new->right = right;
  73. return index;
  74. }
  75. void TriangleMesh::get_indices(Vector<int> *r_triangles_indices) const {
  76. if (!valid) {
  77. return;
  78. }
  79. const int triangles_num = triangles.size();
  80. // Parse vertices indices
  81. const Triangle *triangles_read = triangles.ptr();
  82. r_triangles_indices->resize(triangles_num * 3);
  83. int *r_indices_write = r_triangles_indices->ptrw();
  84. for (int i = 0; i < triangles_num; ++i) {
  85. r_indices_write[3 * i + 0] = triangles_read[i].indices[0];
  86. r_indices_write[3 * i + 1] = triangles_read[i].indices[1];
  87. r_indices_write[3 * i + 2] = triangles_read[i].indices[2];
  88. }
  89. }
  90. void TriangleMesh::create(const Vector<Vector3> &p_faces, const Vector<int32_t> &p_surface_indices) {
  91. valid = false;
  92. ERR_FAIL_COND(p_surface_indices.size() && p_surface_indices.size() != p_faces.size());
  93. int fc = p_faces.size();
  94. ERR_FAIL_COND(!fc || ((fc % 3) != 0));
  95. fc /= 3;
  96. triangles.resize(fc);
  97. bvh.resize(fc * 3); //will never be larger than this (todo make better)
  98. BVH *bw = bvh.ptrw();
  99. {
  100. //create faces and indices and base bvh
  101. //except for the Set for repeated triangles, everything
  102. //goes in-place.
  103. const Vector3 *r = p_faces.ptr();
  104. const int32_t *si = p_surface_indices.ptr();
  105. Triangle *w = triangles.ptrw();
  106. HashMap<Vector3, int> db;
  107. for (int i = 0; i < fc; i++) {
  108. Triangle &f = w[i];
  109. const Vector3 *v = &r[i * 3];
  110. for (int j = 0; j < 3; j++) {
  111. int vidx = -1;
  112. Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001));
  113. HashMap<Vector3, int>::Iterator E = db.find(vs);
  114. if (E) {
  115. vidx = E->value;
  116. } else {
  117. vidx = db.size();
  118. db[vs] = vidx;
  119. }
  120. f.indices[j] = vidx;
  121. if (j == 0) {
  122. bw[i].aabb.position = vs;
  123. } else {
  124. bw[i].aabb.expand_to(vs);
  125. }
  126. }
  127. f.normal = Face3(r[i * 3 + 0], r[i * 3 + 1], r[i * 3 + 2]).get_plane().get_normal();
  128. f.surface_index = si ? si[i] : 0;
  129. bw[i].left = -1;
  130. bw[i].right = -1;
  131. bw[i].face_index = i;
  132. bw[i].center = bw[i].aabb.get_center();
  133. }
  134. vertices.resize(db.size());
  135. Vector3 *vw = vertices.ptrw();
  136. for (const KeyValue<Vector3, int> &E : db) {
  137. vw[E.value] = E.key;
  138. }
  139. }
  140. Vector<BVH *> bwptrs;
  141. bwptrs.resize(fc);
  142. BVH **bwp = bwptrs.ptrw();
  143. for (int i = 0; i < fc; i++) {
  144. bwp[i] = &bw[i];
  145. }
  146. max_depth = 0;
  147. int max_alloc = fc;
  148. _create_bvh(bw, bwp, 0, fc, 1, max_depth, max_alloc);
  149. bvh.resize(max_alloc); //resize back
  150. valid = true;
  151. }
  152. Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const {
  153. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  154. enum {
  155. TEST_AABB_BIT = 0,
  156. VISIT_LEFT_BIT = 1,
  157. VISIT_RIGHT_BIT = 2,
  158. VISIT_DONE_BIT = 3,
  159. VISITED_BIT_SHIFT = 29,
  160. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  161. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  162. };
  163. int n_count = 0;
  164. Vector3 n;
  165. int level = 0;
  166. const Triangle *triangleptr = triangles.ptr();
  167. // const Vector3 *verticesr = vertices.ptr();
  168. const BVH *bvhptr = bvh.ptr();
  169. int pos = bvh.size() - 1;
  170. stack[0] = pos;
  171. while (true) {
  172. uint32_t node = stack[level] & NODE_IDX_MASK;
  173. const BVH &b = bvhptr[node];
  174. bool done = false;
  175. switch (stack[level] >> VISITED_BIT_SHIFT) {
  176. case TEST_AABB_BIT: {
  177. bool valid = b.aabb.intersects(p_aabb);
  178. if (!valid) {
  179. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  180. } else {
  181. if (b.face_index >= 0) {
  182. const Triangle &s = triangleptr[b.face_index];
  183. n += s.normal;
  184. n_count++;
  185. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  186. } else {
  187. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  188. }
  189. }
  190. continue;
  191. }
  192. case VISIT_LEFT_BIT: {
  193. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  194. level++;
  195. stack[level] = b.left | TEST_AABB_BIT;
  196. continue;
  197. }
  198. case VISIT_RIGHT_BIT: {
  199. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  200. level++;
  201. stack[level] = b.right | TEST_AABB_BIT;
  202. continue;
  203. }
  204. case VISIT_DONE_BIT: {
  205. if (level == 0) {
  206. done = true;
  207. break;
  208. } else {
  209. level--;
  210. }
  211. continue;
  212. }
  213. }
  214. if (done) {
  215. break;
  216. }
  217. }
  218. if (n_count > 0) {
  219. n /= n_count;
  220. }
  221. return n;
  222. }
  223. bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index) const {
  224. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  225. enum {
  226. TEST_AABB_BIT = 0,
  227. VISIT_LEFT_BIT = 1,
  228. VISIT_RIGHT_BIT = 2,
  229. VISIT_DONE_BIT = 3,
  230. VISITED_BIT_SHIFT = 29,
  231. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  232. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  233. };
  234. Vector3 n = (p_end - p_begin).normalized();
  235. real_t d = 1e10;
  236. bool inters = false;
  237. int level = 0;
  238. const Triangle *triangleptr = triangles.ptr();
  239. const Vector3 *vertexptr = vertices.ptr();
  240. const BVH *bvhptr = bvh.ptr();
  241. int pos = bvh.size() - 1;
  242. stack[0] = pos;
  243. while (true) {
  244. uint32_t node = stack[level] & NODE_IDX_MASK;
  245. const BVH &b = bvhptr[node];
  246. bool done = false;
  247. switch (stack[level] >> VISITED_BIT_SHIFT) {
  248. case TEST_AABB_BIT: {
  249. bool valid = b.aabb.intersects_segment(p_begin, p_end);
  250. //bool valid = b.aabb.intersects(ray_aabb);
  251. if (!valid) {
  252. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  253. } else {
  254. if (b.face_index >= 0) {
  255. const Triangle &s = triangleptr[b.face_index];
  256. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  257. Vector3 res;
  258. if (f3.intersects_segment(p_begin, p_end, &res)) {
  259. real_t nd = n.dot(res);
  260. if (nd < d) {
  261. d = nd;
  262. r_point = res;
  263. r_normal = f3.get_plane().get_normal();
  264. if (r_surf_index) {
  265. *r_surf_index = s.surface_index;
  266. }
  267. inters = true;
  268. }
  269. }
  270. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  271. } else {
  272. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  273. }
  274. }
  275. continue;
  276. }
  277. case VISIT_LEFT_BIT: {
  278. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  279. level++;
  280. stack[level] = b.left | TEST_AABB_BIT;
  281. continue;
  282. }
  283. case VISIT_RIGHT_BIT: {
  284. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  285. level++;
  286. stack[level] = b.right | TEST_AABB_BIT;
  287. continue;
  288. }
  289. case VISIT_DONE_BIT: {
  290. if (level == 0) {
  291. done = true;
  292. break;
  293. } else {
  294. level--;
  295. }
  296. continue;
  297. }
  298. }
  299. if (done) {
  300. break;
  301. }
  302. }
  303. if (inters) {
  304. if (n.dot(r_normal) > 0) {
  305. r_normal = -r_normal;
  306. }
  307. }
  308. return inters;
  309. }
  310. bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index) const {
  311. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  312. enum {
  313. TEST_AABB_BIT = 0,
  314. VISIT_LEFT_BIT = 1,
  315. VISIT_RIGHT_BIT = 2,
  316. VISIT_DONE_BIT = 3,
  317. VISITED_BIT_SHIFT = 29,
  318. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  319. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  320. };
  321. Vector3 n = p_dir;
  322. real_t d = 1e20;
  323. bool inters = false;
  324. int level = 0;
  325. const Triangle *triangleptr = triangles.ptr();
  326. const Vector3 *vertexptr = vertices.ptr();
  327. const BVH *bvhptr = bvh.ptr();
  328. int pos = bvh.size() - 1;
  329. stack[0] = pos;
  330. while (true) {
  331. uint32_t node = stack[level] & NODE_IDX_MASK;
  332. const BVH &b = bvhptr[node];
  333. bool done = false;
  334. switch (stack[level] >> VISITED_BIT_SHIFT) {
  335. case TEST_AABB_BIT: {
  336. bool valid = b.aabb.intersects_ray(p_begin, p_dir);
  337. if (!valid) {
  338. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  339. } else {
  340. if (b.face_index >= 0) {
  341. const Triangle &s = triangleptr[b.face_index];
  342. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  343. Vector3 res;
  344. if (f3.intersects_ray(p_begin, p_dir, &res)) {
  345. real_t nd = n.dot(res);
  346. if (nd < d) {
  347. d = nd;
  348. r_point = res;
  349. r_normal = f3.get_plane().get_normal();
  350. if (r_surf_index) {
  351. *r_surf_index = s.surface_index;
  352. }
  353. inters = true;
  354. }
  355. }
  356. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  357. } else {
  358. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  359. }
  360. }
  361. continue;
  362. }
  363. case VISIT_LEFT_BIT: {
  364. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  365. level++;
  366. stack[level] = b.left | TEST_AABB_BIT;
  367. continue;
  368. }
  369. case VISIT_RIGHT_BIT: {
  370. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  371. level++;
  372. stack[level] = b.right | TEST_AABB_BIT;
  373. continue;
  374. }
  375. case VISIT_DONE_BIT: {
  376. if (level == 0) {
  377. done = true;
  378. break;
  379. } else {
  380. level--;
  381. }
  382. continue;
  383. }
  384. }
  385. if (done) {
  386. break;
  387. }
  388. }
  389. if (inters) {
  390. if (n.dot(r_normal) > 0) {
  391. r_normal = -r_normal;
  392. }
  393. }
  394. return inters;
  395. }
  396. bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
  397. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  398. //p_fully_inside = true;
  399. enum {
  400. TEST_AABB_BIT = 0,
  401. VISIT_LEFT_BIT = 1,
  402. VISIT_RIGHT_BIT = 2,
  403. VISIT_DONE_BIT = 3,
  404. VISITED_BIT_SHIFT = 29,
  405. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  406. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  407. };
  408. int level = 0;
  409. const Triangle *triangleptr = triangles.ptr();
  410. const Vector3 *vertexptr = vertices.ptr();
  411. const BVH *bvhptr = bvh.ptr();
  412. int pos = bvh.size() - 1;
  413. stack[0] = pos;
  414. while (true) {
  415. uint32_t node = stack[level] & NODE_IDX_MASK;
  416. const BVH &b = bvhptr[node];
  417. bool done = false;
  418. switch (stack[level] >> VISITED_BIT_SHIFT) {
  419. case TEST_AABB_BIT: {
  420. bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
  421. if (!valid) {
  422. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  423. } else {
  424. if (b.face_index >= 0) {
  425. const Triangle &s = triangleptr[b.face_index];
  426. for (int j = 0; j < 3; ++j) {
  427. const Vector3 &point = vertexptr[s.indices[j]];
  428. const Vector3 &next_point = vertexptr[s.indices[(j + 1) % 3]];
  429. Vector3 res;
  430. bool over = true;
  431. for (int i = 0; i < p_plane_count; i++) {
  432. const Plane &p = p_planes[i];
  433. if (p.intersects_segment(point, next_point, &res)) {
  434. bool inisde = true;
  435. for (int k = 0; k < p_plane_count; k++) {
  436. if (k == i) {
  437. continue;
  438. }
  439. const Plane &pp = p_planes[k];
  440. if (pp.is_point_over(res)) {
  441. inisde = false;
  442. break;
  443. }
  444. }
  445. if (inisde) {
  446. return true;
  447. }
  448. }
  449. if (p.is_point_over(point)) {
  450. over = false;
  451. break;
  452. }
  453. }
  454. if (over) {
  455. return true;
  456. }
  457. }
  458. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  459. } else {
  460. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  461. }
  462. }
  463. continue;
  464. }
  465. case VISIT_LEFT_BIT: {
  466. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  467. level++;
  468. stack[level] = b.left | TEST_AABB_BIT;
  469. continue;
  470. }
  471. case VISIT_RIGHT_BIT: {
  472. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  473. level++;
  474. stack[level] = b.right | TEST_AABB_BIT;
  475. continue;
  476. }
  477. case VISIT_DONE_BIT: {
  478. if (level == 0) {
  479. done = true;
  480. break;
  481. } else {
  482. level--;
  483. }
  484. continue;
  485. }
  486. }
  487. if (done) {
  488. break;
  489. }
  490. }
  491. return false;
  492. }
  493. bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale) const {
  494. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  495. enum {
  496. TEST_AABB_BIT = 0,
  497. VISIT_LEFT_BIT = 1,
  498. VISIT_RIGHT_BIT = 2,
  499. VISIT_DONE_BIT = 3,
  500. VISITED_BIT_SHIFT = 29,
  501. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  502. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  503. };
  504. int level = 0;
  505. const Triangle *triangleptr = triangles.ptr();
  506. const Vector3 *vertexptr = vertices.ptr();
  507. const BVH *bvhptr = bvh.ptr();
  508. Transform3D scale(Basis().scaled(p_scale));
  509. int pos = bvh.size() - 1;
  510. stack[0] = pos;
  511. while (true) {
  512. uint32_t node = stack[level] & NODE_IDX_MASK;
  513. const BVH &b = bvhptr[node];
  514. bool done = false;
  515. switch (stack[level] >> VISITED_BIT_SHIFT) {
  516. case TEST_AABB_BIT: {
  517. bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
  518. if (!intersects) {
  519. return false;
  520. }
  521. bool inside = scale.xform(b.aabb).inside_convex_shape(p_planes, p_plane_count);
  522. if (inside) {
  523. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  524. } else {
  525. if (b.face_index >= 0) {
  526. const Triangle &s = triangleptr[b.face_index];
  527. for (int j = 0; j < 3; ++j) {
  528. Vector3 point = scale.xform(vertexptr[s.indices[j]]);
  529. for (int i = 0; i < p_plane_count; i++) {
  530. const Plane &p = p_planes[i];
  531. if (p.is_point_over(point)) {
  532. return false;
  533. }
  534. }
  535. }
  536. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  537. } else {
  538. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  539. }
  540. }
  541. continue;
  542. }
  543. case VISIT_LEFT_BIT: {
  544. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  545. level++;
  546. stack[level] = b.left | TEST_AABB_BIT;
  547. continue;
  548. }
  549. case VISIT_RIGHT_BIT: {
  550. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  551. level++;
  552. stack[level] = b.right | TEST_AABB_BIT;
  553. continue;
  554. }
  555. case VISIT_DONE_BIT: {
  556. if (level == 0) {
  557. done = true;
  558. break;
  559. } else {
  560. level--;
  561. }
  562. continue;
  563. }
  564. }
  565. if (done) {
  566. break;
  567. }
  568. }
  569. return true;
  570. }
  571. bool TriangleMesh::is_valid() const {
  572. return valid;
  573. }
  574. Vector<Face3> TriangleMesh::get_faces() const {
  575. if (!valid) {
  576. return Vector<Face3>();
  577. }
  578. Vector<Face3> faces;
  579. int ts = triangles.size();
  580. faces.resize(triangles.size());
  581. Face3 *w = faces.ptrw();
  582. const Triangle *r = triangles.ptr();
  583. const Vector3 *rv = vertices.ptr();
  584. for (int i = 0; i < ts; i++) {
  585. for (int j = 0; j < 3; j++) {
  586. w[i].vertex[j] = rv[r[i].indices[j]];
  587. }
  588. }
  589. return faces;
  590. }
  591. TriangleMesh::TriangleMesh() {
  592. valid = false;
  593. max_depth = 0;
  594. }