File: SphereNeighbours.hpp

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 (276 lines) | stat: -rw-r--r-- 7,710 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
/////////////////////////////////////////////////////////////
//                                                         //
// 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          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "Foundation/StringUtil.h"
#include <functional>
#include <limits>

namespace esys
{
  namespace lsm
  {
    template <typename TmplSphere, typename TmplIdPair>
    SphereNeighbours<TmplSphere,TmplIdPair>::SphereNeighbours(
      double maxDist,
      const BoundingBox &bBox,
      const BoolVector &circDimensions
    )
      : m_connectionPoolPtr(new IdPairPool(4096)),
        m_connectionSet(),
        m_nTablePtr(),
        m_minRadius(std::numeric_limits<double>::max()),
        m_maxRadius(-std::numeric_limits<double>::max()),
        m_maxDist(maxDist),
        m_minPt(bBox.getMinPt()),
        m_maxPt(bBox.getMaxPt())
    {
      const double gridSize =
        (
          max(
            bBox.getSizes()[0],
            max(
              bBox.getSizes()[1],
              bBox.getSizes()[2]
            )
          )
        )/5.0;
      m_nTablePtr =
        NTablePtr(
          new NTable(
            bBox,
            gridSize,
            circDimensions,
            2*gridSize
          )
      );
    }

    template <typename TmplSphere, typename TmplIdPair>
    SphereNeighbours<TmplSphere,TmplIdPair>::~SphereNeighbours()
    {
    }

    template <typename TmplSphere, typename TmplIdPair>
    int SphereNeighbours<TmplSphere,TmplIdPair>::getNumSpheres() const
    {
      return m_nTablePtr->size();
    }

    template <typename TmplSphere, typename TmplIdPair>
    int SphereNeighbours<TmplSphere,TmplIdPair>::getNumIdPairs() const
    {
      return m_connectionSet.size();
    }

    template <typename TmplSphere, typename TmplIdPair>
    double SphereNeighbours<TmplSphere,TmplIdPair>::getMinRadius() const
    {
      return m_minRadius;
    }

    template <typename TmplSphere, typename TmplIdPair>
    double SphereNeighbours<TmplSphere,TmplIdPair>::getMaxRadius() const
    {
      return m_maxRadius;
    }

    template <typename TmplSphere, typename TmplIdPair>
    typename SphereNeighbours<TmplSphere,TmplIdPair>::SphereConstIterator
    SphereNeighbours<TmplSphere,TmplIdPair>::getSphereIterator() const
    {
      return m_nTablePtr->getIterator();
    }

    template <typename TmplSphere, typename TmplIdPair>
    const typename SphereNeighbours<TmplSphere,TmplIdPair>::IdPair &
    SphereNeighbours<TmplSphere,TmplIdPair>::createIdPair(
      const Sphere &p1,
      const Sphere &p2
    )
    {
      return 
        **(m_connectionSet.insert(
          m_connectionPoolPtr->construct(p1.getId(), p2.getId())
        ).first);
    }

    template <typename TmplSphere>
    class CmpSphereId
    {
    public:
      bool operator()(const TmplSphere &p1, const TmplSphere &p2) const
      {
        return (p1.getId() < p2.getId());
      }

      bool operator()(const TmplSphere *p1, const TmplSphere *p2) const
      {
        return (p1->getId() < p2->getId());
      }
    };

    template <typename TmplType>
    class Deref
    {
    public:
      typedef const TmplType& result_type;
      typedef const TmplType* argument_type;
  
      result_type
      operator()(argument_type a) const
      { return *a; }
    };
    
