14 #ifndef ESYS_LSMNEIGHBOURTABLE_HPP
15 #define ESYS_LSMNEIGHBOURTABLE_HPP
21 template <
class TmplParticle>
22 NeighbourTable<TmplParticle>::NeighbourTable(
23 const BoundingBox &bBox,
28 m_maxIndex(Vec3L(-1, -1, -1)),
29 m_gridSpacing(gridSpacing),
31 m_insertedParticles(),
34 resize(bBox, gridSpacing);
37 template <
class TmplParticle>
38 NeighbourTable<TmplParticle>::NeighbourTable(
39 const NeighbourTable &nTable
41 : m_dimensions(nTable.m_dimensions),
42 m_minIndex(nTable.m_minIndex),
43 m_maxIndex(nTable.m_maxIndex),
44 m_gridSpacing(nTable.m_gridSpacing),
45 m_bBox(nTable.m_bBox),
46 m_insertedParticles(nTable.m_insertedParticles),
50 ParticleVectorArrayPtr(
51 new ParticleVector[nTable.getNumCells()]
53 for (
int i = 0; i < nTable.getNumCells(); i++)
55 m_tablePtr[i] = nTable.m_tablePtr[i];
59 template <
class TmplParticle>
60 NeighbourTable<TmplParticle>::~NeighbourTable()
64 template <
class TmplParticle>
65 void NeighbourTable<TmplParticle>::clear()
67 for (
int i = getMinVecIndex().X(); i <= getMaxVecIndex().X(); i++) {
68 for (
int j = getMinVecIndex().Y(); j <= getMaxVecIndex().Y(); j++) {
69 for (
int k = getMinVecIndex().Z(); k <= getMaxVecIndex().Z(); k++) {
70 m_tablePtr[getScalarIndex(i, j, k)].clear();
74 m_insertedParticles.clear();
77 template <
class TmplParticle>
78 double NeighbourTable<TmplParticle>::getGridSpacing()
const
83 template <
class TmplParticle>
84 void NeighbourTable<TmplParticle>::resize(
85 const BoundingBox &bBox,
89 ParticleVector particles = getInsertedParticles();
90 clearAndRecomputeGrid(bBox, gridSpacing);
92 typename ParticleVector::iterator it = particles.begin();
93 it != particles.end();
101 template <
class TmplParticle>
103 NeighbourTable<TmplParticle>::getDimensions()
const
108 template <
class TmplParticle>
110 NeighbourTable<TmplParticle>::getBBox()
const
115 template <
class TmplParticle>
117 NeighbourTable<TmplParticle>::getMinPt()
const
119 return getBBox().getMinPt();
122 template <
class TmplParticle>
125 return m_insertedParticles.size();
128 template <
class TmplParticle>
136 xIdx*m_dimensions.Z()*m_dimensions.Y()
138 yIdx*m_dimensions.Z()
143 template <
class TmplParticle>
144 int NeighbourTable<TmplParticle>::getScalarIndex(
const Vec3L &index)
const
146 return getScalarIndex(index.X(), index.Y(), index.Z());
149 template <
class TmplParticle>
151 NeighbourTable<TmplParticle>::getScalarIndex(
const Vec3 &pt)
const
153 return getScalarIndex(getVecIndex(pt));
156 template <
class TmplParticle>
158 NeighbourTable<TmplParticle>::getMinVecIndex()
const
163 template <
class TmplParticle>
165 NeighbourTable<TmplParticle>::getMaxVecIndex()
const
170 template <
class TmplParticle>
172 NeighbourTable<TmplParticle>::getVecIndex(
const Vec3 &pt)
const
174 const Vec3 relPos =
Vec3((pt - getMinPt())/m_gridSpacing);
175 const Vec3L index = Vec3L(
int(floor(relPos.X())), int(floor(relPos.Y())), int(floor(relPos.Z())));
176 return getMinVecIndex().max(getMaxVecIndex().min(index));
179 template <
class TmplParticle>
180 typename NeighbourTable<TmplParticle>::ParticleVector
181 NeighbourTable<TmplParticle>::getNeighbourVector(
186 ParticleVector neighbours;
187 neighbours.reserve(128);
188 const Vec3L min = getVecIndex(pt - radius);
189 const Vec3L max = getVecIndex(pt + radius);
190 for (
int i = min.X(); i <= max.X(); i++) {
191 for (
int j = min.Y(); j <= max.Y(); j++) {
192 for (
int k = min.Z(); k <= max.Z(); k++) {
195 m_tablePtr[getScalarIndex(i, j, k)].begin(),
196 m_tablePtr[getScalarIndex(i, j, k)].end()
204 template <
class TmplParticle>
205 typename NeighbourTable<TmplParticle>::ParticleVector
206 NeighbourTable<TmplParticle>::getUniqueNeighbourVector(
211 ParticleVector neighbours = getNeighbourVector(pt, radius);
212 std::sort(neighbours.begin(), neighbours.end());
213 typename ParticleVector::iterator uniqueEnd =
214 std::unique(neighbours.begin(), neighbours.end());
223 template <
class TmplParticle>
224 typename NeighbourTable<TmplParticle>::ParticleVector
225 NeighbourTable<TmplParticle>::getNeighbourVector(
229 return m_tablePtr[getScalarIndex(pt)];
232 template <
class TmplParticle>
233 void NeighbourTable<TmplParticle>::insert(Particle *pParticle)
235 const Vec3L minIdx = getVecIndex(pParticle->getPos() - pParticle->getRad());
236 const Vec3L maxIdx = getVecIndex(pParticle->getPos() + pParticle->getRad());
237 insertInTable(pParticle, minIdx, maxIdx);
238 addInserted(pParticle);
241 template <
class TmplParticle>
242 void NeighbourTable<TmplParticle>::insert(Particle &particle)
247 template <
class TmplParticle>
248 typename NeighbourTable<TmplParticle>::ParticleIterator
249 NeighbourTable<TmplParticle>::getParticleIterator()
251 return ParticleIterator(m_insertedParticles);
254 template <
class TmplParticle>
255 typename NeighbourTable<TmplParticle>::ParticleConstIterator
256 NeighbourTable<TmplParticle>::getParticleIterator()
const
258 return ParticleConstIterator(m_insertedParticles);
261 template <
class TmplParticle>
262 void NeighbourTable<TmplParticle>::insertInTable(
268 for (
int i = minIdx.X(); i <= maxIdx.X(); i++) {
269 for (
int j = minIdx.Y(); j <= maxIdx.Y(); j++) {
270 for (
int k = minIdx.Z(); k <= maxIdx.Z(); k++) {
271 m_tablePtr[getScalarIndex(i, j, k)].push_back(pParticle);
277 template <
class TmplParticle>
278 void NeighbourTable<TmplParticle>::addInserted(Particle *pParticle)
280 m_insertedParticles.push_back(pParticle);
283 template <
class TmplParticle>
284 int NeighbourTable<TmplParticle>::getNumCells()
const
286 return getDimensions()[0]*getDimensions()[1]*getDimensions()[2];
289 template <
class TmplParticle>
290 typename NeighbourTable<TmplParticle>::ParticleVector
291 NeighbourTable<TmplParticle>::getInsertedParticles()
const
293 return m_insertedParticles;
296 template <
class TmplParticle>
297 void NeighbourTable<TmplParticle>::clearAndRecomputeGrid(
298 const BoundingBox &bBox,
304 m_gridSpacing = gridSpacing;
306 const Vec3 dims = m_bBox.getSizes()/gridSpacing;
313 m_dimensions = m_dimensions.max(Vec3L(1, 1, 1));
316 ParticleVectorArrayPtr(
318 m_dimensions.X()*m_dimensions.Y()*m_dimensions.Z()
321 m_minIndex = Vec3L(0, 0, 0);
322 m_maxIndex = (m_dimensions - 1);