File: CartesianGrid.h

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (512 lines) | stat: -rw-r--r-- 12,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////


#ifndef ESYS_LSMCARTESIANGRID_H
#define ESYS_LSMCARTESIANGRID_H

#include "Foundation/vec3.h"
#include "Geometry/Vec3L.h"
#include "Foundation/BoundingBox.h"
#include "Foundation/StlIterator.h"

#include <vector>
#include <map>
#include <stdexcept>
#include <sstream>

#include <boost/pool/object_pool.hpp>
#include <boost/shared_ptr.hpp>

namespace esys
{
  namespace lsm
  {
    template <typename TmplValue>
    class CartesianGrid
    {
    public:
      typedef TmplValue         value_type;
      typedef value_type&       reference;
      typedef const value_type& const_reference;
      typedef value_type*       pointer;

      class Cell
      {
      public:
        class PosValuePair
        {
        public:
          PosValuePair(const Vec3 &pos, reference value)
            : m_pos(pos),
              m_pValue(&value)
          {
          }
          
          const Vec3 &getPos() const
          {
            return m_pos;
          }
          
          const_reference getValue() const
          {
            return *m_pValue;
          }

          reference getValue()
          {
            return *m_pValue;
          }

        private:
          Vec3    m_pos;
          pointer m_pValue;
        };

        typedef std::vector<PosValuePair>            PosValueVector;
        typedef ForwardIterator<PosValueVector>      Iterator;
        typedef ForwardConstIterator<PosValueVector> ConstIterator;
        
        Cell() : m_pos(), m_pointerVector(), m_pGrid(NULL)
        {
        }

        void insertRef(reference value)
        {
          m_pointerVector.push_back(PosValuePair(getPos(), value));
        }

        void insertRef(const Vec3 &pos, reference value)
        {
          m_pointerVector.push_back(PosValuePair(pos, value));
        }

        void setPos(const Vec3 &pos)
        {
          m_pos = pos;
        }

        const Vec3 &getPos() const
        {
          return m_pos;
        }

        CartesianGrid &getGrid()
        {
          return *m_pGrid;
        }

        const CartesianGrid &getGrid() const
        {
          return *m_pGrid;
        }

        void setGrid(CartesianGrid &grid)
        {
          m_pGrid = &grid;
        }

        BoundingBox getBox() const
        {
          return 
            BoundingBox(
              getPos() - getGrid().getGridSpacing()/2.0,
              getPos() + getGrid().getGridSpacing()/2.0
            );
        }

        /**
         * Return the number of elements in this cell.
         */
        int size() const
        {
          return m_pointerVector.size();
        }

        /**
         * Clears all elements from this cell.
         */
        void clear()
        {
          m_pointerVector.clear();
        }

        ConstIterator getIterator() const
        {
          return ConstIterator(m_pointerVector);
        }

        Iterator getIterator()
        {
          return Iterator(m_pointerVector);
        }

      private:
        Vec3           m_pos;
        PosValueVector m_pointerVector;
        CartesianGrid  *m_pGrid;
      };

      class VecIndexIterator
      {
      public:
        VecIndexIterator(const Vec3L &minIdx, const Vec3L &maxIdx)
          : m_minIndex(minIdx),
            m_maxIndex(maxIdx),
            m_index(minIdx)
        {
        }
        
        bool hasNext() const
        {
          return (m_index[2] <= m_maxIndex[2]);
        }
        
        const Vec3L &current()
        {
          return m_index;
        }

        void increment()
        {
          m_index[0]++;          
          if (m_index[0] > m_maxIndex[0]) {
            m_index[0] = m_minIndex[0];
            m_index[1]++;
            if (m_index[1] > m_maxIndex[1]) {
              m_index[1] = m_minIndex[1];
              m_index[2]++;
            }
          }        
        }

        Vec3L next()
        {
          const Vec3L index = current();
          increment();
          return index;
        }

      private:
        Vec3L         m_minIndex;
        Vec3L         m_maxIndex;
        Vec3L         m_index;
      };

