UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
aabb.h
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17  See the GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23  */
24 
25 #pragma once
26 
27 #include "cxx.h"
28 #include "ufotypes.h"
29 #include "vector.h"
30 #include "line.h"
31 #include "byte.h"
32 #include "defines.h" /* for MAX_WORLD_WIDTH */
33 #include <algorithm>
34 #include <stdio.h>
35 
39 const float MWW = MAX_WORLD_WIDTH;
40 #define AABB_STRING 64
41 
42 class AABB {
43 public:
44  static const AABB EMPTY;
45  /*==================
46  * ctors
47  *==================*/
48  AABB ();
49  AABB (const vec3_t mini, const vec3_t maxi);
50  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);
51  AABB (const Line& line);
52 
53  /*==================
54  * setters
55  *==================*/
60  inline void set (const AABB& other) {
61  VectorCopy(other.mins, mins);
62  VectorCopy(other.maxs, maxs);
63  }
64  inline void set (const vec3_t mini, const vec3_t maxi) {
65  VectorCopy(mini, mins);
66  VectorCopy(maxi, maxs);
67  }
68  inline void setMins (const vec3_t mini) {
69  VectorCopy(mini, mins);
70  }
71  inline void setMaxs (const vec3_t maxi) {
72  VectorCopy(maxi, maxs);
73  }
74  inline void setMins (int x, int y, int z) {
75  VectorSet(mins, x, y, z);
76  }
77  inline void setMaxs (int x, int y, int z) {
78  VectorSet(maxs, x, y, z);
79  }
80  inline void setMaxZ (float zVal) {
81  maxs[2] = zVal;
82  }
83  inline void reset () {
84  mins[0] = mins[1] = mins[2] = 0;
85  maxs[0] = maxs[1] = maxs[2] = 0;
86  }
87  inline void setFromLittleFloat (const AABB& other) {
88  mins[0] = LittleFloat(other.mins[0]);
89  mins[1] = LittleFloat(other.mins[1]);
90  mins[2] = LittleFloat(other.mins[2]);
91  maxs[0] = LittleFloat(other.maxs[0]);
92  maxs[1] = LittleFloat(other.maxs[1]);
93  maxs[2] = LittleFloat(other.maxs[2]);
94  }
98  inline void setNegativeVolume () {
99  mins[0] = mins[1] = mins[2] = 99999;
100  maxs[0] = maxs[1] = maxs[2] = -99999;
101  }
105  inline void set (const AABB& trBox, const Line& trLine) {
106  AABB endBox(trBox); /* get the moving object */
107  endBox.shift(trLine.stop); /* move it to end position */
108  set(trBox);
109  shift(trLine.start); /* object in starting position */
110  add(endBox); /* the whole box */
111  }
112 
113  /*==================
114  * getters
115  *==================*/
116  inline const vec3_t& getMins () const {
117  return mins;
118  }
119  inline float getMinX () const {
120  return mins[0];
121  }
122  inline float getMinY () const {
123  return mins[1];
124  }
125  inline float getMinZ () const {
126  return mins[2];
127  }
128  inline const vec3_t& getMaxs () const {
129  return maxs;
130  }
131  inline float getMaxX () const {
132  return maxs[0];
133  }
134  inline float getMaxY () const {
135  return maxs[1];
136  }
137  inline float getMaxZ () const {
138  return maxs[2];
139  }
140 
141  inline float getWidthX () const {
142  return getMaxX() - getMinX();
143  }
144  inline float getWidthY () const {
145  return getMaxY() - getMinY();
146  }
147  inline float getWidthZ () const {
148  return getMaxZ() - getMinZ();
149  }
150 
155  inline void getCenter (vec3_t center) const {
156  VectorAdd(mins, maxs, center);
157  VectorScale(center, 0.5, center);
158  }
159  inline void getDiagonal (vec3_t diagonal) const {
160  VectorSubtract(maxs, mins, diagonal);
161  }
167  inline void asIntString (char* str, size_t len) {
168  snprintf(str, len, "(%i, %i, %i) (%i, %i, %i)",
169  (int) mins[0], (int) mins[1], (int) mins[2],
170  (int) maxs[0], (int) maxs[1], (int) maxs[2] );
171  }
172 
173  /*==================
174  * checkers
175  *==================*/
176  inline bool isZero () const {
177  return VectorEmpty(mins) && VectorEmpty(maxs);
178  }
183  inline bool doesIntersect (const AABB& other) const {
184  return !(getMinX() > other.getMaxX() || getMinY() > other.getMaxY() || getMinZ() > other.getMaxZ() || getMaxX() < other.getMinX()
185  || getMaxY() < other.getMinY() || getMaxZ() < other.getMinZ());
186  }
192  inline bool canBeHitBy (const Line& line) const {
193  return !( (line.start[0] > getMaxX() && line.stop[0] > getMaxX())
194  || (line.start[1] > getMaxY() && line.stop[1] > getMaxY())
195  || (line.start[2] > getMaxZ() && line.stop[2] > getMaxZ())
196  || (line.start[0] < getMinX() && line.stop[0] < getMinX())
197  || (line.start[1] < getMinY() && line.stop[1] < getMinY())
198  || (line.start[2] < getMinZ() && line.stop[2] < getMinZ()));
199  }
200  inline bool contains (const vec3_t point) const {
201  return (point[0] >= getMinX() && point[0] <= getMaxX()
202  && point[1] >= getMinY() && point[1] <= getMaxY()
203  && point[2] >= getMinZ() && point[2] <= getMaxZ() );
204  }
205  inline bool contains (const AABB& other) const {
206  return (other.getMinX() >= getMinX() && other.getMaxX() <= getMaxX()
207  && other.getMinY() >= getMinY() && other.getMaxY() <= getMaxY()
208  && other.getMinZ() >= getMinZ() && other.getMaxZ() <= getMaxZ() );
209  }
210 
211  /*==================
212  * manipulators
213  *==================*/
214  void add (const vec3_t point);
215  void add (const AABB& other);
216 
221  void rotateAround(const vec3_t origin, const vec3_t angles);
222 
224  inline void clipToWorld () {
225  mins[0] = std::max(mins[0], -MWW);
226  mins[1] = std::max(mins[1], -MWW);
227  /* Hmm, we don't have a MAX_WORLD_HEIGHT ?!? */
228  maxs[0] = std::min(maxs[0], MWW);
229  maxs[1] = std::min(maxs[1], MWW);
230  }
232  inline void expandXY (const float byVal) {
233  mins[0] -= byVal;
234  mins[1] -= byVal;
235  maxs[0] += byVal;
236  maxs[1] += byVal;
237  clipToWorld();
238  }
240  inline void expand (const float byVal) {
241  mins[2] -= byVal;
242  maxs[2] += byVal;
243  expandXY(byVal);
244  }
246  inline void shift (const vec3_t shiftVec) {
247  VectorAdd(mins, shiftVec, mins);
248  VectorAdd(maxs, shiftVec, maxs);
249  clipToWorld();
250  }
251 
252  /*==================
253  * data
254  *==================*/
256 // private:
259 };
float getMaxX() const
Definition: aabb.h:131
#define VectorCopy(src, dest)
Definition: vector.h:51
void setNegativeVolume()
Sets mins and maxs to their starting points before using addPoint.
Definition: aabb.h:98
void set(const vec3_t mini, const vec3_t maxi)
Definition: aabb.h:64
#define VectorSet(v, x, y, z)
Definition: vector.h:59
bool canBeHitBy(const Line &line) const
Checks if the given line has a chance to hit our box.
Definition: aabb.h:192
voidpf uLong int origin
Definition: ioapi.h:45
bool doesIntersect(const AABB &other) const
Checks if the aabb touches or intersects with the given aabb.
Definition: aabb.h:183
void set(const AABB &trBox, const Line &trLine)
Set from another box and a (trace)Line.
Definition: aabb.h:105
Definition: aabb.h:42
float getMaxZ() const
Definition: aabb.h:137
float vec_t
Definition: ufotypes.h:37
float getMinZ() const
Definition: aabb.h:125
void set(const AABB &other)
Copies the values from the given aabb.
Definition: aabb.h:60
vec3_t maxs
Definition: aabb.h:258
#define VectorScale(in, scale, out)
Definition: vector.h:79
void setMins(const vec3_t mini)
Definition: aabb.h:68
void getDiagonal(vec3_t diagonal) const
Definition: aabb.h:159
void expand(const float byVal)
expand the box in all directions, but clip them to the maximum boundaries
Definition: aabb.h:240
const float MWW
Axis-aligned bounding box.
Definition: aabb.h:39
void add(const vec3_t point)
If the point is outside the box, expand the box to accommodate it.
Definition: aabb.cpp:57
void setMaxZ(float zVal)
Definition: aabb.h:80
void setMins(int x, int y, int z)
Definition: aabb.h:74
void clipToWorld()
clip the box to the maximum boundaries
Definition: aabb.h:224
bool contains(const vec3_t point) const
Definition: aabb.h:200
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
Defined CONSTANTS (Macros are elsewhere)
Byte order functions header.
float getWidthY() const
Definition: aabb.h:144
#define VectorEmpty(a)
Definition: vector.h:73
bool contains(const AABB &other) const
Definition: aabb.h:205
Definition: line.h:31
float getMaxY() const
Definition: aabb.h:134
vec3_t stop
Definition: line.h:55
AABB()
Definition: aabb.cpp:29
void getCenter(vec3_t center) const
Calculates the center of the bounding box.
Definition: aabb.h:155
void expandXY(const float byVal)
expand the box in four directions, but clip them to the maximum boundaries
Definition: aabb.h:232
void setMaxs(int x, int y, int z)
Definition: aabb.h:77
bool isZero() const
Definition: aabb.h:176
void setFromLittleFloat(const AABB &other)
Definition: aabb.h:87
void asIntString(char *str, size_t len)
Prints a representation of the box.
Definition: aabb.h:167
#define VectorAdd(a, b, dest)
Definition: vector.h:47
float getWidthX() const
Definition: aabb.h:141
float getMinY() const
Definition: aabb.h:122
const vec3_t & getMins() const
Definition: aabb.h:116
QGL_EXTERN GLuint GLchar GLuint * len
Definition: r_gl.h:99
vec3_t start
Definition: line.h:54
float getMinX() const
Definition: aabb.h:119
vec_t vec3_t[3]
Definition: ufotypes.h:39
#define MAX_WORLD_WIDTH
-MAX_WORLD_WIDTH up tp +MAX_WORLD_WIDTH
Definition: defines.h:288
void shift(const vec3_t shiftVec)
shove the whole box by the given vector
Definition: aabb.h:246
vec3_t mins
Definition: aabb.h:257
Cross-platform type definitions.
static const AABB EMPTY
Definition: aabb.h:44
void setMaxs(const vec3_t maxi)
Definition: aabb.h:71
const vec3_t & getMaxs() const
Definition: aabb.h:128
void reset()
Definition: aabb.h:83
#define VectorSubtract(a, b, dest)
Definition: vector.h:45
#define LittleFloat(X)
Definition: byte.h:57
float getWidthZ() const
Definition: aabb.h:147
A simple line between two points.