MultiResSparseGridTopology.cpp

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 #include <sofa/component/topology/MultiResSparseGridTopology.h>
00026 #include <sofa/simulation/tree/xml/ObjectFactory.h>
00027 #include <sofa/helper/system/gl.h>
00028 #include <string>
00029 #include <fstream>
00030 using sofa::defaulttype::Vec3f;
00031 using std::ifstream;
00032 using std::string;
00033 
00034 namespace sofa
00035 {
00036 
00037 namespace component
00038 {
00039 
00040 namespace topology
00041 {
00042 
00043 using namespace sofa::defaulttype;
00044 using std::cerr;
00045 using std::cout;
00046 using std::endl;
00047 
00048 void create(MultiResSparseGridTopology*& obj, simulation::tree::xml::ObjectDescription* arg)
00049 {
00050     const char* nx = arg->getAttribute("nx","1.0");
00051     const char* ny = arg->getAttribute("ny","1.0");
00052     const char* nz = arg->getAttribute("nz","1.0");
00053     const char* reso = arg->getAttribute("resolution");
00054     const char* filevoxel = arg->getAttribute("filevoxel");
00055 
00056     if (!filevoxel)
00057     {
00058         std::cerr << arg->getType() << " requires a voxel file attribute\n";
00059         obj = NULL;
00060     }
00061     if (!nx || !ny || !nz)
00062     {
00063         std::cerr << "GridTopology requires nx, ny and nz attributes\n";
00064     }
00065     else
00066     {
00067         obj = new MultiResSparseGridTopology(filevoxel,atoi(reso),
00068                                              (float) atof(arg->getAttribute("scale","1.0f")));
00069     }
00070 }
00071 
00072 SOFA_DECL_CLASS(MultiResSparseGridTopology)
00073 
00074 helper::Creator<simulation::tree::xml::ObjectFactory, MultiResSparseGridTopology> MultiResSparseGridTopologyClass("MultiResSparseGridTopology");
00075 
00076 MultiResSparseGridTopology::MultiResSparseGridTopology()//:GridTopology(nx,ny,nz)
00077 {}
00078 
00079 MultiResSparseGridTopology::MultiResSparseGridTopology(const char* filevoxel, int resol, float scale)//:GridTopology(nx,ny,nz)
00080 {
00081     int indiceRes = resol;
00082     resolution = resol;
00083 
00084     SparseGrid voxels1,hteDef,*voxel;
00085     
00086     hteDef = SparseGrid(scale);
00087     hteDef.initFromVOX((char *)filevoxel,255);
00088 
00089     int i = 0;
00090     vectorSparseGrid.push_back(hteDef);
00091     while((vectorSparseGrid[i].getSparseGridMap().size())>1)
00092     {
00093         voxels1 = SparseGrid(scale);
00094         voxels1.pasResolution(vectorSparseGrid[i]);
00095         vectorSparseGrid.push_back(voxels1);
00096         i++;
00097     }
00098 
00099     indiceRes = vectorSparseGrid.size() - indiceRes - 1;
00100     if(indiceRes < 0)
00101     {
00102         indiceRes = 0;
00103     }
00104     voxel = &(vectorSparseGrid[indiceRes]);
00105     resolution = indiceRes;
00106     nbPoints =  (vectorSparseGrid[resolution].getNumVertices());
00107     
00109     //setP0(Vec3(0.0,0.0,0.0));
00110     setDx(Vec3(voxel->getDimVoxX(),0,0));
00111     setDy(Vec3(0,voxel->getDimVoxY(),0));
00112     setDz(Vec3(0,0,voxel->getDimVoxZ()));
00113 
00114 }
00115 
00116 MultiResSparseGridTopology::Vec3 MultiResSparseGridTopology::getPoint(int i) const
00117 {
00118     float x,y,z;
00119     x = ((vectorSparseGrid[resolution]).getVertices())[i][0];
00120     y = ((vectorSparseGrid[resolution]).getVertices())[i][1];
00121     z = ((vectorSparseGrid[resolution]).getVertices())[i][2];
00122     return (Vec3f((float)p0[0]+x,(float)p0[1]+y,(float)p0[2]+z));
00123 }
00124 
00125 // MultiResSparseGridTopology::Vec3 MultiResSparseGridTopology::getPoint(int i, int j, int k)
00126 // {
00127 //     MultiResSparseGridTopology::SparseGrid::Vertex *tmp =  
00128 //          &((vectorSparseGrid[resolution]).getVertexMap())[MultiResSparseGridTopology::SparseGrid::Index3D(i,j,k)];
00129 //     float x,y,z;
00130 //     x = tmp->vertexPosition[0];
00131 //     y = tmp->vertexPosition[1];
00132 //     z = tmp->vertexPosition[2];
00133 //     return (Vec3f((float)p0[0]+x,(float)p0[1]+y,(float)p0[2]+z));
00134 // }
00135 
00136 
00137 double MultiResSparseGridTopology::getPX(int i) const
00138 {
00139     return getPoint(i)[0];
00140 }
00141 double MultiResSparseGridTopology::getPY(int i) const
00142 {
00143     return getPoint(i)[1];
00144 }
00145 double MultiResSparseGridTopology::getPZ(int i) const
00146 {
00147     return getPoint(i)[2];
00148 }
00149 
00150 bool MultiResSparseGridTopology::hasPos() const
00151 {
00152     return true;
00153 }
00154 
00155 int MultiResSparseGridTopology::getNbVoxels() const
00156 {
00157     return (vectorSparseGrid[resolution].getSparseGridMap().size());
00158 }
00159 
00160 int MultiResSparseGridTopology::getNbPoints() const
00161 {
00162     return (vectorSparseGrid[resolution].getNumVertices());
00163 }
00164 
00165 int MultiResSparseGridTopology::point(int x, int y, int z)
00166 {
00167     MultiResSparseGridTopology::SparseGrid::Vertex tmp =
00168             ((vectorSparseGrid[resolution]).getVertexMap())[MultiResSparseGridTopology::SparseGrid::Index3D(x,y,z)];
00169     return tmp.index;
00170 }
00171 
00172 
00173 GridTopology::Cube MultiResSparseGridTopology::getCube(int i)
00174 {
00175     int DimX = (vectorSparseGrid[resolution]).getDimX();
00176     int DimY = (vectorSparseGrid[resolution]).getDimY();
00177 
00178     int x = i%(DimX);
00179     i/=(DimX);
00180     int y = i%(DimY);
00181     i/=(DimY);
00182     int z = i;
00183 
00184     return Cube(point(x  ,y  ,z  ),point(x+1,y  ,z  ),
00185                       point(x  ,y+1,z  ),point(x+1,y+1,z  ),
00186                       point(x  ,y  ,z+1),point(x+1,y  ,z+1),
00187                       point(x  ,y+1,z+1),point(x+1,y+1,z+1));
00188 }
00189 
00190 int MultiResSparseGridTopology::findCube(const Vec3& pos, double& fx, double &fy, double &fz) const
00191 {
00192     SparseGrid const * voxels = &vectorSparseGrid[resolution];
00194     Vec3 p = pos-p0;
00195     double x = p*dx*inv_dx2;
00196     double y = p*dy*inv_dy2;
00197     double z = p*dz*inv_dz2;
00198     int ix = int(x+1000000)-1000000; // Do not round toward 0...
00199     int iy = int(y+1000000)-1000000;
00200     int iz = int(z+1000000)-1000000;
00201     fx = x-ix;
00202     fy = y-iy;
00203     fz = z-iz;
00204     int DimX = (vectorSparseGrid[resolution]).getDimX();
00205     int DimY = (vectorSparseGrid[resolution]).getDimY();
00206 
00207     if (voxels->getSparseGridMap().find(MultiResSparseGridTopology::SparseGrid::Index3D(ix,iy,iz)) ==
00208             voxels->getSparseGridMap().end())
00209         return -1;
00210     return ix + iy*DimX + iz*DimX*DimY;
00211 
00212 }
00213 
00214 int MultiResSparseGridTopology::findNearestCube(const Vec3& pos, double& fx, double &fy, double &fz) const
00215 {
00216     SparseGrid const *voxels = & vectorSparseGrid[resolution];
00217     bool cond = false;
00218 
00219     int DimX = (vectorSparseGrid[resolution]).getDimX();
00220     int DimY = (vectorSparseGrid[resolution]).getDimY();
00221     int DimZ = (vectorSparseGrid[resolution]).getDimZ();
00222     int i = 0,j,k,l;
00224     Vec3 p = pos-p0;
00225     double x = p*dx*inv_dx2;
00226     double y = p*dy*inv_dy2;
00227     double z = p*dz*inv_dz2;
00228     double dist,distX,distY,distZ, distRef = -1;
00229     int ix = int(x+1000000)-1000000; // Do not round toward 0...
00230     int iy = int(y+1000000)-1000000;
00231     int iz = int(z+1000000)-1000000;
00232     fx = x-ix;
00233     fy = y-iy;
00234     fz = z-iz;
00235     int ixTmp = ix, iyTmp = iy, izTmp = iz;
00236     while (cond == false)
00237     {
00238         i++;
00239         for(j = -i; j<=i; j++)
00240         {
00241             for(k = -(i-abs(j)); k <= (i-abs(j)) ; k++)
00242             {
00243                 for(l = -(i-abs(j)-abs(k)); l<= (i-abs(j)-abs(k)); l++)
00244                 {
00245                     if((abs(l)+abs(j)+abs(k))==i)
00246                     {
00247                         if (voxels->getSparseGridMap().find(MultiResSparseGridTopology::SparseGrid::Index3D(ix+j,iy+k,iz+l)) !=
00248                                 voxels->getSparseGridMap().end())
00249                         {
00250                             distX = (j * DimX)- (fx*DimX - DimX/2);
00251                             distY = ((k * DimY)- (fy*DimY - DimY/2));
00252                             distZ = ((l * DimZ)- (fz*DimZ - DimZ/2));
00253                             dist = sqrt((distX*distX)+(distY*distY)+(distZ*distZ));
00254                             if (distRef==-1)
00255                             {
00256                                 distRef = dist;
00257                                 ixTmp = ix + j;
00258                                 iyTmp = iy + k;
00259                                 izTmp =  iz + l;
00260                             }
00261                             if (dist < distRef)
00262                             {
00263                                 ixTmp = ix + j;
00264                                 iyTmp = iy + k;
00265                                 izTmp =  iz + l;
00266                                 distRef = dist;
00267                             }
00268                         }
00269                     }
00270                 }
00271             }
00272         }
00273         if (distRef != -1)
00274             cond=true;
00275     }
00276 
00277     fx = x - ixTmp;
00278     fy = y - iyTmp;
00279     fz = z - izTmp;
00280     return ixTmp + iyTmp*DimX + izTmp*DimX*DimY;
00281 
00282 }
00283 // int MultiResSparseGridTopology::getIndicesInSpace( sofa::helper::vector<int>& indices,
00284 //         float xmin,float xmax,float ymin,float ymax,float zmin,float zmax )
00285 // {
00286 //  float x,y,z;
00287 //  std::map<SparseGrid::Index3D,SparseGrid::Vertex>::iterator i;
00288 //  for(i = (vectorSparseGrid[resolution]).getVertexMap().begin();
00289 //         i !=  (vectorSparseGrid[resolution]).getVertexMap().end();i++)
00290 //  {
00291 //             x = (*i).second.vertexPosition[0];
00292 //             y = (*i).second.vertexPosition[1];
00293 //             z = (*i).second.vertexPosition[2];
00294 //      if ( xmin <= x && x <= xmax)
00295 //          if( ymin <= y && y <= ymax)
00296 //              if( zmin <= z && z <= zmax){
00297 //                  indices.push_back((*i).second.index);
00298 //              }
00299 //  }
00300 //     return 0;
00301 // }
00302 
00303 
00304 
00305 MultiResSparseGridTopology::SparseGrid::SparseGrid(void)
00306 {
00307     FIN_DE_LISTE = -1;
00308     scale = 1;
00309 }
00310 
00311 MultiResSparseGridTopology::SparseGrid::SparseGrid(int x)
00312 {
00313     FIN_DE_LISTE = x;
00314     scale = 1;
00315 }
00316 
00317 MultiResSparseGridTopology::SparseGrid::SparseGrid(float scale_)
00318 {
00319     FIN_DE_LISTE = -1;
00320     scale = scale_;
00321 }
00322 
00323 MultiResSparseGridTopology::SparseGrid::~SparseGrid(void)
00324 {}
00325 
00326 
00327 void MultiResSparseGridTopology::SparseGrid::afficherSparseGridMap()
00328 {
00329     cout << "VoxelMap" << endl;
00330     std::map<Index3D,Voxel>::iterator i;
00331     for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00332     {
00333         cout << (*i).first.i << " " << (*i).first.j << " " << (*i).first.k << " : ";
00334         for(int l=0; l<8;l++)
00335             cout << (*i).second.vertices[l] << " ";
00336         cout << endl;
00337     }
00338 
00339 
00340 /*    for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00341     {
00342         //V1 (x0,y0,z0)
00343         for(int l=0; l<3;l++)
00344             cout << vertexMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertexPosition[l] <<" ";
00345         cout << "     ";
00346         //V1 (x0,y0,z0)
00347         for(int l=0; l<3;l++)
00348             cout << vertexMap[Index3D((*i).first.i+1,(*i).first.j,(*i).first.k)].vertexPosition[l] << " ";
00349         cout << "     ";
00350         //V1 (x0,y0,z0)
00351         for(int l=0; l<3;l++)
00352             cout << vertexMap[Index3D((*i).first.i,(*i).first.j+1,(*i).first.k)].vertexPosition[l] <<" " ;
00353         cout << "     ";
00354         //V1 (x0,y0,z0)
00355         for(int l=0; l<3;l++)
00356             cout << vertexMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k+1)].vertexPosition[l] <<" ";
00357         cout << "     ";
00358 
00359         cout << endl;
00360     }
00361     cout << "end voxelMap" << endl;
00362 
00363 }
00364 
00365 
00366 void MultiResSparseGridTopology::SparseGrid::afficherVertexMap()
00367 {
00368 
00369     cout << "VertexMap" << endl;
00370     std::map<Index3D,Vertex>::iterator i;
00371     for(i = vertexMap.begin(); i != vertexMap.end();i++)
00372     {
00373         cout << (*i).first.i << " " << (*i).first.j << " " << (*i).first.k << " : ";
00374         for(int l=0; l<3;l++)
00375             cout << (*i).second.vertexPosition[l] << " ";
00376         cout << endl;
00377     }*/
00378     cout << "end of the vertexMap" << endl;
00379 
00380 }
00381 
00382 
00383 void MultiResSparseGridTopology::SparseGrid::afficherVertices()
00384 {
00385 
00386     cout << "Vertices" << endl;
00387     for(int l=0;l<(int) vertexMap.size();l++)
00388     {
00389         for(int k=0; k<3;k++)
00390             cout<< vertices[l][k] << " ";
00391         cout << " ";
00392     }
00393     cout << endl;
00394     cout << "end of vertices" << endl;
00395 
00396 }
00397 
00398 
00399 void MultiResSparseGridTopology::SparseGrid::afficherIndices()
00400 {
00401     cout << "Indices" << endl;
00402 
00403     for(int l=0; l<numIndices;l++)
00404         cout << indices[l] << " ";
00405     cout << "end of indices" << endl;
00406 
00407 }
00408 
00409 int MultiResSparseGridTopology::SparseGrid::pasResolution(SparseGrid vg)
00410 {
00411 
00412     float dvx,dvy,dvz;
00413     int dx,dy,dz;
00414     std::map<Index3D,Voxel>::iterator i;
00415 
00417     
00418     dvx = vg.getDimVoxX();
00419     dvy = vg.getDimVoxY();
00420     dvz = vg.getDimVoxZ();
00421         
00422     setDimVoxX(2.0f*dvx);
00423     setDimVoxY(2.0f*dvy);
00424     setDimVoxZ(2.0f*dvz);
00425 
00426     dx = vg.getDimX();
00427     dy = vg.getDimY();
00428     dz = vg.getDimZ();
00429 
00430     if(dx!=1) dx/=2; if(dy!=1) dy/=2;if(dz!=1) dz/=2;
00431 
00432     if (dx%2 && dx!=1)   setDimX(dx+1);
00433     else setDimX( dx );
00434 
00435     if (dy%2 && dy!=1) setDimY(dy+1);
00436     else setDimY( dy );
00437 
00438     if (dz%2 && dz!=1) setDimZ(dz+1);
00439     else setDimZ( dz );
00440 
00441     for(i = vg.voxelsMap.begin(); i != vg.voxelsMap.end();i++)
00442     {
00443         insertVoxel( ((*i).first.i)/2, ((*i).first.j)/2, ((*i).first.k)/2, (*i).second.density*0.125f );
00444     }
00445 
00446     buildFromSparseGridMap(voxelsMap.size());
00447     return 0;
00448 }
00449 
00450 void MultiResSparseGridTopology::SparseGrid::setIndicesMap()
00451 {
00452 
00453     int num = 0;
00454     std::map<Index3D, Vertex>::iterator iter;
00456     int v1,v2,v3,v4,v5,v6,v7,v8;
00457     std::map<Index3D,Voxel>::iterator i;
00459     for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00460     {
00462         iter = vertexMap.find(Index3D((*i).first.i,(*i).first.j,(*i).first.k));
00463         v1 = (*iter).second.index;
00464         //
00465         iter = vertexMap.find(Index3D((*i).first.i+1,(*i).first.j,(*i).first.k));
00466         v2 = (*iter).second.index;
00467         //
00468         iter = vertexMap.find(Index3D((*i).first.i+1,(*i).first.j+1,(*i).first.k));
00469         v3 = (*iter).second.index;
00470         //
00471         iter = vertexMap.find(Index3D((*i).first.i,(*i).first.j+1,(*i).first.k));
00472         v4 = (*iter).second.index;
00473         //
00474         iter = vertexMap.find(Index3D((*i).first.i,(*i).first.j,(*i).first.k+1));
00475         v5 = (*iter).second.index;
00476         //
00477         iter = vertexMap.find(Index3D((*i).first.i+1,(*i).first.j,(*i).first.k+1));
00478         v6 = (*iter).second.index;
00479         //
00480         iter = vertexMap.find(Index3D((*i).first.i+1,(*i).first.j+1,(*i).first.k+1));
00481         v7 = (*iter).second.index;
00482         //
00483         iter = vertexMap.find(Index3D((*i).first.i,(*i).first.j+1,(*i).first.k+1));
00484         v8 = (*iter).second.index;
00485 
00487         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[0]= v1;
00488         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[1]= v2;
00489         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[2]= v3;
00490         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[3]= v4;
00491         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[4]= v5;
00492         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[5]= v6;
00493         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[6]= v7;
00494         voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].vertices[7]= v8;
00495 
00496         //indices for face1
00497         indices[num+0]= v1;
00498         indices[num+1] = v2;
00499         indices[num+2] = v3;
00500         indices[num+3] = v4;
00501         indices[num+4] = FIN_DE_LISTE;
00502         //indices for face2
00503         indices[num+5]= v5;
00504         indices[num+8] = v6;
00505         indices[num+7] = v7;
00506         indices[num+6] = v8;
00507         indices[num+9] = FIN_DE_LISTE;
00508         //indices for face3
00509         indices[num+10]= v3;
00510         indices[num+13] = v4;
00511         indices[num+12] = v8;
00512         indices[num+11] = v7;
00513         indices[num+14] = FIN_DE_LISTE;
00514         //indices for face4
00515         indices[num+15]= v2;
00516         indices[num+16] = v1;
00517         indices[num+17] = v5;
00518         indices[num+18] = v6;
00519         indices[num+19] = FIN_DE_LISTE;
00520         //indices for face5
00521         indices[num+20]= v1;
00522         indices[num+21] = v4;
00523         indices[num+22] = v8;
00524         indices[num+23] = v5;
00525         indices[num+24] = FIN_DE_LISTE;
00526         //indices for face6
00527         indices[num+25]= v2;
00528         indices[num+28] = v3;
00529         indices[num+27] = v7;
00530         indices[num+26] = v6;
00531         indices[num+29] = FIN_DE_LISTE;
00532         num = num+30;
00533     }
00534 }
00535 
00536 void MultiResSparseGridTopology::SparseGrid::insertVoxel(int i, int j, int k, float d)
00537 {
00538 
00539     voxelsMap[Index3D(i,j,k)].density += d;
00540 
00541 }
00542 
00543 int MultiResSparseGridTopology::SparseGrid::setPixels(const char *FileName, int color,float plane)
00544 {
00545     char line[256];
00546     int j=0;
00547     string f;
00548     int width=0;
00549     int height=0;
00550     ifstream infile;
00551     int i = 0;
00552     int x=0;
00553     int y=0;
00554     infile.open(FileName);
00555     if (!infile)
00556         cerr<<"error.."<<endl;
00557     else
00558         for(i=0;i<4;i++)
00559         {
00560             infile>>line;
00561             if ( i==1)
00562                 width = atoi(line);
00563             if (i==2)
00564                 height = atoi(line);
00565         }
00566     cout<<"Width is "<<width<<endl;
00567     cout<<"Height is "<<height<<endl;
00568 
00569     setDimX( width );
00570     setDimY( height );
00571 
00572     for (y=0;y<height;y++)
00573         for (x=0;x<width;x++)
00574         {
00575             infile>>line;
00576             if (atoi(line)==color)
00577             {
00578                 insertVoxel(x,y,(int)plane,1.0);
00579                 j++;
00580             }
00581         }
00582 
00583     cout<<"Number of black pixels in file "<<FileName<<" is "<<j<<endl;
00584     infile.close();
00585     return j;
00586 }
00587 
00588 void MultiResSparseGridTopology::SparseGrid::setVertexMap()
00589 {
00590     std::map<Index3D,Voxel>::iterator i;
00591     float dx = getDimVoxX(), dy = getDimVoxY(), dz = getDimVoxZ();
00593     for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00594     {
00595         //V1 (x0,y0,z0)
00596         vertexMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)];// = Vertex((*i).first.i*dx,(*i).first.j*dy,(*i).first.k*dz);
00597         //V2 (x1,y0,z0)
00598         vertexMap[Index3D((*i).first.i+1,(*i).first.j,(*i).first.k)];// Vertex(((*i).first.i+1)*dx,(*i).first.j*dy,(*i).first.k*dz);
00599         //V3 (x1,y1,z0)
00600         vertexMap[Index3D((*i).first.i+1,(*i).first.j+1,(*i).first.k)];// Vertex(((*i).first.i+1)*dx,((*i).first.j+1)*dy,(*i).first.k*dz);
00601         //V4 (x0,y1,z0)
00602         vertexMap[Index3D((*i).first.i,(*i).first.j+1,(*i).first.k)];// Vertex((*i).first.i*dx,((*i).first.j+1)*dy,((*i).first.k)*dz);
00603         //V1 (x0,y0,z1)
00604         vertexMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k+1)];// Vertex((*i).first.i*dx,(*i).first.j*dy,((*i).first.k+1)*dz);
00605         //V2 (x1,y0,z1)
00606         vertexMap[Index3D((*i).first.i+1,(*i).first.j,(*i).first.k+1)];// Vertex(((*i).first.i+1)*dx,(*i).first.j*dy,((*i).first.k+1)*dz);
00607         //V3 (x1,y1,z1)
00608         vertexMap[Index3D((*i).first.i+1,(*i).first.j+1,(*i).first.k+1)];// Vertex(((*i).first.i+1)*dx,((*i).first.j+1)*dy,((*i).first.k+1)*dz);
00609         //V4 (x0,y1,z1)
00610         vertexMap[Index3D((*i).first.i,(*i).first.j+1,(*i).first.k+1)];// Vertex((*i).first.i*dx,((*i).first.j+1)*dy,((*i).first.k+1)*dz);
00611 
00612 
00613     }
00615     int num = 0;
00616     std::map<Index3D, Vertex>::iterator iter;
00617     vertices.resize(vertexMap.size());
00618     for(iter= vertexMap.begin();iter!= vertexMap.end();iter++)
00619     {
00620         (*iter).second.index = num;
00621         //vertices[num] = ((*iter).second.vertexPosition);
00622         vertices[num] = Vec3((*iter).first.i*dx, (*iter).first.j*dy, (*iter).first.k*dz);
00623         num++;
00624     }
00625 
00626 }
00627 
00628 void MultiResSparseGridTopology::SparseGrid::setSurfaceSparseGrid()
00629 {
00630     std::map<Index3D,Voxel>::iterator i;
00632     for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00633     {
00634         if (voxelsMap.find(Index3D((*i).first.i+1,(*i).first.j,(*i).first.k))== voxelsMap.end())
00635             surfaceSparseGrid.push_back((*i).first);
00636 
00637         else if (voxelsMap.find(Index3D((*i).first.i-1,(*i).first.j,(*i).first.k))== voxelsMap.end())
00638             surfaceSparseGrid.push_back((*i).first);
00639 
00640         else if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j+1,(*i).first.k))== voxelsMap.end())
00641             surfaceSparseGrid.push_back((*i).first);
00642 
00643         else if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j-1,(*i).first.k))== voxelsMap.end())
00644             surfaceSparseGrid.push_back((*i).first);
00645 
00646         else if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j,(*i).first.k+1))== voxelsMap.end())
00647             surfaceSparseGrid.push_back((*i).first);
00648 
00649         else if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j,(*i).first.k-1))== voxelsMap.end())
00650             surfaceSparseGrid.push_back((*i).first);
00651     }
00652     surfaceIndices.resize(surfaceSparseGrid.size()*30);
00653     int counter = 0;
00654     for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00655     {
00656         if (voxelsMap.find(Index3D((*i).first.i+1,(*i).first.j,(*i).first.k))== voxelsMap.end())
00657         {
00658             surfaceIndices[counter] = (*i).second.vertices[1];
00659             surfaceIndices[counter+1] = (*i).second.vertices[5];
00660             surfaceIndices[counter+2] = (*i).second.vertices[6];
00661             surfaceIndices[counter+3] = (*i).second.vertices[2];
00662             surfaceIndices[counter+4] = FIN_DE_LISTE;
00663             counter = counter + 5;
00664         }
00665         if (voxelsMap.find(Index3D((*i).first.i-1,(*i).first.j,(*i).first.k))== voxelsMap.end())
00666         {
00667             surfaceIndices[counter] = (*i).second.vertices[0];
00668             surfaceIndices[counter+1] = (*i).second.vertices[3];
00669             surfaceIndices[counter+2] = (*i).second.vertices[7];
00670             surfaceIndices[counter+3] = (*i).second.vertices[4];
00671             surfaceIndices[counter+4] = FIN_DE_LISTE;
00672             counter = counter + 5;
00673         }
00674         if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j+1,(*i).first.k))== voxelsMap.end())
00675         {
00676             surfaceIndices[counter] = (*i).second.vertices[2];
00677             surfaceIndices[counter+1] = (*i).second.vertices[6];
00678             surfaceIndices[counter+2] = (*i).second.vertices[7];
00679             surfaceIndices[counter+3] = (*i).second.vertices[3];
00680             surfaceIndices[counter+4] = FIN_DE_LISTE;
00681             counter = counter + 5;
00682         }
00683         if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j-1,(*i).first.k))== voxelsMap.end())
00684         {
00685             surfaceIndices[counter] = (*i).second.vertices[1];
00686             surfaceIndices[counter+1] = (*i).second.vertices[0];
00687             surfaceIndices[counter+2] = (*i).second.vertices[4];
00688             surfaceIndices[counter+3] = (*i).second.vertices[5];
00689             surfaceIndices[counter+4] = FIN_DE_LISTE;
00690             counter = counter + 5;
00691         }
00692         if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j,(*i).first.k+1))== voxelsMap.end())
00693         {
00694             surfaceIndices[counter] = (*i).second.vertices[4];
00695             surfaceIndices[counter+1] = (*i).second.vertices[7];
00696             surfaceIndices[counter+2] = (*i).second.vertices[6];
00697             surfaceIndices[counter+3] = (*i).second.vertices[5];
00698             surfaceIndices[counter+4] = FIN_DE_LISTE;
00699             counter = counter + 5;
00700         }
00701         if (voxelsMap.find(Index3D((*i).first.i,(*i).first.j,(*i).first.k-1))== voxelsMap.end())
00702         {
00703             surfaceIndices[counter] = (*i).second.vertices[0];
00704             surfaceIndices[counter+1] = (*i).second.vertices[1];
00705             surfaceIndices[counter+2] = (*i).second.vertices[2];
00706             surfaceIndices[counter+3] = (*i).second.vertices[3];
00707             surfaceIndices[counter+4] = FIN_DE_LISTE;
00708             counter = counter + 5;
00709         }
00710     }
00711     numSurfaceIndices = counter;
00712 }
00713 
00714 void MultiResSparseGridTopology::SparseGrid::setDensity()
00715 {
00716 
00717     std::map<Index3D, Voxel>::iterator i;
00718 
00719     voxelsDensity = new float [voxelsMap.size()];
00720     int j = 0;
00722     for(i = voxelsMap.begin(); i != voxelsMap.end();i++)
00723     {
00724         voxelsDensity[j] = 1.0f-voxelsMap[Index3D((*i).first.i,(*i).first.j,(*i).first.k)].density;
00725         j++;
00726     }
00727 }
00728 
00729 
00730 void MultiResSparseGridTopology::SparseGrid::buildFromSparseGridMap(int allPixels)
00731 {
00732 
00733     setVertexMap();
00735     numIndices = allPixels*6*5;
00736     indices.resize(numIndices);
00738     setIndicesMap();
00740     setSurfaceSparseGrid();
00741     setDensity();
00742 }
00743 
00744 int MultiResSparseGridTopology::SparseGrid::readFilePNG( char* fileName,int color,int filesNo )
00745 {
00747     int pixel_no=0;
00748     char file_extension[64];
00749     string theFileName(fileName);
00750     for( int i=1; i<=filesNo; i++ )
00751     {
00752         setDimZ( filesNo );
00754         sprintf(file_extension,"%02d.pgm",i);
00755         string thisFileName = theFileName + string(file_extension);
00756         cout<<"Reading file "<<thisFileName<<endl;
00758         pixel_no += setPixels(thisFileName.c_str(),color,(float)i-1);
00759     }
00760 
00761     return pixel_no;
00762 }
00763 
00764 void MultiResSparseGridTopology::SparseGrid::initFromPNG( char* fileName,int color,int filesNo )
00765 {
00766 
00767     int allPixels=0;
00768 
00769     allPixels = readFilePNG(fileName,color,filesNo);
00770 
00771     setDimVoxX(0.35f); // Dimension des voxels selon x.
00772     setDimVoxY(0.35f); // Dimension des voxels  selon y.
00773     setDimVoxZ(0.35f); // Dimension des voxels  selon z.
00774 
00775     buildFromSparseGridMap(allPixels);
00776 }
00777 
00778 int MultiResSparseGridTopology::SparseGrid::readFileVOX( char* fileName,int color )
00779 {
00780 
00781     // le constructeur de ifstream permet d'ouvrir un fichier en lecture
00782     ifstream fichier( fileName );
00783     int pixel_no = 0;
00784     if ( fichier != NULL ) // ce test �houe si le fichier n'est pas ouvert
00785     {
00786         cout << "Ouverture du fichier " <<  fileName<< " reussit" << endl;
00787         int nb1, nb2, nb3,nb ;
00788         // Dimensions d'un voxels selon x, y, z
00789         float v1, v2, v3 ;
00790         // Recuperation des dimension de l'image. ( se trouvent dans l'entete du fichier VOXEL )
00791         fichier >> nb1 >> nb2 >> nb3;
00792         setDimX( nb1 );
00793         setDimY( nb2 );
00794         setDimZ( nb3 );
00795 
00796         //ces valeurs ne servent a rien pour l'instant
00797         fichier >> v1 >> v2 >> v3 ;
00798 
00799         setDimVoxX(v1*scale); // Dimension des voxels selon x.
00800         setDimVoxY(v2*scale); // Dimension des voxels  selon y.
00801         setDimVoxZ(v3*scale); // Dimension des voxels  selon z.
00802 
00803         for(int r=0; r<nb3; r++)
00804         {
00805             for(int s = 0; s < nb2; s++)
00806             {
00807                 for(int t = 0; t < nb1; t++)
00808                 {
00809                     fichier >> nb ;
00810                     if (nb == color)
00811                     {
00812                         insertVoxel(t,s,r,1.0);
00813                         pixel_no++;
00814                     }
00815                 }
00816             }
00817         }
00818         fichier.close();
00819         return pixel_no;
00820     }
00821     else
00822     {
00823         cout << "Le fichier "<<fileName<<" n'existe pas !!\n";
00824         exit(0);
00825     }
00826 }
00827 
00828 void MultiResSparseGridTopology::SparseGrid::initFromVOX( char* fileName,int color)
00829 {
00830 
00831     int allPixels=0;
00832     allPixels = readFileVOX(fileName,color);
00833     buildFromSparseGridMap(allPixels);
00834 }
00835 
00836 float MultiResSparseGridTopology::SparseGrid::getDensity(int i, int j, int k)
00837 {
00838 
00839     return voxelsMap[Index3D(i,j,k)].density;
00840 }
00841 
00842 
00843 
00844 } // namespace topology
00845 
00846 } // namespace component
00847 
00848 } // namespace sofa
00849 

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