      template <typename TmplGridPointer, typename TmplCellRef, typename TmplCell>
      class TCellIterator
      {
      public:
        typedef TmplCell    value_type;
        typedef TmplCellRef reference;
        TCellIterator(const Vec3L &minIdx, const Vec3L &maxIdx, TmplGridPointer pGrid)
          : m_idxIt(minIdx, maxIdx),
            m_pGrid(pGrid)
        {
        }

        bool hasNext() const
        {
          return m_idxIt.hasNext();
        }
        
        reference next()
        {
          return m_pGrid->getCell(m_pGrid->getScalarIndex(m_idxIt.next()));
        }
      private:
        VecIndexIterator m_idxIt;
        TmplGridPointer  m_pGrid;
      };

      typedef TCellIterator<CartesianGrid *, Cell &, Cell>             CellIterator;
      typedef TCellIterator<const CartesianGrid *, const Cell &, Cell> CellConstIterator;

      typedef std::vector<pointer> ValueVector;
      class ValueIterator : public ForwardIterator<ValueVector>
      {
      public:
        ValueIterator(ValueVector &v) : ForwardIterator<ValueVector>(v)
        {
        }

        typename CartesianGrid::reference next()
        {
          return *(ForwardIterator<ValueVector>::next());
        }
      };

      class ValueConstIterator : public ForwardConstIterator<ValueVector>
      {
      public:
        ValueConstIterator(const ValueVector &v) : ForwardConstIterator<ValueVector>(v)
        {
        }

        typename CartesianGrid::const_reference next()
        {
          return *(ForwardConstIterator<ValueVector>::next());
        }
      };

      CartesianGrid(const BoundingBox &bBox, double gridSpacing)
        : m_bBox(bBox),
          m_gridSpacing(gridSpacing),
          m_dimensions(),
          m_minIndex(),
          m_maxIndex(),
          m_cellVector(),
          m_valuePoolPtr(new Pool(1024)),
          m_valueVector()
      {
        initialise(bBox, gridSpacing);
      }

      const Vec3L &getDimensions() const
      {
        return m_dimensions;
      }

      const BoundingBox &getBBox() const
      {
        return m_bBox;
      }

      const Vec3 &getMinPt() const
      {
        return getBBox().getMinPt();
      }

      int getScalarIndex(int xIdx, int yIdx, int zIdx) const
      {
        return 
          xIdx*m_dimensions.Z()*m_dimensions.Y()
          +
          yIdx*m_dimensions.Z()
          +
          zIdx;
      }

      Vec3L getVecIndex(const Vec3 &pt) const
      {
        const Vec3 relPos = Vec3((pt - getMinPt())/m_gridSpacing);
        const Vec3L index = Vec3L(int(nearbyint(relPos.X())), int(nearbyint(relPos.Y())), int(nearbyint(relPos.Z())));
        return getMinVecIndex().max(getMaxVecIndex().min(index));
      }
      
      Vec3 getPos(const Vec3L &index) const
      {
        return 
          (
            getMinPt()
            +
            (Vec3(index.X(), index.Y(), index.Z())*m_gridSpacing)
          );
      }

      int getScalarIndex(const Vec3L &index) const
      {
        return getScalarIndex(index.X(), index.Y(), index.Z()); 
      }

      int getScalarIndex(const Vec3 &pt) const
      {
        return getScalarIndex(getVecIndex(pt));
      }

      const Vec3L &getMinVecIndex() const
      {
        return m_minIndex;
      }

      const Vec3L &getMaxVecIndex() const
      {
        return m_maxIndex;
      }

      void insert(const Vec3 &pos, const_reference data)
      {
        insertRef(pos, *(m_valuePoolPtr->construct(data)));
      }

      const Cell &getCell(int scalarIndex) const
      {
        return m_cellVector[scalarIndex];
      }
      
      Cell &getCell(int scalarIndex)
      {
        return m_cellVector[scalarIndex];
      }

      const Cell &getCell(const Vec3 &pos) const
      {
        return getCell(getScalarIndex(pos));
      }
      
