File: GridIterator.h

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (282 lines) | stat: -rw-r--r-- 8,023 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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////


#ifndef ESYS_LSMGRIDITERATOR_H
#define ESYS_LSMGRIDITERATOR_H

#include "Foundation/BoundingBox.h"
#include "Foundation/vec3.h"

namespace esys
{
  namespace lsm
  {
    /**
     * Class for iterating over the centre-points of spheres arranged
     * in a regular lattice within a specified bounding box.
     */
    class GridIterator
    {
    public:
      inline GridIterator()
        : m_sphereRadius(0.0),
          m_minI(0),
          m_minJ(0),
          m_minK(0),
          m_maxI(0),
          m_maxJ(0),
          m_maxK(0),
          m_i(0),
          m_j(0),
          m_k(0),
          m_centrePtBBox()
      {
      }

      inline GridIterator(int numPtsX, int numPtsY, int numPtsZ, double
sphereRadius)
        : m_sphereRadius(sphereRadius),
          m_minI(0),
          m_minJ(0),
          m_minK(0),
          m_maxI(numPtsX),
          m_maxJ(numPtsZ),
          m_maxK(numPtsY),
          m_i(0),
          m_j(0),
          m_k(0),
          m_centrePtBBox()
      {
        Vec3 minPt;
        Vec3 maxPt;
        if (numPtsZ > 1)
        {
          minPt = Vec3(0,0,0) + sphereRadius;
          maxPt = 
            Vec3(
              (numPtsX-1)*2.0*sphereRadius
              +
              (
                (numPtsZ > 1)
                ?
                sphereRadius
                :
                0.0
              )
              +
              (
                (numPtsY > 1)
                ?
                sphereRadius
                :
                0.0
              ),
              (numPtsY-1)*(sphereRadius*2.0*sqrt(2.0/3.0)),
              (numPtsZ-1)*(sphereRadius*sqrt(3.0))
              +
              ((numPtsY > 1) ? sphereRadius*sqrt(3.0)/3.0 : 0.0)
            );
        } else {
          minPt = Vec3(sphereRadius, sphereRadius, 0);
          maxPt = 
            Vec3(
              (numPtsX-1)*2.0*sphereRadius
              +
              (
                (numPtsY > 1)
                ?
                sphereRadius :
                0.0
              ),
              (numPtsY-1)*(sphereRadius*sqrt(3.0)),
              0
            );
          m_minJ = m_maxJ;
        }
        m_centrePtBBox = BoundingBox(minPt, (minPt + maxPt));
      }

	/*!
	  setup GridIterator from bounding box and sphere radius
	*/
	inline GridIterator(const BoundingBox &particleBBox, double sphereRadius, bool hard_limit=false)
        : m_sphereRadius(sphereRadius),
          m_minI(0),
          m_minJ(0),
          m_minK(0),
          m_maxI(0),
          m_maxJ(0),
          m_maxK(0),
          m_i(0),
          m_j(0),
          m_k(0),
          m_centrePtBBox()
      {
	int numPtsX ,numPtsY ,numPtsZ;
	if(!hard_limit){ // find "best fit"
	  numPtsX = max(1, int(nearbyint((particleBBox.getSizes().X()-(sphereRadius/4.0))/(sphereRadius*2.0))));
	  numPtsY = max(1, int(nearbyint(particleBBox.getSizes().Y()/(sphereRadius*2.0*sqrt(2.0/3.0)))));
	  numPtsZ = max(1, int(nearbyint(particleBBox.getSizes().Z()/(sphereRadius*sqrt(3.0)))));
	} else { // bounding box is hard limit
	  numPtsX = max(1, int(floor((particleBBox.getSizes().X()-(sphereRadius/4.0))/(sphereRadius*2.0))));
	  numPtsY = max(1, int(floor(particleBBox.getSizes().Y()/(sphereRadius*2.0*sqrt(2.0/3.0)))));
	  numPtsZ = max(1, int(floor(particleBBox.getSizes().Z()/(sphereRadius*sqrt(3.0)))));
	}
        if ((numPtsZ > 1) && (numPtsY > 1)) {
          numPtsX--;
        }
        if (particleBBox.getSizes().Z() > 0.0) {
          const Vec3 minPt = particleBBox.getMinPt() + sphereRadius;
          const Vec3 maxPt = 
            Vec3(
              (numPtsX-1)*2.0*sphereRadius + ((numPtsZ > 1) ? sphereRadius : 0.0) + ((numPtsY > 1) ? sphereRadius : 0.0),
              (numPtsY-1)*(sphereRadius*2.0*sqrt(2.0/3.0)),
              (numPtsZ-1)*(sphereRadius*sqrt(3.0))
              +
              ((numPtsY > 1) ? sphereRadius*sqrt(3.0)/3.0 : 0.0)
            );
          m_centrePtBBox = BoundingBox(minPt, (minPt + maxPt));
        } else {
          numPtsX = int(nearbyint((particleBBox.getSizes().X()-(sphereRadius/4.0))/(sphereRadius*2.0)));
          numPtsY = int(nearbyint(particleBBox.getSizes().Y()/(sphereRadius*sqrt(3.0))));
          numPtsZ = 0;

          Vec3 minPt = particleBBox.getMinPt() + sphereRadius;
          minPt.Z() = particleBBox.getMinPt().Z();
          const Vec3 maxPt = 
            Vec3(
              (numPtsX-1)*2.0*sphereRadius + ((numPtsY > 1) ? sphereRadius : 0.0),
              (numPtsY-1)*(sphereRadius*sqrt(3.0)),
              particleBBox.getMaxPt().Z()
            );
          m_centrePtBBox = BoundingBox(minPt, (minPt + maxPt));
        }
        m_minI = 0;
        m_minJ = 0;
        m_minK = 0;
        m_maxI = numPtsX;
        m_maxK = numPtsY;          
        m_maxJ = numPtsZ;

        m_i = m_minI;
        m_j = m_minJ;
        m_k = m_minK;
      }

