gwnavruntime/math/box2i.h Source File

box2i.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_Box2i_H
10 #define Navigation_Box2i_H
11 
14 
15 
16 namespace Kaim
17 {
18 
19 /*
20 ----------------- CoordBox -----------------
21 minX=2 , maxX=8 , countX=7 (=8-2 +1)
22 minY=3 , maxY=6 , countY=4 (=6-3 +1)
23  /\Y
24  | | | | | | | | | |
25 7+---+---+---+---+---+---+---+---+---+--
26  | | | | | | | | | |
27 6+---+---#########################---+--
28  | | # | | | | | # |
29 5+---+---#---+---+---+---+---+---#---+--
30  | | # | | | | | # |
31 4+---+---#---+---+---+---+---+---#---+--
32  | | # | | | | | # |
33 3+---+---#########################---+--
34  | | | | | | | | | |
35 2+---+---+---+---+---+---+---+---+---+--
36  | | | | | | | | | |
37 1+---+---+---+---+---+---+---+---+---+--
38  | | | | | | | | | |
39 0+---+---+---+---+---+---+---+---+---+-> X
40  0 1 2 3 4 5 6 7 8 9
41 
42 --------- PixelBox, CellBox, TileBox ---------
43 minX=2 , maxX=7 , countX=6 (=7-2 +1)
44 minY=3 , maxY=5 , countY=3 (=5-3 +1)
45 /\Y
46 | | | | | | | | | |
47 +---+---+---+---+---+---+---+---+---+--
48 |0,6|1,6|2,6| | | | | | |
49 +---+---#########################---+--
50 |0,5|1,5#2,5|3,5|4,5|5,5|6,5|7,5# |
51 +---+---#---+---+---+---+---+---#---+--
52 |0,4|1,4#2,4|3,4|4,4|5,4|6,4|7,4# |
53 +---+---#---+---+---+---+---+---#---+--
54 |0,3|1,3#2,3|3,3|4,3|5,3|6,3|7,3# |
55 +---+---#########################---+--
56 |0,2|1,2|2,2| | | | | | |
57 +---+---+---+---+---+---+---+---+---+--
58 |0,1|1,1|2,1| | | | | | |
59 +---+---+---+---+---+---+---+---+---+--
60 |0,0|1,0|2,0| | | | | | |
61 +---+---+---+---+---+---+---+---+---+-> X
62 */
63 
118 class Box2i
119 {
120  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
121 
122 public:
123  // ---------------------------------- Constructors ----------------------------------
124 
126  Box2i() { Clear(); }
127 
131  Box2i(const Vec2i& min_, const Vec2i& max_) : m_min(min_), m_max(max_) { UpdateCountXY(); }
138  Box2i(KyInt32 min_x, KyInt32 min_y, KyInt32 max_x, KyInt32 max_y) : m_min(min_x, min_y), m_max(max_x, max_y) { UpdateCountXY(); }
139 
140 
141  // ---------------------------------- Main API Functions ----------------------------------
142 
144  KY_INLINE bool IsValid() const { return CountX() > 0 && CountY() > 0; }
145 
147  KY_INLINE void MakeZero() { m_min.Set(0, 0); m_max.Set(0, 0); UpdateCountXY(); }
148 
152  KY_INLINE void Set(const Vec2i& min_, const Vec2i& max_) { m_min = min_; m_max = max_; UpdateCountXY(); }
153 
154  KY_INLINE void Set(KyInt32 min_x, KyInt32 min_y, KyInt32 max_x, KyInt32 max_y) { m_min.x = min_x; m_min.y = min_y; m_max.x = max_x; m_max.y = max_y; UpdateCountXY(); }
155 
159  KY_INLINE void SetSafe(const Vec2i& min_, const Vec2i& max_) { m_min = min_; m_max = max_; UpdateCountXYSafe(); }
160 
162  KY_INLINE void SetMin(const Vec2i& min_) { m_min = min_; UpdateCountXY(); }
163 
165  KY_INLINE void SetMax(const Vec2i& max_) { m_max = max_; UpdateCountXY(); }
166 
168  KY_INLINE const Vec2i& Min() const { return m_min; }
171  KY_INLINE const Vec2i& Max() const { return m_max; }
176  KY_INLINE Vec2i CornerSouthWest() const { return m_min; }
177 
181  KY_INLINE Vec2i CornerNorthEast() const { return m_max; }
182 
186  KY_INLINE Vec2i CornerSouthEast() const { return Vec2i(m_max.x, m_min.y); }
187 
191  KY_INLINE Vec2i CornerNorthWest() const { return Vec2i(m_min.x, m_max.y); }
192 
194  KY_INLINE const Vec2i& CountXY() const { return m_countXY; }
195 
197  KY_INLINE KyInt32 CountX() const { return m_countXY.x; }
198 
200  KY_INLINE KyInt32 CountY() const { return m_countXY.y; }
204  KY_INLINE bool IsInside(const Vec2i& pos) const { return pos.x >= m_min.x && pos.x <= m_max.x && pos.y >= m_min.y && pos.y <= m_max.y; }
205 
209  KY_INLINE bool FastIsInside(const Vec2i& pos) const
210  {
211  const KyInt32 operand1 = Isel(pos.x - m_min.x, 1, 0); // operand1 == 1.f if pos.x >= cellBox.m_min.x
212  const KyInt32 operand2 = Isel(m_max.x - pos.x, 1, 0); // operand2 == 1.f if cellBox.m_max.x >= pos.x
213  const KyInt32 operand3 = Isel(pos.y - m_min.y, 1, 0); // operand3 == 1.f if pos.y >= cellBox.m_min.y
214  const KyInt32 operand4 = Isel(m_max.y - pos.y, 1, 0); // operand4 == 1.f if cellBox.m_max.y >= pos.y
215 
216  return (operand1 & operand2 & operand3 & operand4) != 0;
217  }
218 
220  KY_INLINE bool IsInsideStrictly(const Vec2i& pos) const { return pos.x > m_min.x && pos.x < m_max.x && pos.y > m_min.y && pos.y < m_max.y; }
221 
224  void Clear()
225  {
227  m_max.Set(-KyInt32MAXVAL, -KyInt32MAXVAL);
228  ClearCountXY();
229  }
230 
232  KY_INLINE Box2i& operator*=(KyInt32 s) { m_min *= s; m_max *= s; UpdateCountXY(); return *this; }
233 
235  KY_INLINE Box2i& operator/=(KyInt32 s) { m_min /= s; m_max /= s; UpdateCountXY(); return *this; }
236 
238  KY_INLINE Box2i& operator+=(const Vec2i& v) { m_min += v; m_max += v; return *this; }
239 
241  KY_INLINE Box2i& operator-=(const Vec2i& v) { m_min -= v; m_max -= v; return *this; }
242 
244  KY_INLINE bool operator==(const Box2i& other) const { return other.m_min == m_min && other.m_max == m_max; }
245  KY_INLINE bool operator!=(const Box2i& other) const { return !(*this == other); }
247 
265  KY_INLINE KyInt32 GetColumnMajorIndex(const Vec2i& pos) const { return (pos.x - m_min.x) * CountY() + pos.y - m_min.y; }
266 
284  KY_INLINE KyInt32 GetRowMajorIndex(const Vec2i& pos) const { return (pos.y - m_min.y) * CountX() + pos.x - m_min.x; }
285 
289  KY_INLINE KyInt32 GetColumnMajorIndexFromLocalPos(const Vec2i& localPos) const { return localPos.x * CountY() + localPos.y; }
290 
294  KY_INLINE KyInt32 GetRowMajorIndexFromLocalPos(const Vec2i& localPos) const { return localPos.y * CountX() + localPos.x; }
295 
298  void ExpandByVec2(const Vec2i& pos)
299  {
300  ExpandByVec2_MinMaxOnly(pos);
301  UpdateCountXY();
302  }
303 
307  void ExpandByVec3(const Vec3i& pos)
308  {
309  ExpandByVec3_MinMaxOnly(pos);
310  UpdateCountXY();
311  }
312 
316  void ExpandByTriangle(const Triangle3i& triangle)
317  {
318  ExpandByVec3_MinMaxOnly(triangle.A);
319  ExpandByVec3_MinMaxOnly(triangle.B);
320  ExpandByVec3_MinMaxOnly(triangle.C);
321  UpdateCountXY();
322  }
323 
326  void ExpandByBox2(const Box2i& box)
327  {
328  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
329  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
330  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
331  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
332  UpdateCountXY();
333  }
336  void Enlarge(KyInt32 enlargement)
337  {
338  m_min -= enlargement;
339  m_max += enlargement;
340  UpdateCountXY();
341  }
342 
347  void GetEnlarged(KyInt32 enlargement, Box2i& enlarged) const
348  {
349  enlarged = *this;
350  enlarged.Enlarge(enlargement);
351  }
352 
366  KY_INLINE bool IntersectWith(const Box2i& box) { return this->SetAsIntersection(*this, box); }
367 
379  bool SetAsIntersection(const Box2i& box_1, const Box2i& box_2)
380  {
381  m_min.x = Kaim::Max(box_1.m_min.x, box_2.m_min.x);
382  m_max.x = Kaim::Min(box_1.m_max.x, box_2.m_max.x);
383  m_min.y = Kaim::Max(box_1.m_min.y, box_2.m_min.y);
384  m_max.y = Kaim::Min(box_1.m_max.y, box_2.m_max.y);
385  if (m_min.x <= m_max.x && m_min.y <= m_max.y)
386  {
387  UpdateCountXY();
388  return true;
389  }
390 
391  Clear();
392  return false;
393  }
394 
395 private:
396  KY_INLINE void UpdateCountX() { m_countXY.x = m_max.x - m_min.x + 1; }
397  KY_INLINE void UpdateCountY() { m_countXY.y = m_max.y - m_min.y + 1; }
398  KY_INLINE void UpdateCountXY() { UpdateCountX(); UpdateCountY(); }
399  KY_INLINE void ClearCountXY() { m_countXY.x = -1; m_countXY.y = -1; }
401  KY_INLINE void UpdateCountXYSafe()
402  {
403  KyInt64 countX = (KyInt64)m_max.x - (KyInt64)m_min.x + 1;
404  KyInt64 countY = (KyInt64)m_max.y - (KyInt64)m_min.y + 1;
405  if (countX < 0 || countY < 0 || countX > KyInt32MAXVAL || countY > KyInt32MAXVAL)
406  {
407  Clear();
408  return;
409  }
410 
411  m_countXY.x = (KyInt32)countX;
412  m_countXY.y = (KyInt32)countY;
413  }
414 
415  void ExpandByVec2_MinMaxOnly(const Vec2i& pos)
416  {
417  const KyInt32 posx = pos.x;
418  const KyInt32 posy = pos.y;
419  m_min.x = Kaim::Min(m_min.x, posx);
420  m_max.x = Kaim::Max(m_max.x, posx);
421  m_min.y = Kaim::Min(m_min.y, posy);
422  m_max.y = Kaim::Max(m_max.y, posy);
423  }
424 
425  void ExpandByVec3_MinMaxOnly(const Vec3i& pos)
426  {
427  const KyInt32 posx = pos.x;
428  const KyInt32 posy = pos.y;
429  m_min.x = Kaim::Min(m_min.x, posx);
430  m_max.x = Kaim::Max(m_max.x, posx);
431  m_min.y = Kaim::Min(m_min.y, posy);
432  m_max.y = Kaim::Max(m_max.y, posy);
433  }
434 
435 public:
436  Vec2i m_min;
437  Vec2i m_max;
438 private:
439  Vec2i m_countXY;
440 
441 // friends declarations
442  friend void SwapEndianness(Kaim::Endianness::Target e, Box2i& self);
443  friend class GridUtils;
444 };
445 
448 inline void SwapEndianness(Kaim::Endianness::Target e, Box2i& self)
449 {
450  SwapEndianness(e, self.m_min);
451  SwapEndianness(e, self.m_max);
452  SwapEndianness(e, self.m_countXY);
453 }
454 
455 
456 template <class OSTREAM>
457 inline OSTREAM& operator<<(OSTREAM& os, const Kaim::Box2i& v)
458 {
459  os << "{" << v.Min() << ";" << v.Max() << "}";
460  return os;
461 }
462 
463 
464 } // namespace Kaim
465 
466 
467 #endif
468 
void MakeZero()
Sets both the minima and maxima of the bounding box to (0,0).
Definition: box2i.h:157
Box2i & operator+=(const Vec2i &v)
Adds the specified two-dimensional vector to both the minima and maxima of the box.
Definition: box2i.h:249
const Vec2i & Max() const
Retrieves the maxima of the bounding box.
Definition: box2i.h:181
Vec3i A
The position of the first corner of the triangle.
Definition: triangle3i.h:65
void SwapEndianness(Kaim::Endianness::Target e, Box2i &self)
Swaps the endianness of the data in the specified object.
Definition: box2i.h:473
Box2i & operator-=(const Vec2i &v)
Subtracts the specified two-dimensional vector from both the minima and maxima of the box...
Definition: box2i.h:252
This class represents a two-dimensional axis-aligned bounding box whose dimensions are stored using 3...
Definition: box2i.h:119
void SetSafe(const Vec2i &min_, const Vec2i &max_)
Sets the extents of the bounding box to the specified values.
Definition: box2i.h:169
KyInt32 GetColumnMajorIndex(const Vec2i &pos) const
Retrieves the index of the grid cell at the specified (X,Y) position, counting cells column-by-column...
Definition: box2i.h:277
KyInt32 Isel(KyInt32 a, KyInt32 x, KyInt32 y)
If a is greater than 0, returns x. Otherwise, returns y.
Definition: fastmath.h:70
void ExpandByTriangle(const Triangle3i &triangle)
Enlarges the extents of the bounding box to include the (X,Y) extents of the specified three-dimensio...
Definition: box2i.h:334
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
void Set(KyInt32 _x, KyInt32 _y)
Sets the coordinates of the vector to match the specified values.
Definition: vec2i.h:54
T Min(const T &a, const T &b)
Returns the lesser of the two specified values.
Definition: fastmath.h:113
KyInt32 x
The size of the vector along the X axis.
Definition: vec2i.h:283
Vec2i CornerNorthEast() const
Retrieves the coordinates of the North-East corner of the bounding box.
Definition: box2i.h:191
Box2i()
Creates a new Box2i with invalid extents: you must call Set() before using it.
Definition: box2i.h:132
bool IntersectWith(const Box2i &box)
Resizes this bounding box to contain only the grid cells that are common to both it and box...
Definition: box2i.h:386
KyInt32 GetColumnMajorIndexFromLocalPos(const Vec2i &localPos) const
Retrieves the index of the grid cell at the specified (X,Y) position, counting cells column-by-column...
Definition: box2i.h:304
void GetEnlarged(KyInt32 enlargement, Box2i &enlarged) const
Enlarges the extents of the bounding box by the specified amount in all directions, and stores the new dimensions in the enlarged parameter.
Definition: box2i.h:366
Vec2i CornerSouthWest() const
Retrieves the coordinates of the South-West corner of the bounding box.
Definition: box2i.h:186
KyInt32 y
The size of the vector along the Y axis.
Definition: vec2i.h:284
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:35
KyInt32 GetRowMajorIndex(const Vec2i &pos) const
Retrieves the index of the grid cell at the specified (X,Y) position, counting cells row-by-row...
Definition: box2i.h:297
#define KyInt32MAXVAL
The maximum value that can be stored in the KyInt32 variable type.
Definition: types.h:224
bool SetAsIntersection(const Box2i &box_1, const Box2i &box_2)
Resizes this bounding box to contain only the grid cells that are common to both box_1 and box_2...
Definition: box2i.h:400
bool IsInsideStrictly(const Vec2i &pos) const
Returns true if the specified position is contained entirely within the extents of the bounding box...
Definition: box2i.h:231
void SetMax(const Vec2i &max_)
Sets the maxima of the bounding box to the specified coordinates.
Definition: box2i.h:175
Vec2i CornerSouthEast() const
Retrieves the coordinates of the South-East corner of the bounding box.
Definition: box2i.h:196
T Max(const T &a, const T &b)
Returns the greater of the two specified values.
Definition: fastmath.h:121
This class defines a two-dimensional vector whose coordinates are stored using 32-bit integers...
Definition: vec2i.h:26
Definition: gamekitcrowddispersion.h:20
void ExpandByVec3(const Vec3i &pos)
Enlarges the extents of the bounding box to include the (X,Y) coordinates of the specified three-dime...
Definition: box2i.h:325
Vec3i B
The position of the second corner of the triangle.
Definition: triangle3i.h:66
KyInt32 CountY() const
Retrieves the number of grid cells contained within this box along its Y axis.
Definition: box2i.h:210
Vec2i CornerNorthWest() const
Retrieves the coordinates of the North-West corner of the bounding box.
Definition: box2i.h:201
bool FastIsInside(const Vec2i &pos) const
Returns true if the specified position is contained within the extents of the bounding box or if the ...
Definition: box2i.h:220
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
Box2i & operator*=(KyInt32 s)
Multiplies both the minima and maxima of the box by the specified value.
Definition: box2i.h:243
void Set(const Vec2i &min_, const Vec2i &max_)
Sets the extents of the bounding box to the specified values.
Definition: box2i.h:162
const Vec2i & Min() const
Retrieves the minima of the bounding box.
Definition: box2i.h:178
KyInt32 GetRowMajorIndexFromLocalPos(const Vec2i &localPos) const
Retrieves the index of the grid cell at the specified (X,Y) position, counting cells row-by-row...
Definition: box2i.h:311
const Vec2i & CountXY() const
Retrieves the number of grid cells contained within this box along its X and Y axes.
Definition: box2i.h:204
void SetMin(const Vec2i &min_)
Sets the minima of the bounding box to the specified coordinates.
Definition: box2i.h:172
bool IsInside(const Vec2i &pos) const
Returns true if the specified position is contained within the extents of the bounding box or if the ...
Definition: box2i.h:214
friend void SwapEndianness(Kaim::Endianness::Target e, Box2i &self)
Swaps the endianness of the data in the specified object.
Definition: box2i.h:473
void Enlarge(KyInt32 enlargement)
Enlarges the extents of the bounding box by the specified amount in all directions.
Definition: box2i.h:355
KyInt32 CountX() const
Retrieves the number of grid cells contained within this box along its X axis.
Definition: box2i.h:207
void ExpandByBox2(const Box2i &box)
Enlarges the extents of the bounding box to include the area covered by the specified bounding box...
Definition: box2i.h:345
Box2i & operator/=(KyInt32 s)
Divides both the minima and maxima of the box by the specified value.
Definition: box2i.h:246
void ExpandByVec2(const Vec2i &pos)
Enlarges the extents of the bounding box to include the specified two-dimensional point...
Definition: box2i.h:316
This class represents a triangle in three-dimensional space, whose dimensions are stored using intege...
Definition: triangle3i.h:23
bool IsValid() const
Indicates whether or not the extents of the bounding box are valid.
Definition: box2i.h:154
void Clear()
Clears all information maintained by this object.
Definition: box2i.h:235
bool operator==(const Box2i &other) const
Check equality.
Definition: box2i.h:255
Vec3i C
The position of the third corner of the triangle.
Definition: triangle3i.h:67
__int64 KyInt64
Type used internally to represent a 64-bit integer.
Definition: types.h:37