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