UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
aabb.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "aabb.h"
27 #include "mathlib.h"
28 
30 {
33 }
34 AABB::AABB (const vec3_t mini, const vec3_t maxi)
35 {
36  VectorCopy(mini, mins);
37  VectorCopy(maxi, maxs);
38 }
39 AABB::AABB (const vec_t minX, const vec_t minY, const vec_t minZ, const vec_t maxX, const vec_t maxY, const vec_t maxZ)
40 {
41  mins[0] = minX;
42  mins[1] = minY;
43  mins[2] = minZ;
44  maxs[0] = maxX;
45  maxs[1] = maxY;
46  maxs[2] = maxZ;
47 }
48 AABB::AABB (const Line& line)
49 {
50  VectorSet(mins, std::min(line.start[0], line.stop[0]), std::min(line.start[1], line.stop[1]), std::min(line.start[2], line.stop[2]));
51  VectorSet(maxs, std::max(line.start[0], line.stop[0]), std::max(line.start[1], line.stop[1]), std::max(line.start[2], line.stop[2]));
52 }
53 
57 void AABB::add (const vec3_t point)
58 {
59  for (int i = 0; i < 3; i++) {
60  const vec_t val = point[i];
61  if (val < mins[i])
62  mins[i] = val;
63  if (val > maxs[i])
64  maxs[i] = val;
65  }
66 }
72 void AABB::add (const AABB& other)
73 {
74  for (int i = 0; i < 3; i++) {
75  if (other.mins[i] < mins[i])
76  mins[i] = other.mins[i];
77  if (other.maxs[i] > maxs[i])
78  maxs[i] = other.maxs[i];
79  }
80 }
81 
86 void AABB::rotateAround (const vec3_t origin, const vec3_t angles) {
87  /* reject non-rotations */
88  if (VectorEmpty(angles))
89  return;
90 
91  /* construct box-centered coordinates (center and corners) */
92  vec3_t center, halfDiagonal;
93 
94  VectorInterpolation(mins, maxs, 0.5f, center);
95  VectorSubtract(maxs, center, halfDiagonal);
96 
97  /* offset coordinate frame to rotation origin */
98  VectorSubtract(center, origin, center);
99 
100  /* rotate center by given angles */
101  vec3_t m[3];
102  VectorCreateRotationMatrix(angles, m);
103 
104  vec3_t newCenter;
105  VectorRotate(m, center, newCenter);
106 
107  /* short-circuit calculation of the rotated box half-extents */
108  /* shortcut is: instead of calculating all 8 AABB corners, use the symmetry by rotating box around it's center. */
109  VectorAbs(m[0]);
110  VectorAbs(m[1]);
111  VectorAbs(m[2]);
112 
113  vec3_t newHalfDiagonal;
114  VectorRotate(m, halfDiagonal, newHalfDiagonal);
115 
116  /* de-offset coordinate frame from rotation origin */
117  VectorAdd(newCenter, origin, newCenter);
118 
119  /* finally, combine results into new AABB */
120  VectorAdd(newCenter, newHalfDiagonal, maxs);
121  VectorSubtract(newCenter, newHalfDiagonal, mins);
122 }
123 
124 const AABB AABB::EMPTY;
#define VectorCopy(src, dest)
Definition: vector.h:51
#define VectorSet(v, x, y, z)
Definition: vector.h:59
#define VectorAbs(a)
Definition: vector.h:81
voidpf uLong int origin
Definition: ioapi.h:45
Definition: aabb.h:42
const vec3_t vec3_origin
Definition: mathlib.cpp:35
float vec_t
Definition: ufotypes.h:37
void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3])
Definition: mathlib.cpp:592
vec3_t maxs
Definition: aabb.h:258
void add(const vec3_t point)
If the point is outside the box, expand the box to accommodate it.
Definition: aabb.cpp:57
void rotateAround(const vec3_t origin, const vec3_t angles)
Rotates bounding box around given origin point; note that it will expand the box unless all angles ar...
Definition: aabb.cpp:86
#define VectorEmpty(a)
Definition: vector.h:73
Definition: line.h:31
#define VectorInterpolation(p1, p2, frac, mid)
Definition: vector.h:80
vec3_t stop
Definition: line.h:55
AABB()
Definition: aabb.cpp:29
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
#define VectorAdd(a, b, dest)
Definition: vector.h:47
QGL_EXTERN GLint i
Definition: r_gl.h:113
vec3_t start
Definition: line.h:54
vec_t vec3_t[3]
Definition: ufotypes.h:39
vec3_t mins
Definition: aabb.h:257
void VectorRotate(vec3_t m[3], const vec3_t va, vec3_t vb)
Rotate a vector with a rotation matrix.
Definition: mathlib.cpp:395
static const AABB EMPTY
Definition: aabb.h:44
static struct mdfour * m
Definition: md4.cpp:35
#define VectorSubtract(a, b, dest)
Definition: vector.h:45