2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 void aabb_construct_for_vec3(aabb_t *aabb, const vec3_t min, const vec3_t max)
28 VectorMid(min, max, aabb->origin);
29 VectorSubtract(max, aabb->origin, aabb->extents);
32 void aabb_update_radius(aabb_t *aabb)
34 aabb->radius = VectorLength(aabb->extents);
37 void aabb_clear(aabb_t *aabb)
39 aabb->origin[0] = aabb->origin[1] = aabb->origin[2] = 0;
40 aabb->extents[0] = aabb->extents[1] = aabb->extents[2] = -FLT_MAX;
43 void aabb_extend_by_point(aabb_t *aabb, const vec3_t point)
46 vec_t min, max, displacement;
49 displacement = point[i] - aabb->origin[i];
50 if(fabs(displacement) > aabb->extents[i])
52 if(aabb->extents[i] < 0) // degenerate
56 else if(displacement > 0)
58 min = aabb->origin[i] - aabb->extents[i];
59 max = aabb->origin[i] + displacement;
63 max = aabb->origin[i] + aabb->extents[i];
64 min = aabb->origin[i] + displacement;
66 aabb->origin[i] = (min + max) * 0.5f;
67 aabb->extents[i] = max - aabb->origin[i];
72 void aabb_extend_by_aabb(aabb_t *aabb, const aabb_t *aabb_src)
75 vec_t min, max, displacement, difference;
78 displacement = aabb_src->origin[i] - aabb->origin[i];
79 difference = aabb_src->extents[i] - aabb->extents[i];
80 if(aabb->extents[i] < 0
81 || difference >= fabs(displacement))
84 aabb->extents[i] = aabb_src->extents[i];
85 aabb->origin[i] = aabb_src->origin[i];
87 else if(aabb_src->extents[i] < 0
88 || -difference >= fabs(displacement))
98 min = aabb->origin[i] - aabb->extents[i];
99 max = aabb_src->origin[i] + aabb_src->extents[i];
103 min = aabb_src->origin[i] - aabb_src->extents[i];
104 max = aabb->origin[i] + aabb->extents[i];
106 aabb->origin[i] = (min + max) * 0.5f;
107 aabb->extents[i] = max - aabb->origin[i];
112 void aabb_extend_by_vec3(aabb_t *aabb, vec3_t extension)
114 VectorAdd(aabb->extents, extension, aabb->extents);
117 int aabb_intersect_point(const aabb_t *aabb, const vec3_t point)
121 if(fabs(point[i] - aabb->origin[i]) >= aabb->extents[i])
126 int aabb_intersect_aabb(const aabb_t *aabb, const aabb_t *aabb_src)
130 if ( fabs(aabb_src->origin[i] - aabb->origin[i]) > (fabs(aabb->extents[i]) + fabs(aabb_src->extents[i])) )
135 int aabb_intersect_plane(const aabb_t *aabb, const float *plane)
137 float fDist, fIntersect;
139 // calc distance of origin from plane
140 fDist = DotProduct(plane, aabb->origin) + plane[3];
142 // trivial accept/reject using bounding sphere
143 if (fabs(fDist) > aabb->radius)
146 return 2; // totally inside
148 return 0; // totally outside
151 // calc extents distance relative to plane normal
152 fIntersect = (vec_t)(fabs(plane[0] * aabb->extents[0]) + fabs(plane[1] * aabb->extents[1]) + fabs(plane[2] * aabb->extents[2]));
153 // accept if origin is less than or equal to this distance
154 if (fabs(fDist) < fIntersect) return 1; // partially inside
155 else if (fDist < 0) return 2; // totally inside
156 return 0; // totally outside
160 Fast Ray-Box Intersection
162 from "Graphics Gems", Academic Press, 1990
170 int aabb_intersect_ray(const aabb_t *aabb, const ray_t *ray, vec_t *dist)
173 char quadrant[NUMDIM];
177 double candidatePlane[NUMDIM];
178 vec3_t coord, segment;
180 const float *origin = ray->origin;
181 const float *direction = ray->direction;
183 /* Find candidate planes; this loop can be avoided if
184 rays cast all from the eye(assume perpsective view) */
185 for (i=0; i<NUMDIM; i++)
187 if(origin[i] < (aabb->origin[i] - aabb->extents[i]))
190 candidatePlane[i] = (aabb->origin[i] - aabb->extents[i]);
193 else if (origin[i] > (aabb->origin[i] + aabb->extents[i]))
196 candidatePlane[i] = (aabb->origin[i] + aabb->extents[i]);
201 quadrant[i] = MIDDLE;
205 /* Ray origin inside bounding box */
213 /* Calculate T distances to candidate planes */
214 for (i = 0; i < NUMDIM; i++)
216 if (quadrant[i] != MIDDLE && direction[i] !=0.)
217 maxT[i] = (candidatePlane[i] - origin[i]) / direction[i];
222 /* Get largest of the maxT's for final choice of intersection */
224 for (i = 1; i < NUMDIM; i++)
225 if (maxT[whichPlane] < maxT[i])
228 /* Check final candidate actually inside box */
229 if (maxT[whichPlane] < 0.)
231 for (i = 0; i < NUMDIM; i++)
235 coord[i] = (vec_t)(origin[i] + maxT[whichPlane] * direction[i]);
236 if (fabs(coord[i] - aabb->origin[i]) > aabb->extents[i])
241 coord[i] = (vec_t)candidatePlane[i];
245 VectorSubtract(coord, origin, segment);
246 *dist = DotProduct(segment, direction);
248 return 1; /* ray hits box */
251 int aabb_test_ray(const aabb_t* aabb, const ray_t* ray)
253 vec3_t displacement, ray_absolute;
256 displacement[0] = ray->origin[0] - aabb->origin[0];
257 if(fabs(displacement[0]) > aabb->extents[0] && displacement[0] * ray->direction[0] >= 0.0f)
260 displacement[1] = ray->origin[1] - aabb->origin[1];
261 if(fabs(displacement[1]) > aabb->extents[1] && displacement[1] * ray->direction[1] >= 0.0f)
264 displacement[2] = ray->origin[2] - aabb->origin[2];
265 if(fabs(displacement[2]) > aabb->extents[2] && displacement[2] * ray->direction[2] >= 0.0f)
268 ray_absolute[0] = (float)fabs(ray->direction[0]);
269 ray_absolute[1] = (float)fabs(ray->direction[1]);
270 ray_absolute[2] = (float)fabs(ray->direction[2]);
272 f = ray->direction[1] * displacement[2] - ray->direction[2] * displacement[1];
273 if((float)fabs(f) > aabb->extents[1] * ray_absolute[2] + aabb->extents[2] * ray_absolute[1])
276 f = ray->direction[2] * displacement[0] - ray->direction[0] * displacement[2];
277 if((float)fabs(f) > aabb->extents[0] * ray_absolute[2] + aabb->extents[2] * ray_absolute[0])
280 f = ray->direction[0] * displacement[1] - ray->direction[1] * displacement[0];
281 if((float)fabs(f) > aabb->extents[0] * ray_absolute[1] + aabb->extents[1] * ray_absolute[0])
287 void aabb_for_bbox(aabb_t *aabb, const bbox_t *bbox)
292 VectorCopy(bbox->aabb.origin, aabb->origin);
294 // calculate the AABB extents in local coord space from the OBB extents and axes
295 VectorScale(bbox->axes[0], bbox->aabb.extents[0], temp[0]);
296 VectorScale(bbox->axes[1], bbox->aabb.extents[1], temp[1]);
297 VectorScale(bbox->axes[2], bbox->aabb.extents[2], temp[2]);
298 for(i=0;i<3;i++) aabb->extents[i] = (vec_t)(fabs(temp[0][i]) + fabs(temp[1][i]) + fabs(temp[2][i]));
301 void aabb_for_area(aabb_t *aabb, vec3_t area_tl, vec3_t area_br, int axis)
304 aabb->extents[axis] = FLT_MAX;
305 aabb_extend_by_point(aabb, area_tl);
306 aabb_extend_by_point(aabb, area_br);
309 void aabb_for_transformed_aabb(aabb_t* dst, const aabb_t* src, const m4x4_t transform)
311 VectorCopy(src->origin, dst->origin);
312 m4x4_transform_point(transform, dst->origin);
314 dst->extents[0] = (vec_t)(fabs(transform[0] * src->extents[0])
315 + fabs(transform[4] * src->extents[1])
316 + fabs(transform[8] * src->extents[2]));
317 dst->extents[1] = (vec_t)(fabs(transform[1] * src->extents[0])
318 + fabs(transform[5] * src->extents[1])
319 + fabs(transform[9] * src->extents[2]));
320 dst->extents[2] = (vec_t)(fabs(transform[2] * src->extents[0])
321 + fabs(transform[6] * src->extents[1])
322 + fabs(transform[10] * src->extents[2]));
326 void bbox_for_oriented_aabb(bbox_t *bbox, const aabb_t *aabb, const m4x4_t matrix, const vec3_t euler, const vec3_t scale)
329 double pi_180 = Q_PI / 180;
330 double A, B, C, D, E, F, AD, BD;
332 VectorCopy(aabb->origin, bbox->aabb.origin);
334 m4x4_transform_point(matrix, bbox->aabb.origin);
336 bbox->aabb.extents[0] = aabb->extents[0] * scale[0];
337 bbox->aabb.extents[1] = aabb->extents[1] * scale[1];
338 bbox->aabb.extents[2] = aabb->extents[2] * scale[2];
340 rad[0] = euler[0] * pi_180;
341 rad[1] = euler[1] * pi_180;
342 rad[2] = euler[2] * pi_180;
354 bbox->axes[0][0] = (vec_t)(C*E);
355 bbox->axes[0][1] = (vec_t)(-BD*E + A*F);
356 bbox->axes[0][2] = (vec_t)(AD*E + B*F);
357 bbox->axes[1][0] = (vec_t)(-C*F);
358 bbox->axes[1][1] = (vec_t)(BD*F + A*E);
359 bbox->axes[1][2] = (vec_t)(-AD*F + B*E);
360 bbox->axes[2][0] = (vec_t)D;
361 bbox->axes[2][1] = (vec_t)(-B*C);
362 bbox->axes[2][2] = (vec_t)(A*C);
364 aabb_update_radius(&bbox->aabb);
367 int bbox_intersect_plane(const bbox_t *bbox, const vec_t* plane)
369 vec_t fDist, fIntersect;
371 // calc distance of origin from plane
372 fDist = DotProduct(plane, bbox->aabb.origin) + plane[3];
374 // trivial accept/reject using bounding sphere
375 if (fabs(fDist) > bbox->aabb.radius)
378 return 2; // totally inside
380 return 0; // totally outside
383 // calc extents distance relative to plane normal
384 fIntersect = (vec_t)(fabs(bbox->aabb.extents[0] * DotProduct(plane, bbox->axes[0]))
385 + fabs(bbox->aabb.extents[1] * DotProduct(plane, bbox->axes[1]))
386 + fabs(bbox->aabb.extents[2] * DotProduct(plane, bbox->axes[2])));
387 // accept if origin is less than this distance
388 if (fabs(fDist) < fIntersect) return 1; // partially inside
389 else if (fDist < 0) return 2; // totally inside
390 return 0; // totally outside