Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_IO_MESH_DEFAULT_READ_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_COMMON_IO_MESH_DEFAULT_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <cassert>
00013 #include <string>
00014 #include <fstream>
00015 #include <iostream>
00016 #include <vector>
00017
00018 namespace OpenTissue
00019 {
00020 namespace mesh
00021 {
00031 template<typename mesh_type>
00032 bool default_read(std::string const & filename,mesh_type & mesh)
00033 {
00034 typedef typename mesh_type::math_types math_types;
00035 typedef typename math_types::value_traits value_traits;
00036 typedef typename math_types::vector3_type vector3_type;
00037 typedef typename math_types::real_type real_type;
00038 typedef typename mesh_type::vertex_handle vertex_handle;
00039 typedef typename mesh_type::face_handle face_handle;
00040 typedef typename mesh_type::index_type index_type;
00041
00042 mesh.clear();
00043
00044 std::ifstream file(filename.c_str());
00045 if(!file.is_open())
00046 {
00047 std::cerr << "Error unable to open file '" << filename << "'" << std::endl;
00048 return false;
00049 }
00050 std::string token;
00051 do
00052 {
00053 file >> token;
00054 }
00055 while(!file.eof() && (token != "NODES" ));
00056 if(file.eof())
00057 {
00058 std::cerr << "Warning, file '" << filename << "' contains no nodes" << std::endl;
00059 return false;
00060 }
00061 do
00062 {
00063 file >> token;
00064 }
00065 while(!file.eof() && (token != "{" ));
00066 if(file.eof())
00067 {
00068 std::cerr << "Warning, NODES in file '" << filename << "' is corrupt" << std::endl;
00069 return false;
00070 }
00071
00072 std::vector<vertex_handle> vertex_lut;
00073 vector3_type coord;
00074
00075 do
00076 {
00077 file >> token;
00078 if(token != "}")
00079 {
00080 coord(0) = static_cast<real_type>( std::atof(token.c_str()) );
00081 if(file.eof())
00082 {
00083 std::cerr << "Warning, NODES in file '" << filename << "' is corrupt" << std::endl;
00084 return false;
00085 }
00086 file >> token;
00087 coord(1) = static_cast<real_type>( std::atof(token.c_str()) );
00088 if(file.eof())
00089 {
00090 std::cerr << "Warning, NODES in file '" << filename << "' is corrupt" << std::endl;
00091 return false;
00092 }
00093 file >> token;
00094 coord(2) = static_cast<real_type>( std::atof(token.c_str()) );
00095 vertex_handle h = mesh.add_vertex(coord);
00096 if(h.is_null())
00097 {
00098 std::cerr << "Could not create vertes" << std::endl;
00099 return false;
00100 }
00101 vertex_lut.push_back(h);
00102 }
00103 }
00104 while( token != "}" );
00105
00106 do
00107 {
00108 file >> token;
00109 }
00110 while(!file.eof() && (token != "FACES" ));
00111 if(file.eof())
00112 {
00113 std::cerr << "Warning, file '" << filename << "' contains no face pointers" << std::endl;
00114 return false;
00115 }
00116 do
00117 {
00118 file >> token;
00119 }
00120 while(!file.eof() && (token != "{" ));
00121 if(file.eof())
00122 {
00123 std::cerr << "Warning, FACES in file '" << filename << "' is corrupt" << std::endl;
00124 return false;
00125 }
00126
00127 std::size_t N = vertex_lut.size();
00128 unsigned int v0,v1,v2;
00129
00130 do
00131 {
00132 file >> token;
00133 if(token != "}")
00134 {
00135 v0 = static_cast<unsigned int>( std::atoi(token.c_str()) );
00136 if(file.eof())
00137 {
00138 std::cout << "Unexpected error?" << std::endl;
00139 return false;
00140 }
00141 file >> v1;
00142 if(file.eof())
00143 {
00144 std::cout << "Unexpected error?" << std::endl;
00145 return false;
00146 }
00147 file >> v2;
00148
00149 if(v0>=N)
00150 {
00151 std::cerr << "Warning vertex index 0 of face is out of range" << std::endl;
00152 return false;
00153 }
00154 if(v1>=N)
00155 {
00156 std::cerr << "Warning vertex index 1 of face is out of range" << std::endl;
00157 return false;
00158 }
00159 if(v2>=N)
00160 {
00161 std::cerr << "Warning vertex index 2 of face is out of range" << std::endl;
00162 return false;
00163 }
00164
00165
00166 vertex_handle h0 = vertex_lut[v0];
00167 vertex_handle h1 = vertex_lut[v1];
00168 vertex_handle h2 = vertex_lut[v2];
00169
00170 face_handle h = mesh.add_face(h0,h1,h2);
00171 if(h.is_null())
00172 {
00173 std::cerr << "Could not create face?" << std::endl;
00174 return false;
00175 }
00176 }
00177 }
00178 while(token != "}" );
00179
00180 std::cout << "mesh::default_read() : "
00181 << filename
00182 << " nodes = "
00183 << mesh.size_vertices()
00184 << " faces = "
00185 << mesh.size_faces()
00186 << std::endl;
00187 return true;
00188 }
00189
00190 }
00191 }
00192
00193
00194 #endif