Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
obbox.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : wwmath *
24 * *
25 * $Archive:: /Commando/Code/wwmath/obbox.h $*
26 * *
27 * Org Author:: Greg_h *
28 * *
29 * Author : Kenny Mitchell *
30 * *
31 * $Modtime:: 06/26/02 4:04p $*
32 * *
33 * $Revision:: 24 $*
34 * *
35 * 06/26/02 KM Matrix name change to avoid MAX conflicts *
36 *---------------------------------------------------------------------------------------------*
37 * Functions: *
38 * OBBoxClass::Transform -- transform an oriented box *
39 * OBBoxClass::Project_To_Axis -- compute projection onto the given axis *
40 * OBBoxClass::Compute_Point -- computes position of a parametricly defined point *
41 * OBBoxClass::Compute_Axis_Aligned_Extent -- computes extent of an AABox enclosing this box *
42 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
43
44#if defined(_MSC_VER)
45#pragma once
46#endif
47
48#ifndef OBBOX_H
49#define OBBOX_H
50
51#include "always.h"
52#include "vector3.h"
53#include "matrix3.h"
54#include "matrix3d.h"
55#include "wwmath.h"
56#include "castres.h"
57
58class TriClass;
59class AABoxClass;
60class PlaneClass;
61
62
63/*
64** OBBoxClass
65**
66** Oriented-Bounding-Box Class.
67** This is a collision box in world space.
68** Center - position of the center of the box
69** Extents - size of the box
70** Basis - rotation matrix defining the orientation of the box
71**
72** To find the world space coordinates of the "+x,+y,+z" corner of
73** the bounding box you could use this equation:
74** Vector3 corner = Center + Basis * Extent;
75*/
77{
78public:
79
80 OBBoxClass(void) { }
81
82 OBBoxClass(const OBBoxClass & that) :
83 Basis(that.Basis),
84 Center(that.Center),
85 Extent(that.Extent)
86 { }
87
88 OBBoxClass(const Vector3 & center,const Vector3 & extent) :
89 Basis(1),
90 Center(center),
91 Extent(extent)
92 { }
93
94 OBBoxClass(const Vector3 & center,const Vector3 & extent,const Matrix3x3 & basis) :
95 Basis(basis),
96 Center(center),
97 Extent(extent)
98 { }
99
100 OBBoxClass(const Vector3 * points, int num_points);
101
102 bool operator== (const OBBoxClass &src);
103 bool operator!= (const OBBoxClass &src);
104
105 void Init_From_Box_Points(Vector3 * points,int num_points);
106 void Init_Random(float min_extent = 0.5f,float max_extent = 1.0f);
107 float Project_To_Axis(const Vector3 & axis) const;
108 float Volume(void) const { return 2.0*Extent.X * 2.0*Extent.Y * 2.0*Extent.Z; }
109 void Compute_Point(float params[3],Vector3 * set_point) const;
110 void Compute_Axis_Aligned_Extent(Vector3 * set_extent) const;
111
115
116 static void Transform(const Matrix3D & tm,const OBBoxClass & in,OBBoxClass * out);
117};
118
119// Test functions: slow, easy to understand version of box intersection code :)
120bool Oriented_Boxes_Intersect(const OBBoxClass & box0,const OBBoxClass & box1);
121bool Oriented_Boxes_Collide(const OBBoxClass & box0,const Vector3 & v0,const OBBoxClass & box1,const Vector3 & v1,float dt);
122bool Oriented_Box_Intersects_Tri(const OBBoxClass & box,const TriClass & tri);
123
124
125/***********************************************************************************************
126 * OBBoxClass::Project_To_Axis -- compute projection onto the given axis *
127 * *
128 * INPUT: *
129 * *
130 * OUTPUT: *
131 * *
132 * WARNINGS: *
133 * *
134 * HISTORY: *
135 * 2/24/98 GTH : Created. *
136 *=============================================================================================*/
137inline float OBBoxClass::Project_To_Axis(const Vector3 & axis) const
138{
139 float x = Extent[0] * Vector3::Dot_Product(axis,Vector3(Basis[0][0],Basis[1][0],Basis[2][0]));
140 float y = Extent[1] * Vector3::Dot_Product(axis,Vector3(Basis[0][1],Basis[1][1],Basis[2][1]));
141 float z = Extent[2] * Vector3::Dot_Product(axis,Vector3(Basis[0][2],Basis[1][2],Basis[2][2]));
142
143 // projection is the sum of the absolute values of the projections of the three extents
144 return (WWMath::Fabs(x) + WWMath::Fabs(y) + WWMath::Fabs(z));
145}
146
147
148/***********************************************************************************************
149 * OBBoxClass::Transform -- transform an oriented box *
150 * *
151 * INPUT: *
152 * *
153 * OUTPUT: *
154 * *
155 * WARNINGS: *
156 * *
157 * HISTORY: *
158 * 2/24/98 GTH : Created. *
159 *=============================================================================================*/
161(
162 const Matrix3D & tm,
163 const OBBoxClass & in,
164 OBBoxClass * out
165)
166{
167 WWASSERT(out);
168 WWASSERT(out!=&in);
169
170 out->Extent = in.Extent;
172 Matrix3x3::Multiply(tm,in.Basis,&(out->Basis));
173}
174
175
176/***********************************************************************************************
177 * OBBoxClass::Compute_Point -- computes position of a parametricly defined point *
178 * *
179 * set_point = Center + params[0]*A0 + params[1]*A1 + params[2]*A2 *
180 * *
181 * INPUT: *
182 * params - parametric description of a point in the box. -1 < params[i] < 1 *
183 * set_point - pointer to a Vector3 to set. *
184 * *
185 * OUTPUT: *
186 * *
187 * WARNINGS: *
188 * *
189 * HISTORY: *
190 * 4/2/99 GTH : Created. *
191 *=============================================================================================*/
192inline void OBBoxClass::Compute_Point(float params[3],Vector3 * set_point) const
193{
194 Vector3 point = Extent;
195 point.X *= params[0];
196 point.Y *= params[1];
197 point.Z *= params[2];
198
199 Matrix3x3::Rotate_Vector(Basis,point,set_point);
200 Vector3::Add(Center,*set_point,set_point);
201}
202
203
204/***********************************************************************************************
205 * OBBoxClass::Compute_Axis_Aligned_Extent -- computes extent of an AABox enclosing this box *
206 * *
207 * INPUT: *
208 * set_extent - pointer to a Vector3 to put the result into *
209 * *
210 * OUTPUT: *
211 * *
212 * WARNINGS: *
213 * *
214 * HISTORY: *
215 * 11/15/99 gth : Created. *
216 *=============================================================================================*/
218{
219 WWASSERT(set_extent != NULL);
220
221 // x extent is the box projected onto the x axis
222 set_extent->X = WWMath::Fabs(Extent[0] * Basis[0][0]) +
223 WWMath::Fabs(Extent[1] * Basis[0][1]) +
224 WWMath::Fabs(Extent[2] * Basis[0][2]);
225
226 set_extent->Y = WWMath::Fabs(Extent[0] * Basis[1][0]) +
227 WWMath::Fabs(Extent[1] * Basis[1][1]) +
228 WWMath::Fabs(Extent[2] * Basis[1][2]);
229
230 set_extent->Z = WWMath::Fabs(Extent[0] * Basis[2][0]) +
231 WWMath::Fabs(Extent[1] * Basis[2][1]) +
232 WWMath::Fabs(Extent[2] * Basis[2][2]);
233}
234
235
236/***********************************************************************************************
237 * OBBoxClass::operator== -- Comparison operator *
238 * *
239 * INPUT: *
240 * *
241 * OUTPUT: *
242 * *
243 * WARNINGS: *
244 * *
245 * HISTORY: *
246 * 6/21/00 PDS : Created. *
247 *=============================================================================================*/
248inline bool OBBoxClass::operator== (const OBBoxClass &src)
249{
250 return (Center == src.Center) && (Extent == src.Extent) && (Basis == src.Basis);
251}
252
253
254/***********************************************************************************************
255 * OBBoxClass::operator!= -- Comparison operator *
256 * *
257 * INPUT: *
258 * *
259 * OUTPUT: *
260 * *
261 * WARNINGS: *
262 * *
263 * HISTORY: *
264 * 6/21/00 PDS : Created. *
265 *=============================================================================================*/
266inline bool OBBoxClass::operator!= (const OBBoxClass &src)
267{
268 return (Center != src.Center) || (Extent != src.Extent) && (Basis == src.Basis);
269}
270
271#endif
#define NULL
Definition BaseType.h:92
#define WWASSERT
static WWINLINE void Transform_Vector(const Matrix3D &tm, const Vector3 &in, Vector3 *out)
Definition matrix3d.h:1742
static WWINLINE void Rotate_Vector(const Matrix3x3 &tm, const Vector3 &in, Vector3 *out)
Definition matrix3.h:982
static void Multiply(const Matrix3x3 &a, const Matrix3x3 &b, Matrix3x3 *res)
Definition matrix3.cpp:290
OBBoxClass(const OBBoxClass &that)
Definition obbox.h:82
OBBoxClass(void)
Definition obbox.h:80
void Compute_Axis_Aligned_Extent(Vector3 *set_extent) const
Definition obbox.h:217
OBBoxClass(const Vector3 &center, const Vector3 &extent, const Matrix3x3 &basis)
Definition obbox.h:94
bool operator==(const OBBoxClass &src)
Definition obbox.h:248
Vector3 Extent
Definition obbox.h:114
void Compute_Point(float params[3], Vector3 *set_point) const
Definition obbox.h:192
void Init_Random(float min_extent=0.5f, float max_extent=1.0f)
Definition obbox.cpp:291
bool operator!=(const OBBoxClass &src)
Definition obbox.h:266
OBBoxClass(const Vector3 &center, const Vector3 &extent)
Definition obbox.h:88
Matrix3x3 Basis
Definition obbox.h:112
void Init_From_Box_Points(Vector3 *points, int num_points)
Definition obbox.cpp:200
float Volume(void) const
Definition obbox.h:108
Vector3 Center
Definition obbox.h:113
float Project_To_Axis(const Vector3 &axis) const
Definition obbox.h:137
static void Transform(const Matrix3D &tm, const OBBoxClass &in, OBBoxClass *out)
Definition obbox.h:161
Definition tri.h:61
static WWINLINE float Dot_Product(const Vector3 &a, const Vector3 &b)
Definition vector3.h:293
float X
Definition vector3.h:90
float Z
Definition vector3.h:92
float Y
Definition vector3.h:91
static WWINLINE void Add(const Vector3 &a, const Vector3 &b, Vector3 *c)
Definition vector3.h:555
static WWINLINE float Fabs(float val)
Definition wwmath.h:113
bool Oriented_Boxes_Intersect(const OBBoxClass &box0, const OBBoxClass &box1)
Definition obbox.cpp:363
bool Oriented_Boxes_Collide(const OBBoxClass &box0, const Vector3 &v0, const OBBoxClass &box1, const Vector3 &v1, float dt)
Definition obbox.cpp:488
bool Oriented_Box_Intersects_Tri(const OBBoxClass &box, const TriClass &tri)
Definition obbox.cpp:743