Chaste  Build::
NetworkToImage.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 "RegularGrid.hpp"
38 #include "DistanceMap.hpp"
39 #include "NetworkToImage.hpp"
40 #include "UnitCollection.hpp"
41 #include "BaseUnits.hpp"
42 #include "UblasIncludes.hpp"
43 #include "UblasVectorInclude.hpp"
44 
45 template<unsigned DIM>
47  : mpImage(vtkSmartPointer<vtkImageData>::New()),
48  mpNetwork(),
49  mGridSpacing(BaseUnits::Instance()->GetReferenceLengthScale()),
50  mPaddingFactors(zero_vector<double>(DIM)),
51  mImageDimension(DIM)
52 {
53 
54 }
55 
56 template<unsigned DIM>
57 boost::shared_ptr<NetworkToImage<DIM> > NetworkToImage<DIM>::Create()
58 {
59  MAKE_PTR(NetworkToImage<DIM>, pSelf);
60  return pSelf;
61 }
62 
63 template<unsigned DIM>
65 {
66 
67 }
68 
69 template<unsigned DIM>
70 vtkSmartPointer<vtkImageData> NetworkToImage<DIM>::GetOutput()
71 {
72  if(mpImage)
73  {
74  return(mpImage);
75  }
76  else
77  {
78  EXCEPTION("No output set. Did you run 'Update()' ?");
79  }
80 }
81 
82 template<unsigned DIM>
83 void NetworkToImage<DIM>::SetNetwork(boost::shared_ptr<VesselNetwork<DIM> > pNetwork)
84 {
85  mpNetwork = pNetwork;
86 }
87 
88 template<unsigned DIM>
89 void NetworkToImage<DIM>::SetGridSpacing(units::quantity<unit::length> spacing)
90 {
91  mGridSpacing = spacing;
92 }
93 
94 template<unsigned DIM>
95 void NetworkToImage<DIM>::SetPaddingFactors(double paddingX, double paddingY, double paddingZ)
96 {
97  mPaddingFactors[0] = paddingX;
98  mPaddingFactors[1] = paddingY;
99  if(DIM==3)
100  {
101  mPaddingFactors[2] = paddingZ;
102  }
103 }
104 
105 template<unsigned DIM>
107 {
108  mImageDimension = dimension;
109 }
110 
111 template<unsigned DIM>
113 {
114  if(!mpNetwork)
115  {
116  EXCEPTION("No input vessel network set.");
117  }
118 
119  std::pair<DimensionalChastePoint<DIM>, DimensionalChastePoint<DIM> > extents = mpNetwork->GetExtents(true);
120  c_vector<double, DIM> range = ublas::element_prod(scalar_vector<double>(DIM, 1.0) + 2.0*mPaddingFactors, (extents.second - extents.first).GetLocation(mGridSpacing));
121  c_vector<double, DIM> origin = (extents.first.GetLocation(mGridSpacing) - ublas::element_prod((extents.second - extents.first).GetLocation(mGridSpacing), mPaddingFactors));
122 
123  boost::shared_ptr<RegularGrid<DIM> > p_grid = RegularGrid<DIM>::Create();
124  p_grid->SetSpacing(mGridSpacing);
125  std::vector<unsigned> final_extents;
126  final_extents.push_back(unsigned(range[0])+1);
127  final_extents.push_back(unsigned(range[1])+1);
128  if(mImageDimension == 3 and DIM==3)
129  {
130  final_extents.push_back(unsigned(range[2])+1);
131  }
132  else
133  {
134  final_extents.push_back(1);
135  }
136  p_grid->SetExtents(final_extents);
137  p_grid->SetOrigin(DimensionalChastePoint<DIM>(origin, mGridSpacing));
138 
139  boost::shared_ptr<DistanceMap<DIM> > p_distance_map = DistanceMap<DIM>::Create();
140  p_distance_map->SetVesselNetwork(mpNetwork);
141  p_distance_map->SetGrid(p_grid);
142  p_distance_map->SetUseSegmentRadii(true);
143  p_distance_map->Setup();
144  p_distance_map->Solve();
145 
146  std::vector<double> point_solution = p_distance_map->GetSolution(p_grid);
147  for(unsigned idx=0; idx<point_solution.size();idx++)
148  {
149  if(point_solution[idx]==0.0)
150  {
151  point_solution[idx] = 1.0;
152  }
153  else
154  {
155  point_solution[idx] = 0.0;
156  }
157  }
158 
159  p_grid->SetUpVtkGrid();
160  p_grid->SetPointValues(point_solution);
161  mpImage = p_grid->GetVtkGrid();
162  mpImage->GetPointData()->SetScalars(mpImage->GetPointData()->GetArray("Point Values"));
163 }
164 
165 // Explicit instantiation
166 template class NetworkToImage<2>;
167 template class NetworkToImage<3>;
void SetPaddingFactors(double paddingX, double paddingY, double paddingZ)
void SetImageDimension(unsigned dimension)
static boost::shared_ptr< DistanceMap< DIM > > Create()
Definition: DistanceMap.cpp:51
boost::shared_ptr< VesselNetwork< DIM > > mpNetwork
vtkSmartPointer< vtkImageData > mpImage
unsigned mImageDimension
static boost::shared_ptr< RegularGrid< DIM > > Create()
Definition: RegularGrid.cpp:72
void SetGridSpacing(units::quantity< unit::length > spacing)
void SetNetwork(boost::shared_ptr< VesselNetwork< DIM > > pNetwork)
units::quantity< unit::length > mGridSpacing
vtkSmartPointer< vtkImageData > GetOutput()
c_vector< double, DIM > mPaddingFactors
static boost::shared_ptr< NetworkToImage< DIM > > Create()