Fission FSN
Loading...
Searching...
No Matches
fsGrid.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: fsGrid.h
4*
5* Purpose: Template class for 2d grid
6*
7*********************************************************************/
8
9#ifndef fsGridhdr
10#define fsGridhdr
11
12#include <vector>
13#include <functional>
14#include <algorithm>
15
16#include "fsBaseTypes.h"
17#include "fsPoint.h"
18
19
20template <typename fsTCell>
21class fsGrid
22{
23public:
24
25 fsBool onGrid(fsPoint const& pPos) const
26 {
27 const auto hs = halfCell();
28 return
29 pPos.x >= mOrigin.x - hs.x
30 && pPos.x < mOrigin.x - hs.x + rowStrideGet() * mDimensions.x
31 && pPos.y >= mOrigin.y - hs.y
32 && pPos.y < mOrigin.y - hs.y + columnStrideGet() * mDimensions.y;
33 }
34
35 fsPoint worlddLocation(fsPoint const& pGridPos) const
36 {
37 return {
38 originGet().x + pGridPos.x * cellBorderedDimensionsGet().x,
39 originGet().y + pGridPos.y * cellBorderedDimensionsGet().y
40 };
41 }
42
43 fsPoint gridLocation(fsPoint const& pPos) const
44 {
45 return {
48 };
49 }
50
52 {
53 return (mCellDimensions + mCellBorder) / 2;
54 }
55
56 fsTCell& getCell(fsS32 pColumn, fsS32 pRow);
57 fsPoint const& cellDimensionsGet() const { return mCellDimensions; }
58 fsPoint cellBorderedDimensionsGet() const { return mCellDimensions + mCellBorder; }
59
60 fsS32 columnsGet() const { return mDimensions.x; }
61 fsS32 rowsGet() const { return mDimensions.y; }
62
63 fsS32 rowStrideGet() const { return mCellDimensions.x + mCellBorder.x; }
64 fsS32 columnStrideGet() const { return mCellDimensions.y + mCellBorder.y; }
65
66 fsPoint const& originGet() const { return mOrigin; }
67
68 typename std::vector<fsTCell>::iterator begin() { return mCells.begin(); }
69 typename std::vector<fsTCell>::iterator end() { return mCells.end(); }
70 typename std::vector<fsTCell>::reverse_iterator rbegin() { return mCells.rbegin(); }
71 typename std::vector<fsTCell>::reverse_iterator rend() { return mCells.rend(); }
72
73 void drop(std::function<void(fsTCell, fsTCell)> pAction);
74
75 void swap(fsTCell pLHS, fsTCell pRHS);
76
77 typename std::vector<fsTCell>::iterator
78 findIf(typename std::vector<fsTCell>::iterator pBegin, typename std::vector<fsTCell>::iterator pEnd,
79 std::function<fsBool(fsTCell)> pPredicate);
80
81 template <typename FwdIt>
82 void forEachRow(FwdIt pBegin, FwdIt pEnd,
83 std::function<void(fsTCell)> pCellAction, std::function<void()> pRowAction);
84 template <typename FwdIt>
85 void forEachColumn(FwdIt pBegin, FwdIt pEnd,
86 std::function<void(fsTCell)> pCellAction, std::function<void()> pColumnAction);
87 template <typename FwdIt>
88 void forEachDescendingDiagonal(FwdIt pBegin, FwdIt pEnd,
89 std::function<void(fsTCell)> pCellAction, std::function<void()> pDiagonalAction);
90 template <typename FwdIt>
91 void forEachAscendingDiagonal(FwdIt pBegin, FwdIt pEnd,
92 std::function<void(fsTCell)> pCellAction, std::function<void()> pDiagonalAction);
93
94
95 void forEach(fsPoint const& pBegin, fsPoint const& pEnd,
96 std::function<void(fsTCell&)> pCellOperation);
97
98 void forEach(typename std::vector<fsTCell>::iterator pBegin, typename std::vector<fsTCell>::iterator pEnd,
99 std::function<void(fsTCell&)> pCellOperation);
100
101 void generate(std::function<fsTCell const(fsS32, fsS32)> pCellCreate);
102
103 void init(fsPoint const& pOrigin, fsPoint const& pDimensions, fsPoint const& pCellDimensions,
104 fsPoint const& pCellBorder);
105
106 fsGrid() = default;
107 ~fsGrid() = default;
108
109protected:
110
111private:
112
113 std::vector<fsTCell> mCells;
114
115 fsPoint mDimensions;
116 fsPoint mOrigin;
117
118 fsPoint mCellDimensions;
119 fsPoint mCellBorder;
120};
121
122
123template <typename fsTCell>
124fsTCell& fsGrid<fsTCell>::getCell(fsS32 pColumn, fsS32 pRow)
125{
126 return mCells[pColumn + pRow * mDimensions.x];
127}
128
129template <typename fsTCell>
130void fsGrid<fsTCell>::init(fsPoint const& pOrigin, fsPoint const& pDimensions, fsPoint const& pCellDimensions,
131 fsPoint const& pCellBorder)
132{
133 mDimensions = pDimensions;
134 mOrigin = pOrigin;
135 mCellDimensions = pCellDimensions;
136 mCellBorder = pCellBorder;
137 mCells.resize(mDimensions.x * mDimensions.y);
138}
139
140
141template <typename fsTCell>
142typename std::vector<fsTCell>::iterator
143fsGrid<fsTCell>::findIf(typename std::vector<fsTCell>::iterator pBegin, typename std::vector<fsTCell>::iterator pEnd,
144 std::function<fsBool(fsTCell)> pPredicate)
145{
146 return std::find_if(pBegin, pEnd, pPredicate);
147}
148
149template <typename fsTCell>
150void fsGrid<fsTCell>::forEach(fsPoint const& pBegin, fsPoint const& pEnd,
151 std::function<void(fsTCell&)> pCellOperation)
152{
153 if(pEnd.y >= pBegin.y)
154 {
155 if(pEnd.x >= pBegin.x)
156 {
157 for (fsS32 j = pBegin.y; j <= pEnd.y; ++j)
158 {
159 for (fsS32 i = pBegin.x; i <= pEnd.x; ++i)
160 {
161 pCellOperation(getCell(i, j));
162 }
163 }
164 }
165 else
166 {
167 for (fsS32 j = pBegin.y; j <= pEnd.y; ++j)
168 {
169 for (fsS32 i = pBegin.x; i >= pEnd.x; --i)
170 {
171 pCellOperation(getCell(i, j));
172 }
173 }
174 }
175 }
176 else
177 {
178 {
179 if(pEnd.x >= pBegin.x)
180 {
181 for (fsS32 j = pBegin.y; j >= pEnd.y; --j)
182 {
183 for (fsS32 i = pBegin.x; i <= pEnd.x; ++i)
184 {
185 pCellOperation(getCell(i, j));
186 }
187 }
188 }
189 else
190 {
191 for (fsS32 j = pBegin.y; j >= pEnd.y; --j)
192 {
193 for (fsS32 i = pBegin.x; i >= pEnd.x; --i)
194 {
195 pCellOperation(getCell(i, j));
196 }
197 }
198 }
199 }
200 }
201}
202
203template <typename fsTCell>
204void fsGrid<fsTCell>::forEach(typename std::vector<fsTCell>::iterator pBegin,
205 typename std::vector<fsTCell>::iterator pEnd,
206 std::function<void(fsTCell&)> pCellOperation)
207{
208 std::for_each(pBegin, pEnd, pCellOperation);
209}
210
211template <typename fsTCell>
212void fsGrid<fsTCell>::generate(std::function<fsTCell const(fsS32, fsS32)> pCellCreate)
213{
214 fsS32 i = 0;
215 std::generate(mCells.begin(), mCells.end(),
216 [&]()
217 {
218 const fsS32 col = i % mDimensions.x;
219 const fsS32 row = i++ / mDimensions.x;
220 return pCellCreate(col, row);
221 });
222}
223
224template <typename fsTCell>
225void fsGrid<fsTCell>::swap(fsTCell const pLHS, fsTCell const pRHS)
226{
227 std::iter_swap(std::find(mCells.begin(), mCells.end(), pLHS),
228 std::find(mCells.begin(), mCells.end(), pRHS));
229}
230
231template <typename fsTCell>
232template <typename FwdIt>
233void fsGrid<fsTCell>::forEachRow(FwdIt pBegin, FwdIt pEnd,
234 std::function<void(fsTCell)> pCellAction, std::function<void()> pRowAction)
235{
236 FwdIt rowStart = pBegin;
237 FwdIt rowEnd = pBegin;
238 std::advance(rowEnd, mDimensions.x);
239
240 while (rowEnd != pEnd)
241 {
242 std::for_each(rowStart, rowEnd, pCellAction);
243 std::advance(rowStart, mDimensions.x);
244 std::advance(rowEnd, mDimensions.x);
245 pRowAction();
246 }
247 std::for_each(rowStart, rowEnd, pCellAction);
248 pRowAction();
249}
250
251
252template <typename fsTCell>
253template <typename FwdIt>
254void fsGrid<fsTCell>::forEachColumn(FwdIt pBegin, FwdIt pEnd,
255 std::function<void(fsTCell)> pCellAction, std::function<void()> pColumnAction)
256{
257 FwdIt columnStart = pBegin;
258 FwdIt columnEnd = pEnd;
259 std::advance(columnEnd, -mDimensions.x);
260
261 while (columnEnd != pEnd)
262 {
263 FwdIt it = columnStart;
264 while (it != columnEnd)
265 {
266 pCellAction(*it);
267 it += mDimensions.x;
268 }
269
270 pCellAction(*it);
271 pColumnAction();
272 ++columnStart;
273 ++columnEnd;
274 }
275}
276
277template <typename fsTCell>
278template <typename FwdIt>
279void fsGrid<fsTCell>::forEachDescendingDiagonal(FwdIt pBegin, FwdIt pEnd,
280 std::function<void(fsTCell)> pCellAction,
281 std::function<void()> pDiagonalAction)
282{
283 FwdIt columnStart = pBegin;
284 FwdIt columnEnd = pEnd;
285 --columnEnd;
286
287 while (columnStart != columnEnd)
288 {
289 FwdIt it = columnStart;
290 while (it != columnEnd)
291 {
292 pCellAction(*it);
293 it += mDimensions.x + 1;
294 }
295
296 pCellAction(*it);
297 pDiagonalAction();
298 ++columnStart;
299 columnEnd -= mDimensions.x;
300 }
301
302 columnStart = pBegin;
303 columnEnd = pEnd;
304 --columnEnd;
305
306 columnStart += mDimensions.x;
307 --columnEnd;
308
309 while (columnStart != columnEnd)
310 {
311 FwdIt it = columnStart;
312 while (it != columnEnd)
313 {
314 pCellAction(*it);
315 it += mDimensions.x + 1;
316 }
317
318 pCellAction(*it);
319 pDiagonalAction();
320 columnStart += mDimensions.x;
321 --columnEnd;
322 }
323}
324
325template <typename fsTCell>
326template <typename FwdIt>
327void fsGrid<fsTCell>::forEachAscendingDiagonal(FwdIt pBegin, FwdIt pEnd,
328 std::function<void(fsTCell)> pCellAction,
329 std::function<void()> pDiagonalAction)
330{
331 FwdIt columnStart = pEnd;
332 FwdIt columnEnd = pBegin;
333
334 std::advance(columnStart, -mDimensions.x);
335 std::advance(columnEnd, mDimensions.x - 1);
336
337 while (columnStart != columnEnd)
338 {
339 FwdIt it = columnStart;
340 while (it != columnEnd)
341 {
342 pCellAction(*it);
343 it -= mDimensions.x - 1;
344 }
345
346 pCellAction(*it);
347 pDiagonalAction();
348 --columnEnd;
349 columnStart -= mDimensions.x;
350 }
351
352 columnStart = pEnd;
353 columnEnd = pBegin;
354
355 std::advance(columnStart, -mDimensions.x);
356 std::advance(columnEnd, mDimensions.x - 1);
357
358 while (columnStart != columnEnd)
359 {
360 FwdIt it = columnStart;
361 while (it != columnEnd)
362 {
363 pCellAction(*it);
364 it -= mDimensions.x - 1;
365 }
366
367 pCellAction(*it);
368 pDiagonalAction();
369 ++columnStart;
370 columnEnd += mDimensions.x;
371 }
372}
373
374template <typename fsTCell>
376 std::function<void(fsTCell, fsTCell)> pAction)
377{
378 typename std::vector<fsTCell>::iterator it = mCells.begin();
379 typename std::vector<fsTCell>::iterator itBelow = mCells.begin();
380 std::advance(itBelow, mDimensions.x);
381
382 typename std::vector<fsTCell>::iterator limit = mCells.begin();
383 std::advance(limit, std::distance(mCells.begin(), mCells.end()) - mDimensions.x);
384
385 while (it < limit)
386 {
387 pAction(*it, *itBelow);
388
389 ++it;
390 ++itBelow;
391 }
392}
393
394#endif
fsS32 columnsGet() const
Definition fsGrid.h:60
void forEachDescendingDiagonal(FwdIt pBegin, FwdIt pEnd, std::function< void(fsTCell)> pCellAction, std::function< void()> pDiagonalAction)
Definition fsGrid.h:279
void generate(std::function< fsTCell const(fsS32, fsS32)> pCellCreate)
Definition fsGrid.h:212
~fsGrid()=default
void forEachColumn(FwdIt pBegin, FwdIt pEnd, std::function< void(fsTCell)> pCellAction, std::function< void()> pColumnAction)
Definition fsGrid.h:254
void drop(std::function< void(fsTCell, fsTCell)> pAction)
Definition fsGrid.h:375
std::vector< fsTCell >::reverse_iterator rbegin()
Definition fsGrid.h:70
fsS32 rowStrideGet() const
Definition fsGrid.h:63
std::vector< fsTCell >::reverse_iterator rend()
Definition fsGrid.h:71
void forEachRow(FwdIt pBegin, FwdIt pEnd, std::function< void(fsTCell)> pCellAction, std::function< void()> pRowAction)
Definition fsGrid.h:233
fsPoint gridLocation(fsPoint const &pPos) const
Definition fsGrid.h:43
fsS32 columnStrideGet() const
Definition fsGrid.h:64
std::vector< fsTCell >::iterator findIf(typename std::vector< fsTCell >::iterator pBegin, typename std::vector< fsTCell >::iterator pEnd, std::function< fsBool(fsTCell)> pPredicate)
Definition fsGrid.h:143
fsPoint halfCell() const
Definition fsGrid.h:51
void forEachAscendingDiagonal(FwdIt pBegin, FwdIt pEnd, std::function< void(fsTCell)> pCellAction, std::function< void()> pDiagonalAction)
Definition fsGrid.h:327
std::vector< fsTCell >::iterator begin()
Definition fsGrid.h:68
fsPoint worlddLocation(fsPoint const &pGridPos) const
Definition fsGrid.h:35
fsS32 rowsGet() const
Definition fsGrid.h:61
void init(fsPoint const &pOrigin, fsPoint const &pDimensions, fsPoint const &pCellDimensions, fsPoint const &pCellBorder)
Definition fsGrid.h:130
fsPoint const & originGet() const
Definition fsGrid.h:66
fsTCell & getCell(fsS32 pColumn, fsS32 pRow)
Definition fsGrid.h:124
std::vector< fsTCell >::iterator end()
Definition fsGrid.h:69
void forEach(fsPoint const &pBegin, fsPoint const &pEnd, std::function< void(fsTCell &)> pCellOperation)
Definition fsGrid.h:150
void swap(fsTCell pLHS, fsTCell pRHS)
Definition fsGrid.h:225
fsBool onGrid(fsPoint const &pPos) const
Definition fsGrid.h:25
fsGrid()=default
fsPoint const & cellDimensionsGet() const
Definition fsGrid.h:57
fsPoint cellBorderedDimensionsGet() const
Definition fsGrid.h:58
bool fsBool
Definition fsBaseTypes.h:26
int fsS32
Definition fsBaseTypes.h:20
Definition fsPoint.h:17
fsS32 x
Definition fsPoint.h:39
fsS32 y
Definition fsPoint.h:40