~esys-p-dev/esys-particle/2.1

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
/////////////////////////////////////////////////////////////
//                                                         //
// 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/console.h"
#include "Geometry/RandomBlockGenerator.h"
#include "Geometry/SimpleParticle.h"
#include "Geometry/GridIterator.h"
#include "Geometry/SimpleNTable.h"
#include "Geometry/Sphere3d.h"
#include "Geometry/Plane.h"
#include "Geometry/ParticleFitter.h"

#include <algorithm>
#include <stdexcept>
#include <float.h>

namespace esys
{
  namespace lsm
  {
    RandomBlockGenerator::RandomBlockGenerator(
      NTable            &nTable,
      ParticlePool      &particlePool,
      const BoundingBox &bBox,
      const BoolVector  &periodicDimensions,
      double            tolerance,
      double            minSphereRadius,
      double            maxSphereRadius,
      const PlaneVector &fitPlaneVector,
      int               maxInsertionFailures
    )
      : BlockGenerator(nTable, particlePool, bBox, periodicDimensions, tolerance),
        m_minRadius(minSphereRadius),
        m_maxRadius(maxSphereRadius),
        m_fitPlaneVector(fitPlaneVector),
        m_maxInsertionFailures(maxInsertionFailures)
    {
    }

    RandomBlockGenerator::~RandomBlockGenerator()
    {
    }

    double RandomBlockGenerator::getRandom(double minVal, double maxVal) const
    {
      return 
        minVal
        +
        ((maxVal-minVal)*(static_cast<double>(rand()))/(static_cast<double>(RAND_MAX)));
    }

    double RandomBlockGenerator::getGridRadius() const
    {
      return m_maxRadius;
    }

    double RandomBlockGenerator::getRadius() const
    {
      return getRandom(m_minRadius, m_maxRadius);
    }

    class PlaneComparer
    {
    public:
      PlaneComparer(const SimpleParticle &particle)
        : m_pParticle(&particle)
      {
      }
      
      inline bool operator()(const Plane &plane1, const Plane &plane2) const
      {
        return 
          (
            plane1.sep(m_pParticle->getPos())
            <
            plane2.sep(m_pParticle->getPos())
          );
      }
    private:
      const SimpleParticle *m_pParticle;
    };

    const PlaneVector &RandomBlockGenerator::getFitPlaneVector() const
    {
      return m_fitPlaneVector;
    }

    Plane RandomBlockGenerator::getClosestFitPlane(const SimpleParticle &particle) const
    {
      PlaneVector fitPlanes = getFitPlaneVector();
      if (fitPlanes.size() > 0) {
        std::sort(fitPlanes.begin(), fitPlanes.end(), PlaneComparer(particle));

        return fitPlanes[0];
      }
      return Plane(Vec3(-1.0, 0, 0), Vec3(DBL_MAX/2, DBL_MAX/2, DBL_MAX/2));
    }

    Vec3 RandomBlockGenerator::getRandomPoint() const
    {
      return 
        Vec3(
          getRandom(getBBox().getMinPt().X(), getBBox().getMaxPt().X()),
          getRandom(getBBox().getMinPt().Y(), getBBox().getMaxPt().Y()),
          getRandom(getBBox().getMinPt().Z(), getBBox().getMaxPt().Z())
        );
    }

    RandomBlockGenerator::ParticleVector RandomBlockGenerator::getClosestNeighbors(
      const SimpleParticle& particle,
      int numClosest
    )
    {
      ParticleVector neighbourVector =
        getNTable().getUniqueNeighbourVector(particle.getPos(), m_maxRadius + getTolerance());
      std::sort(neighbourVector.begin(), neighbourVector.end(), ParticleComparer(particle));

      if (neighbourVector.size() > static_cast<size_t>(numClosest)) {
        neighbourVector.erase(neighbourVector.begin() + numClosest, neighbourVector.end());
      }

      return neighbourVector;
    }

    bool RandomBlockGenerator::particleFits(const SimpleParticle &particle) const
    {
      return 
        (
          (particle.getRad() >= m_minRadius)
          &&
          (particle.getRad() <= m_maxRadius)
          &&
          BlockGenerator::particleFits(particle)
        );
    }

    int RandomBlockGenerator::getMaxInsertionFailures() const
    {
      return m_maxInsertionFailures;
    }

    FitterPtrVector RandomBlockGenerator::getFitterPtrVector()
    {
      FitterPtrVector fitters;
      fitters.push_back(FitterPtr(new MoveToSurfaceFitter(*this)));
      if (is2d())
      {
        fitters.push_back(FitterPtr(new TwoDParticleFitter(*this)));
        if (getFitPlaneVector().size() > 0) {
          fitters.push_back(FitterPtr(new TwoDPlaneParticleFitter(*this)));
        }
      }
      else
      {
        fitters.push_back(FitterPtr(new ThreeDParticleFitter(*this)));
        if (getFitPlaneVector().size() > 0) {
          fitters.push_back(FitterPtr(new ThreeDPlaneParticleFitter(*this)));
        }
      }

      return fitters;
    }

    void RandomBlockGenerator::generateFillParticles()
    {
      int numFails    = 0;
      int insertCount = 0;
      FitterPtrVector fitters = getFitterPtrVector();
      SimpleParticle particle = ParticleFitter::INVALID;
      SimpleParticle fitParticle = ParticleFitter::INVALID;
      Plane plane;
      ParticleVector neighbours;
      while (numFails < getMaxInsertionFailures()) {
        fitParticle = ParticleFitter::INVALID;
        particle = generateParticle(getRandomPoint());
        neighbours = getClosestNeighbors(particle, 4);
        plane = getClosestFitPlane(particle);
        
        for (
          FitterPtrVector::iterator it = fitters.begin();
          (
            (it != fitters.end())
            &&
            (!fitParticle.isValid())
          );
          it++
        )
        {
          fitParticle = (*it)->getFitParticle(particle, neighbours, plane);
        }
        if (fitParticle.isValid()) {
          numFails = 0;
          insertCount++;
          insertParticle(fitParticle);
        } else {
          numFails++;
        }
      }
      console.Info()
        << "BoundingBox: minPt = " << getBBox().getMinPt()
        << ", maxPt = " << getBBox().getMaxPt() << "\n"
        << "BoundingBox: sizes = " << getBBox().getSizes() << "\n";
      for (
          FitterPtrVector::iterator it = fitters.begin();
          (it != fitters.end());
          it++      
      ) {
        console.Info() << (*(*it)).toString() << "\n";
      }
      console.Info() << "Total inserted = " << insertCount << "\n";
    }

    void RandomBlockGenerator::generate()
    {
      generateSeedParticles();
      generateFillParticles();
    }
  };
};