Chaste  Build::
RegularGridActorGenerator.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 RegularGrid 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 RegularGridICULAR 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 #if VTK_MAJOR_VERSION > 5
49  #include <vtkAutoInit.h>
50  VTK_MODULE_INIT(vtkRenderingOpenGL);
51  VTK_MODULE_INIT(vtkRenderingFreeType);
52  #include <vtkOggTheoraWriter.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 <vtkFeatureEdges.h>
67 #include <vtkColorTransferFunction.h>
68 #include <vtkScalarBarActor.h>
69 #include <vtkTextProperty.h>
70 #include "UblasIncludes.hpp"
71 #include "UblasVectorInclude.hpp"
72 #include "Exception.hpp"
73 #include "BaseUnits.hpp"
74 #include "VesselNetworkWriter.hpp"
75 
76 #include "RegularGridActorGenerator.hpp"
77 
78 template<unsigned DIM>
80  : AbstractActorGenerator<DIM>(),
81  mpRegularGrid(),
82  mEdgeOpacity(1.0),
83  mUseTubesForEdges(false)
84 {
85 
86 }
87 
88 template<unsigned DIM>
90 {
91 
92 }
93 
94 template<unsigned DIM>
95 void RegularGridActorGenerator<DIM>::AddActor(vtkSmartPointer<vtkRenderer> pRenderer)
96 {
97  if(mpRegularGrid)
98  {
99  vtkSmartPointer<vtkImageData> p_grid = mpRegularGrid->GetVtkGrid();
100 
101  vtkSmartPointer<vtkGeometryFilter> p_geom_filter = vtkSmartPointer<vtkGeometryFilter>::New();
102  #if VTK_MAJOR_VERSION <= 5
103  p_geom_filter->SetInput(p_grid);
104  #else
105  p_geom_filter->SetInputData(p_grid);
106  #endif
107 
108  vtkSmartPointer<vtkColorTransferFunction> p_scaled_ctf = vtkSmartPointer<vtkColorTransferFunction>::New();
109  if(!this->mDataLabel.empty())
110  {
111  double range[2];
112  p_grid->GetPointData()->GetArray(this->mDataLabel.c_str())->GetRange(range);
113  for(unsigned idx=0; idx<256; idx++)
114  {
115  double color[3];
116  this->mpColorTransferFunction->GetColor(double(idx)/255.0, color);
117  p_scaled_ctf->AddRGBPoint(range[0] + double(idx)*(range[1]-range[0])/255.0, color[0], color[1], color[2]);
118  }
119  }
120 
121  // Add the points
122  if(this->mShowPoints)
123  {
124  vtkSmartPointer<vtkSphereSource> p_spheres = vtkSmartPointer<vtkSphereSource>::New();
125  p_spheres->SetRadius(this->mPointSize);
126  p_spheres->SetPhiResolution(16);
127  p_spheres->SetThetaResolution(16);
128 
129  vtkSmartPointer<vtkGlyph3D> p_glyph = vtkSmartPointer<vtkGlyph3D>::New();
130  #if VTK_MAJOR_VERSION <= 5
131  p_glyph->SetInputConnection(p_geom_filter->GetOutputPort());
132  p_glyph->SetSource(p_spheres->GetOutput());
133  #else
134  p_glyph->SetInputConnection(p_geom_filter->GetOutputPort());
135  p_glyph->SetSourceConnection(p_spheres->GetOutputPort());
136  #endif
137  p_glyph->ClampingOff();
138  p_glyph->SetScaleModeToScaleByScalar();
139  p_glyph->SetScaleFactor(1.0);
140  p_glyph->Update();
141 
142  vtkSmartPointer<vtkPolyDataMapper> p_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
143  #if VTK_MAJOR_VERSION <= 5
144  p_mapper->SetInput(p_glyph->GetOutput());
145  #else
146  p_mapper->SetInputData(p_glyph->GetOutput());
147  #endif
148  p_mapper->ScalarVisibilityOn();
149 
150  vtkSmartPointer<vtkActor> p_actor = vtkSmartPointer<vtkActor>::New();
151  p_actor->SetMapper(p_mapper);
152  p_actor->GetProperty()->SetColor(this->mPointColor[0]/255.0, this->mPointColor[1]/255.0, this->mPointColor[2]/255.0);
153  pRenderer->AddActor(p_actor);
154  }
155 
156  // Add the edges
157  if(this->mShowEdges)
158  {
159  vtkSmartPointer<vtkFeatureEdges> p_edges = vtkSmartPointer<vtkFeatureEdges>::New();
160  p_edges->SetInputConnection(p_geom_filter->GetOutputPort());
161  p_edges->SetFeatureEdges(false);
162  p_edges->SetBoundaryEdges(true);
163  p_edges->SetManifoldEdges(true);
164  p_edges->SetNonManifoldEdges(false);
165 
166  if(mUseTubesForEdges)
167  {
168  vtkSmartPointer<vtkTubeFilter> p_voronoi_tubes = vtkSmartPointer<vtkTubeFilter>::New();
169  p_voronoi_tubes->SetInputConnection(p_edges->GetOutputPort());
170  p_voronoi_tubes->SetRadius(this->mEdgeSize);
171  p_voronoi_tubes->SetNumberOfSides(12);
172 
173  vtkSmartPointer<vtkPolyDataMapper> p_voronoi_tube_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
174  p_voronoi_tube_mapper->SetInputConnection(p_voronoi_tubes->GetOutputPort());
175 
176  vtkSmartPointer<vtkActor> p_voronoi_tube_actor = vtkSmartPointer<vtkActor>::New();
177  p_voronoi_tube_actor->SetMapper(p_voronoi_tube_mapper);
178  p_voronoi_tube_actor->GetProperty()->SetColor(this->mVolumeColor[0]/255.0, this->mVolumeColor[1]/255.0, this->mVolumeColor[2]/255.0);
179  p_voronoi_tube_actor->GetProperty()->SetOpacity(this->mVolumeOpacity);
180  pRenderer->AddActor(p_voronoi_tube_actor);
181  }
182  else
183  {
184  vtkSmartPointer<vtkPolyDataMapper> p_edge_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
185  p_edge_mapper->SetInputConnection(p_edges->GetOutputPort());
186  p_edge_mapper->ScalarVisibilityOff();
187 
188  vtkSmartPointer<vtkActor> p_edge_actor = vtkSmartPointer<vtkActor>::New();
189  p_edge_actor->SetMapper(p_edge_mapper);
190  p_edge_actor->GetProperty()->SetColor(this->mEdgeColor[0]/255.0, this->mEdgeColor[1]/255.0, this->mEdgeColor[2]/255.0);
191  p_edge_actor->GetProperty()->SetOpacity(mEdgeOpacity);
192  pRenderer->AddActor(p_edge_actor);
193  }
194  }
195 
196  // Add the volume
197  if(this->mShowVolume)
198  {
199  vtkSmartPointer<vtkPolyDataMapper> p_grid_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
200  p_grid_mapper->SetInputConnection(p_geom_filter->GetOutputPort());
201  if(!this->mDataLabel.empty())
202  {
203  p_grid_mapper->SetLookupTable(p_scaled_ctf);
204  p_grid_mapper->ScalarVisibilityOn();
205  //p_grid_mapper->InterpolateScalarsBeforeMappingOn();
206  p_grid_mapper->SelectColorArray(this->mDataLabel.c_str());
207  p_grid_mapper->SetScalarModeToUsePointFieldData();
208  p_grid_mapper->SetColorModeToMapScalars();
209  }
210 
211  // Show edges
212  vtkSmartPointer<vtkActor> p_volume_actor = vtkSmartPointer<vtkActor>::New();
213  p_volume_actor->SetMapper(p_grid_mapper);
214  p_volume_actor->GetProperty()->SetEdgeVisibility(1);
215  p_volume_actor->GetProperty()->SetLineWidth(this->mEdgeSize);
216  if(this->mDataLabel.empty())
217  {
218  p_volume_actor->GetProperty()->SetColor(this->mEdgeColor[0]/255.0, this->mEdgeColor[1]/255.0, this->mEdgeColor[2]/255.0);
219  }
220  p_volume_actor->GetProperty()->SetOpacity(this->mVolumeOpacity);
221  pRenderer->AddActor(p_volume_actor);
222 
223  if(!this->mDataLabel.empty())
224  {
225  vtkSmartPointer<vtkScalarBarActor> p_scale_bar = vtkSmartPointer<vtkScalarBarActor>::New();
226  p_scale_bar->SetLookupTable(p_scaled_ctf);
227  p_scale_bar->SetTitle(this->mDataLabel.c_str());
228  p_scale_bar->SetOrientationToHorizontal();
229  p_scale_bar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
230  p_scale_bar->GetPositionCoordinate()->SetValue(0.25, 0.84);
231  p_scale_bar->SetWidth(0.5);
232  p_scale_bar->SetHeight(0.1);
233  p_scale_bar->GetTitleTextProperty()->ItalicOff();
234  p_scale_bar->GetLabelTextProperty()->ItalicOff();
235  p_scale_bar->GetTitleTextProperty()->BoldOff();
236  p_scale_bar->GetLabelTextProperty()->BoldOff();
237  p_scale_bar->SetLabelFormat("%.2g");
238  p_scale_bar->GetTitleTextProperty()->SetFontSize(5.0);
239  p_scale_bar->GetLabelTextProperty()->SetFontSize(5.0);
240  p_scale_bar->GetTitleTextProperty()->SetColor(0.0, 0.0, 0.0);
241  p_scale_bar->GetLabelTextProperty()->SetColor(0.0, 0.0, 0.0);
242  pRenderer->AddActor(p_scale_bar);
243  }
244  }
245  }
246 }
247 
248 template<unsigned DIM>
250 {
251  mpRegularGrid = pRegularGrid;
252 }
253 
254 template<unsigned DIM>
256 {
257  mEdgeOpacity = opacity;
258 }
259 
260 
261 template class RegularGridActorGenerator<2>;
262 template class RegularGridActorGenerator<3>;
c_vector< double, 3 > mEdgeColor
boost::shared_ptr< RegularGrid< DIM > > mpRegularGrid
void SetRegularGrid(boost::shared_ptr< RegularGrid< DIM > > pRegularGrid)
void AddActor(vtkSmartPointer< vtkRenderer > pRenderer)
c_vector< double, 3 > mPointColor
vtkSmartPointer< vtkColorTransferFunction > mpColorTransferFunction
c_vector< double, 3 > mVolumeColor