FemlabMeshReader.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 
00037 #ifndef _FEMLABMESHREADER_H_
00038 #define _FEMLABMESHREADER_H_
00039 
00040 #include "AbstractCachedMeshReader.hpp"
00041 #include "Exception.hpp"
00042 
00043 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00044 class FemlabMeshReader : public AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>
00045 {
00046 private:
00047     std::vector<std::vector<double> > TokenizeStringsToDoubles(
00048         std::vector<std::string> rawData);
00049 
00050     std::vector<std::vector<unsigned> > TokenizeStringsToInts(
00051         std::vector<std::string> rawData,
00052         unsigned dimensionOfObject);
00053 public:
00054     FemlabMeshReader(std::string pathBaseName, std::string nodeFileName, std::string elementFileName, std::string edgeFileName);
00055     virtual ~FemlabMeshReader();
00056 };
00057 
00058 
00059 
00071 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00072 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::FemlabMeshReader (std::string pathBaseName,
00073                                                             std::string nodeFileName,
00074                                                             std::string elementFileName,
00075                                                             std::string edgeFileName)
00076 {
00077 
00078     //Open node file and store the lines as a vector of strings (minus the comments)
00079     nodeFileName = pathBaseName + nodeFileName;
00080     this->mNodeRawData = this->GetRawDataFromFile (nodeFileName);
00081 
00082     // Read the node data using TokenizeStringsToDoubles method
00083     this->mNodeData = TokenizeStringsToDoubles (this->mNodeRawData);
00084 
00085     //Initialise iterator for public GetNextNode method
00086     this->mpNodeIterator = this->mNodeData.begin ();
00087 
00088 
00089     //Open element file and store the lines as a vector of strings (minus the comments)
00090     elementFileName = pathBaseName + elementFileName;
00091     this->mElementRawData = this->GetRawDataFromFile (elementFileName);
00092 
00093     // Read the rest of the element data using TokenizeStringsToInts method
00094     this->mElementData = TokenizeStringsToInts (this->mElementRawData, SPACE_DIM + 1);
00095     this->mpElementIterator = this->mElementData.begin ();
00096 
00097 
00098     /*Open edge file and store the lines as a vector of strings (minus the comments)
00099      * We store edges as "faces" but the superclass
00100      * provides a GetNextEdgeData method which queries this data.
00101      */
00102 
00103     edgeFileName = pathBaseName + edgeFileName;
00104     this->mFaceRawData = this->GetRawDataFromFile (edgeFileName);
00105 
00106     // Read the rest of the face/edge data using TokenizeStringsToInts method
00107     this->mFaceData = TokenizeStringsToInts (this->mFaceRawData, SPACE_DIM);
00108     this->mpFaceIterator = this->mFaceData.begin ();
00109 }
00110 
00120 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00121 std::vector < std::vector < double > >
00122 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToDoubles (std::vector < std::string >
00123         rawData)
00124 {
00125     std::vector < std::vector < double > >tokenized_data;        // Output
00126 
00127     //Iterate over the lines of input
00128     unsigned dimension_count = 0;
00129     std::vector < std::string >::iterator the_iterator;
00130     for (the_iterator = rawData.begin (); the_iterator != rawData.end ();
00131          the_iterator++)
00132     {
00133         std::string line_of_data = *the_iterator;
00134         std::stringstream line_stream (line_of_data);
00135 
00136         if (dimension_count == 0)
00137         {
00138             //First iteration, build the tokenized_data vector and push in x coordinates
00139             while (!line_stream.eof ())
00140             {
00141                 double item_coord;
00142 
00143                 std::vector < double >x_coord;
00144                 line_stream >> item_coord;
00145                 x_coord.push_back (item_coord);
00146                 tokenized_data.push_back (x_coord);
00147             }
00148         }
00149         else
00150         {
00151             unsigned current_node = 0;
00152 
00153             //Other iterations, push in coordinates other than x.
00154             while (!line_stream.eof ())
00155             {
00156                 double item_coord;
00157                 line_stream >> item_coord;
00158                 tokenized_data[current_node].push_back (item_coord);
00159                 current_node++;
00160             }
00161         }
00162         //dimension of mesh is the same as the line of rawData.
00163         dimension_count++;
00164 
00165     }
00166 
00167     if (SPACE_DIM != dimension_count)
00168     {
00169         EXCEPTION("SPACE_DIM  != dimension read from file");
00170     }
00171     return (tokenized_data);
00172 }
00173 
00174 
00186 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00187 std::vector < std::vector < unsigned > >
00188 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts (std::vector < std::string > rawData,
00189         unsigned dimensionOfObject)
00190 {
00191     std::vector < std::vector < unsigned > >tokenized_data;
00192 
00193     /* There are dimensionOfObject lines to be read */
00194     for (unsigned i = 0; i < dimensionOfObject; i++)
00195     {
00196         std::string line_of_data = rawData[i];
00197         std::stringstream line_stream (line_of_data);
00198 
00199         if (i == 0)
00200         {
00201             //First iteration, build the tokenized_data vector and push in x coordinates
00202             while (!line_stream.eof ())
00203             {
00204                 double item_index;
00205 
00206                 std::vector < unsigned >first_index;
00207                 line_stream >> item_index;
00208                 first_index.push_back ((unsigned) (item_index - 0.5));       //item indices should be minus 1.
00209                 tokenized_data.push_back (first_index);
00210             }
00211         }
00212         else
00213         {
00214             unsigned current_node = 0;
00215 
00216             //Other iterations, push in coordinates other than x.
00217             while (!line_stream.eof ())
00218             {
00219                 double item_index;
00220                 line_stream >> item_index;
00221                 tokenized_data[current_node].
00222                 push_back ((unsigned) (item_index - 0.5));
00223                 current_node++;
00224             }
00225         }
00226     }
00227     return (tokenized_data);
00228 }
00229 
00230 
00234 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00235 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::~FemlabMeshReader ()
00236 {}
00237 
00238 #endif //_FEMLABMESHREADER_H_

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