      inline ~GridIterator()
      {
      }

      inline const BoundingBox &getBoundingBox() const
      {
        return m_centrePtBBox;
      }

      inline BoundingBox getSphereBBox() const
      {
        return
          (
            is2d()
            ?
              BoundingBox(
                m_centrePtBBox.getMinPt() - Vec3(m_sphereRadius, m_sphereRadius, 0.0),
                m_centrePtBBox.getMaxPt() + Vec3(m_sphereRadius, m_sphereRadius, 0.0)
              )
            :
              BoundingBox(
                m_centrePtBBox.getMinPt() - m_sphereRadius,
                m_centrePtBBox.getMaxPt() + m_sphereRadius
              )
          );
      }

      /**
       * Returns whether there are any points remaining
       * in the iteration.
       */
      inline bool hasNext() const
      {
        return (m_i < m_maxI);
      }

      inline bool is2d() const
      {
        return (m_minJ == m_maxJ);
      }

      inline Vec3 getPoint() const
      {
        if (is2d()) {
          return 
            Vec3(
              m_centrePtBBox.getMinPt().X() + (double(m_i)+0.5*double(m_k%2))*m_sphereRadius*2.0,
              m_centrePtBBox.getMinPt().Y() + double(m_k)*sqrt(3.0)*m_sphereRadius,
              0.0
            );
        } else {
          return
            Vec3(
              m_centrePtBBox.getMinPt().X() + (double(m_i)+0.5*double(m_j%2)+0.5*double(m_k%2))*m_sphereRadius*2.0,
              m_centrePtBBox.getMinPt().Y() + (double(m_k)*2.0*sqrt(2.0/3.0))*m_sphereRadius,
              m_centrePtBBox.getMinPt().Z() + ((double(m_j)+double(m_k%2)/3.0)*sqrt(3.0))*m_sphereRadius
            );
        }
      }

      inline void increment()
      {
        if (m_k+1 < m_maxK) {
          m_k++;
        }
        else if (m_j+1 < m_maxJ) {
          m_k = m_minK;
          m_j++;
        }
        else {
          m_k = m_minK;
          m_j = m_minJ;
          m_i++;
        }
      }

      /**
       * Returns the next regular-grid-point in the iteration.
       */
      inline Vec3 next()
      {
        const Vec3 pt = getPoint();
        increment();
        return pt;
      }

    private:
      double m_sphereRadius;

      int m_minI;
      int m_minJ;
      int m_minK;

      int m_maxI;
      int m_maxJ;
      int m_maxK;

      int m_i; //! index in x-direction
      int m_j; //! index in y-direction
      int m_k; //! index in z-direction
      
      BoundingBox m_centrePtBBox;
    };
  };
};

#endif