SphereLoader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00066
00067 int totalNumSpheres=0;
00068
00069
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
00098 {
00099 printf("%s: Unknown Sphere keyword: %s\n", filename, cmd);
00100 skipToEOL(file);
00101 }
00102 }
00103
00104
00105 (void) fclose(file);
00106
00107 return true;
00108 }
00109
00110 }
00111
00112 }
00113
00114 }
00115