MemfemMeshReader.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
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     //Open node file and store the lines as a vector of strings (minus the comments)
00078     std::string nodeFileName=pathBaseName+".pts";
00079     this->mNodeRawData=this->GetRawDataFromFile(nodeFileName);
00080 
00081     /* Read single line header which is the number of nodes */
00082     std::stringstream node_header_stream(this->mNodeRawData[0]);
00083     unsigned num_nodes;
00084     node_header_stream >> num_nodes;
00085 
00086     /* All Memfem data is in 3-d. */
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     // Read the rest of the node data using TokenizeStringsToDoubles method
00093     this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
00094     //Initialise iterator for public GetNextNode method
00095     this->mpNodeIterator = this->mNodeData.begin();
00096 
00097     //Check that the size of the data matches the information in the header
00098     if (num_nodes != this->mNodeData.size())
00099     {
00100         // ignored from coverage because otherwise would have to create files
00101         // for a bad mesh just to test this line
00102 #define COVERAGE_IGNORE
00103         EXCEPTION("Number of nodes does not match expected number declared in header");
00104 #undef COVERAGE_IGNORE
00105     }
00106 
00107     //Open element file and store the lines as a vector of strings (minus the comments)
00108     std::string elementFileName=pathBaseName+".tetras";
00109     this->mElementRawData=this->GetRawDataFromFile(elementFileName);
00110 
00111     /* Read single line header which is the number of elements   */
00112     std::stringstream element_header_stream(this->mElementRawData[0]);
00113     unsigned num_elements;
00114     element_header_stream >> num_elements;
00115 
00116 
00117     // Read the rest of the element data using TokenizeStringsToInts method
00118     this->mElementData = TokenizeStringsToInts(this->mElementRawData,SPACE_DIM+1, true);
00119     this->mpElementIterator = this->mElementData.begin();
00120 
00121 
00122     //Check that the size of the data matches the information in the header
00123     if (num_elements != this->mElementData.size())
00124     {
00125         // ignored from coverage because otherwise would have to create files
00126         // for a bad mesh just to test this line
00127 #define COVERAGE_IGNORE
00128         EXCEPTION("Number of elements does not match expected number declared in header");
00129 #undef COVERAGE_IGNORE
00130     }
00131 
00132     //Open boundary face file and store the lines as a vector of strings (minus the comments)
00133     std::string faceFileName=pathBaseName+".tri";
00134     this->mFaceRawData=this->GetRawDataFromFile(faceFileName);
00135 
00136     /* There is no header.
00137      */
00138 
00139     // Read the face/edge data using TokenizeStringsToInts method
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; // Output
00158 
00159     //Iterate over the lines of input
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         //std::cout << line_of_data << std::endl;
00165         std::stringstream line_stream(line_of_data);
00166 
00167         if (the_iterator!=rawData.begin()) //Ignore the header string
00168         {
00169             std::vector<double> current_coords;
00170 
00171             //Form the vector which represents the position of this item
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             //Put item onto main output vector
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                 //The nodes have been indexed from one so we need to shift the indices
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_

Generated on Wed Mar 18 12:51:52 2009 for Chaste by  doxygen 1.5.5