triangle_mesh.h
1 /*************************************************************************/
2 /* triangle_mesh.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef TRIANGLE_MESH_H
30 #define TRIANGLE_MESH_H
31 
32 #include "reference.h"
33 #include "face3.h"
34 class TriangleMesh : public Reference {
35 
36  OBJ_TYPE( TriangleMesh, Reference);
37 
38  struct Triangle {
39 
40  Vector3 normal;
41  int indices[3];
42  };
43 
44  DVector<Triangle> triangles;
45  DVector<Vector3> vertices;
46 
47  struct BVH {
48 
49  AABB aabb;
50  Vector3 center; //used for sorting
51  int left;
52  int right;
53 
54  int face_index;
55  };
56 
57  struct BVHCmpX {
58 
59  bool operator()(const BVH* p_left, const BVH* p_right) const {
60 
61  return p_left->center.x < p_right->center.x;
62  }
63  };
64 
65  struct BVHCmpY {
66 
67  bool operator()(const BVH* p_left, const BVH* p_right) const {
68 
69  return p_left->center.y < p_right->center.y;
70  }
71  };
72  struct BVHCmpZ {
73 
74  bool operator()(const BVH* p_left, const BVH* p_right) const {
75 
76  return p_left->center.z < p_right->center.z;
77  }
78  };
79 
80  int _create_bvh(BVH*p_bvh,BVH** p_bb,int p_from,int p_size,int p_depth,int&max_depth,int&max_alloc);
81 
82  DVector<BVH> bvh;
83  int max_depth;
84  bool valid;
85 
86 public:
87 
88  bool is_valid() const;
89  bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_point, Vector3 &r_normal) const;
90  bool intersect_ray(const Vector3& p_begin,const Vector3& p_dir,Vector3 &r_point, Vector3 &r_normal) const;
91  Vector3 get_area_normal(const AABB& p_aabb) const;
92  DVector<Face3> get_faces() const;
93 
94 
95  void create(const DVector<Vector3>& p_faces);
96  TriangleMesh();
97 };
98 
99 #endif // TRIANGLE_MESH_H
Definition: aabb.h:43
Definition: reference.h:40
Definition: vector3.h:38
Definition: triangle_mesh.h:34