48 float get_area()
const;
51 return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON);
54 _FORCE_INLINE_
bool has_no_surface()
const {
56 return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON);
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; }
65 bool operator==(
const AABB& p_rval)
const;
66 bool operator!=(
const AABB& p_rval)
const;
68 _FORCE_INLINE_
bool intersects(
const AABB& p_aabb)
const;
73 void merge_with(
const AABB& p_aabb);
77 _FORCE_INLINE_
bool smits_intersect_ray(
const Vector3 &from,
const Vector3& p_dir,
float t0,
float t1)
const;
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;
82 _FORCE_INLINE_
bool has_point(
const Vector3& p_point)
const;
86 Vector3 get_longest_axis()
const;
87 int get_longest_axis_index()
const;
88 _FORCE_INLINE_ real_t get_longest_axis_size()
const;
90 Vector3 get_shortest_axis()
const;
91 int get_shortest_axis_index()
const;
92 _FORCE_INLINE_ real_t get_shortest_axis_size()
const;
94 AABB grow(real_t p_by)
const;
95 _FORCE_INLINE_
void grow_by(real_t p_amount);
98 _FORCE_INLINE_
Vector3 get_endpoint(
int p_point)
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);
106 _FORCE_INLINE_
AABB() {}
112 inline bool AABB::intersects(
const AABB& p_aabb)
const {
114 if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) )
116 if ( (pos.x+size.x) <= p_aabb.pos.x )
118 if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) )
120 if ( (pos.y+size.y) <= p_aabb.pos.y )
122 if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) )
124 if ( (pos.z+size.z) <= p_aabb.pos.z )
132 if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
134 if ( (pos.x+size.x) < p_aabb.pos.x )
136 if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
138 if ( (pos.y+size.y) < p_aabb.pos.y )
140 if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
142 if ( (pos.z+size.z) < p_aabb.pos.z )
153 Vector3 dst_max=p_aabb.pos+p_aabb.size;
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) );
167 Vector3 half_extents = size * 0.5;
168 Vector3 ofs = pos + half_extents;
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
178 Vector3 AABB::get_endpoint(
int p_point)
const {
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 );
194 bool AABB::intersects_convex_shape(
const Plane *p_planes,
int p_plane_count)
const {
198 Vector3 half_extents = size * 0.5;
199 Vector3 ofs = pos + half_extents;
201 for(
int i=0;i<p_plane_count;i++) {
202 const Plane &p=p_planes[i];
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
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 ),
228 for (
int i=0;i<p_plane_count;i++) {
230 const Plane & plane=p_planes[i];
231 bool all_points_over=
true;
234 for (
int j=0;j<8;j++) {
239 all_points_over=
false;
245 if (all_points_over) {
254 bool AABB::has_point(
const Vector3& p_point)
const {
262 if (p_point.x>pos.x+size.x)
264 if (p_point.y>pos.y+size.y)
266 if (p_point.z>pos.z+size.z)
273 inline void AABB::expand_to(
const Vector3& p_vector) {
278 if (p_vector.x<begin.x)
280 if (p_vector.y<begin.y)
282 if (p_vector.z<begin.z)
285 if (p_vector.x>end.x)
287 if (p_vector.y>end.y)
289 if (p_vector.z>end.z)
296 void AABB::project_range_in_plane(
const Plane& p_plane,
float &r_min,
float& r_max)
const {
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 );
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;
307 inline real_t AABB::get_longest_axis_size()
const {
309 real_t max_size=size.x;
311 if (size.y > max_size ) {
315 if (size.z > max_size ) {
322 inline real_t AABB::get_shortest_axis_size()
const {
324 real_t max_size=size.x;
326 if (size.y < max_size ) {
330 if (size.z < max_size ) {
337 bool AABB::smits_intersect_ray(
const Vector3 &from,
const Vector3& dir,
float t0,
float t1)
const {
339 float divx=1.0/dir.x;
340 float divy=1.0/dir.y;
341 float divz=1.0/dir.z;
344 float tmin, tmax, tymin, tymax, tzmin, tzmax;
346 tmin = (pos.x - from.x) * divx;
347 tmax = (upbound.x - from.x) * divx;
350 tmin = (upbound.x - from.x) * divx;
351 tmax = (pos.x - from.x) * divx;
354 tymin = (pos.y - from.y) * divy;
355 tymax = (upbound.y - from.y) * divy;
358 tymin = (upbound.y - from.y) * divy;
359 tymax = (pos.y - from.y) * divy;
361 if ( (tmin > tymax) || (tymin > tmax) )
368 tzmin = (pos.z - from.z) * divz;
369 tzmax = (upbound.z - from.z) * divz;
372 tzmin = (upbound.z - from.z) * divz;
373 tzmax = (pos.z - from.z) * divz;
375 if ( (tmin > tzmax) || (tzmin > tmax) )
381 return ( (tmin < t1) && (tmax > t0) );
384 void AABB::grow_by(real_t p_amount) {
389 size.x+=2.0*p_amount;
390 size.y+=2.0*p_amount;
391 size.z+=2.0*p_amount;
_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
_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
_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