MeshTrian.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 :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include <sofa/helper/io/MeshTrian.h>
00028 #include <sofa/helper/system/FileRepository.h>
00029 #include <stdlib.h>
00030 #include <cstdio>
00031 #include <iostream>
00032 #include <string>
00033 
00034 namespace sofa
00035 {
00036 
00037 namespace helper
00038 {
00039 
00040 namespace io
00041 {
00042 
00043 using namespace sofa::defaulttype;
00044 
00045 SOFA_DECL_CLASS(MeshTrian)
00046 
00047 Creator<Mesh::FactoryMesh,MeshTrian> MeshTrianClass("trian");
00048 
00049 void MeshTrian::init (std::string filename)
00050 {
00051     if (!sofa::helper::system::DataRepository.findFile(filename))
00052     {
00053     std::cerr << "File " << filename << " not found " << std::endl;
00054     return;
00055     }
00056     FILE *f = fopen(filename.c_str(), "r");
00057     if (f)
00058     {
00059         readTrian (f);
00060         fclose(f);
00061     }
00062     else
00063         std::cerr << "File " << filename << " not found " << std::endl;
00064 }
00065 
00066 void MeshTrian::readTrian (FILE* file)
00067 {
00068     int nbp=0;
00069     fscanf(file, "%d\n", &nbp);
00070 
00071     vertices.resize(nbp);
00072     Vec3d fromFile;
00073     for (int p=0;p<nbp;p++)
00074     {
00075       fscanf(file, "%lf %lf %lf\n", &fromFile[0], &fromFile[1], &fromFile[2]);
00076       vertices[p][0] = (SReal)fromFile[0];
00077       vertices[p][1] = (SReal)fromFile[1];
00078       vertices[p][2] = (SReal)fromFile[2];
00079     }
00080 
00081     int nbf=0;
00082     fscanf(file, "%d\n", &nbf);
00083 
00084     facets.resize(nbf);
00085     for (int f=0;f<nbf;f++)
00086     {
00087         facets[f].resize(3);
00088         facets[f][0].resize(3);
00089         facets[f][1].resize(3);
00090         facets[f][2].resize(3);
00091         int dummy = 0;
00092         fscanf(file, "%d %d %d %d %d %d\n", &facets[f][0][0], &facets[f][0][1], &facets[f][0][2], &dummy, &dummy, &dummy);
00093     }
00094 
00095     // announce the model statistics 
00096     std::cout << " Vertices: " << vertices.size() << std::endl;
00097     std::cout << " Normals: " << normals.size() << std::endl;
00098     std::cout << " Texcoords: " << texCoords.size() << std::endl;
00099     std::cout << " Triangles: " << facets.size() << std::endl;
00100     if (vertices.size()>0)
00101     {
00102         // compute bbox
00103         Vector3 minBB = vertices[0];
00104         Vector3 maxBB = vertices[0];
00105         for (unsigned int i=1; i<vertices.size();i++)
00106         {
00107             Vector3 p = vertices[i];
00108             for (int c=0;c<3;c++)
00109             {
00110                 if (minBB[c] > p[c])
00111                     minBB[c] = p[c];
00112                 if (maxBB[c] < p[c])
00113                     maxBB[c] = p[c];
00114             }
00115         }
00116         
00117         std::cout << "BBox: <"<<minBB[0]<<','<<minBB[1]<<','<<minBB[2]<<">-<"<<maxBB[0]<<','<<maxBB[1]<<','<<maxBB[2]<<">\n";
00118     }
00119 
00120 }
00121 
00122 } // namespace io
00123 
00124 } // namespace helper
00125 
00126 } // namespace sofa
00127 

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