Chaste Release::3.1
FemlabMeshReader.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "FemlabMeshReader.hpp"
00037 #include "Exception.hpp"
00038 
00039 #include <sstream>
00040 
00042 // Implementation
00044 
00045 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::FemlabMeshReader(const std::string& rPathBaseName,
00047                                                            const std::string& rNodeFileName,
00048                                                            const std::string& rElementFileName,
00049                                                            const std::string& rEdgeFileName)
00050 {
00051 
00052     // Open node file and store the lines as a vector of strings (minus the comments)
00053     std::string node_file_name = rPathBaseName + rNodeFileName;
00054     this->mNodeRawData = this->GetRawDataFromFile(node_file_name);
00055 
00056     // Read the node data using TokenizeStringsToDoubles method
00057     this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
00058 
00059     // Initialise iterator for public GetNextNode method
00060     this->mpNodeIterator = this->mNodeData.begin();
00061 
00062 
00063     // Open element file and store the lines as a vector of strings (minus the comments)
00064     std::string element_file_name = rPathBaseName + rElementFileName;
00065     this->mElementRawData = this->GetRawDataFromFile(element_file_name);
00066 
00067     // Read the rest of the element data using TokenizeStringsToInts method
00068     this->mElementData = TokenizeStringsToInts(this->mElementRawData, SPACE_DIM + 1);
00069     this->mpElementIterator = this->mElementData.begin();
00070 
00071     /*
00072      * Open edge file and store the lines as a vector of strings (minus the comments)
00073      * We store edges as "faces" but the superclass
00074      * provides a GetNextEdgeData method which queries this data.
00075      */
00076 
00077     std::string edge_file_name = rPathBaseName + rEdgeFileName;
00078     this->mFaceRawData = this->GetRawDataFromFile(edge_file_name);
00079 
00080     // Read the rest of the face/edge data using TokenizeStringsToInts method
00081     this->mFaceData = TokenizeStringsToInts(this->mFaceRawData, SPACE_DIM);
00082     this->mpFaceIterator = this->mFaceData.begin();
00083 }
00084 
00085 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00086 FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::~FemlabMeshReader()
00087 {}
00088 
00089 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00090 std::vector < std::vector<double> >
00091     FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToDoubles(const std::vector<std::string>& rRawData)
00092 {
00093     std::vector < std::vector < double > >tokenized_data; // Output
00094 
00095     // Iterate over the lines of input
00096     unsigned dimension_count = 0;
00097     std::vector < std::string >::const_iterator the_iterator;
00098     for (the_iterator = rRawData.begin(); the_iterator != rRawData.end();
00099          the_iterator++)
00100     {
00101         const std::string& line_of_data = *the_iterator;
00102         std::stringstream line_stream (line_of_data);
00103 
00104         if (dimension_count == 0)
00105         {
00106             // First iteration, build the tokenized_data vector and push in x coordinates
00107             while (!line_stream.eof())
00108             {
00109                 double item_coord;
00110 
00111                 std::vector < double >x_coord;
00112                 line_stream >> item_coord;
00113                 x_coord.push_back (item_coord);
00114                 tokenized_data.push_back (x_coord);
00115             }
00116         }
00117         else
00118         {
00119             unsigned current_node = 0;
00120 
00121             // Other iterations, push in coordinates other than x
00122             while (!line_stream.eof())
00123             {
00124                 double item_coord;
00125                 line_stream >> item_coord;
00126                 tokenized_data[current_node].push_back (item_coord);
00127                 current_node++;
00128             }
00129         }
00130         // Dimension of mesh is the same as the line of rawData
00131         dimension_count++;
00132     }
00133 
00134     if (SPACE_DIM != dimension_count)
00135     {
00136         EXCEPTION("SPACE_DIM  != dimension read from file");
00137     }
00138     return (tokenized_data);
00139 }
00140 
00141 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00142 std::vector < std::vector < unsigned > >
00143     FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts(const std::vector<std::string>& rRawData, unsigned dimensionOfObject)
00144 {
00145     std::vector < std::vector < unsigned > >tokenized_data;
00146 
00147     // There are dimensionOfObject lines to be read
00148     for (unsigned i = 0; i < dimensionOfObject; i++)
00149     {
00150         const std::string& line_of_data = rRawData[i];
00151         std::stringstream line_stream (line_of_data);
00152 
00153         if (i == 0)
00154         {
00155             // First iteration, build the tokenized_data vector and push in x coordinates
00156             while (!line_stream.eof())
00157             {
00158                 double item_index;
00159 
00160                 std::vector < unsigned >first_index;
00161                 line_stream >> item_index;
00162                 first_index.push_back ((unsigned) (item_index - 0.5)); // item indices should be minus 1
00163                 tokenized_data.push_back (first_index);
00164             }
00165         }
00166         else
00167         {
00168             unsigned current_node = 0;
00169 
00170             // Other iterations, push in coordinates other than x.
00171             while (!line_stream.eof())
00172             {
00173                 double item_index;
00174                 line_stream >> item_index;
00175                 tokenized_data[current_node].
00176                 push_back ((unsigned) (item_index - 0.5));
00177                 current_node++;
00178             }
00179         }
00180     }
00181     return (tokenized_data);
00182 }
00183 
00184 
00186 // Explicit instantiation
00188 
00189 template class FemlabMeshReader<1,1>;
00190 template class FemlabMeshReader<1,2>;
00191 template class FemlabMeshReader<1,3>;
00192 template class FemlabMeshReader<2,2>;
00193 template class FemlabMeshReader<2,3>;
00194 template class FemlabMeshReader<3,3>;