FemlabMeshReader.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
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 #include "FemlabMeshReader.hpp"
00030 #include "Exception.hpp"
00031 
00032 #include <sstream>
00033 
00035 // Implementation
00037 
00038 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::FemlabMeshReader(const std::string& rPathBaseName,
00040                                                            const std::string& rNodeFileName,
00041                                                            const std::string& rElementFileName,
00042                                                            const std::string& rEdgeFileName)
00043 {
00044 
00045     // Open node file and store the lines as a vector of strings (minus the comments)
00046     std::string node_file_name = rPathBaseName + rNodeFileName;
00047     this->mNodeRawData = this->GetRawDataFromFile(node_file_name);
00048 
00049     // Read the node data using TokenizeStringsToDoubles method
00050     this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
00051 
00052     // Initialise iterator for public GetNextNode method
00053     this->mpNodeIterator = this->mNodeData.begin();
00054 
00055 
00056     // Open element file and store the lines as a vector of strings (minus the comments)
00057     std::string element_file_name = rPathBaseName + rElementFileName;
00058     this->mElementRawData = this->GetRawDataFromFile(element_file_name);
00059 
00060     // Read the rest of the element data using TokenizeStringsToInts method
00061     this->mElementData = TokenizeStringsToInts(this->mElementRawData, SPACE_DIM + 1);
00062     this->mpElementIterator = this->mElementData.begin();
00063 
00064     /*
00065      * Open edge file and store the lines as a vector of strings (minus the comments)
00066      * We store edges as "faces" but the superclass
00067      * provides a GetNextEdgeData method which queries this data.
00068      */
00069 
00070     std::string edge_file_name = rPathBaseName + rEdgeFileName;
00071     this->mFaceRawData = this->GetRawDataFromFile(edge_file_name);
00072 
00073     // Read the rest of the face/edge data using TokenizeStringsToInts method
00074     this->mFaceData = TokenizeStringsToInts(this->mFaceRawData, SPACE_DIM);
00075     this->mpFaceIterator = this->mFaceData.begin();
00076 }
00077 
00078 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00079 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::~FemlabMeshReader()
00080 {}
00081 
00082 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00083 std::vector < std::vector<double> >
00084     FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToDoubles(const std::vector<std::string>& rRawData)
00085 {
00086     std::vector < std::vector < double > >tokenized_data; // Output
00087 
00088     // Iterate over the lines of input
00089     unsigned dimension_count = 0;
00090     std::vector < std::string >::const_iterator the_iterator;
00091     for (the_iterator = rRawData.begin(); the_iterator != rRawData.end();
00092          the_iterator++)
00093     {
00094         const std::string& line_of_data = *the_iterator;
00095         std::stringstream line_stream (line_of_data);
00096 
00097         if (dimension_count == 0)
00098         {
00099             // First iteration, build the tokenized_data vector and push in x coordinates
00100             while (!line_stream.eof())
00101             {
00102                 double item_coord;
00103 
00104                 std::vector < double >x_coord;
00105                 line_stream >> item_coord;
00106                 x_coord.push_back (item_coord);
00107                 tokenized_data.push_back (x_coord);
00108             }
00109         }
00110         else
00111         {
00112             unsigned current_node = 0;
00113 
00114             // Other iterations, push in coordinates other than x
00115             while (!line_stream.eof())
00116             {
00117                 double item_coord;
00118                 line_stream >> item_coord;
00119                 tokenized_data[current_node].push_back (item_coord);
00120                 current_node++;
00121             }
00122         }
00123         // Dimension of mesh is the same as the line of rawData
00124         dimension_count++;
00125     }
00126 
00127     if (SPACE_DIM != dimension_count)
00128     {
00129         EXCEPTION("SPACE_DIM  != dimension read from file");
00130     }
00131     return (tokenized_data);
00132 }
00133 
00134 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00135 std::vector < std::vector < unsigned > >
00136     FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts(const std::vector<std::string>& rRawData, unsigned dimensionOfObject)
00137 {
00138     std::vector < std::vector < unsigned > >tokenized_data;
00139 
00140     // There are dimensionOfObject lines to be read
00141     for (unsigned i = 0; i < dimensionOfObject; i++)
00142     {
00143         const std::string& line_of_data = rRawData[i];
00144         std::stringstream line_stream (line_of_data);
00145 
00146         if (i == 0)
00147         {
00148             // First iteration, build the tokenized_data vector and push in x coordinates
00149             while (!line_stream.eof())
00150             {
00151                 double item_index;
00152 
00153                 std::vector < unsigned >first_index;
00154                 line_stream >> item_index;
00155                 first_index.push_back ((unsigned) (item_index - 0.5)); // item indices should be minus 1
00156                 tokenized_data.push_back (first_index);
00157             }
00158         }
00159         else
00160         {
00161             unsigned current_node = 0;
00162 
00163             // Other iterations, push in coordinates other than x.
00164             while (!line_stream.eof())
00165             {
00166                 double item_index;
00167                 line_stream >> item_index;
00168                 tokenized_data[current_node].
00169                 push_back ((unsigned) (item_index - 0.5));
00170                 current_node++;
00171             }
00172         }
00173     }
00174     return (tokenized_data);
00175 }
00176 
00177 
00179 // Explicit instantiation
00181 
00182 template class FemlabMeshReader<1,1>;
00183 template class FemlabMeshReader<1,2>;
00184 template class FemlabMeshReader<1,3>;
00185 template class FemlabMeshReader<2,2>;
00186 template class FemlabMeshReader<2,3>;
00187 template class FemlabMeshReader<3,3>;
Generated on Thu Dec 22 13:00:16 2011 for Chaste by  doxygen 1.6.3