gwnavruntime/math/box2ll.h Source File

box2ll.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_Box2LL_H
10 #define Navigation_Box2LL_H
11 
14 
15 
16 namespace Kaim
17 {
18 
19 
24 class Box2LL
25 {
26  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
27 
28 public:
29  // ---------------------------------- Constructors ----------------------------------
30 
32  Box2LL() { Clear(); }
33 
37  Box2LL(const Vec2LL& min_, const Vec2LL& max_) : m_min(min_), m_max(max_) {}
38 
44  Box2LL(KyInt64 min_x, KyInt64 min_y, KyInt64 max_x, KyInt64 max_y) : m_min(min_x, min_y), m_max(max_x, max_y) {}
45 
46 
47  // ---------------------------------- Main API Functions ----------------------------------
48 
50  KY_INLINE bool IsValid() { return CountX() >= 0 && CountY() >= 0; }
51 
53  KY_INLINE void MakeZero() { m_min.Set(0, 0); m_max.Set(0, 0); }
54 
58  KY_INLINE void Set(const Vec2LL& min_, const Vec2LL& max_) { m_min = min_; m_max = max_; }
59 
61  KY_INLINE void SetMin(const Vec2LL& min_) { m_min = min_; }
62 
64  KY_INLINE void SetMax(const Vec2LL& max_) { m_max = max_; }
65 
67  KY_INLINE const Vec2LL& Min() const { return m_min; }
68 
70  KY_INLINE const Vec2LL& Max() const { return m_max; }
71 
75  KY_INLINE Vec2LL CornerSouthWest() const { return m_min; }
76 
80  KY_INLINE Vec2LL CornerNorthEast() const { return m_max; }
81 
85  KY_INLINE Vec2LL CornerSouthEast() const { return Vec2LL(m_max.x, m_min.y); }
86 
90  KY_INLINE Vec2LL CornerNorthWest() const { return Vec2LL(m_min.x, m_max.y); }
91 
94  KY_INLINE bool IsInside(const Vec2LL& pos) const { return pos.x >= m_min.x && pos.x <= m_max.x && pos.y >= m_min.y && pos.y <= m_max.y; }
95 
97  KY_INLINE bool IsInsideStrictly(const Vec2LL& pos) const { return pos.x > m_min.x && pos.x < m_max.x && pos.y > m_min.y && pos.y < m_max.y; }
98 
101  void Clear()
102  {
104  m_max.Set(-KyInt64MAXVAL, -KyInt64MAXVAL);
105  }
108  KY_INLINE Box2LL& operator*=(KyInt64 s) { m_min *= s; m_max *= s; return *this; }
109 
111  KY_INLINE Box2LL& operator/=(KyInt64 s) { m_min /= s; m_max /= s; return *this; }
112 
114  KY_INLINE Box2LL& operator+=(const Vec2LL& v) { m_min += v; m_max += v; return *this; }
115 
117  KY_INLINE Box2LL& operator-=(const Vec2LL& v) { m_min -= v; m_max -= v; return *this; }
118 
120  KY_INLINE bool operator==(const Box2LL& other) const { return other.m_min == m_min && other.m_max == m_max; }
121 
124  void ExpandByVec2(const Vec2LL& pos)
125  {
126  ExpandByVec2_MinMaxOnly(pos);
127  }
128 
131  void ExpandByBox2(const Box2LL& box)
132  {
133  ExpandByVec2_MinMaxOnly(box.m_min);
134  ExpandByVec2_MinMaxOnly(box.m_max);
135  }
136 
138  void Enlarge(KyInt64 enlargement)
139  {
140  m_min -= enlargement;
141  m_max += enlargement;
142  }
143 
148  void GetEnlarged(KyInt64 enlargement, Box2LL& enlarged) const
149  {
150  enlarged = *this;
151  enlarged.Enlarge(enlargement);
152  }
153 
167  bool IntersectWith(const Box2LL& box)
168  {
169  m_min.x = Kaim::Max(m_min.x, box.m_min.x);
170  m_max.x = Kaim::Min(m_max.x, box.m_max.x);
171  m_min.y = Kaim::Max(m_min.y, box.m_min.y);
172  m_max.y = Kaim::Min(m_max.y, box.m_max.y);
173  return CountX() > 0 && CountY() > 0;
174  }
175 
187  bool SetAsIntersection(const Box2LL& box_1, const Box2LL& box_2)
188  {
189  m_min.x = Kaim::Max(box_1.m_min.x, box_2.m_min.x);
190  m_max.x = Kaim::Min(box_1.m_max.x, box_2.m_max.x);
191  m_min.y = Kaim::Max(box_1.m_min.y, box_2.m_min.y);
192  m_max.y = Kaim::Min(box_1.m_max.y, box_2.m_max.y);
193  return CountX() > 0 && CountY() > 0;
194  }
195 
196 private:
197  KY_INLINE KyInt64 CountX() { return m_max.x - m_min.x + 1; }
198  KY_INLINE KyInt64 CountY() { return m_max.y - m_min.y + 1; }
199 
200  void ExpandByVec2_MinMaxOnly(const Vec2LL& pos)
201  {
202  const KyInt64 posx = pos.x;
203  const KyInt64 posy = pos.y;
204  m_min.x = Kaim::Min(m_min.x, posx);
205  m_max.x = Kaim::Max(m_max.x, posx);
206  m_min.y = Kaim::Min(m_min.y, posy);
207  m_max.y = Kaim::Max(m_max.y, posy);
208  }
209 
210 
211 public:
212  Vec2LL m_min;
213  Vec2LL m_max;
214 };
215 
216 
217 template <class OSTREAM>
218 inline OSTREAM& operator<<(OSTREAM& os, const Kaim::Box2LL& v)
219 {
220  os << "{" << v.Min() << ";" << v.Max() << "}";
221  return os;
222 }
223 
224 
225 } // namespace Kaim
226 
227 
228 
229 
230 #endif
231 
Box2LL & operator+=(const Vec2LL &v)
Adds the specified two-dimensional vector to both the minima and maxima of the box.
Definition: box2ll.h:123
KyInt64 x
The size of the vector along the X axis.
Definition: vec2ll.h:175
void ExpandByBox2(const Box2LL &box)
Enlarges the extents of the bounding box to include the area covered by the specified bounding box...
Definition: box2ll.h:142
bool IsValid()
Indicates whether or not the extents of the bounding box are valid.
Definition: box2ll.h:59
void Enlarge(KyInt64 enlargement)
Enlarges the extents of the bounding box by the specified amount in all directions.
Definition: box2ll.h:149
Box2LL & operator-=(const Vec2LL &v)
Subtracts the specified two-dimensional vector from both the minima and maxima of the box...
Definition: box2ll.h:126
void Set(const Vec2LL &min_, const Vec2LL &max_)
Sets the extents of the bounding box to the specified values.
Definition: box2ll.h:67
void SetMax(const Vec2LL &max_)
Sets the maxima of the bounding box to the specified coordinates.
Definition: box2ll.h:73
bool IntersectWith(const Box2LL &box)
Resizes this bounding box to contain only the grid cells that are common to both it and box...
Definition: box2ll.h:179
T Min(const T &a, const T &b)
Returns the lesser of the two specified values.
Definition: fastmath.h:113
Box2LL()
Creates a new Box2LL with invalid extents: you must call Set() before using it.
Definition: box2ll.h:37
This class defines a two-dimensional vector whose coordinates are stored using 64-bit integers...
Definition: vec2ll.h:27
void ExpandByVec2(const Vec2LL &pos)
Enlarges the extents of the bounding box to include the specified two-dimensional point...
Definition: box2ll.h:134
Box2LL & operator*=(KyInt64 s)
Multiplies both the minima and maxima of the box by the specified value.
Definition: box2ll.h:117
bool operator==(const Box2LL &other) const
Check equality.
Definition: box2ll.h:129
KyInt64 y
The size of the vector along the Y axis.
Definition: vec2ll.h:176
Vec2LL CornerNorthEast() const
Retrieves the coordinates of the North-East corner of the bounding box.
Definition: box2ll.h:89
const Vec2LL & Min() const
Retrieves the minima of the bounding box.
Definition: box2ll.h:76
Vec2LL CornerSouthWest() const
Retrieves the coordinates of the South-West corner of the bounding box.
Definition: box2ll.h:84
void MakeZero()
Sets both the minima and maxima of the bounding box to (0,0).
Definition: box2ll.h:62
void Set(KyInt64 _x, KyInt64 _y)
Sets the coordinates of the vector to match the specified values.
Definition: vec2ll.h:55
T Max(const T &a, const T &b)
Returns the greater of the two specified values.
Definition: fastmath.h:121
Vec2LL CornerSouthEast() const
Retrieves the coordinates of the South-East corner of the bounding box.
Definition: box2ll.h:94
void Clear()
Clears all information maintained by this object.
Definition: box2ll.h:110
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
void SetMin(const Vec2LL &min_)
Sets the minima of the bounding box to the specified coordinates.
Definition: box2ll.h:70
const Vec2LL & Max() const
Retrieves the maxima of the bounding box.
Definition: box2ll.h:79
bool IsInside(const Vec2LL &pos) const
Returns true if the specified position is contained within the extents of the bounding box or if the ...
Definition: box2ll.h:103
Vec2LL CornerNorthWest() const
Retrieves the coordinates of the North-West corner of the bounding box.
Definition: box2ll.h:99
bool IsInsideStrictly(const Vec2LL &pos) const
Returns true if the specified position is contained entirely within the extents of the bounding box...
Definition: box2ll.h:106
Box2LL & operator/=(KyInt64 s)
Divides both the minima and maxima of the box by the specified value.
Definition: box2ll.h:120
#define KyInt64MAXVAL
The maximum value that can be stored in the KyInt64 variable type.
Definition: types.h:234
void GetEnlarged(KyInt64 enlargement, Box2LL &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: box2ll.h:159
__int64 KyInt64
Type used internally to represent a 64-bit integer.
Definition: types.h:37
bool SetAsIntersection(const Box2LL &box_1, const Box2LL &box_2)
Resizes this bounding box to contain only the grid cells that are common to both box_1 and box_2...
Definition: box2ll.h:200
Each instance of this class also maintains a count of the number of elements (or grid cells) containe...
Definition: box2ll.h:24