SphereLoader.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/SphereLoader.h>
00028 #include <sofa/helper/system/FileRepository.h>
00029 
00030 #include <stdio.h>
00031 #include <iostream>
00032 #include <cstdio>
00033 #include <string.h>
00034 
00035 namespace sofa
00036 {
00037 
00038 namespace helper
00039 {
00040 
00041 namespace io
00042 {
00043 
00044 static void skipToEOL(FILE* f)
00045 {
00046     int ch;
00047     while ((ch = fgetc(f)) != EOF && ch != '\n') ;
00048 }
00049 
00050 bool SphereLoader::load(const char *filename)
00051 {
00052     std::string fname = filename;
00053     if (!sofa::helper::system::DataRepository.findFile(fname)) return false;
00054 
00055     char cmd[64];
00056     FILE* file;
00057     
00058     static const char* SPH_FORMAT = "sph 1.0";
00059     
00060     if ((file = fopen(fname.c_str(), "r")) == NULL)
00061     {
00062         std::cout << "ERROR: cannot read file '" << filename << "'. Exiting..." << std::endl;
00063         return false;
00064     }
00065 //  std::cout << "Loading model'" << filename << "'" << std::endl;
00066     
00067     int totalNumSpheres=0;
00068     
00069     // Check first line 
00070     if (fgets(cmd, 7, file) == NULL || !strcmp(cmd,SPH_FORMAT))
00071     {
00072         fclose(file);
00073         return false;
00074     }
00075     skipToEOL(file);
00076 
00077     while (fscanf(file, "%s", cmd) != EOF)
00078     {
00079         if (!strcmp(cmd,"nums"))
00080         {
00081             fscanf(file, "%d", &totalNumSpheres);
00082             setNumSpheres(totalNumSpheres);
00083         }
00084         else if (!strcmp(cmd,"sphe"))
00085         {
00086             int index;
00087             double cx=0,cy=0,cz=0,r=1;
00088             fscanf(file, "%d %lf %lf %lf %lf\n",
00089                     &index, &cx, &cy, &cz, &r);
00090             addSphere((SReal)cx,(SReal)cy,(SReal)cz,(SReal)r);
00091             ++totalNumSpheres;
00092         }
00093         else if (cmd[0]=='#')
00094         {
00095             skipToEOL(file);
00096         }
00097         else            // it's an unknown keyword
00098         {
00099             printf("%s: Unknown Sphere keyword: %s\n", filename, cmd);
00100             skipToEOL(file);
00101         }
00102     }
00103 //  printf("Model contains %d spheres\n", totalNumSpheres);
00104 
00105     (void) fclose(file);
00106     
00107     return true;
00108 }
00109 
00110 } // namespace io
00111 
00112 } // namespace helper
00113 
00114 } // namespace sofa
00115 

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