    template <typename TmplSphere, typename TmplIdPair>
    template <typename TmplSphereIterator>
    typename SphereNeighbours<TmplSphere,TmplIdPair>::IdPairVector
    SphereNeighbours<TmplSphere,TmplIdPair>::getNeighbours(
      TmplSphereIterator it
    )
    {
      // Put the spheres in the neighbour table
      typedef std::set<Sphere *, CmpSphereId<Sphere> > SphereSet;
      SphereSet pSet;
      while (it.hasNext())
      {
        Sphere &p = it.next();
        insert(p);
        pSet.insert(&p);
      }
      ConstIdPairSet idPairSet;
      // Resize ntable according to min and max sphere radii.
      m_nTablePtr->resize(
        getSphereBBox(),
        4.1*getMinRadius(),
        2.1*getMaxRadius()
      );

      // For each sphere in the iterator it, determine it's
      // neighours within m_maxDist distance.
      for (
        typename SphereSet::const_iterator pIt = pSet.begin();
        pIt != pSet.end();
        pIt++
      )
      {
        // get the vector of spheres which are contained in nearby cells.
        typename NTable::ParticleVector nVector =
          m_nTablePtr->getNeighbourVector(
            (*pIt)->getPos(),
            (*pIt)->getRad() + m_maxDist
          );
        // for each of these cell-spheres, determine if they
        // are closer than m_maxDist.
        for (
          typename NTable::ParticleVector::const_iterator nIt = nVector.begin();
          nIt != nVector.end();
          nIt++
        )
        {
          Sphere *p1 = (*pIt);
          Sphere *p2 = (*nIt);

          if (
            (
              (p1->getId() < p2->getId())
              &&
              (pSet.find(p1) != pSet.end())
              &&
              (pSet.find(p2) != pSet.end())
            )
            ||
            (
              ((pSet.find(p1)==pSet.end()) && (pSet.find(p2)!= pSet.end()))
              ||
              ((pSet.find(p1)!=pSet.end()) && (pSet.find(p2)== pSet.end()))
            )
          )
          {
            p1 =
              ((*pIt)->getId() < (*nIt)->getId())
              ?
              (*pIt)
              :
              (*nIt);
            p2 =
              ((*pIt)->getId() < (*nIt)->getId())
              ?
              (*nIt)
              :
              (*pIt);
            const double radiusSumPlusTol =
              m_maxDist + p1->getRad() + p2->getRad();
            const double radiusSumPlusTolSqrd =
              radiusSumPlusTol*radiusSumPlusTol;
  
            if (
              (p1->getPos() - p2->getPos()).norm2()
              <=
              (radiusSumPlusTolSqrd)
            )
            {
              idPairSet.insert(&createIdPair(*p1, *p2));
            }
          }
        }
      }
      IdPairVector idPairVector;
      idPairVector.reserve(idPairSet.size());
      std::transform(
        idPairSet.begin(),
        idPairSet.end(),
        std::back_insert_iterator<IdPairVector>(idPairVector),
        Deref<IdPair>()
      );
      return idPairVector;
    }

    template <typename TmplSphere, typename TmplIdPair>
    void
    SphereNeighbours<TmplSphere,TmplIdPair>::insert(Sphere &p)
    {
      if (p.getRad() < m_minRadius)
      {
        m_minRadius = p.getRad();
      }
      if (p.getRad() > m_maxRadius)
      {
        m_maxRadius = p.getRad();
      }

      m_nTablePtr->insert(p);

      for (int i = 0; i < 3; i++)
      {
        if (!(m_nTablePtr->getPeriodicDimensions()[i]))
        {
          if (p.getPos()[i]-p.getRad() < m_minPt[i])
          {
            m_minPt[i] = p.getPos()[i]-p.getRad();
          }
          if (p.getPos()[i]+p.getRad() > m_maxPt[i])
          {
            m_maxPt[i] = p.getPos()[i]+p.getRad();
          }
        }
      }
    }

    template <typename TmplSphere, typename TmplIdPair>
    BoundingBox
    SphereNeighbours<TmplSphere,TmplIdPair>::getSphereBBox() const
    {
      return BoundingBox(m_minPt, m_maxPt);
    }
  }
}