aabb.h
1 /*************************************************************************/
2 /* aabb.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 AABB_H
30 #define AABB_H
31 
32 
33 
34 #include "vector3.h"
35 #include "plane.h"
43 class AABB {
44 public:
45  Vector3 pos;
46  Vector3 size;
47 
48  float get_area() const;
49  _FORCE_INLINE_ bool has_no_area() const {
50 
51  return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON);
52  }
53 
54  _FORCE_INLINE_ bool has_no_surface() const {
55 
56  return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON);
57  }
58 
59  const Vector3& get_pos() const { return pos; }
60  void set_pos(const Vector3& p_pos) { pos=p_pos; }
61  const Vector3& get_size() const { return size; }
62  void set_size(const Vector3& p_size) { size=p_size; }
63 
64 
65  bool operator==(const AABB& p_rval) const;
66  bool operator!=(const AABB& p_rval) const;
67 
68  _FORCE_INLINE_ bool intersects(const AABB& p_aabb) const;
69  _FORCE_INLINE_ bool intersects_inclusive(const AABB& p_aabb) const;
70  _FORCE_INLINE_ bool encloses(const AABB & p_aabb) const;
71 
72  AABB merge(const AABB& p_with) const;
73  void merge_with(const AABB& p_aabb);
74  AABB intersection(const AABB& p_aabb) const;
75  bool intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
76  bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
77  _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &from,const Vector3& p_dir, float t0, float t1) const;
78 
79  _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
80  bool intersects_plane(const Plane &p_plane) const;
81 
82  _FORCE_INLINE_ bool has_point(const Vector3& p_point) const;
83  _FORCE_INLINE_ Vector3 get_support(const Vector3& p_normal) const;
84 
85 
86  Vector3 get_longest_axis() const;
87  int get_longest_axis_index() const;
88  _FORCE_INLINE_ real_t get_longest_axis_size() const;
89 
90  Vector3 get_shortest_axis() const;
91  int get_shortest_axis_index() const;
92  _FORCE_INLINE_ real_t get_shortest_axis_size() const;
93 
94  AABB grow(real_t p_by) const;
95  _FORCE_INLINE_ void grow_by(real_t p_amount);
96 
97  void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const;
98  _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
99 
100  AABB expand(const Vector3& p_vector) const;
101  _FORCE_INLINE_ void project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const;
102  _FORCE_INLINE_ void expand_to(const Vector3& p_vector);
104  operator String() const;
105 
106  _FORCE_INLINE_ AABB() {}
107  inline AABB(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; }
108 
109 
110 };
111 
112 inline bool AABB::intersects(const AABB& p_aabb) const {
113 
114  if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) )
115  return false;
116  if ( (pos.x+size.x) <= p_aabb.pos.x )
117  return false;
118  if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) )
119  return false;
120  if ( (pos.y+size.y) <= p_aabb.pos.y )
121  return false;
122  if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) )
123  return false;
124  if ( (pos.z+size.z) <= p_aabb.pos.z )
125  return false;
126 
127  return true;
128 }
129 
130 inline bool AABB::intersects_inclusive(const AABB& p_aabb) const {
131 
132  if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
133  return false;
134  if ( (pos.x+size.x) < p_aabb.pos.x )
135  return false;
136  if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
137  return false;
138  if ( (pos.y+size.y) < p_aabb.pos.y )
139  return false;
140  if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
141  return false;
142  if ( (pos.z+size.z) < p_aabb.pos.z )
143  return false;
144 
145  return true;
146 }
147 
148 inline bool AABB::encloses(const AABB & p_aabb) const {
149 
150  Vector3 src_min=pos;
151  Vector3 src_max=pos+size;
152  Vector3 dst_min=p_aabb.pos;
153  Vector3 dst_max=p_aabb.pos+p_aabb.size;
154 
155  return (
156  (src_min.x <= dst_min.x) &&
157  (src_max.x > dst_max.x) &&
158  (src_min.y <= dst_min.y) &&
159  (src_max.y > dst_max.y) &&
160  (src_min.z <= dst_min.z) &&
161  (src_max.z > dst_max.z) );
162 
163 }
164 
165 Vector3 AABB::get_support(const Vector3& p_normal) const {
166 
167  Vector3 half_extents = size * 0.5;
168  Vector3 ofs = pos + half_extents;
169 
170  return Vector3(
171  (p_normal.x>0) ? -half_extents.x : half_extents.x,
172  (p_normal.y>0) ? -half_extents.y : half_extents.y,
173  (p_normal.z>0) ? -half_extents.z : half_extents.z
174  )+ofs;
175 }
176 
177 
178 Vector3 AABB::get_endpoint(int p_point) const {
179 
180  switch(p_point) {
181  case 0: return Vector3( pos.x , pos.y , pos.z );
182  case 1: return Vector3( pos.x , pos.y , pos.z+size.z );
183  case 2: return Vector3( pos.x , pos.y+size.y , pos.z );
184  case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z );
185  case 4: return Vector3( pos.x+size.x , pos.y , pos.z );
186  case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z );
187  case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z );
188  case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
189  };
190 
191  ERR_FAIL_V(Vector3());
192 }
193 
194 bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
195 
196 #if 1
197 
198  Vector3 half_extents = size * 0.5;
199  Vector3 ofs = pos + half_extents;
200 
201  for(int i=0;i<p_plane_count;i++) {
202  const Plane &p=p_planes[i];
203  Vector3 point(
204  (p.normal.x>0) ? -half_extents.x : half_extents.x,
205  (p.normal.y>0) ? -half_extents.y : half_extents.y,
206  (p.normal.z>0) ? -half_extents.z : half_extents.z
207  );
208  point+=ofs;
209  if (p.is_point_over(point))
210  return false;
211  }
212 
213  return true;
214 #else
215  //cache all points to check against!
216 // #warning should be easy to optimize, just use the same as when taking the support and use only that point
217  Vector3 points[8] = {
218  Vector3( pos.x , pos.y , pos.z ),
219  Vector3( pos.x , pos.y , pos.z+size.z ),
220  Vector3( pos.x , pos.y+size.y , pos.z ),
221  Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
222  Vector3( pos.x+size.x , pos.y , pos.z ),
223  Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
224  Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
225  Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
226  };
227 
228  for (int i=0;i<p_plane_count;i++) { //for each plane
229 
230  const Plane & plane=p_planes[i];
231  bool all_points_over=true;
232  //test if it has all points over!
233 
234  for (int j=0;j<8;j++) {
235 
236 
237  if (!plane.is_point_over( points[j] )) {
238 
239  all_points_over=false;
240  break;
241  }
242 
243  }
244 
245  if (all_points_over) {
246 
247  return false;
248  }
249  }
250  return true;
251 #endif
252 }
253 
254 bool AABB::has_point(const Vector3& p_point) const {
255 
256  if (p_point.x<pos.x)
257  return false;
258  if (p_point.y<pos.y)
259  return false;
260  if (p_point.z<pos.z)
261  return false;
262  if (p_point.x>pos.x+size.x)
263  return false;
264  if (p_point.y>pos.y+size.y)
265  return false;
266  if (p_point.z>pos.z+size.z)
267  return false;
268 
269  return true;
270 }
271 
272 
273 inline void AABB::expand_to(const Vector3& p_vector) {
274 
275  Vector3 begin=pos;
276  Vector3 end=pos+size;
277 
278  if (p_vector.x<begin.x)
279  begin.x=p_vector.x;
280  if (p_vector.y<begin.y)
281  begin.y=p_vector.y;
282  if (p_vector.z<begin.z)
283  begin.z=p_vector.z;
284 
285  if (p_vector.x>end.x)
286  end.x=p_vector.x;
287  if (p_vector.y>end.y)
288  end.y=p_vector.y;
289  if (p_vector.z>end.z)
290  end.z=p_vector.z;
291 
292  pos=begin;
293  size=end-begin;
294 }
295 
296 void AABB::project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const {
297 
298  Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
299  Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z );
300 
301  float length = p_plane.normal.abs().dot(half_extents);
302  float distance = p_plane.distance_to( center );
303  r_min = distance - length;
304  r_max = distance + length;
305 }
306 
307 inline real_t AABB::get_longest_axis_size() const {
308 
309  real_t max_size=size.x;
310 
311  if (size.y > max_size ) {
312  max_size=size.y;
313  }
314 
315  if (size.z > max_size ) {
316  max_size=size.z;
317  }
318 
319  return max_size;
320 }
321 
322 inline real_t AABB::get_shortest_axis_size() const {
323 
324  real_t max_size=size.x;
325 
326  if (size.y < max_size ) {
327  max_size=size.y;
328  }
329 
330  if (size.z < max_size ) {
331  max_size=size.z;
332  }
333 
334  return max_size;
335 }
336 
337 bool AABB::smits_intersect_ray(const Vector3 &from,const Vector3& dir, float t0, float t1) const {
338 
339  float divx=1.0/dir.x;
340  float divy=1.0/dir.y;
341  float divz=1.0/dir.z;
342 
343  Vector3 upbound=pos+size;
344  float tmin, tmax, tymin, tymax, tzmin, tzmax;
345  if (dir.x >= 0) {
346  tmin = (pos.x - from.x) * divx;
347  tmax = (upbound.x - from.x) * divx;
348  }
349  else {
350  tmin = (upbound.x - from.x) * divx;
351  tmax = (pos.x - from.x) * divx;
352  }
353  if (dir.y >= 0) {
354  tymin = (pos.y - from.y) * divy;
355  tymax = (upbound.y - from.y) * divy;
356  }
357  else {
358  tymin = (upbound.y - from.y) * divy;
359  tymax = (pos.y - from.y) * divy;
360  }
361  if ( (tmin > tymax) || (tymin > tmax) )
362  return false;
363  if (tymin > tmin)
364  tmin = tymin;
365  if (tymax < tmax)
366  tmax = tymax;
367  if (dir.z >= 0) {
368  tzmin = (pos.z - from.z) * divz;
369  tzmax = (upbound.z - from.z) * divz;
370  }
371  else {
372  tzmin = (upbound.z - from.z) * divz;
373  tzmax = (pos.z - from.z) * divz;
374  }
375  if ( (tmin > tzmax) || (tzmin > tmax) )
376  return false;
377  if (tzmin > tmin)
378  tmin = tzmin;
379  if (tzmax < tmax)
380  tmax = tzmax;
381  return ( (tmin < t1) && (tmax > t0) );
382 }
383 
384 void AABB::grow_by(real_t p_amount) {
385 
386  pos.x-=p_amount;
387  pos.y-=p_amount;
388  pos.z-=p_amount;
389  size.x+=2.0*p_amount;
390  size.y+=2.0*p_amount;
391  size.z+=2.0*p_amount;
392 }
393 
394 typedef AABB Rect3;
395 
396 #endif // AABB_H
Definition: aabb.h:43
_FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const
Both AABBs overlap.
Definition: aabb.h:130
AABB merge(const AABB &p_with) const
p_aabb is completely inside this
Definition: aabb.cpp:320
Definition: vector3.h:38
_FORCE_INLINE_ bool encloses(const AABB &p_aabb) const
Both AABBs (or their faces) overlap.
Definition: aabb.h:148
_FORCE_INLINE_ bool has_no_area() const
get area
Definition: aabb.h:49
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip=NULL, Vector3 *r_normal=NULL) const
get box where two intersect, empty if no intersection occurs
Definition: aabb.cpp:158
Definition: plane.h:35
Definition: ustring.h:64
_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const
Point is over plane.
Definition: plane.h:90
AABB intersection(const AABB &p_aabb) const
merge with another AABB
Definition: aabb.cpp:73