triangle_mesh.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /**************************************************************************/
  2. /* triangle_mesh.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 "triangle_mesh.h"
  31. #include "core/sort_array.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.position + aabb.size * 0.5f;
  70. _new->face_index = -1;
  71. _new->left = left;
  72. _new->right = right;
  73. return index;
  74. }
  75. void TriangleMesh::get_indices(PoolVector<int> *r_triangles_indices) const {
  76. if (!valid) {
  77. return;
  78. }
  79. const int triangles_num = triangles.size();
  80. // Parse vertices indices
  81. PoolVector<Triangle>::Read triangles_read = triangles.read();
  82. r_triangles_indices->resize(triangles_num * 3);
  83. PoolVector<int>::Write r_indices_write = r_triangles_indices->write();
  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 PoolVector<Vector3> &p_faces) {
  91. valid = false;
  92. int fc = p_faces.size();
  93. ERR_FAIL_COND(!fc || ((fc % 3) != 0));
  94. fc /= 3;
  95. triangles.resize(fc);
  96. bvh.resize(fc * 3); //will never be larger than this (todo make better)
  97. PoolVector<BVH>::Write bw = bvh.write();
  98. {
  99. //create faces and indices and base bvh
  100. //except for the Set for repeated triangles, everything
  101. //goes in-place.
  102. PoolVector<Vector3>::Read r = p_faces.read();
  103. PoolVector<Triangle>::Write w = triangles.write();
  104. Map<Vector3, int> db;
  105. for (int i = 0; i < fc; i++) {
  106. Triangle &f = w[i];
  107. const Vector3 *v = &r[i * 3];
  108. for (int j = 0; j < 3; j++) {
  109. int vidx = -1;
  110. Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001));
  111. Map<Vector3, int>::Element *E = db.find(vs);
  112. if (E) {
  113. vidx = E->get();
  114. } else {
  115. vidx = db.size();
  116. db[vs] = vidx;
  117. }
  118. f.indices[j] = vidx;
  119. if (j == 0) {
  120. bw[i].aabb.position = vs;
  121. } else {
  122. bw[i].aabb.expand_to(vs);
  123. }
  124. }
  125. f.normal = Face3(r[i * 3 + 0], r[i * 3 + 1], r[i * 3 + 2]).get_plane().get_normal();
  126. bw[i].left = -1;
  127. bw[i].right = -1;
  128. bw[i].face_index = i;
  129. bw[i].center = bw[i].aabb.position + bw[i].aabb.size * 0.5f;
  130. }
  131. vertices.resize(db.size());
  132. PoolVector<Vector3>::Write vw = vertices.write();
  133. for (Map<Vector3, int>::Element *E = db.front(); E; E = E->next()) {
  134. vw[E->get()] = E->key();
  135. }
  136. }
  137. PoolVector<BVH *> bwptrs;
  138. bwptrs.resize(fc);
  139. PoolVector<BVH *>::Write bwp = bwptrs.write();
  140. for (int i = 0; i < fc; i++) {
  141. bwp[i] = &bw[i];
  142. }
  143. max_depth = 0;
  144. int max_alloc = fc;
  145. _create_bvh(bw.ptr(), bwp.ptr(), 0, fc, 1, max_depth, max_alloc);
  146. bw.release(); //clearup
  147. bvh.resize(max_alloc); //resize back
  148. valid = true;
  149. }
  150. Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const {
  151. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  152. enum {
  153. TEST_AABB_BIT = 0,
  154. VISIT_LEFT_BIT = 1,
  155. VISIT_RIGHT_BIT = 2,
  156. VISIT_DONE_BIT = 3,
  157. VISITED_BIT_SHIFT = 29,
  158. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  159. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  160. };
  161. int n_count = 0;
  162. Vector3 n;
  163. int level = 0;
  164. PoolVector<Triangle>::Read trianglesr = triangles.read();
  165. PoolVector<Vector3>::Read verticesr = vertices.read();
  166. PoolVector<BVH>::Read bvhr = bvh.read();
  167. const Triangle *triangleptr = trianglesr.ptr();
  168. int pos = bvh.size() - 1;
  169. const BVH *bvhptr = bvhr.ptr();
  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. stack[level + 1] = b.left | TEST_AABB_BIT;
  195. level++;
  196. continue;
  197. }
  198. case VISIT_RIGHT_BIT: {
  199. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  200. stack[level + 1] = b.right | TEST_AABB_BIT;
  201. level++;
  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) 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. PoolVector<Triangle>::Read trianglesr = triangles.read();
  239. PoolVector<Vector3>::Read verticesr = vertices.read();
  240. PoolVector<BVH>::Read bvhr = bvh.read();
  241. const Triangle *triangleptr = trianglesr.ptr();
  242. const Vector3 *vertexptr = verticesr.ptr();
  243. int pos = bvh.size() - 1;
  244. const BVH *bvhptr = bvhr.ptr();
  245. stack[0] = pos;
  246. while (true) {
  247. uint32_t node = stack[level] & NODE_IDX_MASK;
  248. const BVH &b = bvhptr[node];
  249. bool done = false;
  250. switch (stack[level] >> VISITED_BIT_SHIFT) {
  251. case TEST_AABB_BIT: {
  252. bool valid = b.aabb.intersects_segment(p_begin, p_end);
  253. //bool valid = b.aabb.intersects(ray_aabb);
  254. if (!valid) {
  255. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  256. } else {
  257. if (b.face_index >= 0) {
  258. const Triangle &s = triangleptr[b.face_index];
  259. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  260. Vector3 res;
  261. if (f3.intersects_segment(p_begin, p_end, &res)) {
  262. real_t nd = n.dot(res);
  263. if (nd < d) {
  264. d = nd;
  265. r_point = res;
  266. r_normal = f3.get_plane().get_normal();
  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. stack[level + 1] = b.left | TEST_AABB_BIT;
  280. level++;
  281. continue;
  282. }
  283. case VISIT_RIGHT_BIT: {
  284. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  285. stack[level + 1] = b.right | TEST_AABB_BIT;
  286. level++;
  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) 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. PoolVector<Triangle>::Read trianglesr = triangles.read();
  326. PoolVector<Vector3>::Read verticesr = vertices.read();
  327. PoolVector<BVH>::Read bvhr = bvh.read();
  328. const Triangle *triangleptr = trianglesr.ptr();
  329. const Vector3 *vertexptr = verticesr.ptr();
  330. int pos = bvh.size() - 1;
  331. const BVH *bvhptr = bvhr.ptr();
  332. stack[0] = pos;
  333. while (true) {
  334. uint32_t node = stack[level] & NODE_IDX_MASK;
  335. const BVH &b = bvhptr[node];
  336. bool done = false;
  337. switch (stack[level] >> VISITED_BIT_SHIFT) {
  338. case TEST_AABB_BIT: {
  339. bool valid = b.aabb.intersects_ray(p_begin, p_dir);
  340. if (!valid) {
  341. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  342. } else {
  343. if (b.face_index >= 0) {
  344. const Triangle &s = triangleptr[b.face_index];
  345. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  346. Vector3 res;
  347. if (f3.intersects_ray(p_begin, p_dir, &res)) {
  348. real_t nd = n.dot(res);
  349. if (nd < d) {
  350. d = nd;
  351. r_point = res;
  352. r_normal = f3.get_plane().get_normal();
  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. stack[level + 1] = b.left | TEST_AABB_BIT;
  366. level++;
  367. continue;
  368. }
  369. case VISIT_RIGHT_BIT: {
  370. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  371. stack[level + 1] = b.right | TEST_AABB_BIT;
  372. level++;
  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. PoolVector<Triangle>::Read trianglesr = triangles.read();
  410. PoolVector<Vector3>::Read verticesr = vertices.read();
  411. PoolVector<BVH>::Read bvhr = bvh.read();
  412. const Triangle *triangleptr = trianglesr.ptr();
  413. const Vector3 *vertexptr = verticesr.ptr();
  414. int pos = bvh.size() - 1;
  415. const BVH *bvhptr = bvhr.ptr();
  416. stack[0] = pos;
  417. while (true) {
  418. uint32_t node = stack[level] & NODE_IDX_MASK;
  419. const BVH &b = bvhptr[node];
  420. bool done = false;
  421. switch (stack[level] >> VISITED_BIT_SHIFT) {
  422. case TEST_AABB_BIT: {
  423. bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
  424. if (!valid) {
  425. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  426. } else {
  427. if (b.face_index >= 0) {
  428. const Triangle &s = triangleptr[b.face_index];
  429. for (int j = 0; j < 3; ++j) {
  430. const Vector3 &point = vertexptr[s.indices[j]];
  431. const Vector3 &next_point = vertexptr[s.indices[(j + 1) % 3]];
  432. Vector3 res;
  433. bool over = true;
  434. for (int i = 0; i < p_plane_count; i++) {
  435. const Plane &p = p_planes[i];
  436. if (p.intersects_segment(point, next_point, &res)) {
  437. bool inisde = true;
  438. for (int k = 0; k < p_plane_count; k++) {
  439. if (k == i) {
  440. continue;
  441. }
  442. const Plane &pp = p_planes[k];
  443. if (pp.is_point_over(res)) {
  444. inisde = false;
  445. break;
  446. }
  447. }
  448. if (inisde) {
  449. return true;
  450. }
  451. }
  452. if (p.is_point_over(point)) {
  453. over = false;
  454. break;
  455. }
  456. }
  457. if (over) {
  458. return true;
  459. }
  460. }
  461. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  462. } else {
  463. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  464. }
  465. }
  466. continue;
  467. }
  468. case VISIT_LEFT_BIT: {
  469. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  470. stack[level + 1] = b.left | TEST_AABB_BIT;
  471. level++;
  472. continue;
  473. }
  474. case VISIT_RIGHT_BIT: {
  475. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  476. stack[level + 1] = b.right | TEST_AABB_BIT;
  477. level++;
  478. continue;
  479. }
  480. case VISIT_DONE_BIT: {
  481. if (level == 0) {
  482. done = true;
  483. break;
  484. } else {
  485. level--;
  486. }
  487. continue;
  488. }
  489. }
  490. if (done) {
  491. break;
  492. }
  493. }
  494. return false;
  495. }
  496. 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 {
  497. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  498. enum {
  499. TEST_AABB_BIT = 0,
  500. VISIT_LEFT_BIT = 1,
  501. VISIT_RIGHT_BIT = 2,
  502. VISIT_DONE_BIT = 3,
  503. VISITED_BIT_SHIFT = 29,
  504. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  505. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  506. };
  507. int level = 0;
  508. PoolVector<Triangle>::Read trianglesr = triangles.read();
  509. PoolVector<Vector3>::Read verticesr = vertices.read();
  510. PoolVector<BVH>::Read bvhr = bvh.read();
  511. Transform scale(Basis().scaled(p_scale));
  512. const Triangle *triangleptr = trianglesr.ptr();
  513. const Vector3 *vertexptr = verticesr.ptr();
  514. int pos = bvh.size() - 1;
  515. const BVH *bvhptr = bvhr.ptr();
  516. stack[0] = pos;
  517. while (true) {
  518. uint32_t node = stack[level] & NODE_IDX_MASK;
  519. const BVH &b = bvhptr[node];
  520. bool done = false;
  521. switch (stack[level] >> VISITED_BIT_SHIFT) {
  522. case TEST_AABB_BIT: {
  523. bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
  524. if (!intersects) {
  525. return false;
  526. }
  527. bool inside = scale.xform(b.aabb).inside_convex_shape(p_planes, p_plane_count);
  528. if (inside) {
  529. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  530. } else {
  531. if (b.face_index >= 0) {
  532. const Triangle &s = triangleptr[b.face_index];
  533. for (int j = 0; j < 3; ++j) {
  534. Vector3 point = scale.xform(vertexptr[s.indices[j]]);
  535. for (int i = 0; i < p_plane_count; i++) {
  536. const Plane &p = p_planes[i];
  537. if (p.is_point_over(point)) {
  538. return false;
  539. }
  540. }
  541. }
  542. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  543. } else {
  544. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  545. }
  546. }
  547. continue;
  548. }
  549. case VISIT_LEFT_BIT: {
  550. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  551. stack[level + 1] = b.left | TEST_AABB_BIT;
  552. level++;
  553. continue;
  554. }
  555. case VISIT_RIGHT_BIT: {
  556. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  557. stack[level + 1] = b.right | TEST_AABB_BIT;
  558. level++;
  559. continue;
  560. }
  561. case VISIT_DONE_BIT: {
  562. if (level == 0) {
  563. done = true;
  564. break;
  565. } else {
  566. level--;
  567. }
  568. continue;
  569. }
  570. }
  571. if (done) {
  572. break;
  573. }
  574. }
  575. return true;
  576. }
  577. bool TriangleMesh::is_valid() const {
  578. return valid;
  579. }
  580. PoolVector<Face3> TriangleMesh::get_faces() const {
  581. if (!valid) {
  582. return PoolVector<Face3>();
  583. }
  584. PoolVector<Face3> faces;
  585. int ts = triangles.size();
  586. faces.resize(triangles.size());
  587. PoolVector<Face3>::Write w = faces.write();
  588. PoolVector<Triangle>::Read r = triangles.read();
  589. PoolVector<Vector3>::Read rv = vertices.read();
  590. for (int i = 0; i < ts; i++) {
  591. for (int j = 0; j < 3; j++) {
  592. w[i].vertex[j] = rv[r[i].indices[j]];
  593. }
  594. }
  595. w.release();
  596. return faces;
  597. }
  598. TriangleMesh::TriangleMesh() {
  599. valid = false;
  600. max_depth = 0;
  601. }