triangle_mesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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/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].snappedf(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. bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index) 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. Vector3 n = (p_end - p_begin).normalized();
  164. real_t d = 1e10;
  165. bool inters = false;
  166. int level = 0;
  167. const Triangle *triangleptr = triangles.ptr();
  168. const Vector3 *vertexptr = vertices.ptr();
  169. const BVH *bvhptr = bvh.ptr();
  170. int pos = bvh.size() - 1;
  171. stack[0] = pos;
  172. while (true) {
  173. uint32_t node = stack[level] & NODE_IDX_MASK;
  174. const BVH &b = bvhptr[node];
  175. bool done = false;
  176. switch (stack[level] >> VISITED_BIT_SHIFT) {
  177. case TEST_AABB_BIT: {
  178. if (!b.aabb.intersects_segment(p_begin, p_end)) {
  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. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  184. Vector3 res;
  185. if (f3.intersects_segment(p_begin, p_end, &res)) {
  186. real_t nd = n.dot(res);
  187. if (nd < d) {
  188. d = nd;
  189. r_point = res;
  190. r_normal = f3.get_plane().get_normal();
  191. if (r_surf_index) {
  192. *r_surf_index = s.surface_index;
  193. }
  194. inters = true;
  195. }
  196. }
  197. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  198. } else {
  199. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  200. }
  201. }
  202. continue;
  203. }
  204. case VISIT_LEFT_BIT: {
  205. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  206. level++;
  207. stack[level] = b.left | TEST_AABB_BIT;
  208. continue;
  209. }
  210. case VISIT_RIGHT_BIT: {
  211. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  212. level++;
  213. stack[level] = b.right | TEST_AABB_BIT;
  214. continue;
  215. }
  216. case VISIT_DONE_BIT: {
  217. if (level == 0) {
  218. done = true;
  219. break;
  220. } else {
  221. level--;
  222. }
  223. continue;
  224. }
  225. }
  226. if (done) {
  227. break;
  228. }
  229. }
  230. if (inters) {
  231. if (n.dot(r_normal) > 0) {
  232. r_normal = -r_normal;
  233. }
  234. }
  235. return inters;
  236. }
  237. bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index) const {
  238. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  239. enum {
  240. TEST_AABB_BIT = 0,
  241. VISIT_LEFT_BIT = 1,
  242. VISIT_RIGHT_BIT = 2,
  243. VISIT_DONE_BIT = 3,
  244. VISITED_BIT_SHIFT = 29,
  245. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  246. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  247. };
  248. Vector3 n = p_dir;
  249. real_t d = 1e20;
  250. bool inters = false;
  251. int level = 0;
  252. const Triangle *triangleptr = triangles.ptr();
  253. const Vector3 *vertexptr = vertices.ptr();
  254. const BVH *bvhptr = bvh.ptr();
  255. int pos = bvh.size() - 1;
  256. stack[0] = pos;
  257. while (true) {
  258. uint32_t node = stack[level] & NODE_IDX_MASK;
  259. const BVH &b = bvhptr[node];
  260. bool done = false;
  261. switch (stack[level] >> VISITED_BIT_SHIFT) {
  262. case TEST_AABB_BIT: {
  263. if (!b.aabb.intersects_ray(p_begin, p_dir)) {
  264. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  265. } else {
  266. if (b.face_index >= 0) {
  267. const Triangle &s = triangleptr[b.face_index];
  268. Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
  269. Vector3 res;
  270. if (f3.intersects_ray(p_begin, p_dir, &res)) {
  271. real_t nd = n.dot(res);
  272. if (nd < d) {
  273. d = nd;
  274. r_point = res;
  275. r_normal = f3.get_plane().get_normal();
  276. if (r_surf_index) {
  277. *r_surf_index = s.surface_index;
  278. }
  279. inters = true;
  280. }
  281. }
  282. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  283. } else {
  284. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  285. }
  286. }
  287. continue;
  288. }
  289. case VISIT_LEFT_BIT: {
  290. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  291. level++;
  292. stack[level] = b.left | TEST_AABB_BIT;
  293. continue;
  294. }
  295. case VISIT_RIGHT_BIT: {
  296. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  297. level++;
  298. stack[level] = b.right | TEST_AABB_BIT;
  299. continue;
  300. }
  301. case VISIT_DONE_BIT: {
  302. if (level == 0) {
  303. done = true;
  304. break;
  305. } else {
  306. level--;
  307. }
  308. continue;
  309. }
  310. }
  311. if (done) {
  312. break;
  313. }
  314. }
  315. if (inters) {
  316. if (n.dot(r_normal) > 0) {
  317. r_normal = -r_normal;
  318. }
  319. }
  320. return inters;
  321. }
  322. 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 {
  323. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
  324. enum {
  325. TEST_AABB_BIT = 0,
  326. VISIT_LEFT_BIT = 1,
  327. VISIT_RIGHT_BIT = 2,
  328. VISIT_DONE_BIT = 3,
  329. VISITED_BIT_SHIFT = 29,
  330. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  331. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  332. };
  333. int level = 0;
  334. const Triangle *triangleptr = triangles.ptr();
  335. const Vector3 *vertexptr = vertices.ptr();
  336. const BVH *bvhptr = bvh.ptr();
  337. Transform3D scale(Basis().scaled(p_scale));
  338. int pos = bvh.size() - 1;
  339. stack[0] = pos;
  340. while (true) {
  341. uint32_t node = stack[level] & NODE_IDX_MASK;
  342. const BVH &b = bvhptr[node];
  343. bool done = false;
  344. switch (stack[level] >> VISITED_BIT_SHIFT) {
  345. case TEST_AABB_BIT: {
  346. bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
  347. if (!intersects) {
  348. return false;
  349. }
  350. bool inside = scale.xform(b.aabb).inside_convex_shape(p_planes, p_plane_count);
  351. if (inside) {
  352. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  353. } else {
  354. if (b.face_index >= 0) {
  355. const Triangle &s = triangleptr[b.face_index];
  356. for (int j = 0; j < 3; ++j) {
  357. Vector3 point = scale.xform(vertexptr[s.indices[j]]);
  358. for (int i = 0; i < p_plane_count; i++) {
  359. const Plane &p = p_planes[i];
  360. if (p.is_point_over(point)) {
  361. return false;
  362. }
  363. }
  364. }
  365. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  366. } else {
  367. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  368. }
  369. }
  370. continue;
  371. }
  372. case VISIT_LEFT_BIT: {
  373. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  374. level++;
  375. stack[level] = b.left | TEST_AABB_BIT;
  376. continue;
  377. }
  378. case VISIT_RIGHT_BIT: {
  379. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  380. level++;
  381. stack[level] = b.right | TEST_AABB_BIT;
  382. continue;
  383. }
  384. case VISIT_DONE_BIT: {
  385. if (level == 0) {
  386. done = true;
  387. break;
  388. } else {
  389. level--;
  390. }
  391. continue;
  392. }
  393. }
  394. if (done) {
  395. break;
  396. }
  397. }
  398. return true;
  399. }
  400. bool TriangleMesh::is_valid() const {
  401. return valid;
  402. }
  403. Vector<Face3> TriangleMesh::get_faces() const {
  404. if (!valid) {
  405. return Vector<Face3>();
  406. }
  407. Vector<Face3> faces;
  408. int ts = triangles.size();
  409. faces.resize(triangles.size());
  410. Face3 *w = faces.ptrw();
  411. const Triangle *r = triangles.ptr();
  412. const Vector3 *rv = vertices.ptr();
  413. for (int i = 0; i < ts; i++) {
  414. for (int j = 0; j < 3; j++) {
  415. w[i].vertex[j] = rv[r[i].indices[j]];
  416. }
  417. }
  418. return faces;
  419. }
  420. TriangleMesh::TriangleMesh() {
  421. valid = false;
  422. max_depth = 0;
  423. }