UFO: Alien Invasion
Loading...
Searching...
No Matches
aabb.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24*/
25
26#include "aabb.h"
27#include "mathlib.h"
28
34AABB::AABB (const vec3_t mini, const vec3_t maxi)
35{
36 VectorCopy(mini, mins);
37 VectorCopy(maxi, maxs);
38}
39AABB::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}
48AABB::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
57void 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}
67
72void 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
86void 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];
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
124const AABB AABB::EMPTY;
Definition aabb.h:42
vec3_t maxs
Definition aabb.h:258
vec3_t mins
Definition aabb.h:257
AABB()
Definition aabb.cpp:29
static const AABB EMPTY
Definition aabb.h:44
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
void add(const vec3_t point)
If the point is outside the box, expand the box to accommodate it.
Definition aabb.cpp:57
Definition line.h:31
vec3_t start
Definition line.h:54
vec3_t stop
Definition line.h:55
voidpf uLong int origin
Definition ioapi.h:45
const vec3_t vec3_origin
Definition mathlib.cpp:35
void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3])
Definition mathlib.cpp:592
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 struct mdfour * m
Definition md4.cpp:35
QGL_EXTERN GLint i
Definition r_gl.h:113
float vec_t
Definition ufotypes.h:37
vec_t vec3_t[3]
Definition ufotypes.h:39
#define VectorAbs(a)
Definition vector.h:81
#define VectorInterpolation(p1, p2, frac, mid)
Definition vector.h:80
#define VectorSubtract(a, b, dest)
Definition vector.h:45
#define VectorCopy(src, dest)
Definition vector.h:51
#define VectorEmpty(a)
Definition vector.h:73
#define VectorAdd(a, b, dest)
Definition vector.h:47
#define VectorSet(v, x, y, z)
Definition vector.h:59