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
00028 #include "VertexMeshReader.hpp"
00029 #include "Exception.hpp"
00030
00031 #include <sstream>
00032
00033
00034 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00035 VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::VertexMeshReader(std::string pathBaseName)
00036 : mFilesBaseName(pathBaseName),
00037 mIndexFromZero(false),
00038 mNumNodes(0),
00039 mNumElements(0),
00040 mNodesRead(0),
00041 mElementsRead(0),
00042 mNumElementAttributes(0)
00043 {
00044 OpenFiles();
00045 ReadHeaders();
00046 }
00047
00048 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumElements() const
00050 {
00051 return mNumElements;
00052 }
00053
00054 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00055 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumNodes() const
00056 {
00057 return mNumNodes;
00058 }
00059
00060 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00061 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumElementAttributes() const
00062 {
00063 return mNumElementAttributes;
00064 }
00065
00066 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00067 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumFaces() const
00068 {
00070 return 0;
00071 }
00072
00073 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00074 ElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextFaceData()
00075 {
00077 ElementData ret;
00078 ret.NodeIndices = std::vector<unsigned>();
00079 ret.AttributeValue = 0;
00080 return ret;
00081 }
00082
00083 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00084 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumEdges() const
00085 {
00087 return 0;
00088 }
00089
00090 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00091 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::Reset()
00092 {
00093 CloseFiles();
00094 OpenFiles();
00095 ReadHeaders();
00096
00097 mNodesRead = 0;
00098 mElementsRead = 0;
00099 }
00100
00101 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00102 std::vector<double> VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextNode()
00103 {
00104 std::vector<double> node_data;
00105
00106 std::string buffer;
00107 GetNextLineFromStream(mNodesFile, buffer);
00108
00109 std::stringstream buffer_stream(buffer);
00110
00111 unsigned index;
00112 buffer_stream >> index;
00113
00114 unsigned offset = mIndexFromZero ? 0 : 1;
00115 if (index != mNodesRead + offset)
00116 {
00117 std::stringstream error;
00118 error << "Data for node " << mNodesRead << " missing";
00119 EXCEPTION(error.str());
00120 }
00121
00122 double node_value;
00123 for (unsigned i=0; i<SPACE_DIM+1; i++)
00124 {
00125 buffer_stream >> node_value;
00126 node_data.push_back(node_value);
00127 }
00128
00129 mNodesRead++;
00130 return node_data;
00131 }
00132
00133 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00134 ElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00135 {
00136
00137 ElementData element_data;
00138
00139 std::string buffer;
00140 GetNextLineFromStream(mElementsFile, buffer);
00141
00142 std::stringstream buffer_stream(buffer);
00143
00144 unsigned element_index;
00145 buffer_stream >> element_index;
00146
00147 unsigned offset = mIndexFromZero ? 0 : 1;
00148 if (element_index != mElementsRead + offset)
00149 {
00150 std::stringstream error;
00151 error << "Data for element " << mElementsRead << " missing";
00152 EXCEPTION(error.str());
00153 }
00154
00155 unsigned num_nodes_in_element;
00156 buffer_stream >> num_nodes_in_element;
00157
00158
00159 unsigned node_index;
00160 for (unsigned i=0; i<num_nodes_in_element; i++)
00161 {
00162 buffer_stream >> node_index;
00163 element_data.NodeIndices.push_back(node_index - offset);
00164 }
00165
00166 if (mNumElementAttributes > 0)
00167 {
00168 assert(mNumElementAttributes==1);
00169
00170 unsigned attribute_value;
00171 buffer_stream >> attribute_value;
00172 element_data.AttributeValue = attribute_value;
00173 }
00174 else
00175 {
00176 element_data.AttributeValue = 0;
00177 }
00178
00179 mElementsRead++;
00180 return element_data;
00181 }
00182
00183
00184 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00185 VertexElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementDataWithFaces()
00186 {
00187
00188 VertexElementData element_data;
00189
00190 std::string buffer;
00191 GetNextLineFromStream(mElementsFile, buffer);
00192
00193 std::stringstream buffer_stream(buffer);
00194
00195 unsigned element_index;
00196 buffer_stream >> element_index;
00197
00198 unsigned offset = mIndexFromZero ? 0 : 1;
00199 if (element_index != mElementsRead + offset)
00200 {
00201 std::stringstream error;
00202 error << "Data for element " << mElementsRead << " missing";
00203 EXCEPTION(error.str());
00204 }
00205
00206
00207 unsigned num_nodes_in_element;
00208 buffer_stream >> num_nodes_in_element;
00209
00210
00211 unsigned node_index;
00212 for (unsigned i=0; i<num_nodes_in_element; i++)
00213 {
00214 buffer_stream >> node_index;
00215 element_data.NodeIndices.push_back(node_index - offset);
00216 }
00217
00218
00219 unsigned num_faces_in_element;
00220 buffer_stream >> num_faces_in_element;
00221
00222 element_data.Faces.resize(num_faces_in_element);
00223 for (unsigned j=0; j<num_faces_in_element; j++)
00224 {
00225
00226 ElementData face_data;
00227
00228
00229 unsigned face_index;
00230 buffer_stream >> face_index;
00231 face_data.AttributeValue = face_index;
00232
00233
00234 unsigned num_nodes_in_face;
00235 buffer_stream >> num_nodes_in_face;
00236
00237
00238 unsigned node_index;
00239 for (unsigned i=0; i<num_nodes_in_face; i++)
00240 {
00241 buffer_stream >> node_index;
00242 face_data.NodeIndices.push_back(node_index - offset);
00243 }
00244
00246
00247 element_data.Faces[j] = face_data;
00248 }
00249
00250 if (mNumElementAttributes > 0)
00251 {
00252 assert(mNumElementAttributes==1);
00253
00254 unsigned attribute_value;
00255 buffer_stream >> attribute_value;
00256 element_data.AttributeValue = attribute_value;
00257 }
00258 else
00259 {
00260 element_data.AttributeValue = 0;
00261 }
00262
00263 mElementsRead++;
00264 return element_data;
00265 }
00266
00267 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00268 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenFiles()
00269 {
00270 OpenNodeFile();
00271 OpenElementsFile();
00272 }
00273
00274 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00275 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenNodeFile()
00276 {
00277
00278 std::string file_name = mFilesBaseName + ".node";
00279 mNodesFile.open(file_name.c_str());
00280 if (!mNodesFile.is_open())
00281 {
00282 EXCEPTION("Could not open data file: " + file_name);
00283 }
00284 }
00285
00286 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00287 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenElementsFile()
00288 {
00289
00290 std::string file_name;
00291 file_name = mFilesBaseName + ".cell";
00292
00293 mElementsFile.open(file_name.c_str());
00294 if (!mElementsFile.is_open())
00295 {
00296 EXCEPTION("Could not open data file: " + file_name);
00297 }
00298 }
00299
00300 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00301 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::ReadHeaders()
00302 {
00303 std::string buffer;
00304
00305 GetNextLineFromStream(mNodesFile, buffer);
00306 std::stringstream buffer_stream(buffer);
00307 buffer_stream >> mNumNodes >> mNumNodeAttributes;
00308
00309
00310 GetNextLineFromStream(mNodesFile, buffer);
00311 std::stringstream node_buffer_stream(buffer);
00312
00313 unsigned first_index;
00314 node_buffer_stream >> first_index;
00315 assert(first_index == 0 || first_index == 1);
00316 mIndexFromZero = (first_index == 0);
00317
00318
00319 mNodesFile.close();
00320 OpenNodeFile();
00321 GetNextLineFromStream(mNodesFile, buffer);
00322
00323 GetNextLineFromStream(mElementsFile, buffer);
00324 std::stringstream element_buffer_stream(buffer);
00325
00326 element_buffer_stream >> mNumElements >> mNumElementAttributes;
00327 }
00328
00329 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00330 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::CloseFiles()
00331 {
00332 mNodesFile.close();
00333 mElementsFile.close();
00334 }
00335
00336 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00337 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
00338 {
00339 bool line_is_blank;
00340
00341 do
00342 {
00343 getline(fileStream, rawLine);
00344
00345 if (fileStream.eof())
00346 {
00347 EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
00348 }
00349
00350
00351 rawLine = rawLine.substr(0,rawLine.find('#', 0));
00352
00353 line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
00354 }
00355 while (line_is_blank);
00356 }
00357
00358
00360
00362
00363 template class VertexMeshReader<1,1>;
00364 template class VertexMeshReader<1,2>;
00365 template class VertexMeshReader<1,3>;
00366 template class VertexMeshReader<2,2>;
00367 template class VertexMeshReader<2,3>;
00368 template class VertexMeshReader<3,3>;