VisualModelImpl.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_VISUALMODEL_VISUALMODELIMPL_H
00026 #define SOFA_COMPONENT_VISUALMODEL_VISUALMODELIMPL_H
00027 
00028 #include <string>
00029 #include <sofa/core/VisualModel.h>
00030 #include <sofa/core/componentmodel/behavior/MappedModel.h>
00031 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00032 #include <sofa/defaulttype/Vec.h>
00033 #include <sofa/defaulttype/VecTypes.h>
00034 #include <sofa/defaulttype/RigidTypes.h>
00035 #include <sofa/helper/io/Mesh.h>
00036 
00037 #include <map>
00038 
00039 namespace sofa
00040 {
00041 
00042 namespace component
00043 {
00044 
00045 namespace visualmodel
00046 {
00047 
00048 using namespace sofa::defaulttype;
00049 
00050 
00051 class RigidMappedModel : public core::componentmodel::behavior::MappedModel< Rigid3fTypes >
00052 {
00053 public:
00054     VecCoord xforms;
00055     bool xformsModified;
00056 
00057     RigidMappedModel()
00058     : xformsModified(false)
00059     {
00060     }
00061 
00062     const VecCoord* getX()  const { return &xforms; }
00063     const VecDeriv* getV()  const { return NULL; }
00064 
00065     VecCoord* getX()  { xformsModified = true; return &xforms;   }
00066     VecDeriv* getV()  { return NULL; }
00067 
00068     const VecCoord* getRigidX()  const { return getX(); }
00069     VecCoord* getRigidX()  { return getX(); }
00070 };
00071 
00072 class ExtVec3fMappedModel : public core::componentmodel::behavior::MappedModel< ExtVec3fTypes >
00073 {
00074 public:
00075     ResizableExtVector<Coord>* inputVertices;
00076     bool modified; 
00077 
00078     ExtVec3fMappedModel()
00079     : inputVertices(NULL), modified(false)
00080     {
00081     }
00082 
00083     const VecCoord* getX()  const { return inputVertices; }
00084     const VecDeriv* getV()  const { return NULL; }
00085 
00086     VecCoord* getX()  { modified = true; return inputVertices; }
00087     VecDeriv* getV()  { return NULL; }
00088 
00089     const VecCoord* getVecX()  const { return getX(); }
00090     VecCoord* getVecX()  { return getX(); }
00091 };
00092 
00103 class VisualModelImpl : public core::VisualModel, public ExtVec3fMappedModel, public RigidMappedModel
00104 {
00105 protected:
00106     // use types from ExtVec3fTypes
00107 
00108     typedef ExtVec3fTypes::Real Real;
00109     typedef ExtVec3fTypes::Coord Coord;
00110     typedef ExtVec3fTypes::VecCoord VecCoord;
00111     typedef ExtVec3fTypes::Deriv Deriv;
00112     typedef ExtVec3fTypes::VecDeriv VecDeriv;
00113 
00114 
00115     typedef Vec<2, float> TexCoord;
00116     typedef helper::fixed_array<int, 3> Triangle;
00117     typedef helper::fixed_array<int, 4> Quad;
00118     //typedef helper::fixed_array<int, 4> Tetrahedron;
00119 
00120     //ResizableExtVector<Coord>* inputVertices;
00121 
00122     bool useTopology; 
00123     int lastMeshRev; 
00124     bool useNormals; 
00125     bool castShadow; 
00126 
00127     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00128 
00129 /*     Data< ResizableExtVector<Coord> > vertices; */
00130     DataPtr< ResizableExtVector<Coord> > field_vertices;
00131     ResizableExtVector<Coord> vertices;
00132 
00133     DataPtr< ResizableExtVector<Coord> > field_vnormals;
00134     ResizableExtVector<Coord> vnormals;
00135     DataPtr< ResizableExtVector<TexCoord> > field_vtexcoords;
00136     ResizableExtVector<TexCoord> vtexcoords;
00137 
00138     DataPtr< ResizableExtVector<Triangle> > field_triangles;
00139     ResizableExtVector<Triangle> triangles;
00140     DataPtr< ResizableExtVector<Quad> > field_quads;
00141     ResizableExtVector<Quad> quads;
00142 
00146     ResizableExtVector<int> vertPosIdx;
00147 
00150     ResizableExtVector<int> vertNormIdx;
00151 
00152     float scaleTex;
00153     Data< std::string > filename;
00154     Data< std::string > texturename;
00155     Data< Vector3 > translation;
00156     Data< Vector3 > rotation;
00157     Data< SReal > scale;
00158 
00159     Vec3f bbox[2];
00160 
00161     virtual void internalDraw()
00162     {}
00163 
00164 public:
00165     Data< sofa::helper::io::Mesh::Material > material;
00166 
00167     VisualModelImpl();
00168 
00169     ~VisualModelImpl();
00170 
00171     void parse(core::objectmodel::BaseObjectDescription* arg);
00172 
00173     bool isTransparent();
00174 
00175     void drawVisual();
00176     void drawTransparent();
00177     void drawShadow();
00178 
00179     virtual bool loadTexture(const std::string& /*filename*/) { return false; }
00180 
00181     bool load(const std::string& filename, const std::string& loader, const std::string& textureName);
00182 
00183     void applyTranslation(const double dx, const double dy, const double dz);
00184     //Apply Rotation from Euler angles (in degree!)
00185     void applyRotation (const double rx, const double ry, const double rz);
00186     void applyRotation(const Quat q);
00187     void applyScale(const double s);
00188     void applyUVTranslation(const double dU, const double dV);
00189     void applyUVScale(const double su, const double sv);
00190 
00191     void flipFaces();
00192 
00193     void setFilename(std::string s){filename.setValue(s);}
00194     std::string getFilename(){return filename.getValue();}
00195     
00196     void setColor(float r, float g, float b, float a);
00197     void setColor(std::string color);
00198 
00199     void setUseNormals(bool val) { useNormals = val;  }
00200     bool getUseNormals() const   { return useNormals; }
00201 
00202     void setCastShadow(bool val) { castShadow = val;  }
00203     bool getCastShadow() const   { return castShadow; }
00204 
00205     void setMesh(helper::io::Mesh &m, bool tex=false);
00206     bool isUsingTopology() const {return useTopology;};
00207 
00208     ResizableExtVector<Coord> * getVertices() {return &vertices;}
00209     void setVertices(ResizableExtVector<Coord> * x){vertices = *x;}
00210     ResizableExtVector<Triangle> * getTriangles() {return &triangles;}
00211     void setTriangles(ResizableExtVector<Triangle> * t){triangles = *t;}
00212 
00213     virtual void computePositions();
00214     virtual void computeMesh();
00215     virtual void computeNormals();
00216     virtual void computeBBox();
00217 
00218     virtual void updateVisual();
00219 
00220     // handle topological changes
00221     virtual void handleTopologyChange();
00222 
00223     void init();
00224 
00225     void initVisual();
00226 
00227     bool addBBox(double* minBBox, double* maxBBox);
00228 
00229     //const VecCoord* getX()  const; // { return &x;   }
00230     //const VecDeriv* getV()  const { return NULL; }
00231 
00232     //VecCoord* getX(); //  { return &x;   }
00233     //VecDeriv* getV()  { return NULL; }
00234 
00238     virtual void exportOBJ(std::string name, std::ostream* out, std::ostream* mtl, int& vindex, int& nindex, int& tindex);
00239 
00240     virtual std::string getTemplateName() const
00241     {
00242         return ExtVec3fMappedModel::getTemplateName();
00243     }
00244 
00245     static std::string templateName(const VisualModelImpl* p = NULL)
00246     {
00247         return ExtVec3fMappedModel::templateName(p);
00248     }
00249 };
00250 
00251 //typedef Vec<3,GLfloat> GLVec3f;
00252 //typedef ExtVectorTypes<GLVec3f,GLVec3f> GLExtVec3fTypes;
00253 
00254 } // namespace visualmodel
00255 
00256 } // namespace component
00257 
00258 } // namespace sofa
00259 
00260 #endif

Generated on Sun Nov 23 03:50:11 2008 for Sofa by  doxygen 1.5.7.1