gwnavruntime/math/box3f.h Source File

box3f.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 // primary contact: GUAL - secondary contact: NOBODY
9 #ifndef Navigation_Box3f_H
10 #define Navigation_Box3f_H
11 
14 
15 
16 namespace Kaim
17 {
18 
19 
24 class Box3f
25 {
26  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
27 
28 public:
29  // ---------------------------------- Constructors ----------------------------------
30 
32  Box3f() { Clear(); }
33 
37  Box3f(const Vec3f& min_, const Vec3f& max_) :
38  m_min(min_), m_max(max_)
39  {}
40 
41 
42  // ---------------------------------- Main API Functions ----------------------------------
43 
44  bool operator==(const Box3f& other) const { return m_min == other.m_min && m_max == other.m_max; }
45 
46  bool operator!=(const Box3f& other) const { return !operator==(other); }
47 
48  bool IsValid() { return m_min <= m_max; }
49 
52  KY_INLINE void Clear()
53  {
56  }
57 
61  KY_INLINE void Set(const Vec3f& min_, const Vec3f& max_)
62  {
63  m_min = min_;
64  m_max = max_;
65  }
66 
68  KY_INLINE KyFloat32 SizeX() const { return m_max.x - m_min.x; }
69 
71  KY_INLINE KyFloat32 SizeY() const { return m_max.y - m_min.y; }
72 
74  KY_INLINE KyFloat32 SizeZ() const { return m_max.z - m_min.z; }
75 
77  KY_INLINE Vec3f Center() const { return ((m_min + m_max) * 0.5f); }
78 
80  KY_INLINE KyFloat32 Radius() const
81  {
82  return sqrt( (SizeX()*SizeX()) + (SizeY()*SizeY()) + (SizeZ()*SizeZ()) );
83  }
84 
86  KY_INLINE void Translate(const Vec3f& v)
87  {
88  m_min += v;
89  m_max += v;
90  }
91 
95  void ExpandByVec3(const Vec3f& pos)
96  {
97  const KyFloat32 posx = pos.x;
98  const KyFloat32 posy = pos.y;
99  const KyFloat32 posz = pos.z;
100  m_min.x = Kaim::Min(m_min.x, posx);
101  m_min.y = Kaim::Min(m_min.y, posy);
102  m_min.z = Kaim::Min(m_min.z, posz);
103  m_max.x = Kaim::Max(m_max.x, posx);
104  m_max.y = Kaim::Max(m_max.y, posy);
105  m_max.z = Kaim::Max(m_max.z, posz);
106  }
107 
110  void ExpandByBox3(const Box3f& box)
111  {
112  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
113  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
114  m_min.z = Kaim::Min(m_min.z, box.m_min.z);
115  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
116  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
117  m_max.z = Kaim::Max(m_max.z, box.m_max.z);
118  }
119 
121  void Enlarge(KyFloat32 enlargement)
122  {
123  m_min.x -= enlargement;
124  m_max.x += enlargement;
125  m_min.y -= enlargement;
126  m_max.y += enlargement;
127  m_min.z -= enlargement;
128  m_max.z += enlargement;
129  }
130 
132  KY_INLINE bool IsPoint3DInside(const Vec3f& p) const
133  {
134  const KyFloat32 operand1 = Fsel(p.x - m_min.x, 1.f, 0.f);
135  const KyFloat32 operand2 = Fsel(m_max.x - p.x, 1.f, 0.f);
136  const KyFloat32 operand3 = Fsel(p.y - m_min.y, 1.f, 0.f);
137  const KyFloat32 operand4 = Fsel(m_max.y - p.y, 1.f, 0.f);
138  const KyFloat32 operand5 = Fsel(p.z - m_min.z, 1.f, 0.f);
139  const KyFloat32 operand6 = Fsel(m_max.z - p.z, 1.f, 0.f);
140 
141  return operand1 * operand2 * operand3 * operand4 * operand5 * operand6 > 0.f;
142  }
143 
146  KY_INLINE bool IsPoint2DInside(const Vec2f& p) const
147  {
148  const KyFloat32 operand1 = Fsel(p.x - m_min.x, 1.f, 0.f);
149  const KyFloat32 operand2 = Fsel(m_max.x - p.x, 1.f, 0.f);
150  const KyFloat32 operand3 = Fsel(p.y - m_min.y, 1.f, 0.f);
151  const KyFloat32 operand4 = Fsel(m_max.y - p.y, 1.f, 0.f);
152 
153  return operand1 * operand2 * operand3 * operand4 > 0.f;
154  }
155 
157  // ---------------------------------- Public Data Members ----------------------------------
158 
159  Vec3f m_min;
160  Vec3f m_max;
161 };
162 
165 inline void SwapEndianness(Endianness::Target e, Box3f& self)
166 {
167  SwapEndianness(e, self.m_min);
168  SwapEndianness(e, self.m_max);
169 }
170 
171 }
172 
174 #endif
175 
bool IsPoint3DInside(const Vec3f &p) const
Returns true if the specified position is contained within the extents of the bounding box...
Definition: box3f.h:142
#define KyFloat32MAXVAL
The maximum value that can be stored in the KyFloat32 variable type.
Definition: types.h:227
KyFloat32 z
The size of the vector along the Z axis.
Definition: vec3f.h:229
bool IsPoint2DInside(const Vec2f &p) const
Returns true if the (X,Y) coordinates of the specified position are contained within the (X...
Definition: box3f.h:156
void Set(KyFloat32 _x, KyFloat32 _y, KyFloat32 _z)
Sets the coordinates.
Definition: vec3f.h:52
KyFloat32 x
The size of the vector along the X axis.
Definition: vec2f.h:169
Vec3f Center() const
Retrieves the coordinates of the exact center of the box.
Definition: box3f.h:87
Vec3f m_max
The maxima of the bounding box.
Definition: box3f.h:174
void ExpandByBox3(const Box3f &box)
Enlarges the extents of the bounding box to include the area covered by the specified bounding box...
Definition: box3f.h:120
T Min(const T &a, const T &b)
Returns the lesser of the two specified values.
Definition: fastmath.h:113
void Clear()
Clears all information maintained by this object.
Definition: box3f.h:62
KyFloat32 SizeZ() const
Retrieves the extents of the box along the Z axis.
Definition: box3f.h:84
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec3f.h:228
Box3f()
Creates a new Box3f with invalid extents: you must call Set() before using it.
Definition: box3f.h:38
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:35
This class represents a three-dimensional axis-aligned bounding box whose dimensions are stored using...
Definition: box3f.h:25
KyFloat32 x
The size of the vector along the X axis.
Definition: vec3f.h:227
This class defines a two-dimensional vector whose coordinates are stored using floating-point numbers...
Definition: vec2f.h:24
void Translate(const Vec3f &v)
Moves the bounding box by the specified vector.
Definition: box3f.h:96
T Max(const T &a, const T &b)
Returns the greater of the two specified values.
Definition: fastmath.h:121
KyFloat32 Radius() const
Retrieves the distance between the diagonally opposite corners of the box.
Definition: box3f.h:90
KyFloat32 SizeY() const
Retrieves the extents of the box along the Y axis.
Definition: box3f.h:81
Definition: gamekitcrowddispersion.h:20
void Enlarge(KyFloat32 enlargement)
Enlarges the extents of the bounding box by the specified amount in all directions.
Definition: box3f.h:131
void Set(const Vec3f &min_, const Vec3f &max_)
Sets the extents of the bounding box to the specified values.
Definition: box3f.h:71
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
KyFloat32 SizeX() const
Retrieves the extents of the box along the X axis.
Definition: box3f.h:78
Vec3f m_min
The minima of the bounding box.
Definition: box3f.h:173
void ExpandByVec3(const Vec3f &pos)
Enlarges the extents of the bounding box to include the specified three-dimensional point...
Definition: box3f.h:105
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43
This class defines a three-dimensional vector whose coordinates are stored using floating-point numbe...
Definition: vec3f.h:23
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec2f.h:170
KyFloat32 Fsel(KyFloat32 cmp, KyFloat32 v1, KyFloat32 v2)
Ifcmp is greater than 0, returnsv1. Otherwise, returnsv2.
Definition: fastmath.h:58