Chaste  Release::2018.1
FibreWriter.cpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 
39 template<unsigned DIM>
40 FibreWriter<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 
49 template<unsigned DIM>
51 {
52  delete mpOutputFileHandler;
53 }
54 
55 template<unsigned DIM>
56 void 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 
81 template<unsigned DIM>
82 void 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 
123 template<unsigned DIM>
124 out_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 
142 template<unsigned DIM>
144 {
145  mFileIsBinary = true;
146 }
147 
148 // Explicit instantiation
149 template class FibreWriter<1>;
150 template class FibreWriter<2>;
151 template class FibreWriter<3>;
void WriteAllAxi(const std::vector< c_vector< double, DIM > > &fibres)
Definition: FibreWriter.cpp:56
OutputFileHandler * mpOutputFileHandler
Definition: FibreWriter.hpp:56
void SetWriteFileAsBinary()
out_stream OpenFileAndWriteHeader(const std::string &rFileName, unsigned numItems)
FibreWriter(const std::string &rDirectory, const std::string &rBaseName, const bool clearOutputDir=true)
Definition: FibreWriter.cpp:40
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)
Definition: FibreWriter.cpp:82