Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
FibreWriter.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "FibreWriter.hpp"
37#include "Version.hpp"
38
39template<unsigned DIM>
40FibreWriter<DIM>::FibreWriter(const std::string& rDirectory,
41 const std::string& rBaseName,
42 const bool clearOutputDir)
43 : mBaseName(rBaseName),
44 mFileIsBinary(false)
45{
46 mpOutputFileHandler = new OutputFileHandler(rDirectory, clearOutputDir);
47}
48
49template<unsigned DIM>
51{
52 delete mpOutputFileHandler;
53}
54
55template<unsigned DIM>
56void FibreWriter<DIM>::WriteAllAxi(const std::vector< c_vector<double, DIM> >& fibres)
57{
58 // Write axi file
59 out_stream p_axi_file = OpenFileAndWriteHeader(this->mBaseName + ".axi", fibres.size());
60
61 //Now give the fibre directions
62 for (unsigned i=0; i<fibres.size();i++ )
63 {
64 if (this->mFileIsBinary)
65 {
66 p_axi_file->write((char*)&fibres[i][0], DIM*sizeof(double));
67 }
68 else
69 {
70 for (unsigned j=0; j<DIM; j++)
71 {
72 *p_axi_file << fibres[i][j] << "\t";
73 }
74 *p_axi_file <<"\n";
75 }
76 }
77 *p_axi_file << "#\n# " << ChasteBuildInfo::GetProvenanceString();
78 p_axi_file->close();
79}
80
81template<unsigned DIM>
82void FibreWriter<DIM>::WriteAllOrtho(const std::vector< c_vector<double, DIM> >& fibres,
83 const std::vector< c_vector<double, DIM> >& second,
84 const std::vector< c_vector<double, DIM> >& third)
85{
86 assert(fibres.size() == second.size());
87 assert(second.size() == third.size());
88 // Write ortho file
89 out_stream p_file = OpenFileAndWriteHeader(this->mBaseName + ".ortho", fibres.size());
90
91 //Now give the fibre directions
92 for (unsigned i=0; i<fibres.size();i++ )
93 {
94 if (this->mFileIsBinary)
95 {
96 //The binary file is row-major
97 p_file->write((char*)&fibres[i][0], DIM*sizeof(double));
98 p_file->write((char*)&second[i][0], DIM*sizeof(double));
99 p_file->write((char*)&third[i][0], DIM*sizeof(double));
100 }
101 else
102 {
103 //The ascii file is row-major
104 for (unsigned j=0; j<DIM; j++)
105 {
106 *p_file << fibres[i][j] << "\t";
107 }
108 for (unsigned j=0; j<DIM; j++)
109 {
110 *p_file << second[i][j] << "\t";
111 }
112 for (unsigned j=0; j<DIM; j++)
113 {
114 *p_file << third[i][j] << "\t";
115 }
116 *p_file <<"\n";
117 }
118 }
119 *p_file << "#\n# " << ChasteBuildInfo::GetProvenanceString();
120 p_file->close();
121}
122
123template<unsigned DIM>
124out_stream FibreWriter<DIM>::OpenFileAndWriteHeader(const std::string& rFileName, unsigned numItems)
125{
126 out_stream p_fibre_file = this->mpOutputFileHandler->OpenOutputFile(rFileName);
127
128 //Header line says how many fibres are coming...
129 *p_fibre_file << numItems;
130 //... and whether the fibres are binary
131 if (this->mFileIsBinary)
132 {
133 *p_fibre_file << "\tBIN\n";
134 }
135 else
136 {
137 *p_fibre_file << "\n";
138 }
139 return p_fibre_file;
140}
141
142template<unsigned DIM>
144{
145 mFileIsBinary = true;
146}
147
148// Explicit instantiation
149template class FibreWriter<1>;
150template class FibreWriter<2>;
151template class FibreWriter<3>;
static std::string GetProvenanceString()
void WriteAllOrtho(const std::vector< c_vector< double, DIM > > &fibres, const std::vector< c_vector< double, DIM > > &second, const std::vector< c_vector< double, DIM > > &third)
FibreWriter(const std::string &rDirectory, const std::string &rBaseName, const bool clearOutputDir=true)
void WriteAllAxi(const std::vector< c_vector< double, DIM > > &fibres)
void SetWriteFileAsBinary()
out_stream OpenFileAndWriteHeader(const std::string &rFileName, unsigned numItems)
OutputFileHandler * mpOutputFileHandler