ESyS-Particle  4.0.1
ParticleCollection.hpp
00001 
00002 //                                                         //
00003 // Copyright (c) 2003-2011 by The University of Queensland //
00004 // Earth Systems Science Computational Centre (ESSCC)      //
00005 // http://www.uq.edu.au/esscc                              //
00006 //                                                         //
00007 // Primary Business: Brisbane, Queensland, Australia       //
00008 // Licensed under the Open Software License version 3.0    //
00009 // http://www.opensource.org/licenses/osl-3.0.php          //
00010 //                                                         //
00012 
00013 #include "Geometry/ParticleCollection.h"
00014 #include <stdexcept>
00015 #include <fstream>
00016 #include <sstream>
00017 #include <iomanip>
00018 
00019 #include <stdexcept>
00020 #include <boost/limits.hpp>
00021 
00022 namespace esys
00023 {
00024   namespace lsm
00025   {
00026     template <typename TmplParticle>
00027     ParticleCollection<TmplParticle>::ParticleCollection()
00028       : m_particlePoolPtr(),
00029         m_particleVector()
00030     {
00031       m_particlePoolPtr = ParticlePoolPtr(new ParticlePool(2048));
00032     }
00033 
00034     template <typename TmplParticle>
00035     ParticleCollection<TmplParticle>::ParticleCollection(
00036       ParticlePoolPtr particlePoolPtr
00037     )
00038       : m_particlePoolPtr(particlePoolPtr),
00039         m_particleVector()
00040     {
00041     }
00042 
00043     template <typename TmplParticle>
00044     ParticleCollection<TmplParticle>::ParticleCollection(const ParticleCollection &p)
00045       : m_particlePoolPtr(p.m_particlePoolPtr),
00046         m_particleVector(p.m_particleVector)
00047     {
00048     }
00049 
00050     template <typename TmplParticle>
00051     ParticleCollection<TmplParticle> &
00052     ParticleCollection<TmplParticle>::operator=(const ParticleCollection &p)
00053     {
00054       m_particlePoolPtr = p.m_particlePoolPtr;
00055       m_particleVector  = p.m_particleVector;
00056     }
00057 
00058     template <typename TmplParticle>
00059     void
00060     ParticleCollection<TmplParticle>::noCheckInsertRef(Particle &p)
00061     {
00062       m_particleVector.push_back(&p);
00063     }
00064 
00065     template <typename TmplParticle>
00066     void
00067     ParticleCollection<TmplParticle>::insertRef(Particle &p)
00068     {
00069       if (m_particlePoolPtr->is_from(&p))
00070       {
00071         noCheckInsertRef(p);
00072       }
00073       else
00074       {
00075         throw
00076           std::runtime_error(
00077             "ParticleCollection<TmplParticle>::insertRef: Tried to insert"
00078             " reference to non-created particle."
00079           );
00080       }
00081     }
00082 
00083     template <typename TmplParticle>
00084     typename ParticleCollection<TmplParticle>::Particle &
00085     ParticleCollection<TmplParticle>::createParticle(const Particle &p)
00086     {
00087       Particle *newP = m_particlePoolPtr->construct(p);
00088       noCheckInsertRef(*newP);
00089       return *newP;
00090     }
00091 
00092     template <typename TmplParticle>
00093     ParticleCollection<TmplParticle>::~ParticleCollection()
00094     {
00095     }
00096 
00097     template <typename TmplParticle>
00098     int ParticleCollection<TmplParticle>::getNumParticles() const
00099     {
00100       return m_particleVector.size();
00101     }
00102 
00103     template <typename TmplParticle>
00104     void ParticleCollection<TmplParticle>::translateBy(const Vec3 &vec)
00105     {
00106       ParticleIterator it = getParticleIterator();
00107       while (it.hasNext())
00108       {
00109         it.next().translateBy(vec);
00110       }
00111     }
00112 
00113     template <typename TmplParticle>
00114     void ParticleCollection<TmplParticle>::rotate(
00115         const Vec3 &rotation,
00116         const Vec3 &posn
00117     )
00118     {
00119       ParticleIterator it = getParticleIterator();
00120       while (it.hasNext())
00121       {
00122         it.next().rotate(rotation, posn);
00123       }
00124     }
00125 
00126     template <typename TmplParticle>
00127     void ParticleCollection<TmplParticle>::incrementIdBy(
00128       typename Particle::Id idIncr
00129     )
00130     {
00131       ParticleIterator it = getParticleIterator();
00132       while (it.hasNext())
00133       {
00134         Particle &p = it.next();
00135         p.setId(p.getId() + idIncr);
00136       }
00137     }
00138 
00139     template <typename TmplParticle>
00140     BoundingBox ParticleCollection<TmplParticle>::getParticleBBox() const
00141     {
00142       Vec3 minPt = Vec3(std::numeric_limits<double>::max());
00143       Vec3 maxPt = -minPt;
00144       ParticleConstIterator it = getParticleIterator();
00145       while (it.hasNext())
00146       {
00147         const Particle &next = it.next();
00148         minPt = cmin(minPt, next.getPos() - next.getRad());
00149         maxPt = cmax(maxPt, next.getPos() + next.getRad());
00150       }
00151       return BoundingBox(minPt, maxPt);
00152     }
00153   }
00154 }