gwnavruntime/math/box2iiterator.h Source File

box2iiterator.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 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 #pragma once
8 
11 
12 namespace Kaim
13 {
14 
17 {
18  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
19 public:
20  // ------------------------------ Functions -----------------------------
21  Box2iRowMajorIterator() : m_box(nullptr), m_pos(KyInt32MINVAL, KyInt32MINVAL), m_currentIdx(-1) {}
22  Box2iRowMajorIterator(const Box2i& box) : m_box(&box), m_pos(KyInt32MINVAL, KyInt32MINVAL), m_currentIdx(-1)
23  {
24  if (m_box != nullptr && box.IsValid())
25  {
26  m_pos = box.Min();
27  m_currentIdx = 0;
28  }
29  }
30 
31  bool operator==(const Box2iRowMajorIterator& it) const { return m_box == it.m_box && m_currentIdx == it.m_currentIdx; }
32  bool operator!=(const Box2iRowMajorIterator& it) const { return m_box != it.m_box || m_currentIdx != it.m_currentIdx; }
33 
34  Box2iRowMajorIterator& operator++()
35  {
36  if (IsFinished() == false)
37  {
38  if (m_pos.x < m_box->Max().x)
39  {
40  ++m_pos.x;
41  ++m_currentIdx;
42  }
43  else if (m_pos.y < m_box->Max().y)
44  {
45  m_pos.x = m_box->Min().x;
46  ++m_pos.y;
47  ++m_currentIdx;
48  }
49  else
50  {
51  m_currentIdx = -1;
52  }
53  }
54  return *this;
55  }
56 
57  bool IsFinished() const { return m_currentIdx < 0 || m_box == nullptr; }
58 
59  KyInt32 GetRowMajorIndex() const { return m_currentIdx; }
60  const Vec2i& GetPos() const { return m_pos; }
61 
62  // ------------------------------ Data -----------------------------
63  const Box2i* m_box;
64  Vec2i m_pos;
65  KyInt32 m_currentIdx;
66 };
67 
68 
97 {
98 public:
99  // ------------------------------ Functions -----------------------------
100 
101  Box2iIteratorInBiggerRowMajorBox() : m_innerBox(nullptr), m_outerBox(nullptr), m_pos(KyInt32MINVAL, KyInt32MINVAL), m_innerBoxIdx(-1), m_outerBoxIdx(-1) {}
102 
103  Box2iIteratorInBiggerRowMajorBox(const Box2i& innerBox, const Box2i& outerBox)
104  : m_innerBox(&innerBox), m_outerBox(&outerBox), m_pos(KyInt32MINVAL, KyInt32MINVAL), m_innerBoxIdx(-1), m_outerBoxIdx(-1)
105  {
106  Init();
107  }
108 
109  void Init()
110  {
111  if (m_innerBox != nullptr && m_outerBox != nullptr && m_innerBox->IsValid() && m_outerBox->IsValid())
112  {
113  KY_LOG_ERROR_IF(m_outerBox->DoesContain(m_innerBox->Min()) == false || m_outerBox->DoesContain(m_innerBox->Max()) == false ,
114  ("Error in boxes provided for Box2iRowMajorIterator. the innerBox must be fully included in the outerBox"));
115  m_pos = m_innerBox->Min();
116  m_innerBoxIdx = 0;
117  m_outerBoxIdx = m_outerBox->GetRowMajorIndex(m_pos);
118  }
119  }
120 
122  {
123  if (IsFinished() == false)
124  {
125  if (m_pos.x < m_innerBox->Max().x)
126  {
127  ++m_pos.x;
128  ++m_innerBoxIdx;
129  ++m_outerBoxIdx;
130  KY_ASSERT(m_outerBoxIdx == m_outerBox->GetRowMajorIndex(m_pos));
131  KY_ASSERT(m_innerBoxIdx == m_innerBox->GetRowMajorIndex(m_pos));
132 
133  }
134  else if (m_pos.y < m_innerBox->Max().y)
135  {
136  m_pos.x = m_innerBox->Min().x;
137  ++m_pos.y;
138  ++m_innerBoxIdx;
139  m_outerBoxIdx += (m_outerBox->CountX() - m_innerBox->CountX()) + 1;
140  KY_ASSERT(m_outerBoxIdx == m_outerBox->GetRowMajorIndex(m_pos));
141  KY_ASSERT(m_innerBoxIdx == m_innerBox->GetRowMajorIndex(m_pos));
142  }
143  else
144  {
145  m_innerBoxIdx = -1;
146  m_outerBoxIdx = -1;
147  }
148  }
149  return *this;
150  }
151 
152  bool IsFinished() const { return m_innerBoxIdx < 0 || m_outerBoxIdx < 0 || m_innerBox == nullptr || m_outerBox == nullptr; }
153 
154  KyInt32 GetInnerBoxRowMajorIndex() const { return m_innerBoxIdx; }
155  KyInt32 GetOuterBoxRowMajorIndex() const { return m_outerBoxIdx; }
156  const Vec2i& GetPos() const { return m_pos; }
157 
158 private:
159  // ------------------------------ Private Data -----------------------------
160  const Box2i* m_innerBox;
161  const Box2i* m_outerBox;
162  Vec2i m_pos;
163  KyInt32 m_innerBoxIdx;
164  KyInt32 m_outerBoxIdx;
165 };
166 
167 }
168 
169 
170 
#define KyInt32MINVAL
KyInt32 min value
Definition: types.h:61
2d axis aligned box of 32bits integers. Very Important: CountX() returns m_max.x - m_min...
Definition: box2i.h:17
Box2iRowMajorIterator.
Definition: box2iiterator.h:16
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
KyInt32 GetRowMajorIndex(const Vec2i &pos) const
Compute index of position in box, where index is incremented in a outer-loop on Y and inner-loop on X...
Definition: box2i.h:101
2d vector using KyInt32
Definition: vec2i.h:18
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::int32_t KyInt32
int32_t
Definition: types.h:24
KyInt32 CountX() const
Return m_max.x - m_min.x + 1.
Definition: box2i.h:55
Browse a box that is included in a bigger box.
Definition: box2iiterator.h:96