Chaste  Build::
CsvVesselNetworkReader.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 "Exception.hpp"
37 #include "CsvVesselNetworkReader.hpp"
38 #include <boost/tokenizer.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include <fstream>
41 
42 template<unsigned DIM>
44  : mFileName(),
45  mRadiusLabel()
46 {
47 }
48 
49 template<unsigned DIM>
51 {
52 }
53 
54 template <unsigned DIM>
55 boost::shared_ptr<CsvVesselNetworkReader<DIM> > CsvVesselNetworkReader<DIM>::Create()
56 {
57  MAKE_PTR(CsvVesselNetworkReader<DIM>, pSelf);
58  return pSelf;
59 }
60 
61 template<unsigned DIM>
62 boost::shared_ptr<VesselNetwork<DIM> > CsvVesselNetworkReader<DIM>::Read()
63 {
64  if(mFileName.empty())
65  {
66  EXCEPTION("File name not set in vessel network reader");
67  }
68 
69  std::ifstream in(mFileName.c_str());
70  if(!in.is_open())
71  {
72  EXCEPTION("Problem during input file opening in vessel network reader.");
73  }
74 
75  // Create an empty vessel network
76  boost::shared_ptr<VesselNetwork<DIM> > p_network = VesselNetwork<DIM>::Create();
77 
78  typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer;
79  std::vector<std::string> vec;
80  std::string line;
81 
82  unsigned counter = 0;
83  while (std::getline(in, line))
84  {
85  // Skip the header
86  if(counter == 0)
87  {
88  counter ++;
89  continue;
90  }
91  Tokenizer tok(line);
92  vec.assign(tok.begin(),tok.end());
93 
94  if(vec.size()<10)
95  {
96  EXCEPTION("CSV File not in expected format");
97  }
98 
99  double pos_x_1 = boost::lexical_cast<double>(vec[3]);
100  double pos_y_1 = boost::lexical_cast<double>(vec[4]);
101  double pos_z_1 = boost::lexical_cast<double>(vec[5]);
102  double pos_x_2 = boost::lexical_cast<double>(vec[6]);
103  double pos_y_2 = boost::lexical_cast<double>(vec[7]);
104  double pos_z_2 = boost::lexical_cast<double>(vec[8]);
105 
106  p_network->AddVessel(Vessel<DIM>::Create(VesselNode<DIM>::Create(pos_x_1, pos_y_1 ,pos_z_1),
107  VesselNode<DIM>::Create(pos_x_2, pos_y_2 ,pos_z_2)));
108 
109  counter ++;
110  }
111 
112  // Need to flip in 'y' direction for consistency with VTK tools
113  std::pair<DimensionalChastePoint<DIM>, DimensionalChastePoint<DIM> > extents = p_network->GetExtents();
114  units::quantity<unit::length> length_scale = extents.second.GetReferenceLengthScale();
115  double y_max = extents.second.GetLocation(length_scale)[1];
116 
117  for(unsigned idx=0; idx<p_network->GetNumberOfNodes();idx++)
118  {
119  c_vector<double, DIM> current_location = p_network->GetNode(idx)->rGetLocation().GetLocation(length_scale);
120  c_vector<double, DIM> new_location;
121  new_location[0] = current_location[0];
122  new_location[1] = y_max - current_location[1];
123  if(DIM==3)
124  {
125  new_location[2] = current_location[2];
126  }
127  p_network->GetNode(idx)->SetLocation(DimensionalChastePoint<DIM>(new_location, length_scale));
128  }
129  return p_network;
130 }
131 
132 template<unsigned DIM>
133 void CsvVesselNetworkReader<DIM>::SetFileName(const std::string& rFileName)
134 {
135  mFileName = rFileName;
136 }
137 
138 //Explicit instantiation
139 template class CsvVesselNetworkReader<2> ;
140 template class CsvVesselNetworkReader<3> ;
units::quantity< unit::length > GetReferenceLengthScale() const
static boost::shared_ptr< VesselNetwork< DIM > > Create()
static boost::shared_ptr< CsvVesselNetworkReader< DIM > > Create()