ESyS-Particle  4.0.1
IntersectionVolCalculator.h
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 
00014 #ifndef ESYS_LSM_IMPLINTERSECTIONVOLCALCULATOR_H
00015 #define ESYS_LSM_IMPLINTERSECTIONVOLCALCULATOR_H
00016 
00017 #include <math.h>
00018 
00019 namespace esys
00020 {
00021   namespace lsm
00022   {
00023     namespace impl
00024     {
00025       double square(double val);
00026 
00027       template <int tmplDim, typename TmplVec>
00028       class DimBasicBox
00029       {
00030       public:
00031         typedef TmplVec Vec;
00032         DimBasicBox(const Vec &minPt, const Vec &maxPt);
00033   
00034         const Vec &getMinPt() const;
00035   
00036         const Vec &getMaxPt() const;
00037   
00038         double getVolume() const;
00039         
00040         template <typename TmplSphere>
00041         bool intersectsWith(const TmplSphere &sphere) const;
00042   
00043         bool intersectsWith(const Vec &pt) const;
00044   
00045         template <typename TmplSphere>
00046         bool contains(const TmplSphere &sphere) const;
00047   
00048       private:
00049         Vec m_minPt;
00050         Vec m_maxPt;
00051       };
00052   
00053       template <int tmplDim, typename TmplVec>
00054       class DimPlane
00055       {
00056       public:
00057         typedef TmplVec Vec;
00058   
00059         static double norm(const Vec &pt);
00060   
00061         static double dot(const Vec &p1, const Vec &p2);
00062         
00063         DimPlane();
00064   
00065         DimPlane(const Vec &normal, const Vec &pt);
00066   
00067         DimPlane(const DimPlane &plane);
00068   
00069         DimPlane &operator=(const DimPlane &plane);
00070   
00071         double getSignedDistanceTo(const Vec &pt) const;
00072   
00073         double getDistanceTo(const Vec &pt) const;
00074   
00075         const Vec &getNormal() const;
00076   
00077       private:
00078         Vec    m_normal;
00079         Vec    m_pt;
00080         double m_invNormalNorm;
00081       };
00082   
00083       template <int tmplDim, typename TmplVec>
00084       class DimBasicSphere
00085       {
00086       public:
00087         typedef TmplVec                 Vec;
00088         typedef DimPlane<tmplDim, Vec> Plane;
00089   
00090         static const double FOUR_THIRDS_PI;
00091         static const double ONE_THIRD_PI;
00092 
00093         DimBasicSphere();
00094         
00095         DimBasicSphere(const Vec &centrePt, double radius);
00096 
00097         DimBasicSphere(const DimBasicSphere &sphere);
00098 
00099         DimBasicSphere &operator=(const DimBasicSphere &sphere);
00100 
00101         double getRadius() const;
00102 
00103         const Vec &getCentre() const;
00104 
00105         double getVolume() const;
00106 
00107         double getVolume(const Vec &minPt, const Vec &maxPt, const int dimX = 0, const int dimY = 1) const;
00108 
00109         bool intersectsWith(const Vec &pt) const;
00110 
00111         double getSegmentVolume(const Plane &plane) const;
00112 
00113       private:
00114         Vec    m_centre;
00115         double m_radius;
00116       };
00117   
00118       template <int tmplDim, typename TmplVec>
00119       class IntersectionVolCalculator
00120       {
00121       public:
00122         typedef TmplVec                     Vec;
00123         typedef DimBasicSphere<tmplDim,Vec> BasicSphere;
00124         typedef DimBasicBox<tmplDim,Vec>    BasicBox;
00125         typedef DimPlane<tmplDim,Vec>       Plane;
00126   
00127         static Vec getNormal(int dim);
00128   
00129         static Vec getNegNormal(int dim);
00130         
00131         class VolumeSphere
00132         {
00133         public:
00134           VolumeSphere();
00135 
00136           VolumeSphere(const BasicSphere &sphere);
00137 
00138           VolumeSphere(const VolumeSphere &sphere);
00139 
00140           VolumeSphere &operator=(const VolumeSphere &sphere);
00141 
00142           double getRadius() const;
00143           
00144           const Vec &getCentre() const;
00145           
00146           double getVolume() const;
00147 
00148           double getVolume(const Vec &minPt, const Vec &maxPt, const int dimX = 0, const int dimY = 1) const;
00149 
00150           double calcVolume() const;
00151 
00152           bool intersectsWith(const Vec &pt) const;
00153 
00154           double getSegmentVolume(const Plane &plane) const;
00155 
00156         private:
00157           BasicSphere m_sphere;
00158           double      m_volume;
00159         };
00160   
00161         class Vertex
00162         {
00163         public:
00164           Vertex();
00165   
00166           Vertex(const Vec &pt);
00167           
00168           Vertex(const Vertex &vtx);
00169           
00170           Vertex &operator=(const Vertex &vtx);
00171   
00172           const Vec &getPoint() const;
00173   
00174           void setPoint(const Vec &pt);
00175   
00176         private:
00177           Vec        m_pt;
00178         };
00179         
00180         class VertexBox : public BasicBox
00181         {
00182         public:
00183           VertexBox(const BasicBox &box);
00184 
00185           VertexBox(const VertexBox &box);
00186 
00187           VertexBox &operator=(const VertexBox &box);
00188 
00189           void createVertices();
00190 
00191           const Vertex &getVertex(int i) const;
00192 
00193           static int getNumVertices();
00194 
00195         private:
00196           static const int  s_numVertices = ((tmplDim == 2) ? 4 :  8);
00197           Vertex            m_vertexArray[s_numVertices];
00198         };
00199 
00200         IntersectionVolCalculator(const BasicBox &box);
00201 
00202         const VolumeSphere &getSphere() const;
00203 
00204         void setSphere(const BasicSphere &sphere);
00205 
00206         const BasicBox &getBox() const;
00207 
00208         const VertexBox &getVertexBox() const;
00209 
00210         static Vec componentMin(const Vec &p1, const Vec &p2);
00211 
00212         static Vec componentMax(const Vec &p1, const Vec &p2);
00213 
00214         double getInsidePointVolume(const Vec &pt) const;
00215 
00216         double getTwoPlaneVolume(const Vec &pt, const int orientDim) const;
00217 
00218         double getOutsidePointVolume(const Vec &pt) const;
00219 
00220         double getVolume(const Vertex &vtx);
00221 
00222         double getVertexVolume(const BasicSphere &sphere);
00223 
00224         bool sphereContainsBox(const BasicSphere &sphere) const;
00225 
00226         double getVolume(const BasicSphere &sphere);
00227 
00228       private:
00229         VolumeSphere m_sphere;
00230         VertexBox    m_box;
00231       };
00232     }
00233   }
00234 }
00235 
00236 #include "Geometry/IntersectionVolCalculator.hpp"
00237 
00238 #endif