PottsMeshReader.cpp
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
00029 #include "PottsMeshReader.hpp"
00030 #include "Exception.hpp"
00031
00032 #include <sstream>
00033
00034 template<unsigned SPACE_DIM>
00035 PottsMeshReader<SPACE_DIM>::PottsMeshReader(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 SPACE_DIM>
00049 unsigned PottsMeshReader<SPACE_DIM>::GetNumElements() const
00050 {
00051 return mNumElements;
00052 }
00053
00054 template<unsigned SPACE_DIM>
00055 unsigned PottsMeshReader<SPACE_DIM>::GetNumNodes() const
00056 {
00057 return mNumNodes;
00058 }
00059
00060 template<unsigned SPACE_DIM>
00061 unsigned PottsMeshReader<SPACE_DIM>::GetNumElementAttributes() const
00062 {
00063 return mNumElementAttributes;
00064 }
00065
00066 template<unsigned SPACE_DIM>
00067 ElementData PottsMeshReader<SPACE_DIM>::GetNextFaceData()
00068 {
00070 ElementData ret;
00071 ret.NodeIndices = std::vector<unsigned>();
00072 ret.AttributeValue = 0;
00073 return ret;
00074 }
00075
00076 template<unsigned SPACE_DIM>
00077 unsigned PottsMeshReader<SPACE_DIM>::GetNumFaces() const
00078 {
00080 return 0;
00081 }
00082
00083 template<unsigned SPACE_DIM>
00084 void PottsMeshReader<SPACE_DIM>::Reset()
00085 {
00086 CloseFiles();
00087 OpenFiles();
00088 ReadHeaders();
00089
00090 mNodesRead = 0;
00091 mElementsRead = 0;
00092 }
00093
00094 template<unsigned SPACE_DIM>
00095 std::vector<double> PottsMeshReader<SPACE_DIM>::GetNextNode()
00096 {
00097 std::vector<double> node_data;
00098
00099 std::string buffer;
00100 GetNextLineFromStream(mNodesFile, buffer);
00101
00102 std::stringstream buffer_stream(buffer);
00103
00104 unsigned index;
00105 buffer_stream >> index;
00106
00107 unsigned offset = mIndexFromZero ? 0 : 1;
00108 if (index != mNodesRead + offset)
00109 {
00110 EXCEPTION("Data for node " << mNodesRead << " missing");
00111 }
00112
00113 double node_value;
00114 for (unsigned i=0; i<SPACE_DIM+1; i++)
00115 {
00116 buffer_stream >> node_value;
00117 node_data.push_back(node_value);
00118 }
00119
00120 mNodesRead++;
00121 return node_data;
00122 }
00123
00124 template<unsigned SPACE_DIM>
00125 ElementData PottsMeshReader<SPACE_DIM>::GetNextElementData()
00126 {
00127
00128 ElementData element_data;
00129
00130 std::string buffer;
00131 GetNextLineFromStream(mElementsFile, buffer);
00132
00133 std::stringstream buffer_stream(buffer);
00134
00135 unsigned element_index;
00136 buffer_stream >> element_index;
00137
00138 unsigned offset = mIndexFromZero ? 0 : 1;
00139 if (element_index != mElementsRead + offset)
00140 {
00141 EXCEPTION("Data for element " << mElementsRead << " missing");
00142 }
00143
00144 unsigned num_nodes_in_element;
00145 buffer_stream >> num_nodes_in_element;
00146
00147
00148 unsigned node_index;
00149 for (unsigned i=0; i<num_nodes_in_element; i++)
00150 {
00151 buffer_stream >> node_index;
00152 element_data.NodeIndices.push_back(node_index - offset);
00153 }
00154
00155 if (mNumElementAttributes > 0)
00156 {
00157 assert(mNumElementAttributes==1);
00158
00159 unsigned attribute_value;
00160 buffer_stream >> attribute_value;
00161 element_data.AttributeValue = attribute_value;
00162 }
00163 else
00164 {
00165 element_data.AttributeValue = 0;
00166 }
00167
00168 mElementsRead++;
00169 return element_data;
00170 }
00171
00172 template<unsigned SPACE_DIM>
00173 void PottsMeshReader<SPACE_DIM>::OpenFiles()
00174 {
00175 OpenNodeFile();
00176 OpenElementsFile();
00177 }
00178
00179 template<unsigned SPACE_DIM>
00180 void PottsMeshReader<SPACE_DIM>::OpenNodeFile()
00181 {
00182
00183 std::string file_name = mFilesBaseName + ".node";
00184 mNodesFile.open(file_name.c_str());
00185 if (!mNodesFile.is_open())
00186 {
00187 EXCEPTION("Could not open data file: " + file_name);
00188 }
00189 }
00190
00191 template<unsigned SPACE_DIM>
00192 void PottsMeshReader<SPACE_DIM>::OpenElementsFile()
00193 {
00194
00195 std::string file_name;
00196 file_name = mFilesBaseName + ".cell";
00197
00198 mElementsFile.open(file_name.c_str());
00199 if (!mElementsFile.is_open())
00200 {
00201 EXCEPTION("Could not open data file: " + file_name);
00202 }
00203 }
00204
00205 template<unsigned SPACE_DIM>
00206 void PottsMeshReader<SPACE_DIM>::ReadHeaders()
00207 {
00208 std::string buffer;
00209
00210 GetNextLineFromStream(mNodesFile, buffer);
00211 std::stringstream buffer_stream(buffer);
00212 buffer_stream >> mNumNodes >> mNumNodeAttributes;
00213
00214
00215 GetNextLineFromStream(mNodesFile, buffer);
00216 std::stringstream node_buffer_stream(buffer);
00217
00218 unsigned first_index;
00219 node_buffer_stream >> first_index;
00220 assert(first_index == 0 || first_index == 1);
00221 mIndexFromZero = (first_index == 0);
00222
00223
00224 mNodesFile.close();
00225 OpenNodeFile();
00226 GetNextLineFromStream(mNodesFile, buffer);
00227
00228 GetNextLineFromStream(mElementsFile, buffer);
00229 std::stringstream element_buffer_stream(buffer);
00230
00231 element_buffer_stream >> mNumElements >> mNumElementAttributes;
00232 }
00233
00234 template<unsigned SPACE_DIM>
00235 void PottsMeshReader<SPACE_DIM>::CloseFiles()
00236 {
00237 mNodesFile.close();
00238 mElementsFile.close();
00239 }
00240
00241 template<unsigned SPACE_DIM>
00242 void PottsMeshReader<SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
00243 {
00244 bool line_is_blank;
00245
00246 do
00247 {
00248 getline(fileStream, rawLine);
00249
00250 if (fileStream.eof())
00251 {
00252 EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
00253 }
00254
00255
00256 rawLine = rawLine.substr(0,rawLine.find('#', 0));
00257
00258 line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
00259 }
00260 while (line_is_blank);
00261 }
00262
00264
00266
00267 template class PottsMeshReader<1>;
00268 template class PottsMeshReader<2>;
00269 template class PottsMeshReader<3>;