Chaste  Build::
VesselNetworkActorGenerator.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 VesselNetwork 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 VesselNetworkICULAR 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 <boost/filesystem.hpp>
37 #include <boost/lexical_cast.hpp>
38 #define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the vtk deprecated warning
39 #include <vtkPoints.h>
40 #include <vtkPolyData.h>
41 #include <vtkPolyDataMapper.h>
42 #include <vtkActor.h>
43 #include <vtkProperty.h>
44 #include <vtkUnsignedCharArray.h>
45 #if VTK_MAJOR_VERSION > 5
46  #include <vtkAutoInit.h>
47  VTK_MODULE_INIT(vtkRenderingOpenGL);
48  VTK_MODULE_INIT(vtkRenderingFreeType);
49  #include <vtkOggTheoraWriter.h>
50 #endif
51 #if VTK_MAJOR_VERSION > 5
52  #include <vtkNamedColors.h>
53 #endif
54 #include <vtkSphereSource.h>
55 #include <vtkGlyph3D.h>
56 #include <vtkGlyph2D.h>
57 #include <vtkCubeAxesActor2D.h>
58 #include <vtkImageData.h>
59 #include <vtkGeometryFilter.h>
60 #include <vtkTubeFilter.h>
61 #include <vtkExtractEdges.h>
62 #include <vtkUnstructuredGrid.h>
63 #include <vtkCell.h>
64 #include <vtkPolygon.h>
65 #include <vtkIdList.h>
66 #include <vtkCellData.h>
67 #include <vtkFeatureEdges.h>
68 #include <vtkPointData.h>
69 #include <vtkLookupTable.h>
70 #include <vtkScalarBarActor.h>
71 #include <vtkTextProperty.h>
72 #include "UblasIncludes.hpp"
73 #include "UblasVectorInclude.hpp"
74 #include "Exception.hpp"
75 #include "BaseUnits.hpp"
76 #include "VesselNetworkWriter.hpp"
77 
78 #include "VesselNetworkActorGenerator.hpp"
79 
80 
81 template<unsigned DIM>
83  : AbstractActorGenerator<DIM>(),
84  mpVesselNetwork()
85 {
86 
87 }
88 
89 template<unsigned DIM>
91 {
92 
93 }
94 
95 template<unsigned DIM>
96 void VesselNetworkActorGenerator<DIM>::AddActor(vtkSmartPointer<vtkRenderer> pRenderer)
97 {
98  if(mpVesselNetwork)
99  {
100  VesselNetworkWriter<DIM> network_writer;
101  network_writer.SetVesselNetwork(mpVesselNetwork);
102  vtkSmartPointer<vtkPolyData> p_polydata = network_writer.GetOutput();
103  vtkSmartPointer<vtkColorTransferFunction> p_scaled_ctf = vtkSmartPointer<vtkColorTransferFunction>::New();
104  if(!this->mDataLabel.empty())
105  {
106  double range[2];
107  p_polydata->GetPointData()->GetArray(this->mDataLabel.c_str())->GetRange(range);
108  for(unsigned idx=0; idx<256; idx++)
109  {
110  double color[3];
111  this->mpColorTransferFunction->GetColor(double(idx)/255.0, color);
112  p_scaled_ctf->AddRGBPoint(range[0] + double(idx)*(range[1]-range[0])/255.0, color[0], color[1], color[2]);
113  }
114  }
115  if(this->mShowPoints)
116  {
117  vtkSmartPointer<vtkSphereSource> p_sheres = vtkSmartPointer<vtkSphereSource>::New();
118  p_sheres->SetRadius(this->mPointSize);
119  p_sheres->SetPhiResolution(16);
120  p_sheres->SetThetaResolution(16);
121 
122  vtkSmartPointer<vtkGlyph3D> p_shere_glyphs = vtkSmartPointer<vtkGlyph3D>::New();
123  #if VTK_MAJOR_VERSION <= 5
124  p_shere_glyphs->SetInput(p_polydata);
125  p_shere_glyphs->SetSource(p_sheres->GetOutput());
126  #else
127  p_shere_glyphs->SetInputData(p_polydata);
128  p_shere_glyphs->SetSourceData(p_sheres->GetOutput());
129  #endif
130  p_shere_glyphs->ClampingOff();
131  p_shere_glyphs->SetScaleModeToScaleByScalar();
132  p_shere_glyphs->SetScaleFactor(1.0);
133 
134  vtkSmartPointer<vtkPolyDataMapper> p_polydata_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
135  #if VTK_MAJOR_VERSION <= 5
136  p_polydata_mapper->SetInput(p_shere_glyphs->GetOutput());
137  #else
138  p_polydata_mapper->SetInputData(p_shere_glyphs->GetOutput());
139  #endif
140  p_polydata_mapper->ScalarVisibilityOn();
141 
142  vtkSmartPointer<vtkActor> p_actor = vtkSmartPointer<vtkActor>::New();
143  p_actor->SetMapper(p_polydata_mapper);
144  p_actor->GetProperty()->SetColor(this->mPointColor[0]/255.0, this->mPointColor[1]/255.0, this->mPointColor[2]/255.0);
145  pRenderer->AddActor(p_actor);
146  }
147 
148  if(this->mShowEdges)
149  {
150  vtkSmartPointer<vtkPolyDataMapper> p_polydata_mapper2 = vtkSmartPointer<vtkPolyDataMapper>::New();
151  #if VTK_MAJOR_VERSION <= 5
152  p_polydata_mapper2->SetInput(p_polydata);
153  #else
154  p_polydata_mapper2->SetInputData(p_polydata);
155  #endif
156  p_polydata_mapper2->ScalarVisibilityOn();
157  if(!this->mDataLabel.empty())
158  {
159  p_polydata_mapper2->SetLookupTable(p_scaled_ctf);
160  }
161 
162  vtkSmartPointer<vtkActor> p_actor2 = vtkSmartPointer<vtkActor>::New();
163  p_actor2->SetMapper(p_polydata_mapper2);
164  p_actor2->GetProperty()->SetColor(this->mEdgeColor[0]/255.0, this->mEdgeColor[1]/255.0, this->mEdgeColor[2]/255.0);
165  pRenderer->AddActor(p_actor2);
166  }
167 
168  if(this->mShowVolume)
169  {
170  vtkSmartPointer<vtkTubeFilter> p_tubes = vtkSmartPointer<vtkTubeFilter>::New();
171  #if VTK_MAJOR_VERSION <= 5
172  p_tubes->SetInput(p_polydata);
173  #else
174  p_tubes->SetInputData(p_polydata);
175  #endif
176  p_tubes->SetRadius(this->mEdgeSize);
177  p_tubes->SetNumberOfSides(12);
178 
179  vtkSmartPointer<vtkPolyDataMapper> p_tube_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
180  p_tube_mapper->SetInputConnection(p_tubes->GetOutputPort());
181  if(!this->mDataLabel.empty())
182  {
183  p_tube_mapper->SetLookupTable(p_scaled_ctf);
184  p_tube_mapper->ScalarVisibilityOn();
185  p_tube_mapper->InterpolateScalarsBeforeMappingOn();
186  p_tube_mapper->SelectColorArray(this->mDataLabel.c_str());
187  p_tube_mapper->SetScalarModeToUsePointFieldData();
188  p_tube_mapper->SetColorModeToMapScalars();
189  }
190 
191  vtkSmartPointer<vtkActor> p_tube_actor = vtkSmartPointer<vtkActor>::New();
192  p_tube_actor->SetMapper(p_tube_mapper);
193  if(this->mDataLabel.empty())
194  {
195  p_tube_actor->GetProperty()->SetColor(this->mVolumeColor[0]/255.0, this->mVolumeColor[1]/255.0, this->mVolumeColor[2]/255.0);
196  }
197  p_tube_actor->GetProperty()->SetOpacity(this->mVolumeOpacity);
198  pRenderer->AddActor(p_tube_actor);
199 
200  if(!this->mDataLabel.empty())
201  {
202  vtkSmartPointer<vtkScalarBarActor> p_scale_bar = vtkSmartPointer<vtkScalarBarActor>::New();
203  p_scale_bar->SetLookupTable(p_scaled_ctf);
204  p_scale_bar->SetTitle(this->mDataLabel.c_str());
205  p_scale_bar->SetOrientationToHorizontal();
206  p_scale_bar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
207  p_scale_bar->GetPositionCoordinate()->SetValue(0.25, 0.84);
208  p_scale_bar->SetWidth(0.5);
209  p_scale_bar->SetHeight(0.1);
210  p_scale_bar->GetTitleTextProperty()->ItalicOff();
211  p_scale_bar->GetLabelTextProperty()->ItalicOff();
212  p_scale_bar->GetTitleTextProperty()->BoldOff();
213  p_scale_bar->GetLabelTextProperty()->BoldOff();
214  p_scale_bar->SetLabelFormat("%.2g");
215  p_scale_bar->GetTitleTextProperty()->SetFontSize(5.0);
216  p_scale_bar->GetLabelTextProperty()->SetFontSize(5.0);
217  p_scale_bar->GetTitleTextProperty()->SetColor(0.0, 0.0, 0.0);
218  p_scale_bar->GetLabelTextProperty()->SetColor(0.0, 0.0, 0.0);
219 
220  pRenderer->AddActor(p_scale_bar);
221  }
222  }
223  }
224 }
225 
226 template<unsigned DIM>
228 {
229  this->mpVesselNetwork = pVesselNetwork;
230 }
231 
232 template class VesselNetworkActorGenerator<2>;
233 template class VesselNetworkActorGenerator<3>;
c_vector< double, 3 > mEdgeColor
void AddActor(vtkSmartPointer< vtkRenderer > pRenderer)
void SetVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pNetwork)
boost::shared_ptr< VesselNetwork< DIM > > mpVesselNetwork
c_vector< double, 3 > mPointColor
vtkSmartPointer< vtkColorTransferFunction > mpColorTransferFunction
void SetVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pVesselNetwork)
c_vector< double, 3 > mVolumeColor
vtkSmartPointer< vtkPolyData > GetOutput()