12#ifndef TT_INCLUDE__AABOXCLASS_H
13#define TT_INCLUDE__AABOXCLASS_H
18#include "LineSegClass.h"
19#include "engine_math.h"
20#include "CastResultStruct.h"
21#include "FrustumClass.h"
31class MinMaxAABoxClass;
36 inline AABoxClass(
void)
39 inline AABoxClass(
const Vector3 & center,
const Vector3 & extent) : Center(center), Extent(extent)
42 AABoxClass(
const MinMaxAABoxClass & minmaxbox)
46 AABoxClass(Vector3 * points,
int num)
50 bool operator== (
const AABoxClass &src)
52 return (Center == src.Center) && (Extent == src.Extent);
54 bool operator!= (
const AABoxClass &src)
56 return (Center != src.Center) || (Extent != src.Extent);
58 inline void Init(
const Vector3& center,
const Vector3 & extent)
60 Center = center; Extent = extent;
62 inline void Init(Vector3 * points,
int num)
64 Vector3 Min = points[0];
65 Vector3 Max = points[0];
66 for (
int i=1; i<num; i++)
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;
75 Center = (Max + Min) * 0.5f;
76 Extent = (Max - Min) * 0.5f;
78 inline void Init(
const MinMaxAABoxClass & mmbox);
79 void Init(
const LineSegClass & line)
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;
92 void Init_Min_Max(
const Vector3 & min,
const Vector3 & max)
94 Center = (max + min) * 0.5f;
95 Extent = (max - min) * 0.5f;
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)
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;
111 void Add_Box(
const AABoxClass & b)
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;
120 void Add_Box(
const MinMaxAABoxClass & b);
121 float Project_To_Axis(
const Vector3 & axis)
const
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));
128 void Transform(
const Matrix3D & tm)
130 Vector3 oldcenter = Center;
131 Vector3 oldextent = Extent;
132 tm.Transform_Center_Extent_AABox(oldcenter,oldextent,&Center,&Extent);
134 void Translate(
const Vector3 & pos)
138 inline float Volume(
void)
const
140 return 2.0f*Extent.X * 2.0f*Extent.Y * 2.0f*Extent.Z;
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);
149 float ComputeSquaredDistanceFromPoint(
const Vector3& point)
const
151 Vector3 min = Center - Extent;
152 Vector3 max = Center + Extent;
154 float sq_dist = 0.0f;
155 for (
int i = 0; i < 3; ++i)
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]);
164 float ComputeMaxSquaredDistanceFromPoint(
const Vector3& point)
const
166 const Vector3 d = Center - point;
167 const Vector3 dmin = d - Extent;
168 const Vector3 dmax = d + Extent;
170 max(sqr(dmin[0]), sqr(dmax[0])) +
171 max(sqr(dmin[1]), sqr(dmax[1])) +
172 max(sqr(dmin[2]), sqr(dmax[2]));
177class MinMaxAABoxClass
180 inline MinMaxAABoxClass(
void)
183 inline MinMaxAABoxClass(
const Vector3 & min_corner,
const Vector3 & max_corner) : MinCorner(min_corner),MaxCorner(max_corner)
186 inline MinMaxAABoxClass(Vector3 * points,
int num)
190 inline MinMaxAABoxClass(
const AABoxClass & that)
194 inline void Init(Vector3 * points,
int num)
196 MinCorner = points[0];
197 MaxCorner = points[0];
198 for (
int i=0; i<num; i++)
200 MinCorner.Update_Min(points[i]);
201 MaxCorner.Update_Max(points[i]);
204 inline void Init(
const AABoxClass & box)
206 MinCorner = box.Center - box.Extent;
207 MaxCorner = box.Center + box.Extent;
209 void Init_Empty(
void);
210 void Add_Point(
const Vector3 & point)
212 MinCorner.Update_Min(point);
213 MaxCorner.Update_Max(point);
215 void Add_Box(
const MinMaxAABoxClass & box)
217 if (box.MinCorner == box.MaxCorner)
return;
218 MinCorner.Update_Min(box.MinCorner);
219 MaxCorner.Update_Max(box.MaxCorner);
221 void Add_Box(
const AABoxClass & box)
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);
227 void Add_Box(
const Vector3 & min_corner,
const Vector3 & max_corner)
229 if (min_corner == max_corner)
return;
230 MinCorner.Update_Min(min_corner);
231 MaxCorner.Update_Max(max_corner);
233 void Transform(
const Matrix3D & tm)
235 Vector3 oldmin = MinCorner;
236 Vector3 oldmax = MaxCorner;
237 tm.Transform_Min_Max_AABox(oldmin,oldmax,&MinCorner,&MaxCorner);
239 void Translate(
const Vector3 & pos)
244 inline float Volume(
void)
const
246 Vector3 size = MaxCorner - MinCorner;
return size.X*size.Y*size.Z;
251inline void AABoxClass::Init(
const MinMaxAABoxClass & mmbox)
253 Center = (mmbox.MaxCorner + mmbox.MinCorner) * 0.5f;
254 Extent = (mmbox.MaxCorner - mmbox.MinCorner) * 0.5f;
256inline void AABoxClass::Add_Box(
const MinMaxAABoxClass & b)
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;
265inline bool AABoxClass::Contains(
const MinMaxAABoxClass & other_box)
const
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;
277inline void MinMaxAABoxClass::Init_Empty()
279 MinCorner = Vector3(FLT_MAX,FLT_MAX,FLT_MAX);
280 MaxCorner = Vector3(-FLT_MAX,-FLT_MAX,-FLT_MAX);
282inline void AABoxClass::Transform(
const Matrix3D & tm,
const AABoxClass & in,AABoxClass * out)
284 tm.Transform_Center_Extent_AABox(in.Center,in.Extent,&out->Center,&out->Extent);