FibreWriter.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 "FibreWriter.hpp"
00030 #include "Version.hpp"
00031 
00032 template<unsigned DIM>
00033 FibreWriter<DIM>::FibreWriter(const std::string& rDirectory,
00034                               const std::string& rBaseName,
00035                               const bool clearOutputDir)
00036     : mBaseName(rBaseName),
00037       mFileIsBinary(false)
00038 {
00039     mpOutputFileHandler = new OutputFileHandler(rDirectory, clearOutputDir);
00040 }
00041 
00042 template<unsigned DIM>
00043 FibreWriter<DIM>::~FibreWriter()
00044 {
00045     delete mpOutputFileHandler;
00046 }
00047 
00048 template<unsigned DIM>
00049 void FibreWriter<DIM>::WriteAllAxi(const std::vector< c_vector<double, DIM> >& fibres)
00050 {
00051     // Write axi file
00052     out_stream p_axi_file = OpenFileAndWriteHeader(this->mBaseName + ".axi", fibres.size());
00053 
00054     //Now give the fibre directions
00055     for (unsigned i=0; i<fibres.size();i++ )
00056     {
00057         if (this->mFileIsBinary)
00058         {
00059             p_axi_file->write((char*)&fibres[i][0], DIM*sizeof(double));
00060         }
00061         else
00062         {
00063             for(unsigned j=0; j<DIM; j++)
00064             {
00065                 *p_axi_file << fibres[i][j] << "\t";
00066             }
00067             *p_axi_file <<"\n";
00068         }
00069     }
00070     *p_axi_file << "#\n# " <<  ChasteBuildInfo::GetProvenanceString();
00071     p_axi_file->close();
00072 }
00073 
00074 template<unsigned DIM>
00075 void FibreWriter<DIM>::WriteAllOrtho(const std::vector< c_vector<double, DIM> >& fibres,
00076                                      const std::vector< c_vector<double, DIM> >& second,
00077                                      const std::vector< c_vector<double, DIM> >& third)
00078 {
00079     assert(fibres.size() == second.size());
00080     assert(second.size() == third.size());
00081     // Write ortho file
00082     out_stream p_file = OpenFileAndWriteHeader(this->mBaseName + ".ortho", fibres.size());
00083 
00084     //Now give the fibre directions
00085     for (unsigned i=0; i<fibres.size();i++ )
00086     {
00087         if (this->mFileIsBinary)
00088         {
00089             //The binary file is row-major
00090             p_file->write((char*)&fibres[i][0], DIM*sizeof(double));
00091             p_file->write((char*)&second[i][0], DIM*sizeof(double));
00092             p_file->write((char*)&third[i][0], DIM*sizeof(double));
00093         }
00094         else
00095         {
00096             //The ascii file is row-major
00097             for(unsigned j=0; j<DIM; j++)
00098             {
00099                 *p_file << fibres[i][j] << "\t";
00100             }
00101             for(unsigned j=0; j<DIM; j++)
00102             {
00103                 *p_file << second[i][j] << "\t";
00104             }
00105             for(unsigned j=0; j<DIM; j++)
00106             {
00107                 *p_file << third[i][j] << "\t";
00108             }
00109             *p_file <<"\n";
00110         }
00111     }
00112     *p_file << "#\n# " <<  ChasteBuildInfo::GetProvenanceString();
00113     p_file->close();
00114 }
00115 
00116 template<unsigned DIM>
00117 out_stream FibreWriter<DIM>::OpenFileAndWriteHeader(const std::string& rFileName, unsigned numItems)
00118 {
00119     out_stream p_fibre_file = this->mpOutputFileHandler->OpenOutputFile(rFileName);
00120 
00121     //Header line says how many fibres are coming...
00122     *p_fibre_file << numItems;
00123     //... and whether the fibres are binary
00124     if (this->mFileIsBinary)
00125     {
00126         *p_fibre_file << "\tBIN\n";
00127     }
00128     else
00129     {
00130         *p_fibre_file << "\n";
00131     }
00132     return p_fibre_file;
00133 }
00134 
00135 
00136 template<unsigned DIM>
00137 void FibreWriter<DIM>::SetWriteFileAsBinary()
00138 {
00139     mFileIsBinary = true;
00140 }
00141 
00142 template class FibreWriter<1>;
00143 template class FibreWriter<2>;
00144 template class FibreWriter<3>;
00145 
00146 
00147 
00148 
00149 
Generated on Thu Dec 22 13:00:06 2011 for Chaste by  doxygen 1.6.3