MemfemMeshReader.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
00030
00031
00032
00033
00034
00035
00036 #include "MemfemMeshReader.hpp"
00037 #include "Exception.hpp"
00038
00039 #include <sstream>
00040
00042
00044
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::MemfemMeshReader(const std::string& rPathBaseName)
00047 {
00048
00049 std::string node_file_name = rPathBaseName + ".pts";
00050 this->mNodeRawData = this->GetRawDataFromFile(node_file_name);
00051
00052
00053 std::stringstream node_header_stream(this->mNodeRawData[0]);
00054 unsigned num_nodes;
00055 node_header_stream >> num_nodes;
00056
00057
00058 if (SPACE_DIM != 3 || ELEMENT_DIM != 3)
00059 {
00060 EXCEPTION("You have asked to read non-3D data. All Memfem data is in 3D.");
00061 }
00062
00063
00064 this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
00065
00066
00067 this->mpNodeIterator = this->mNodeData.begin();
00068
00069
00070 if (num_nodes != this->mNodeData.size())
00071 {
00072
00073
00074 #define COVERAGE_IGNORE
00075 EXCEPTION("Number of nodes does not match expected number declared in header");
00076 #undef COVERAGE_IGNORE
00077 }
00078
00079
00080 std::string element_file_name = rPathBaseName + ".tetras";
00081 this->mElementRawData = this->GetRawDataFromFile(element_file_name);
00082
00083
00084 std::stringstream element_header_stream(this->mElementRawData[0]);
00085 unsigned num_elements;
00086 element_header_stream >> num_elements;
00087
00088
00089 this->mElementData = TokenizeStringsToInts(this->mElementRawData, SPACE_DIM+1, true);
00090 this->mpElementIterator = this->mElementData.begin();
00091
00092
00093 if (num_elements != this->mElementData.size())
00094 {
00095
00096
00097 #define COVERAGE_IGNORE
00098 EXCEPTION("Number of elements does not match expected number declared in header");
00099 #undef COVERAGE_IGNORE
00100 }
00101
00102
00103 std::string face_file_name = rPathBaseName + ".tri";
00104 this->mFaceRawData = this->GetRawDataFromFile(face_file_name);
00105
00106
00107
00108
00109 this->mFaceData = TokenizeStringsToInts(this->mFaceRawData, SPACE_DIM, false);
00110 this->mpFaceIterator = this->mFaceData.begin();
00111 }
00112
00113 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00114 MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::~MemfemMeshReader()
00115 {}
00116
00117 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00118 std::vector<std::vector<double> > MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToDoubles(
00119 const std::vector<std::string>& rRawData)
00120 {
00121 std::vector<std::vector<double> > tokenized_data;
00122
00123
00124 std::vector<std::string>::const_iterator the_iterator;
00125 for (the_iterator = rRawData.begin(); the_iterator != rRawData.end(); the_iterator++ )
00126 {
00127 const std::string& line_of_data = *the_iterator;
00128 std::stringstream line_stream(line_of_data);
00129
00130 if (the_iterator != rRawData.begin())
00131 {
00132 std::vector<double> current_coords;
00133
00134
00135 for (unsigned i=0; i<SPACE_DIM; i++)
00136 {
00137 double item_coord;
00138 line_stream >> item_coord;
00139 current_coords.push_back(item_coord);
00140 }
00141
00142
00143 tokenized_data.push_back(current_coords);
00144 }
00145 }
00146
00147 return tokenized_data;
00148 }
00149
00150 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00151 std::vector<std::vector<unsigned> > MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts(
00152 const std::vector<std::string>& rRawData,
00153 unsigned dimensionOfObject,
00154 bool readHeader)
00155 {
00156 std::vector<std::vector<unsigned> > tokenized_data;
00157
00158 std::vector<std::string>::const_iterator the_iterator;
00159 for (the_iterator = rRawData.begin(); the_iterator != rRawData.end(); the_iterator++ )
00160 {
00161 const std::string& line_of_data = *the_iterator;
00162 std::stringstream line_stream(line_of_data);
00163
00164 if ( readHeader == false || the_iterator != rRawData.begin() )
00165 {
00166 std::vector<unsigned> current_indices;
00167
00168 for (unsigned i=0; i<dimensionOfObject; i++)
00169 {
00170 unsigned item_index;
00171 line_stream >> item_index;
00172
00173 item_index -= 1;
00174 current_indices.push_back(item_index);
00175 }
00176
00177 tokenized_data.push_back(current_indices);
00178 }
00179 }
00180
00181 return tokenized_data;
00182 }
00183
00185
00187
00188 template class MemfemMeshReader<1,1>;
00189 template class MemfemMeshReader<1,2>;
00190 template class MemfemMeshReader<1,3>;
00191 template class MemfemMeshReader<2,2>;
00192 template class MemfemMeshReader<2,3>;
00193 template class MemfemMeshReader<3,3>;