MultiResSparseGridTopology.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_TOPOLOGY_MULTIRESSPARSEGRIDTOPOLOGY_H
00026 #define SOFA_COMPONENT_TOPOLOGY_MULTIRESSPARSEGRIDTOPOLOGY_H
00027 
00028 #include <sofa/component/topology/GridTopology.h>
00029 #include <sofa/defaulttype/Vec.h>
00030 #include <sofa/helper/vector.h>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string>
00034 #include <fstream>
00035 #include <iostream>
00036 #include <map>
00037 using std::cout;
00038 using std::cerr;
00039 using std::endl;
00040 namespace sofa
00041 {
00042 
00043 namespace component
00044 {
00045 
00046 namespace topology
00047 {
00048 
00049 using namespace sofa::defaulttype;
00050 using helper::vector;
00051 
00052 class MultiResSparseGridTopology : public core::componentmodel::topology::Topology
00053 {
00054         int nbPoints;
00055 public:
00056 
00057 
00080         class SparseGrid
00081         {
00082         public :
00084                 struct Vertex
00085                 {
00086                         //Vector3 vertexPosition;
00087                         int index;
00088                         /*            Vertex(float x=0, float y=0, float z=0)
00089                                     {
00090                                         //position = new float(3);
00091                                         vertexPosition[0]=x;
00092                                         vertexPosition[1]=y;
00093                                         vertexPosition[2]=z;
00094                                     };*/
00095                 };
00096 
00098                 struct Index3D
00099                 {
00100                         int i,j,k;
00101                         Index3D(int a=0,int b=0,int c=0):i(a),j(b),k(c)
00102                         {}
00103 
00104                         bool operator < (const Index3D& ind ) const
00105                         {
00106                                 return ( i<ind.i || i==ind.i && j<ind.j || i==ind.i && j==ind.j && k<ind.k );
00107                         }
00108                 };
00110                 struct Voxel
00111                 {
00112                         int vertices[8];
00113                         float density;
00114                         Voxel(int v1=0,int v2=0,int v3=0,int v4=0,int v5=0,int v6=0,int v7=0,int v8=0,float d=0.0)
00115                         {
00116                                 vertices[0]=v1;
00117                                 vertices[1]=v2;
00118                                 vertices[2]=v3;
00119                                 vertices[3]=v4;
00120                                 vertices[4]=v5;
00121                                 vertices[5]=v6;
00122                                 vertices[6]=v7;
00123                                 vertices[7]=v8;
00124                                 density = d;
00125                         }
00126                 };
00127 
00128         protected :
00130                 int FIN_DE_LISTE;
00132                 float scale;
00134                 vector<Vector3> vertices;
00136                 vector<Index3D> surfaceSparseGrid;
00138                 int numIndices;
00139                 vector<int> indices;
00141                 int numSurfaceIndices;
00142                 vector<int> surfaceIndices;
00144                 float* voxelsDensity;
00146                 float dimVoxX, dimVoxY, dimVoxZ;
00148                 int dimX,dimY,dimZ;
00149 
00150                 typedef std::map<Index3D, Vertex> VertexMap;
00151                 typedef std::map<Index3D, Voxel> VoxelMap;
00153                 VertexMap vertexMap;
00155                 VoxelMap voxelsMap;
00156 
00157         public:
00158 
00159                 SparseGrid(void);//sans rien
00160                 SparseGrid(int);// avec un indice de fin de liste
00161                 SparseGrid(float); // avec uune echelle
00162                 ~SparseGrid(void);
00164                 void initFromPNG( char* fileName,int color,int filesNo );
00166                 void initFromVOX( char* fileName,int color);
00168                 unsigned getNumSparseGrid() const
00169                 {
00170                         return voxelsMap.size();
00171                 }
00173                 unsigned getNumVertices() const
00174                 {
00175                         return vertexMap.size();
00176                 }
00178                 unsigned getNumFaceIndices() const
00179                 {
00180                         return numIndices;
00181                 }
00183                 unsigned getNumSurfaceIndices() const
00184                 {
00185                         return numSurfaceIndices;
00186                 }
00188                 vector< Vector3>& getVertices()
00189                 {
00190                         return vertices;
00191                 }
00192                 const vector<Vector3>& getVertices() const
00193                 {
00194                         return vertices;
00195                 }
00197                 vector<int>& getFaceIndices()
00198                 {
00199                         return indices;
00200                 }
00202                 std::map<Index3D,Voxel>& getSparseGridMap()
00203                 {
00204                         return voxelsMap;
00205                 }
00206                 const std::map<Index3D,Voxel>& getSparseGridMap() const
00207                 {
00208                         return voxelsMap;
00209                 }
00211                 std::map<Index3D,Voxel>::iterator getVoxelsMapBegin()
00212                 {
00213                         return voxelsMap.begin();
00214                 }
00215                 std::map<Index3D,Voxel>::iterator getVoxelsMapEnd()
00216                 {
00217                         return voxelsMap.end();
00218                 }
00220                 std::map<Index3D,Vertex>& getVertexMap()
00221                 {
00222                         return vertexMap;
00223                 }
00224                 const std::map<Index3D,Vertex>& getVertexMap() const
00225                 {
00226                         return vertexMap;
00227                 }
00229                 std::map<Index3D,Vertex>::iterator getVertexMapBegin()
00230                 {
00231                         return vertexMap.begin();
00232                 }
00233                 std::map<Index3D,Vertex>::iterator  const getVertexMapEnd()
00234                 {
00235                         return vertexMap.end();
00236                 }
00237 
00239                 float* getSparseGridDensity()
00240                 {
00241                         return voxelsDensity;
00242                 }
00244                 vector<int>& getSurfaceIndices()
00245                 {
00246                         return surfaceIndices;
00247                 }
00249                 int pasResolution(SparseGrid vg);
00251                 float getDimVoxX()
00252                 {
00253                         return dimVoxX;
00254                 }
00255                 float getDimVoxY()
00256                 {
00257                         return dimVoxY;
00258                 }
00259                 float getDimVoxZ()
00260                 {
00261                         return dimVoxZ;
00262                 }
00264                 int getDimX() const
00265                 {
00266                         return dimX;
00267                 }
00268                 int getDimY() const
00269                 {
00270                         return dimY;
00271                 }
00272                 int getDimZ() const
00273                 {
00274                         return dimZ;
00275                 }
00277                 float getDensity(int i,int j,int k);
00279                 void afficherVertices();
00281                 void afficherSparseGridMap();
00283                 void afficherVertexMap();
00285                 void afficherIndices();
00286 
00287 
00288         protected:
00290                 int readFilePNG( char* fileName,int color,int filesNo );
00292                 int readFileVOX( char* fileName,int color);
00294                 int setPixels( const char *FileName, int color,float plane);
00296                 void setVertexMap();
00298                 void setIndicesMap();
00300                 void setSurfaceSparseGrid();
00302                 void insertVoxel(int i,int j,int k,float d);
00304                 void buildFromSparseGridMap(int allPixels);
00306                 void setDensity();
00308                 void setDimX(int x)
00309                 {
00310                         dimX = x;
00311                 }
00312                 void setDimY(int y)
00313                 {
00314                         dimY = y;
00315                 }
00316                 void setDimZ(int z)
00317                 {
00318                         dimZ = z;
00319                 }
00321                 void setDimVoxX(float x)
00322                 {
00323                         dimVoxX = x;
00324                 }
00325                 void setDimVoxY(float y)
00326                 {
00327                         dimVoxY = y;
00328                 }
00329                 void setDimVoxZ(float z)
00330                 {
00331                         dimVoxZ = z;
00332                 }
00333         };
00334 
00335 
00336         typedef GridTopology::Cube Cube;
00337         MultiResSparseGridTopology();
00338         MultiResSparseGridTopology(const char* filevoxel, int resol,float scale);
00339 
00341         vector <SparseGrid> vectorSparseGrid;
00342 
00344         int resolution;
00345 
00347         int dimx,dimy,dimz,px,py,pz;
00348 
00350         SReal getPX(int i) const ;
00351         SReal getPY(int i) const ;
00352         SReal getPZ(int i) const ;
00353 
00355         Vector3 getPoint(int i) const ;
00356         //Vector3 getPoint(int ,int ,int );
00357 
00359         bool hasPos() const;
00361         int getNbVoxels() const;
00363         int getNbPoints() const;
00365         int point(int x, int y, int z);
00367         GridTopology::Cube getCube (int i);
00369         int findCube(const Vector3& pos, SReal& fx, SReal &fy, SReal &fz) const;
00371         int findNearestCube(const Vector3& pos, SReal& fx, SReal &fy, SReal &fz) const;
00373         void setP0(const Vector3& val)
00374         {
00375                 p0 = val;
00376         }
00378         void setDx(const Vector3& val)
00379         {
00380                 dx = val;
00381                 inv_dx2 = 1/(dx*dx);
00382         }
00383         void setDy(const Vector3& val)
00384         {
00385                 dy = val;
00386                 inv_dy2 = 1/(dy*dy);
00387         }
00388         void setDz(const Vector3& val)
00389         {
00390                 dz = val;
00391                 inv_dz2 = 1/(dz*dz);
00392         }
00394         const Vector3& getP0() const
00395         {
00396                 return p0;
00397         }
00399         const Vector3& getDx() const
00400         {
00401                 return dx;
00402         }
00403         const Vector3& getDy() const
00404         {
00405                 return dy;
00406         }
00407         const Vector3& getDz() const
00408         {
00409                 return dz;
00410         }
00411 
00412 protected:
00414         Vector3 p0;
00416         Vector3 dx,dy,dz;
00417         SReal inv_dx2, inv_dy2, inv_dz2;
00418 
00419 };
00420 
00421 } // namespace topology
00422 
00423 } // namespace component
00424 
00425 } // namespace sofa
00426 
00427 #endif
00428 

Generated on Sun Nov 23 03:44:45 2008 for Sofa by  doxygen 1.5.7.1