| 1 | /**************************************************************************/ |
| 2 | /* triangle_mesh.h */ |
| 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 | #ifndef TRIANGLE_MESH_H |
| 32 | #define TRIANGLE_MESH_H |
| 33 | |
| 34 | #include "core/math/face3.h" |
| 35 | #include "core/object/ref_counted.h" |
| 36 | |
| 37 | class TriangleMesh : public RefCounted { |
| 38 | GDCLASS(TriangleMesh, RefCounted); |
| 39 | |
| 40 | public: |
| 41 | struct Triangle { |
| 42 | Vector3 normal; |
| 43 | int indices[3]; |
| 44 | int32_t surface_index; |
| 45 | }; |
| 46 | |
| 47 | private: |
| 48 | Vector<Triangle> triangles; |
| 49 | Vector<Vector3> vertices; |
| 50 | |
| 51 | struct BVH { |
| 52 | AABB aabb; |
| 53 | Vector3 center; //used for sorting |
| 54 | int left; |
| 55 | int right; |
| 56 | |
| 57 | int face_index; |
| 58 | }; |
| 59 | |
| 60 | struct BVHCmpX { |
| 61 | bool operator()(const BVH *p_left, const BVH *p_right) const { |
| 62 | return p_left->center.x < p_right->center.x; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | struct BVHCmpY { |
| 67 | bool operator()(const BVH *p_left, const BVH *p_right) const { |
| 68 | return p_left->center.y < p_right->center.y; |
| 69 | } |
| 70 | }; |
| 71 | struct BVHCmpZ { |
| 72 | bool operator()(const BVH *p_left, const BVH *p_right) const { |
| 73 | return p_left->center.z < p_right->center.z; |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc); |
| 78 | |
| 79 | Vector<BVH> bvh; |
| 80 | int max_depth; |
| 81 | bool valid; |
| 82 | |
| 83 | public: |
| 84 | bool is_valid() const; |
| 85 | bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr) const; |
| 86 | bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal, int32_t *r_surf_index = nullptr) const; |
| 87 | bool inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale = Vector3(1, 1, 1)) const; |
| 88 | Vector<Face3> get_faces() const; |
| 89 | |
| 90 | const Vector<Triangle> &get_triangles() const { return triangles; } |
| 91 | const Vector<Vector3> &get_vertices() const { return vertices; } |
| 92 | void get_indices(Vector<int> *r_triangles_indices) const; |
| 93 | |
| 94 | void create(const Vector<Vector3> &p_faces, const Vector<int32_t> &p_surface_indices = Vector<int32_t>()); |
| 95 | TriangleMesh(); |
| 96 | }; |
| 97 | |
| 98 | #endif // TRIANGLE_MESH_H |
| 99 | |