gwnavruntime/math/box2iiterator.h Source File

box2iiterator.h
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_Box2iIterator_H
10 #define Navigation_Box2iIterator_H
11 
14 
15 
16 namespace Kaim
17 {
18 
19 // RowMajorIterator
20 // GetRowMajorIndex(const Vec2i& pos) const { return (pos.y - m_min.y) * CountX() + pos.x - m_min.x; }
21 class Box2iRowMajorIterator
22 {
23  const Box2i* m_box;
24  Vec2i m_pos;
25  KyInt32 m_currentIdx;
26 
27 public:
28  KY_INLINE Box2iRowMajorIterator() : m_box(KY_NULL), m_pos(-KyInt32MAXVAL, -KyInt32MAXVAL), m_currentIdx(-1) {}
29  KY_INLINE Box2iRowMajorIterator(const Box2i& box) : m_box(&box), m_pos(-KyInt32MAXVAL, -KyInt32MAXVAL), m_currentIdx(-1)
30  {
31  if (m_box != KY_NULL && box.IsValid())
32  {
33  m_pos = box.Min();
34  m_currentIdx = 0;
35  }
36  }
37 
38  KY_INLINE bool operator==(const Box2iRowMajorIterator& it) const { return m_box == it.m_box && m_currentIdx == it.m_currentIdx; }
39  KY_INLINE bool operator!=(const Box2iRowMajorIterator& it) const { return m_box != it.m_box || m_currentIdx != it.m_currentIdx; }
40 
41  KY_INLINE Box2iRowMajorIterator& operator++()
42  {
43  if (IsFinished() == false)
44  {
45  if (m_pos.x < m_box->Max().x)
46  {
47  ++m_pos.x;
48  ++m_currentIdx;
49  }
50  else if (m_pos.y < m_box->Max().y)
51  {
52  m_pos.x = m_box->Min().x;
53  ++m_pos.y;
54  ++m_currentIdx;
55  }
56  else
57  {
58  m_currentIdx = -1;
59  }
60  }
61  return *this;
62  }
63 
64  KY_INLINE bool IsFinished() const { return m_currentIdx < 0 || m_box == KY_NULL; }
65 
66  KY_INLINE KyInt32 GetRowMajorIndex() const { return m_currentIdx; }
67  KY_INLINE const Vec2i& GetPos() const { return m_pos; }
68 };
69 
70 // RowMajorIterator
71 
72 
99 
100 class Box2iIteratorInBiggerRowMajorBox
101 {
102 public:
103  KY_INLINE Box2iIteratorInBiggerRowMajorBox() : m_innerBox(KY_NULL), m_outerBox(KY_NULL), m_pos(-KyInt32MAXVAL, -KyInt32MAXVAL),
104  m_innerBoxIdx(-1), m_outerBoxIdx(-1) {}
105  KY_INLINE Box2iIteratorInBiggerRowMajorBox(const Box2i& innerBox, const Box2i& outerBox) : m_innerBox(&innerBox), m_outerBox(&outerBox),
106  m_pos(-KyInt32MAXVAL, -KyInt32MAXVAL), m_innerBoxIdx(-1), m_outerBoxIdx(-1)
107  {
108  Init();
109  }
110 
111  KY_INLINE void Init()
112  {
113  if (m_innerBox != KY_NULL && m_outerBox != KY_NULL && m_innerBox->IsValid() && m_outerBox->IsValid())
114  {
115  KY_LOG_ERROR_IF(m_outerBox->IsInside(m_innerBox->Min()) == false || m_outerBox->IsInside(m_innerBox->Max()) == false ,
116  ("Error in boxes provided for Box2iRowMajorIterator. the innerBox must be fully included in the outerBox"));
117  m_pos = m_innerBox->Min();
118  m_innerBoxIdx = 0;
119  m_outerBoxIdx = m_outerBox->GetRowMajorIndex(m_pos);
120  }
121  }
122 
123  KY_INLINE Box2iIteratorInBiggerRowMajorBox& operator++()
124  {
125  if (IsFinished() == false)
126  {
127  if (m_pos.x < m_innerBox->Max().x)
128  {
129  ++m_pos.x;
130  ++m_innerBoxIdx;
131  ++m_outerBoxIdx;
132  KY_ASSERT(m_outerBoxIdx == m_outerBox->GetRowMajorIndex(m_pos));
133  KY_ASSERT(m_innerBoxIdx == m_innerBox->GetRowMajorIndex(m_pos));
134 
135  }
136  else if (m_pos.y < m_innerBox->Max().y)
137  {
138  m_pos.x = m_innerBox->Min().x;
139  ++m_pos.y;
140  ++m_innerBoxIdx;
141  m_outerBoxIdx += (m_outerBox->CountX() - m_innerBox->CountX()) + 1;
142  KY_ASSERT(m_outerBoxIdx == m_outerBox->GetRowMajorIndex(m_pos));
143  KY_ASSERT(m_innerBoxIdx == m_innerBox->GetRowMajorIndex(m_pos));
144  }
145  else
146  {
147  m_innerBoxIdx = -1;
148  m_outerBoxIdx = -1;
149  }
150  }
151  return *this;
152  }
153 
154  KY_INLINE bool IsFinished() const { return m_innerBoxIdx < 0 || m_outerBoxIdx < 0 || m_innerBox == KY_NULL || m_outerBox == KY_NULL; }
155 
156  KY_INLINE KyInt32 GetInnerBoxRowMajorIndex() const { return m_innerBoxIdx; }
157  KY_INLINE KyInt32 GetOuterBoxRowMajorIndex() const { return m_outerBoxIdx; }
158  KY_INLINE const Vec2i& GetPos() const { return m_pos; }
159 
160 private:
161  const Box2i* m_innerBox;
162  const Box2i* m_outerBox;
163  Vec2i m_pos;
164  KyInt32 m_innerBoxIdx;
165  KyInt32 m_outerBoxIdx;
166 };
167 
168 } // namespace Kaim
169 
170 
171 #endif
172 
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
#define KyInt32MAXVAL
The maximum value that can be stored in the KyInt32 variable type.
Definition: types.h:224
Definition: gamekitcrowddispersion.h:20