Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
AABoxClass.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__AABOXCLASS_H
13#define TT_INCLUDE__AABOXCLASS_H
14
15#include "Vector3.h"
16#include "Matrix3D.h"
17#include "WWMath.h"
18#include "LineSegClass.h"
19#include "engine_math.h"
20#include "CastResultStruct.h"
21#include "FrustumClass.h"
22class Vector3;
23class TriClass;
24class MeshTriClass;
25class FrustumClass;
26class LineSegClass;
27class SphereClass;
28class OBBoxClass;
29class PlaneClass;
30class AAPlaneClass;
31class MinMaxAABoxClass;
32class LineSegClass;
33class AABoxClass
34{
35public:
36 inline AABoxClass(void)
37 {
38 }
39 inline AABoxClass(const Vector3 & center,const Vector3 & extent) : Center(center), Extent(extent)
40 {
41 }
42 AABoxClass(const MinMaxAABoxClass & minmaxbox)
43 {
44 Init(minmaxbox);
45 }
46 AABoxClass(Vector3 * points,int num)
47 {
48 Init(points,num);
49 }
50 bool operator== (const AABoxClass &src)
51 {
52 return (Center == src.Center) && (Extent == src.Extent);
53 }
54 bool operator!= (const AABoxClass &src)
55 {
56 return (Center != src.Center) || (Extent != src.Extent);
57 }
58 inline void Init(const Vector3& center,const Vector3 & extent)
59 {
60 Center = center; Extent = extent;
61 }
62 inline void Init(Vector3 * points,int num)
63 {
64 Vector3 Min = points[0];
65 Vector3 Max = points[0];
66 for (int i=1; i<num; i++)
67 {
68 if (Min.X > points[i].X) Min.X = points[i].X;
69 if (Min.Y > points[i].Y) Min.Y = points[i].Y;
70 if (Min.Z > points[i].Z) Min.Z = points[i].Z;
71 if (Max.X < points[i].X) Max.X = points[i].X;
72 if (Max.Y < points[i].Y) Max.Y = points[i].Y;
73 if (Max.Z < points[i].Z) Max.Z = points[i].Z;
74 }
75 Center = (Max + Min) * 0.5f;
76 Extent = (Max - Min) * 0.5f;
77 }
78 inline void Init(const MinMaxAABoxClass & mmbox);
79 void Init(const LineSegClass & line)
80 {
81 Vector3 min_corner = line.Get_P0();
82 Vector3 max_corner = line.Get_P0();
83 if (min_corner.X > line.Get_P1().X) min_corner.X = line.Get_P1().X;
84 if (min_corner.Y > line.Get_P1().Y) min_corner.Y = line.Get_P1().Y;
85 if (min_corner.Z > line.Get_P1().Z) min_corner.Z = line.Get_P1().Z;
86 if (max_corner.X < line.Get_P1().X) max_corner.X = line.Get_P1().X;
87 if (max_corner.Y < line.Get_P1().Y) max_corner.Y = line.Get_P1().Y;
88 if (max_corner.Z < line.Get_P1().Z) max_corner.Z = line.Get_P1().Z;
89 Center = (max_corner + min_corner) * 0.5f;
90 Extent = (max_corner - min_corner) * 0.5f;
91 }
92 void Init_Min_Max(const Vector3 & min,const Vector3 & max)
93 {
94 Center = (max + min) * 0.5f;
95 Extent = (max - min) * 0.5f;
96 }
97 void Init_Random(float min_center = -1.0f,float max_center = 1.0f,float min_extent = 0.5f,float max_extent = 1.0f);
98 void Add_Point(const Vector3 & point)
99 {
100 Vector3 Min = Center - Extent;
101 Vector3 Max = Center + Extent;
102 if (Min.X > point.X) Min.X = point.X;
103 if (Min.Y > point.Y) Min.Y = point.Y;
104 if (Min.Z > point.Z) Min.Z = point.Z;
105 if (Max.X < point.X) Max.X = point.X;
106 if (Max.Y < point.Y) Max.Y = point.Y;
107 if (Max.Z < point.Z) Max.Z = point.Z;
108 Center = (Max + Min) / 2.0f;
109 Extent = (Max - Min) / 2.0f;
110 }
111 void Add_Box(const AABoxClass & b)
112 {
113 Vector3 newmin = Center - Extent;
114 Vector3 newmax = Center + Extent;
115 newmin.Update_Min(b.Center - b.Extent);
116 newmax.Update_Max(b.Center + b.Extent);
117 Center = (newmax + newmin) * 0.5f;
118 Extent = (newmax - newmin) * 0.5f;
119 }
120 void Add_Box(const MinMaxAABoxClass & b);
121 float Project_To_Axis(const Vector3 & axis) const
122 {
123 float x = Extent[0] * axis[0];
124 float y = Extent[1] * axis[1];
125 float z = Extent[2] * axis[2];
126 return (WWMath::Fabs(x) + WWMath::Fabs(y) + WWMath::Fabs(z));
127 }
128 void Transform(const Matrix3D & tm)
129 {
130 Vector3 oldcenter = Center;
131 Vector3 oldextent = Extent;
132 tm.Transform_Center_Extent_AABox(oldcenter,oldextent,&Center,&Extent);
133 }
134 void Translate(const Vector3 & pos)
135 {
136 Center += pos;
137 }
138 inline float Volume(void) const
139 {
140 return 2.0f*Extent.X * 2.0f*Extent.Y * 2.0f*Extent.Z;
141 }
142 bool Contains(const Vector3 & point) const;
143 bool Contains(const AABoxClass & other_box) const;
144 inline bool Contains(const MinMaxAABoxClass & other_box) const;
145 static void Transform(const Matrix3D & tm,const AABoxClass & in,AABoxClass * out);
146 Vector3 Center;
147 Vector3 Extent;
148
149 float ComputeSquaredDistanceFromPoint(const Vector3& point) const
150 {
151 Vector3 min = Center - Extent;
152 Vector3 max = Center + Extent;
153
154 float sq_dist = 0.0f;
155 for (int i = 0; i < 3; ++i)
156 {
157 const float& v = point[i];
158 if (v < min[i]) sq_dist += (min[i] - v) * (min[i] - v);
159 else if (v > max[i]) sq_dist += (v - max[i]) * (v - max[i]);
160 }
161 return sq_dist;
162 }
163
164 float ComputeMaxSquaredDistanceFromPoint(const Vector3& point) const
165 {
166 const Vector3 d = Center - point;
167 const Vector3 dmin = d - Extent;
168 const Vector3 dmax = d + Extent;
169 return
170 max(sqr(dmin[0]), sqr(dmax[0])) +
171 max(sqr(dmin[1]), sqr(dmax[1])) +
172 max(sqr(dmin[2]), sqr(dmax[2]));
173 }
174
175}; // 0018
176
177class MinMaxAABoxClass
178{
179public:
180 inline MinMaxAABoxClass(void)
181 {
182 }
183 inline MinMaxAABoxClass(const Vector3 & min_corner,const Vector3 & max_corner) : MinCorner(min_corner),MaxCorner(max_corner)
184 {
185 }
186 inline MinMaxAABoxClass(Vector3 * points,int num)
187 {
188 Init(points,num);
189 }
190 inline MinMaxAABoxClass(const AABoxClass & that)
191 {
192 Init(that);
193 }
194 inline void Init(Vector3 * points,int num)
195 {
196 MinCorner = points[0];
197 MaxCorner = points[0];
198 for (int i=0; i<num; i++)
199 {
200 MinCorner.Update_Min(points[i]);
201 MaxCorner.Update_Max(points[i]);
202 }
203 }
204 inline void Init(const AABoxClass & box)
205 {
206 MinCorner = box.Center - box.Extent;
207 MaxCorner = box.Center + box.Extent;
208 }
209 void Init_Empty(void);
210 void Add_Point(const Vector3 & point)
211 {
212 MinCorner.Update_Min(point);
213 MaxCorner.Update_Max(point);
214 }
215 void Add_Box(const MinMaxAABoxClass & box)
216 {
217 if (box.MinCorner == box.MaxCorner) return;
218 MinCorner.Update_Min(box.MinCorner);
219 MaxCorner.Update_Max(box.MaxCorner);
220 }
221 void Add_Box(const AABoxClass & box)
222 {
223 if (box.Extent == Vector3(0.0f, 0.0f, 0.0f)) return;
224 MinCorner.Update_Min(box.Center - box.Extent);
225 MaxCorner.Update_Max(box.Center + box.Extent);
226 }
227 void Add_Box(const Vector3 & min_corner,const Vector3 & max_corner)
228 {
229 if (min_corner == max_corner) return;
230 MinCorner.Update_Min(min_corner);
231 MaxCorner.Update_Max(max_corner);
232 }
233 void Transform(const Matrix3D & tm)
234 {
235 Vector3 oldmin = MinCorner;
236 Vector3 oldmax = MaxCorner;
237 tm.Transform_Min_Max_AABox(oldmin,oldmax,&MinCorner,&MaxCorner);
238 }
239 void Translate(const Vector3 & pos)
240 {
241 MinCorner+=pos;
242 MaxCorner+=pos;
243 }
244 inline float Volume(void) const
245 {
246 Vector3 size = MaxCorner - MinCorner; return size.X*size.Y*size.Z;
247 }
248 Vector3 MinCorner;
249 Vector3 MaxCorner;
250};
251inline void AABoxClass::Init(const MinMaxAABoxClass & mmbox)
252{
253 Center = (mmbox.MaxCorner + mmbox.MinCorner) * 0.5f;
254 Extent = (mmbox.MaxCorner - mmbox.MinCorner) * 0.5f;
255}
256inline void AABoxClass::Add_Box(const MinMaxAABoxClass & b)
257{
258 Vector3 newmin = Center - Extent;
259 Vector3 newmax = Center + Extent;
260 newmin.Update_Min(b.MinCorner);
261 newmax.Update_Max(b.MaxCorner);
262 Center = (newmax + newmin) * 0.5f;
263 Extent = (newmax - newmin) * 0.5f;
264}
265inline bool AABoxClass::Contains(const MinMaxAABoxClass & other_box) const
266{
267 Vector3 bmin = Center - Extent;
268 Vector3 bmax = Center + Extent;
269 if (other_box.MinCorner.X < bmin.X) return false;
270 if (other_box.MinCorner.Y < bmin.Y) return false;
271 if (other_box.MinCorner.Z < bmin.Z) return false;
272 if (other_box.MaxCorner.X > bmax.X) return false;
273 if (other_box.MaxCorner.Y > bmax.Y) return false;
274 if (other_box.MaxCorner.Z > bmax.Z) return false;
275 return true;
276}
277inline void MinMaxAABoxClass::Init_Empty()
278{
279 MinCorner = Vector3(FLT_MAX,FLT_MAX,FLT_MAX);
280 MaxCorner = Vector3(-FLT_MAX,-FLT_MAX,-FLT_MAX);
281}
282inline void AABoxClass::Transform(const Matrix3D & tm,const AABoxClass & in,AABoxClass * out)
283{
284 tm.Transform_Center_Extent_AABox(in.Center,in.Extent,&out->Center,&out->Extent);
285}
286#endif