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 EXCEPTION("Data for node " << mNodesRead << " missing");
00118 }
00119
00120 double node_value;
00121 for (unsigned i=0; i<SPACE_DIM+1; i++)
00122 {
00123 buffer_stream >> node_value;
00124 node_data.push_back(node_value);
00125 }
00126
00127 mNodesRead++;
00128 return node_data;
00129 }
00130
00131 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00132 ElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00133 {
00134
00135 ElementData element_data;
00136
00137 std::string buffer;
00138 GetNextLineFromStream(mElementsFile, buffer);
00139
00140 std::stringstream buffer_stream(buffer);
00141
00142 unsigned element_index;
00143 buffer_stream >> element_index;
00144
00145 unsigned offset = mIndexFromZero ? 0 : 1;
00146 if (element_index != mElementsRead + offset)
00147 {
00148 EXCEPTION("Data for element " << mElementsRead << " missing");
00149 }
00150
00151 unsigned num_nodes_in_element;
00152 buffer_stream >> num_nodes_in_element;
00153
00154
00155 unsigned node_index;
00156 for (unsigned i=0; i<num_nodes_in_element; i++)
00157 {
00158 buffer_stream >> node_index;
00159 element_data.NodeIndices.push_back(node_index - offset);
00160 }
00161
00162 if (mNumElementAttributes > 0)
00163 {
00164 assert(mNumElementAttributes == 1);
00165
00166 unsigned attribute_value;
00167 buffer_stream >> attribute_value;
00168 element_data.AttributeValue = attribute_value;
00169 }
00170 else
00171 {
00172 element_data.AttributeValue = 0;
00173 }
00174
00175 mElementsRead++;
00176 return element_data;
00177 }
00178
00179
00180 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00181 VertexElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementDataWithFaces()
00182 {
00183
00184 VertexElementData element_data;
00185
00186 std::string buffer;
00187 GetNextLineFromStream(mElementsFile, buffer);
00188
00189 std::stringstream buffer_stream(buffer);
00190
00191 unsigned element_index;
00192 buffer_stream >> element_index;
00193
00194 unsigned offset = mIndexFromZero ? 0 : 1;
00195 if (element_index != mElementsRead + offset)
00196 {
00197 EXCEPTION("Data for element " << mElementsRead << " missing");
00198 }
00199
00200
00201 unsigned num_nodes_in_element;
00202 buffer_stream >> num_nodes_in_element;
00203
00204
00205 unsigned node_index;
00206 for (unsigned i=0; i<num_nodes_in_element; i++)
00207 {
00208 buffer_stream >> node_index;
00209 element_data.NodeIndices.push_back(node_index - offset);
00210 }
00211
00212
00213 unsigned num_faces_in_element;
00214 buffer_stream >> num_faces_in_element;
00215
00216 element_data.Faces.resize(num_faces_in_element);
00217 for (unsigned j=0; j<num_faces_in_element; j++)
00218 {
00219
00220 ElementData face_data;
00221
00222
00223 unsigned face_index;
00224 buffer_stream >> face_index;
00225 face_data.AttributeValue = face_index;
00226
00227
00228 unsigned num_nodes_in_face;
00229 buffer_stream >> num_nodes_in_face;
00230
00231
00232 unsigned node_index;
00233 for (unsigned i=0; i<num_nodes_in_face; i++)
00234 {
00235 buffer_stream >> node_index;
00236 face_data.NodeIndices.push_back(node_index - offset);
00237 }
00238
00240
00241 element_data.Faces[j] = face_data;
00242 }
00243
00244 if (mNumElementAttributes > 0)
00245 {
00246 assert(mNumElementAttributes==1);
00247
00248 unsigned attribute_value;
00249 buffer_stream >> attribute_value;
00250 element_data.AttributeValue = attribute_value;
00251 }
00252 else
00253 {
00254 element_data.AttributeValue = 0;
00255 }
00256
00257 mElementsRead++;
00258 return element_data;
00259 }
00260
00261 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00262 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenFiles()
00263 {
00264 OpenNodeFile();
00265 OpenElementsFile();
00266 }
00267
00268 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00269 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenNodeFile()
00270 {
00271
00272 std::string file_name = mFilesBaseName + ".node";
00273 mNodesFile.open(file_name.c_str());
00274 if (!mNodesFile.is_open())
00275 {
00276 EXCEPTION("Could not open data file: " + file_name);
00277 }
00278 }
00279
00280 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00281 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenElementsFile()
00282 {
00283
00284 std::string file_name;
00285 file_name = mFilesBaseName + ".cell";
00286
00287 mElementsFile.open(file_name.c_str());
00288 if (!mElementsFile.is_open())
00289 {
00290 EXCEPTION("Could not open data file: " + file_name);
00291 }
00292 }
00293
00294 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00295 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::ReadHeaders()
00296 {
00297 std::string buffer;
00298
00299 GetNextLineFromStream(mNodesFile, buffer);
00300 std::stringstream buffer_stream(buffer);
00301 buffer_stream >> mNumNodes >> mNumNodeAttributes;
00302
00303
00304 GetNextLineFromStream(mNodesFile, buffer);
00305 std::stringstream node_buffer_stream(buffer);
00306
00307 unsigned first_index;
00308 node_buffer_stream >> first_index;
00309 assert(first_index == 0 || first_index == 1);
00310 mIndexFromZero = (first_index == 0);
00311
00312
00313 mNodesFile.close();
00314 OpenNodeFile();
00315 GetNextLineFromStream(mNodesFile, buffer);
00316
00317 GetNextLineFromStream(mElementsFile, buffer);
00318 std::stringstream element_buffer_stream(buffer);
00319
00320 element_buffer_stream >> mNumElements >> mNumElementAttributes;
00321 }
00322
00323 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00324 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::CloseFiles()
00325 {
00326 mNodesFile.close();
00327 mElementsFile.close();
00328 }
00329
00330 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00331 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
00332 {
00333 bool line_is_blank;
00334
00335 do
00336 {
00337 getline(fileStream, rawLine);
00338
00339 if (fileStream.eof())
00340 {
00341 EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
00342 }
00343
00344
00345 rawLine = rawLine.substr(0,rawLine.find('#', 0));
00346
00347 line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
00348 }
00349 while (line_is_blank);
00350 }
00351
00353
00355
00356 template class VertexMeshReader<1,1>;
00357 template class VertexMeshReader<1,2>;
00358 template class VertexMeshReader<1,3>;
00359 template class VertexMeshReader<2,2>;
00360 template class VertexMeshReader<2,3>;
00361 template class VertexMeshReader<3,3>;