triangle_mesh.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*************************************************************************/
  2. /* triangle_mesh.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef TRIANGLE_MESH_H
  31. #define TRIANGLE_MESH_H
  32. #include "face3.h"
  33. #include "reference.h"
  34. class TriangleMesh : public Reference {
  35. OBJ_TYPE(TriangleMesh, Reference);
  36. struct Triangle {
  37. Vector3 normal;
  38. int indices[3];
  39. };
  40. DVector<Triangle> triangles;
  41. DVector<Vector3> vertices;
  42. struct BVH {
  43. AABB aabb;
  44. Vector3 center; //used for sorting
  45. int left;
  46. int right;
  47. int face_index;
  48. };
  49. struct BVHCmpX {
  50. bool operator()(const BVH *p_left, const BVH *p_right) const {
  51. return p_left->center.x < p_right->center.x;
  52. }
  53. };
  54. struct BVHCmpY {
  55. bool operator()(const BVH *p_left, const BVH *p_right) const {
  56. return p_left->center.y < p_right->center.y;
  57. }
  58. };
  59. struct BVHCmpZ {
  60. bool operator()(const BVH *p_left, const BVH *p_right) const {
  61. return p_left->center.z < p_right->center.z;
  62. }
  63. };
  64. int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc);
  65. DVector<BVH> bvh;
  66. int max_depth;
  67. bool valid;
  68. public:
  69. bool is_valid() const;
  70. bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const;
  71. bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const;
  72. Vector3 get_area_normal(const AABB &p_aabb) const;
  73. DVector<Face3> get_faces() const;
  74. void create(const DVector<Vector3> &p_faces);
  75. TriangleMesh();
  76. };
  77. #endif // TRIANGLE_MESH_H