face3.h
1 /*************************************************************************/
2 /* face3.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 FACE3_H
30 #define FACE3_H
31 
32 #include "vector3.h"
33 #include "plane.h"
34 #include "aabb.h"
35 #include "transform.h"
36 
37 class Face3 {
38 public:
39 
40  enum Side {
41  SIDE_OVER,
42  SIDE_UNDER,
43  SIDE_SPANNING,
44  SIDE_COPLANAR
45  };
46 
47 
48  Vector3 vertex[3];
49 
59  int split_by_plane(const Plane& p_plane,Face3 *p_res,bool *p_is_point_over) const;
60 
61  Plane get_plane(ClockDirection p_dir=CLOCKWISE) const;
62  Vector3 get_random_point_inside() const;
63 
64 
65  Side get_side_of(const Face3& p_face,ClockDirection p_clock_dir=CLOCKWISE) const;
66 
67  bool is_degenerate() const;
68  real_t get_area() const;
69 
70  Vector3 get_median_point() const;
71  Vector3 get_closest_point_to(const Vector3& p_point) const;
72 
73  bool intersects_ray(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
74  bool intersects_segment(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
75 
76  ClockDirection get_clock_dir() const;
77 
78  void get_support(const Vector3& p_normal,const Transform& p_transform,Vector3 *p_vertices,int* p_count,int p_max) const;
79  void project_range(const Vector3& p_normal,const Transform& p_transform,float& r_min, float& r_max) const;
80 
81  AABB get_aabb() const {
82 
83  AABB aabb( vertex[0], Vector3() );
84  aabb.expand_to( vertex[1] );
85  aabb.expand_to( vertex[2] );
86  return aabb;
87  }
88 
89  bool intersects_aabb(const AABB& p_aabb) const;
90  _FORCE_INLINE_ bool intersects_aabb2(const AABB& p_aabb) const;
91  operator String() const;
92 
93  inline Face3() {}
94  inline Face3(const Vector3 &p_v1,const Vector3 &p_v2,const Vector3 &p_v3) { vertex[0]=p_v1; vertex[1]=p_v2; vertex[2]=p_v3; }
95 
96 };
97 
98 
99 bool Face3::intersects_aabb2(const AABB& p_aabb) const {
100 
101  Vector3 perp = (vertex[0]-vertex[2]).cross(vertex[0]-vertex[1]);
102 
103  Vector3 half_extents = p_aabb.size * 0.5;
104  Vector3 ofs = p_aabb.pos + half_extents;
105 
106  Vector3 sup =Vector3(
107  (perp.x>0) ? -half_extents.x : half_extents.x,
108  (perp.y>0) ? -half_extents.y : half_extents.y,
109  (perp.z>0) ? -half_extents.z : half_extents.z
110  );
111 
112  float d = perp.dot(vertex[0]);
113  float dist_a = perp.dot(ofs+sup)-d;
114  float dist_b = perp.dot(ofs-sup)-d;
115 
116  if (dist_a*dist_b > 0)
117  return false; //does not intersect the plane
118 
119 
120 #define TEST_AXIS(m_ax)\
121  {\
122  float aabb_min=p_aabb.pos.m_ax;\
123  float aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
124  float tri_min,tri_max;\
125  for (int i=0;i<3;i++) {\
126  if (i==0 || vertex[i].m_ax > tri_max)\
127  tri_max=vertex[i].m_ax;\
128  if (i==0 || vertex[i].m_ax < tri_min)\
129  tri_min=vertex[i].m_ax;\
130  }\
131 \
132  if (tri_max<aabb_min || aabb_max<tri_min)\
133  return false;\
134  }
135 
136  TEST_AXIS(x);
137  TEST_AXIS(y);
138  TEST_AXIS(z);
139 
140 #undef TEST_AXIS
141 
142 
143  Vector3 edge_norms[3]={
144  vertex[0]-vertex[1],
145  vertex[1]-vertex[2],
146  vertex[2]-vertex[0],
147  };
148 
149  for (int i=0;i<12;i++) {
150 
151  Vector3 from,to;
152  switch(i) {
153 
154  case 0:{
155 
156  from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
157  to=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
158  } break;
159  case 1:{
160 
161  from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
162  to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
163  } break;
164  case 2:{
165  from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
166  to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
167 
168  } break;
169  case 3:{
170 
171  from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
172  to=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
173 
174  } break;
175  case 4:{
176 
177  from=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
178  to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
179  } break;
180  case 5:{
181 
182  from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
183  to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
184  } break;
185  case 6:{
186  from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
187  to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
188 
189  } break;
190  case 7:{
191 
192  from=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
193  to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
194 
195  } break;
196  case 8:{
197 
198  from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
199  to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
200 
201  } break;
202  case 9:{
203 
204  from=Vector3( p_aabb.pos.x , p_aabb.pos.y , p_aabb.pos.z );
205  to=Vector3( p_aabb.pos.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
206 
207  } break;
208  case 10:{
209 
210  from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z );
211  to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z );
212 
213  } break;
214  case 11:{
215 
216  from=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y , p_aabb.pos.z+p_aabb.size.z );
217  to=Vector3( p_aabb.pos.x+p_aabb.size.x , p_aabb.pos.y+p_aabb.size.y , p_aabb.pos.z+p_aabb.size.z );
218 
219  } break;
220 
221  }
222 
223  Vector3 e1=from-to;
224  for (int j=0;j<3;j++) {
225  Vector3 e2=edge_norms[j];
226 
227  Vector3 axis=vec3_cross( e1, e2 );
228 
229  if (axis.length_squared()<0.0001)
230  continue; // coplanar
231  //axis.normalize();
232 
233  Vector3 sup2 =Vector3(
234  (axis.x>0) ? -half_extents.x : half_extents.x,
235  (axis.y>0) ? -half_extents.y : half_extents.y,
236  (axis.z>0) ? -half_extents.z : half_extents.z
237  );
238 
239  float maxB = axis.dot(ofs+sup2);
240  float minB = axis.dot(ofs-sup2);
241  if (minB>maxB) {
242  SWAP(maxB,minB);
243  }
244 
245  float minT=1e20,maxT=-1e20;
246  for (int k=0;k<3;k++) {
247 
248  float d=axis.dot(vertex[k]);
249 
250  if (d > maxT)
251  maxT=d;
252 
253  if (d < minT)
254  minT=d;
255  }
256 
257  if (maxB<minT || maxT<minB)
258  return false;
259  }
260  }
261  return true;
262 
263 
264 }
265 
266 
267 #endif // FACE3_H
Definition: face3.h:37
void get_support(const Vector3 &p_normal, const Transform &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const
Definition: face3.cpp:292
Definition: aabb.h:43
ClockDirection get_clock_dir() const
todo, test if this is returning the proper clockwisity
Definition: face3.cpp:198
Definition: vector3.h:38
Definition: transform.h:38
bool intersects_aabb(const AABB &p_aabb) const
Definition: face3.cpp:208
Definition: plane.h:35
Definition: ustring.h:64
int split_by_plane(const Plane &p_plane, Face3 *p_res, bool *p_is_point_over) const
Definition: face3.cpp:32