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
00030 #ifndef _MEMFEMMESHREADER_HPP_
00031 #define _MEMFEMMESHREADER_HPP_
00032
00042 #include "AbstractCachedMeshReader.hpp"
00043 #include "Exception.hpp"
00044
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 class MemfemMeshReader : public AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>
00047 {
00048 private:
00049 std::vector<std::vector<double> > TokenizeStringsToDoubles(
00050 std::vector<std::string> rawData);
00051
00052 std::vector<std::vector<unsigned> > TokenizeStringsToInts(
00053 std::vector<std::string> rawData,
00054 unsigned dimensionOfObject,
00055 bool readHeader);
00056
00057 public:
00058 MemfemMeshReader(std::string pathBaseName);
00059 virtual ~MemfemMeshReader();
00060 };
00061
00062
00063
00073 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00074 MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::MemfemMeshReader(std::string pathBaseName)
00075 {
00076
00077
00078 std::string nodeFileName=pathBaseName+".pts";
00079 this->mNodeRawData=this->GetRawDataFromFile(nodeFileName);
00080
00081
00082 std::stringstream node_header_stream(this->mNodeRawData[0]);
00083 unsigned num_nodes;
00084 node_header_stream >> num_nodes;
00085
00086
00087 if (SPACE_DIM != 3 || ELEMENT_DIM != 3)
00088 {
00089 EXCEPTION("You have asked to read non-3D data. All Memfem data is in 3D.");
00090 }
00091
00092
00093 this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
00094
00095 this->mpNodeIterator = this->mNodeData.begin();
00096
00097
00098 if (num_nodes != this->mNodeData.size())
00099 {
00100
00101
00102 #define COVERAGE_IGNORE
00103 EXCEPTION("Number of nodes does not match expected number declared in header");
00104 #undef COVERAGE_IGNORE
00105 }
00106
00107
00108 std::string elementFileName=pathBaseName+".tetras";
00109 this->mElementRawData=this->GetRawDataFromFile(elementFileName);
00110
00111
00112 std::stringstream element_header_stream(this->mElementRawData[0]);
00113 unsigned num_elements;
00114 element_header_stream >> num_elements;
00115
00116
00117
00118 this->mElementData = TokenizeStringsToInts(this->mElementRawData,SPACE_DIM+1, true);
00119 this->mpElementIterator = this->mElementData.begin();
00120
00121
00122
00123 if (num_elements != this->mElementData.size())
00124 {
00125
00126
00127 #define COVERAGE_IGNORE
00128 EXCEPTION("Number of elements does not match expected number declared in header");
00129 #undef COVERAGE_IGNORE
00130 }
00131
00132
00133 std::string faceFileName=pathBaseName+".tri";
00134 this->mFaceRawData=this->GetRawDataFromFile(faceFileName);
00135
00136
00137
00138
00139
00140 this->mFaceData = TokenizeStringsToInts(this->mFaceRawData,SPACE_DIM,false);
00141 this->mpFaceIterator = this->mFaceData.begin();
00142 }
00143
00144
00153 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00154 std::vector<std::vector<double> > MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToDoubles(
00155 std::vector<std::string> rawData)
00156 {
00157 std::vector<std::vector<double> > tokenized_data;
00158
00159
00160 std::vector<std::string>::iterator the_iterator;
00161 for ( the_iterator = rawData.begin(); the_iterator != rawData.end(); the_iterator++ )
00162 {
00163 std::string line_of_data=*the_iterator;
00164
00165 std::stringstream line_stream(line_of_data);
00166
00167 if (the_iterator!=rawData.begin())
00168 {
00169 std::vector<double> current_coords;
00170
00171
00172 for (unsigned i = 0; i < SPACE_DIM; i++)
00173 {
00174 double item_coord;
00175 line_stream >> item_coord;
00176 current_coords.push_back(item_coord);
00177 }
00178
00179
00180 tokenized_data.push_back(current_coords);
00181 }
00182
00183 }
00184
00185 return tokenized_data;
00186 }
00187
00188
00201 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00202 std::vector<std::vector<unsigned> > MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts(
00203 std::vector<std::string> rawData,
00204 unsigned dimensionOfObject,
00205 bool readHeader)
00206 {
00207 std::vector<std::vector<unsigned> > tokenized_data;
00208
00209 std::vector<std::string>::iterator the_iterator;
00210 for ( the_iterator = rawData.begin(); the_iterator != rawData.end(); the_iterator++ )
00211 {
00212 std::string line_of_data=*the_iterator;
00213 std::stringstream line_stream(line_of_data);
00214
00215
00216 if ( readHeader == false || the_iterator!=rawData.begin() )
00217 {
00218 std::vector<unsigned> current_indices;
00219
00220 for (unsigned i = 0; i < dimensionOfObject; i++)
00221 {
00222 unsigned item_index;
00223 line_stream >> item_index;
00224
00225 item_index -= 1;
00226 current_indices.push_back(item_index);
00227 }
00228
00229 tokenized_data.push_back(current_indices);
00230 }
00231
00232 }
00233
00234 return tokenized_data;
00235 }
00236
00237
00238 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00239 MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::~MemfemMeshReader()
00240 {}
00241 #endif //_MEMFEMMESHREADER_HPP_