gwnavruntime/math/box2f.h Source File

box2f.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_Box2f_H
10 #define Navigation_Box2f_H
11 
13 
14 namespace Kaim
15 {
16 
19 class Box2f
20 {
21  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
22 
23 public:
24  // ---------------------------------- Constructors ----------------------------------
25 
27  Box2f() { Clear(); }
28 
32  Box2f(const Vec2f& _min, const Vec2f& _max) :
33  m_min(_min), m_max(_max) {}
34 
40  Box2f(KyFloat32 min_x, KyFloat32 min_y, KyFloat32 max_x, KyFloat32 max_y) :
41  m_min(min_x, min_y), m_max(max_x, max_y) {}
42 
43 
44  // ---------------------------------- Main API Functions ----------------------------------
45 
47  bool operator==(const Box2f& other) const { return m_min == other.m_min && m_max == other.m_max; }
48 
50  bool operator!=(const Box2f& other) const { return !operator==(other); }
51 
54  KY_INLINE void Clear()
55  {
58  }
59 
61  KY_INLINE bool IsValid()
62  {
63  return m_min.x <= m_max.x && m_min.y <= m_max.y;
64  }
65 
69  KY_INLINE void Set(const Vec2f& min_, const Vec2f& max_)
70  {
71  m_min = min_;
72  m_max = max_;
73  }
74 
75  KY_INLINE void Set(KyFloat32 min_x, KyFloat32 min_y, KyFloat32 max_x, KyFloat32 max_y)
76  {
77  m_min.x = min_x;
78  m_min.y = min_y;
79  m_max.x = max_x;
80  m_max.y = max_y;
81  }
82 
84  KY_INLINE KyFloat32 SizeX() { return m_max.x - m_min.x; }
85 
87  KY_INLINE KyFloat32 SizeY() { return m_max.y - m_min.y; }
88 
91  void ExpandByVec2(const Vec2f& pos)
92  {
93  const KyFloat32 posx = pos.x;
94  const KyFloat32 posy = pos.y;
95  m_min.x = Kaim::Min(m_min.x, posx);
96  m_min.y = Kaim::Min(m_min.y, posy);
97  m_max.x = Kaim::Max(m_max.x, posx);
98  m_max.y = Kaim::Max(m_max.y, posy);
99  }
104  void ExpandByVec3(const Vec3f& pos)
105  {
106  const KyFloat32 posx = pos.x;
107  const KyFloat32 posy = pos.y;
108  m_min.x = Kaim::Min(m_min.x, posx);
109  m_min.y = Kaim::Min(m_min.y, posy);
110  m_max.x = Kaim::Max(m_max.x, posx);
111  m_max.y = Kaim::Max(m_max.y, posy);
112  }
116  void ExpandByBox2(const Box2f& box)
117  {
118  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
119  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
120  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
121  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
122  }
123 
127  void ExpandByBox3(const Box3f& box)
128  {
129  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
130  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
131  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
132  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
133  }
134 
136  void Enlarge(KyFloat32 enlargement)
137  {
138  m_min.x -= enlargement;
139  m_max.x += enlargement;
140  m_min.y -= enlargement;
141  m_max.y += enlargement;
142  }
143 
145  KY_INLINE bool IsPoint2DInside(const Vec2f& p) const {
146  const KyFloat32 operand1 = Fsel(p.x - m_min.x, 1.f, 0.f); // operand1 == 1.f if pos.x >= cellBox.m_min.x
147  const KyFloat32 operand2 = Fsel(m_max.x - p.x, 1.f, 0.f); // operand2 == 1.f if cellBox.m_max.x >= pos.x
148  const KyFloat32 operand3 = Fsel(p.y - m_min.y, 1.f, 0.f); // operand3 == 1.f if pos.y >= cellBox.m_min.y
149  const KyFloat32 operand4 = Fsel(m_max.y - p.y, 1.f, 0.f); // operand4 == 1.f if cellBox.m_max.y >= pos.y
150  return operand1 * operand2 * operand3 * operand4 > 0.f;
151  }
152 
155  KY_INLINE bool IsPoint3DInside(const Vec3f& p) const
156  {
157  const KyFloat32 operand1 = Fsel(p.x - m_min.x, 1.f, 0.f); // operand1 == 1.f if pos.x >= cellBox.m_min.x
158  const KyFloat32 operand2 = Fsel(m_max.x - p.x, 1.f, 0.f); // operand2 == 1.f if cellBox.m_max.x >= pos.x
159  const KyFloat32 operand3 = Fsel(p.y - m_min.y, 1.f, 0.f); // operand3 == 1.f if pos.y >= cellBox.m_min.y
160  const KyFloat32 operand4 = Fsel(m_max.y - p.y, 1.f, 0.f); // operand4 == 1.f if cellBox.m_max.y >= pos.y
161  return operand1 * operand2 * operand3 * operand4 > 0.f;
162  }
163 
164 public:
165  Vec2f m_min; //< Stores the minima of the box.
166  Vec2f m_max; //< Stores the maxima of the box.
167 };
168 
171 inline void SwapEndianness(Endianness::Target e, Box2f& self)
172 {
173  SwapEndianness(e, self.m_min);
174  SwapEndianness(e, self.m_max);
175 }
176 
177 
178 }
179 
180 
181 #endif
void Enlarge(KyFloat32 enlargement)
Enlarges the extents of the bounding box by the specified amount in all directions.
Definition: box2f.h:145
void ExpandByVec2(const Vec2f &pos)
Enlarges the extents of the bounding box to include the specified two-dimensional point...
Definition: box2f.h:100
This class represents a two-dimensional axis-aligned bounding box whose dimensions are stored using f...
Definition: box2f.h:19
#define KyFloat32MAXVAL
The maximum value that can be stored in the KyFloat32 variable type.
Definition: types.h:227
bool IsPoint3DInside(const Vec3f &p) const
Indicates whether or not the (X,Y) coordinates of the specified three-dimensional point are inside th...
Definition: box2f.h:164
Box2f()
Creates a new Box2f with invalid extents: you must call Set() before using it.
Definition: box2f.h:32
KyFloat32 SizeY()
Retrieves the extents of the box along the Y axis.
Definition: box2f.h:96
KyFloat32 x
The size of the vector along the X axis.
Definition: vec2f.h:169
void Set(const Vec2f &min_, const Vec2f &max_)
Sets the extents of the bounding box to the specified values.
Definition: box2f.h:78
Vec3f m_max
The maxima of the bounding box.
Definition: box3f.h:174
void Set(KyFloat32 _x, KyFloat32 _y)
Sets the coordinates of the vector.
Definition: vec2f.h:47
T Min(const T &a, const T &b)
Returns the lesser of the two specified values.
Definition: fastmath.h:113
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec3f.h:228
bool IsValid()
Return true if m_min.x <= m_max.x="" &&="" m_min.y=""><= m_max.y.="">
Definition: box2f.h:70
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
void ExpandByBox3(const Box3f &box)
Enlarges the extents of the bounding box to include the area covered by the specified bounding box on...
Definition: box2f.h:136
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
bool IsPoint2DInside(const Vec2f &p) const
Indicates whether or not the specified two-dimensional point is inside the area covered by the boundi...
Definition: box2f.h:154
void ExpandByVec3(const Vec3f &pos)
Enlarges the extents of the bounding box to include the (X,Y) coordinates of the specified three-dime...
Definition: box2f.h:113
T Max(const T &a, const T &b)
Returns the greater of the two specified values.
Definition: fastmath.h:121
Definition: gamekitcrowddispersion.h:20
void Clear()
Clears all information maintained by this object.
Definition: box2f.h:63
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
void ExpandByBox2(const Box2f &box)
Enlarges the extents of the bounding box to include the area covered by the specified bounding box...
Definition: box2f.h:125
Vec3f m_min
The minima of the bounding box.
Definition: box3f.h:173
bool operator==(const Box2f &other) const
Returns true if both the minima and maxima of other are the same as the minima and maxima of this obj...
Definition: box2f.h:56
bool operator!=(const Box2f &other) const
Returns true if either the minima and maxima of other is different from the minima or maxima of this ...
Definition: box2f.h:59
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
KyFloat32 SizeX()
Retrieves the extents of the box along the X axis.
Definition: box2f.h:93