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 #ifndef ABSTRACTCACHEDMESHREADER_HPP_
00030 #define ABSTRACTCACHEDMESHREADER_HPP_
00031
00032
00048 #include <vector>
00049 #include <string>
00050 #include <fstream>
00051 #include <iostream>
00052 #include <sstream>
00053 #include "Exception.hpp"
00054 #include "AbstractMeshReader.hpp"
00055
00056 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00057 class AbstractCachedMeshReader : public AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>
00058 {
00059 protected:
00060 unsigned mNumNodeAttributes;
00061 unsigned mMaxNodeBdyMarker;
00062 unsigned mNumElementNodes;
00063 unsigned mNumElementAttributes;
00064 unsigned mMaxFaceBdyMarker;
00066 std::vector<std::string> mNodeRawData;
00067 std::vector<std::string> mElementRawData;
00068 std::vector<std::string> mFaceRawData;
00070 std::vector< std::vector<double> > mNodeData;
00071 std::vector< std::vector<unsigned> > mElementData;
00072 std::vector< std::vector<unsigned> > mFaceData;
00074 std::vector< std::vector<double> >::iterator mpNodeIterator;
00075 std::vector< std::vector<unsigned> >::iterator mpElementIterator;
00076 std::vector< std::vector<unsigned> >::iterator mpFaceIterator;
00078 bool mIndexFromZero;
00080 std::vector<std::string> GetRawDataFromFile(std::string fileName);
00083 public:
00084 AbstractCachedMeshReader()
00085 {
00086 mNumNodeAttributes = 0;
00087 mMaxNodeBdyMarker = 0;
00088 mNumElementNodes = 0;
00089 mNumElementAttributes = 0;
00090 mMaxFaceBdyMarker = 0;
00091
00092
00093
00094 mIndexFromZero = false;
00095 }
00096 virtual ~AbstractCachedMeshReader()
00097 {}
00098
00099
00100 unsigned GetNumElements() const
00101 {
00102 return mElementData.size();
00103 }
00104 unsigned GetNumNodes() const
00105 {
00106 return mNodeData.size();
00107 }
00108 unsigned GetNumFaces() const
00109 {
00110 return mFaceData.size();
00111 }
00112 unsigned GetNumEdges() const
00113 {
00114 return mFaceData.size();
00115 }
00117 unsigned GetMaxNodeIndex();
00118 unsigned GetMinNodeIndex();
00120 std::vector<double> GetNextNode();
00121 void Reset();
00122 ElementData GetNextElementData();
00123 ElementData GetNextEdgeData();
00124 ElementData GetNextFaceData();
00125 };
00126
00127
00137 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00138 std::vector<std::string> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetRawDataFromFile(std::string fileName)
00139 {
00140
00141
00142 std::vector<std::string> RawDataFromFile;
00143 std::ifstream dataFile(fileName.c_str());
00144
00145
00146
00147 if (!dataFile.is_open())
00148 {
00149 EXCEPTION("Could not open data file "+fileName+" .");
00150 }
00151
00152
00153 std::string RawLineFromFile;
00154 getline(dataFile, RawLineFromFile);
00155
00156 while (dataFile)
00157 {
00158
00159
00160
00161 long hash_location=RawLineFromFile.find('#',0);
00162 if (hash_location >= 0)
00163 {
00164 RawLineFromFile=RawLineFromFile.substr(0,hash_location);
00165 }
00166
00167
00168 long not_blank_location=RawLineFromFile.find_first_not_of(" \t",0);
00169 if (not_blank_location >= 0)
00170 {
00171 RawDataFromFile.push_back(RawLineFromFile);
00172 }
00173
00174
00175 getline(dataFile, RawLineFromFile);
00176 }
00177
00178 dataFile.close();
00179 return(RawDataFromFile);
00180 }
00181
00182
00183
00188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00189 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMaxNodeIndex()
00190 {
00191
00192 std::vector<std::vector<unsigned> >::iterator the_iterator;
00193
00194 unsigned max_node_index = 0;
00195
00196 for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00197 {
00198 std::vector<unsigned> indices = *the_iterator;
00199
00200 for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00201 {
00202 if ( indices[i] > max_node_index)
00203 {
00204 max_node_index = indices[i];
00205 }
00206 }
00207 }
00208
00209 return max_node_index;
00210 }
00211
00212
00213
00218 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00219 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMinNodeIndex()
00220 {
00221
00222 std::vector<std::vector<unsigned> >::iterator the_iterator;
00223
00224 unsigned min_node_index = UINT_MAX;
00225
00226 for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00227 {
00228 std::vector<unsigned> indices = *the_iterator;
00229
00230 for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00231 {
00232 if (indices[i] < min_node_index)
00233 {
00234 min_node_index = indices[i];
00235 }
00236 }
00237 }
00238
00239 return min_node_index;
00240 }
00241
00242
00247 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00248 std::vector<double> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextNode()
00249 {
00250
00251
00252 if (mpNodeIterator == mNodeData.end())
00253 {
00254 EXCEPTION("All nodes already got");
00255 }
00256
00257 std::vector<double> next_node = *mpNodeIterator;
00258
00259 mpNodeIterator++;
00260
00261 return next_node;
00262 }
00263
00264
00265
00271 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00272 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00273 {
00274
00275
00276 if (mpElementIterator == mElementData.end())
00277 {
00278 EXCEPTION("All elements already got");
00279 }
00280
00281 ElementData ret;
00282 ret.NodeIndices = *mpElementIterator;
00283 ret.AttributeValue = 0;
00284
00285 mpElementIterator++;
00286
00287 return ret;
00288 }
00289
00290
00291 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00292 void AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::Reset()
00293 {
00294 mpElementIterator = mElementData.begin();
00295 mpFaceIterator = mFaceData.begin();
00296 mpNodeIterator = mNodeData.begin();
00297 }
00298
00299
00307 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00308 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextFaceData()
00309 {
00310
00311
00312 if (mpFaceIterator == mFaceData.end())
00313 {
00314 EXCEPTION("All faces (or edges) already got");
00315 }
00316
00317 ElementData ret;
00318 ret.NodeIndices = *mpFaceIterator;
00319 ret.AttributeValue = 0;
00320
00321 mpFaceIterator++;
00322
00323 return ret;
00324 }
00325
00326
00327
00335 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00336 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextEdgeData()
00337 {
00338 return GetNextFaceData();
00339 }
00340
00341
00342 #endif