Fluid2D.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_FLUID2D_H
00026 #define SOFA_COMPONENT_BEHAVIORMODEL_EULERIANFLUID_FLUID2D_H
00027 
00028 #include <sofa/component/behaviormodel/eulerianfluid/Grid2D.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 
00034 
00035 namespace sofa
00036 {
00037 
00038 namespace component
00039 {
00040 
00041 namespace behaviormodel
00042 {
00043 
00044 namespace eulerianfluid
00045 {
00046 
00047   class Fluid2D : public sofa::core::BehaviorModel, public sofa::core::VisualModel
00048 {
00049 public:
00050     typedef Grid2D::real real;
00051     typedef Grid2D::vec2 vec2;
00052     typedef Vec<3,real> vec3;
00053 protected:
00054     int nx,ny;
00055     real cellwidth;
00056     
00057     Grid2D* fluid;
00058     Grid2D* fnext;
00059     Grid2D* ftemp;
00060     
00061 public:
00062     sofa::core::objectmodel::DataPtr<int> f_nx;
00063     sofa::core::objectmodel::DataPtr<int> f_ny;
00064     sofa::core::objectmodel::DataPtr<real> f_cellwidth;
00065     sofa::core::objectmodel::Data<real> f_height;
00066     sofa::core::objectmodel::Data<vec2> f_dir;
00067     sofa::core::objectmodel::Data<real> f_tstart;
00068     sofa::core::objectmodel::Data<real> f_tstop;
00069     
00070     Fluid2D();
00071     virtual ~Fluid2D();
00072     
00073     int getNx() const { return f_nx.getValue(); }
00074     void setNx(int v) { f_nx.setValue(v);       }
00075     
00076     int getNy() const { return f_ny.getValue(); }
00077     void setNy(int v) { f_ny.setValue(v);       }
00078 
00079     virtual void init();
00080 
00081     virtual void reset();
00082 
00083     virtual void updatePosition(double dt);
00084     
00085     virtual void drawVisual();
00086     
00087     virtual bool addBBox(double* minBBox, double* maxBBox);
00088 
00089     virtual void initVisual() {}
00090     
00091     virtual void updateVisual();
00092 
00093 protected:
00094     // marching cube
00095     
00096     struct Vertex
00097     {
00098         vec3 p;
00099         vec3 n;
00100     };
00101     
00102     struct Face
00103     {
00104         int p[3];
00105     };
00106     
00107     sofa::helper::vector<Vertex> points;
00108     sofa::helper::vector<Face> facets;
00109     
00111     struct CubeData
00112     {
00113         int p[3];
00114     };
00115 
00116     // temporary storage for marching cube
00117     sofa::helper::vector<CubeData> planes;
00118     //typename sofa::helper::vector<CubeData>::iterator P0; /// Pointer to first plane
00119     //typename sofa::helper::vector<CubeData>::iterator P1; /// Pointer to second plane
00120 
00121     template<int C>
00122     int addPoint(int x,int y,int z, real v0, real v1, real iso)
00123     {
00124         int p = points.size();
00125         vec3 pos((real)x,(real)y,(real)z);
00126         pos[C] -= (iso-v0)/(v1-v0);
00127         points.resize(p+1);
00128         points[p].p = pos; // * cellwidth;
00129         return p;
00130     }
00131 
00132     int addFace(int p1, int p2, int p3)
00133     {
00134         int nbp = points.size();
00135         if ((unsigned)p1<(unsigned)nbp &&
00136              (unsigned)p2<(unsigned)nbp &&
00137              (unsigned)p3<(unsigned)nbp)
00138         {
00139             int f = facets.size();
00140             facets.resize(f+1);
00141             facets[f].p[0] = p1;
00142             facets[f].p[1] = p2;
00143             facets[f].p[2] = p3;
00144             return f;
00145         }
00146         else
00147         {
00148             std::cerr << "ERROR: Invalid face "<<p1<<" "<<p2<<" "<<p3<<std::endl;
00149             return -1;
00150         }
00151     }
00152 
00153 };
00154 
00155 } // namespace eulerianfluid
00156 
00157 } // namespace behaviormodel
00158 
00159 } // namespace component
00160 
00161 } // namespace sofa
00162 
00163 #endif

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