Fluid3D.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_BEHAVIORMODEL_EULERIANFLUID_FLUID3D_H
00026 #define SOFA_COMPONENT_BEHAVIORMODEL_EULERIANFLUID_FLUID3D_H
00027 
00028 #include <sofa/component/behaviormodel/eulerianfluid/Grid3D.h>
00029 #include <sofa/core/BehaviorModel.h>
00030 #include <sofa/core/VisualModel.h>
00031 #include <sofa/core/objectmodel/DataPtr.h>
00032 #include <sofa/core/objectmodel/Data.h>
00033 #include <sofa/helper/MarchingCubeUtility.h>
00034 
00035 namespace sofa
00036 {
00037 
00038 namespace component
00039 {
00040 
00041 namespace behaviormodel
00042 {
00043 
00044 namespace eulerianfluid
00045 {
00046 
00047 class Fluid3D : public sofa::core::BehaviorModel, public sofa::core::VisualModel
00048 {
00049 public:
00050     typedef Grid3D::real real;
00051     typedef Grid3D::vec3 vec3;
00052 protected:
00053     int nx,ny,nz;
00054     real cellwidth;
00055     
00056     Grid3D* fluid;
00057     Grid3D* fnext;
00058     Grid3D* ftemp;
00059     
00060 public:
00061     sofa::core::objectmodel::DataPtr<int> f_nx;
00062     sofa::core::objectmodel::DataPtr<int> f_ny;
00063     sofa::core::objectmodel::DataPtr<int> f_nz;
00064     sofa::core::objectmodel::DataPtr<real> f_cellwidth;
00065     sofa::core::objectmodel::Data<vec3> f_center;
00066     sofa::core::objectmodel::Data<real> f_height;
00067     sofa::core::objectmodel::Data<vec3> f_dir;
00068     sofa::core::objectmodel::Data<real> f_tstart;
00069     sofa::core::objectmodel::Data<real> f_tstop;
00070     
00071     Fluid3D();
00072     virtual ~Fluid3D();
00073     
00074     int getNx() const { return f_nx.getValue(); }
00075     void setNx(int v) { f_nx.setValue(v);       }
00076     
00077     int getNy() const { return f_ny.getValue(); }
00078     void setNy(int v) { f_ny.setValue(v);       }
00079     
00080     int getNz() const { return f_nz.getValue(); }
00081     void setNz(int v) { f_nz.setValue(v);       }
00082 
00083     virtual void init();
00084 
00085     virtual void reset();
00086 
00087     virtual void updatePosition(double dt);
00088     
00089     virtual void drawVisual();
00090     
00091     virtual void exportOBJ(std::string name, std::ostream* out, std::ostream* mtl, int& vindex, int& nindex, int& tindex);
00092     
00093     virtual void initVisual() {}
00094     
00095     virtual void updateVisual();
00096     
00097     virtual bool addBBox(double* minBBox, double* maxBBox);
00098 
00099 protected:
00100     // marching cube
00101     
00102     struct Vertex
00103     {
00104         vec3 p;
00105         vec3 n;
00106     };
00107     
00108     struct Face
00109     {
00110         int p[3];
00111     };
00112     
00113     sofa::helper::vector<Vertex> points;
00114     sofa::helper::vector<Face> facets;
00115     
00117     struct CubeData
00118     {
00119         int p[3];
00120     };
00121 
00122     // temporary storage for marching cube
00123     sofa::helper::vector<CubeData> planes;
00124     //typename sofa::helper::vector<CubeData>::iterator P0; /// Pointer to first plane
00125     //typename sofa::helper::vector<CubeData>::iterator P1; /// Pointer to second plane
00126 
00127     template<int C>
00128     int addPoint(int x,int y,int z, real v0, real v1, real iso)
00129     {
00130         int p = points.size();
00131         vec3 pos((real)x,(real)y,(real)z);
00132         pos[C] -= (iso-v0)/(v1-v0);
00133         points.resize(p+1);
00134         points[p].p = pos; // * cellwidth;
00135         return p;
00136     }
00137 
00138     int addFace(int p1, int p2, int p3)
00139     {
00140         int nbp = points.size();
00141         if ((unsigned)p1<(unsigned)nbp &&
00142              (unsigned)p2<(unsigned)nbp &&
00143              (unsigned)p3<(unsigned)nbp)
00144         {
00145             int f = facets.size();
00146             facets.resize(f+1);
00147             facets[f].p[0] = p1;
00148             facets[f].p[1] = p2;
00149             facets[f].p[2] = p3;
00150             return f;
00151         }
00152         else
00153         {
00154             std::cerr << "ERROR: Invalid face "<<p1<<" "<<p2<<" "<<p3<<std::endl;
00155             return -1;
00156         }
00157     }
00158 
00159 };
00160 
00161 } // namespace eulerianfluid
00162 
00163 } // namespace behaviormodel
00164 
00165 } // namespace component
00166 
00167 } // namespace sofa
00168 
00169 #endif

Generated on Sun Nov 23 02:29:31 2008 for Sofa by  doxygen 1.5.7.1