      Cell &getCell(const Vec3 &pos)
      {
        return getCell(getScalarIndex(pos));
      }

      CellIterator getCellIterator(const Vec3 &pos, double radius)
      {
        return 
          CellIterator(
            getVecIndex(pos - radius),
            getVecIndex(pos + radius),
            this
          );
      }

      CellConstIterator getCellIterator(const Vec3 &pos, double radius) const
      {
        return 
          CellConstIterator(
            getVecIndex(pos - radius),
            getVecIndex(pos + radius),
            this
          );
      }

      CellIterator getCellIterator(const Vec3 &pos)
      {
        return getCellIterator(pos, 0.0);
      }

      CellConstIterator getCellIterator(const Vec3 &pos) const
      {
        return getCellIterator(pos, 0.0);
      }

      CellIterator getCellIterator()
      {
        return 
          CellIterator(
            getMinVecIndex(),
            getMaxVecIndex(),
            this
          );
      }

      CellConstIterator getCellIterator() const
      {
        return 
          CellConstIterator(
            getMinVecIndex(),
            getMaxVecIndex(),
            this
          );
      }

      ValueIterator getValueIterator()
      {
        return ValueIterator(m_valueVector);
      }

      ValueConstIterator getValueIterator() const
      {
        return ValueConstIterator(m_valueVector);
      }

      size_t size() const
      {
        return m_valueVector.size();
      }

      double getGridSpacing() const
      {
        return m_gridSpacing;
      }

      void clear()
      {
        CellIterator cellIt = getCellIterator();
        while (cellIt.hasNext())
        {
          cellIt.next().clear();
        }
        m_valuePoolPtr = PoolPtr(new Pool(1024));
        m_valueVector.clear();
      }

    protected:
      void insertRef(const Vec3 &pos, reference data)
      {
        getCell(pos).insertRef(pos, data);
        m_valueVector.push_back(&data);
      }

      void initialise(const BoundingBox &bBox, double gridSpacing)
      {
        m_bBox        = bBox;
        m_gridSpacing = gridSpacing;

        const Vec3 dims  = m_bBox.getSizes()/gridSpacing;
        m_dimensions = Vec3L(int(nearbyint(dims[0])), int(nearbyint(dims[1])), int(nearbyint(dims[2])));
        m_dimensions = m_dimensions.max(Vec3L(1, 1, 1));

        m_cellVector.clear();
        m_cellVector.insert(
          m_cellVector.end(),
          m_dimensions.X()*m_dimensions.Y()*m_dimensions.Z(),
          Cell()
        );

        m_valueVector.clear();
        m_valueVector.reserve(m_cellVector.size());

        m_minIndex = Vec3L(0, 0, 0);
        m_maxIndex = (m_dimensions - 1);

        for (int i = getMinVecIndex()[0]; i <= getMaxVecIndex()[0]; i++) {
          for (int j = getMinVecIndex()[1]; j <= getMaxVecIndex()[1]; j++) {
            for (int k = getMinVecIndex()[2]; k <= getMaxVecIndex()[2]; k++) {
              const Vec3L index(i, j, k);
              const Vec3 pos = getPos(index);
              Cell &cell = getCell(getScalarIndex(index));
              if ((&(getCell(pos))) == (&(cell)))
              {
                cell.setPos(pos);
                cell.setGrid(*this);
              }
              else
              {
                std::stringstream sStream;
                sStream 
                  << "Couldn't set grid location for cell ("
                  << i << "," << j << "," << k << "), pos = " << pos;
                throw std::runtime_error(sStream.str());
              }
            }
          }
        }
      }

    private:
      typedef std::vector<Cell>              CellVector;
      typedef boost::object_pool<value_type> Pool;
      typedef boost::shared_ptr<Pool>        PoolPtr;

      BoundingBox m_bBox;
      double      m_gridSpacing;
      Vec3L       m_dimensions;
      Vec3L       m_minIndex;
      Vec3L       m_maxIndex;
      CellVector  m_cellVector;      
      PoolPtr     m_valuePoolPtr;
      ValueVector m_valueVector;
    };
  }
}

#endif