Chaste  Build::
PartActorGenerator.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 <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 <vtkNamedColors.h>
47 #endif
48 #include <vtkSphereSource.h>
49 #include <vtkGlyph3D.h>
50 #include <vtkGlyph2D.h>
51 #include <vtkCubeAxesActor2D.h>
52 #include <vtkImageData.h>
53 #include <vtkGeometryFilter.h>
54 #include <vtkTubeFilter.h>
55 #include <vtkExtractEdges.h>
56 #include <vtkUnstructuredGrid.h>
57 #include <vtkCell.h>
58 #include <vtkPolygon.h>
59 #include <vtkIdList.h>
60 #include <vtkFeatureEdges.h>
61 #include "UblasIncludes.hpp"
62 #include "UblasVectorInclude.hpp"
63 #include "Exception.hpp"
64 #include "BaseUnits.hpp"
65 #include "VesselNetworkWriter.hpp"
66 
67 #include "PartActorGenerator.hpp"
68 
69 
70 template<unsigned DIM>
72  : AbstractActorGenerator<DIM>(),
73  mpPart()
74 {
75 
76 }
77 
78 template<unsigned DIM>
80 {
81 
82 }
83 
84 template<unsigned DIM>
85 void PartActorGenerator<DIM>::AddActor(vtkSmartPointer<vtkRenderer> pRenderer)
86 {
87  if(mpPart)
88  {
89  vtkSmartPointer<vtkPolyData> p_polydata = mpPart->GetVtk();
90 
91  // Add the volume
92  if(this->mShowVolume)
93  {
94  vtkSmartPointer<vtkPolyDataMapper> p_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
95  #if VTK_MAJOR_VERSION <= 5
96  p_mapper->SetInput(p_polydata);
97  #else
98  p_mapper->SetInputData(p_polydata);
99  #endif
100 
101  vtkSmartPointer<vtkActor> p_actor = vtkSmartPointer<vtkActor>::New();
102  p_actor->GetProperty()->SetColor(this->mVolumeColor[0]/255.0, this->mVolumeColor[1]/255.0, this->mVolumeColor[2]/255.0);
103  p_actor->GetProperty()->SetOpacity(this->mVolumeOpacity);
104  p_actor->SetMapper(p_mapper);
105  pRenderer->AddActor(p_actor);
106  }
107 
108  // Add the points
109  if(this->mShowPoints)
110  {
111  vtkSmartPointer<vtkSphereSource> p_spheres = vtkSmartPointer<vtkSphereSource>::New();
112  p_spheres->SetRadius(this->mPointSize);
113  p_spheres->SetPhiResolution(16);
114  p_spheres->SetThetaResolution(16);
115 
116  vtkSmartPointer<vtkGlyph3D> p_glyph = vtkSmartPointer<vtkGlyph3D>::New();
117  #if VTK_MAJOR_VERSION <= 5
118  p_glyph->SetInput(p_polydata);
119  p_glyph->SetSource(p_spheres->GetOutput());
120  #else
121  p_glyph->SetInputData(p_polydata);
122  p_glyph->SetSourceConnection(p_spheres->GetOutputPort());
123  #endif
124  p_glyph->ClampingOff();
125  p_glyph->SetScaleModeToScaleByScalar();
126  p_glyph->SetScaleFactor(1.0);
127  p_glyph->Update();
128 
129  vtkSmartPointer<vtkPolyDataMapper> p_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
130  #if VTK_MAJOR_VERSION <= 5
131  p_mapper->SetInput(p_glyph->GetOutput());
132  #else
133  p_mapper->SetInputData(p_glyph->GetOutput());
134  #endif
135  p_mapper->ScalarVisibilityOn();
136 
137  vtkSmartPointer<vtkActor> p_actor = vtkSmartPointer<vtkActor>::New();
138  p_actor->SetMapper(p_mapper);
139  p_actor->GetProperty()->SetColor(this->mPointColor[0]/255.0, this->mPointColor[1]/255.0, this->mPointColor[2]/255.0);
140  pRenderer->AddActor(p_actor);
141  }
142 
143  // Add the edges
144  if(this->mShowEdges)
145  {
146  vtkSmartPointer<vtkFeatureEdges> p_edges = vtkSmartPointer<vtkFeatureEdges>::New();
147  #if VTK_MAJOR_VERSION <= 5
148  p_edges->SetInput(p_polydata);
149  #else
150  p_edges->SetInputData(p_polydata);
151  #endif
152  p_edges->SetFeatureEdges(false);
153  p_edges->SetBoundaryEdges(true);
154  p_edges->SetManifoldEdges(true);
155  p_edges->SetNonManifoldEdges(false);
156 
157  vtkSmartPointer<vtkTubeFilter> p_voronoi_tubes = vtkSmartPointer<vtkTubeFilter>::New();
158  p_voronoi_tubes->SetInputConnection(p_edges->GetOutputPort());
159  p_voronoi_tubes->SetRadius(this->mEdgeSize);
160  p_voronoi_tubes->SetNumberOfSides(12);
161 
162  vtkSmartPointer<vtkPolyDataMapper> p_voronoi_tube_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
163  p_voronoi_tube_mapper->SetInputConnection(p_voronoi_tubes->GetOutputPort());
164 
165  vtkSmartPointer<vtkActor> p_voronoi_tube_actor = vtkSmartPointer<vtkActor>::New();
166  p_voronoi_tube_actor->SetMapper(p_voronoi_tube_mapper);
167  p_voronoi_tube_actor->GetProperty()->SetColor(this->mEdgeColor[0]/255.0, this->mEdgeColor[1]/255.0, this->mEdgeColor[2]/255.0);
168  pRenderer->AddActor(p_voronoi_tube_actor);
169  }
170 
171  }
172 }
173 
174 template<unsigned DIM>
175 void PartActorGenerator<DIM>::SetPart(boost::shared_ptr<Part<DIM> > pPart)
176 {
177  this->mpPart = pPart;
178 }
179 
180 template class PartActorGenerator<2>;
181 template class PartActorGenerator<3>;
c_vector< double, 3 > mEdgeColor
boost::shared_ptr< Part< DIM > > mpPart
void SetPart(boost::shared_ptr< Part< DIM > > pPart)
c_vector< double, 3 > mPointColor
void AddActor(vtkSmartPointer< vtkRenderer > pRenderer)
c_vector< double, 3 > mVolumeColor
Definition: Part.hpp:60