Chaste  Build::
MicrovesselVtkScene.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 <vtkWindowToImageFilter.h>
40 #include <vtkPNGWriter.h>
41 #include <vtkPoints.h>
42 #include <vtkPolyData.h>
43 #include <vtkPolyDataMapper.h>
44 #include <vtkActor.h>
45 #include <vtkProperty.h>
46 #include <vtkUnsignedCharArray.h>
47 #if VTK_MAJOR_VERSION > 5
48  #include <vtkNamedColors.h>
49 #endif
50 #include <vtkSphereSource.h>
51 #include <vtkGlyph3D.h>
52 #include <vtkGlyph2D.h>
53 #include <vtkCubeAxesActor2D.h>
54 #include <vtkImageData.h>
55 #include <vtkInteractorStyleTrackballCamera.h>
56 #include <vtkObjectFactory.h>
57 #include <vtkActorCollection.h>
58 #include <vtkUnstructuredGrid.h>
59 #include <vtkGeometryFilter.h>
60 #include <vtkTubeFilter.h>
61 #include <vtkExtractEdges.h>
62 #include <vtkCamera.h>
63 #include <vtkVertexGlyphFilter.h>
64 #include <vtkUnstructuredGrid.h>
65 #include <vtkCell.h>
66 #include <vtkPolygon.h>
67 #include <vtkConvexPointSet.h>
68 #include <vtkIdList.h>
69 #include <vtkGeometryFilter.h>
70 #include <vtkTetra.h>
71 #include <vtkTriangle.h>
72 #include <vtkLine.h>
73 #include <vtkFeatureEdges.h>
74 #include <vtkTextProperty.h>
75 #include <vtkCubeAxesActor.h>
76 #include "UblasIncludes.hpp"
77 #include "UblasVectorInclude.hpp"
78 #include "Exception.hpp"
79 #include "MicrovesselVtkScene.hpp"
80 #include "BaseUnits.hpp"
81 #include "VesselNetworkWriter.hpp"
82 
83 // For some reason an explicit interactor style is needed capture mouse events
85 {
86  public:
87  static customMouseInteractorStyle* New();
89 
90  virtual void OnLeftButtonDown()
91  {
92  // Forward events
93  vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
94  }
95 
96  virtual void OnMiddleButtonDown()
97  {
98  // Forward events
99  vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
100  }
101 
102  virtual void OnRightButtonDown()
103  {
104  // Forward events
105  vtkInteractorStyleTrackballCamera::OnRightButtonDown();
106  }
107 
108 };
109 
110 vtkStandardNewMacro(customMouseInteractorStyle);
111 
112 template<unsigned DIM>
114  : mpRenderer(vtkSmartPointer<vtkRenderer>::New()),
115  mpRenderWindow(vtkSmartPointer<vtkRenderWindow>::New()),
116  mpRenderWindowInteractor(vtkSmartPointer<vtkRenderWindowInteractor>::New()),
117  mOutputFilePath(),
118  mpColorLookUpTable(vtkSmartPointer<vtkLookupTable>::New()),
119  #if VTK_MAJOR_VERSION > 5
120  mAnimationWriter(vtkSmartPointer<vtkOggTheoraWriter>::New()),
121  #endif
122  mWindowToImageFilter(vtkSmartPointer<vtkWindowToImageFilter>::New()),
123  mIsInteractive(true),
124  mSaveAsAnimation(false),
125  mSaveAsImages(false),
126  mHasStarted(false),
127  mAddAnnotations(false),
128  mOutputFrequency(1),
129  mIncludeAxes(false),
130  mpCellPopulationGenerator(boost::shared_ptr<CellPopulationActorGenerator<DIM> >(new CellPopulationActorGenerator<DIM>())),
131  mpPartGenerator(boost::shared_ptr<PartActorGenerator<DIM> >(new PartActorGenerator<DIM>())),
132  mpNetworkGenerator(boost::shared_ptr<VesselNetworkActorGenerator<DIM> >(new VesselNetworkActorGenerator<DIM>())),
133  mpDiscreteContinuumMeshGenerator(boost::shared_ptr<DiscreteContinuumMeshActorGenerator<DIM> >(new DiscreteContinuumMeshActorGenerator<DIM>())),
134  mpGridGenerator(boost::shared_ptr<RegularGridActorGenerator<DIM> >(new RegularGridActorGenerator<DIM>())),
135  mLengthScale(BaseUnits::Instance()->GetReferenceLengthScale())
136 {
137  mpRenderer->SetBackground(1.0, 1.0, 1.0);
138  mpRenderWindow->AddRenderer(mpRenderer);
139  mpRenderWindow->SetSize(800.0, 600.0);
140  mpRenderWindowInteractor->SetRenderWindow(mpRenderWindow);
141 
142  vtkSmartPointer<customMouseInteractorStyle> style = vtkSmartPointer<customMouseInteractorStyle>::New();
143  mpRenderWindowInteractor->SetInteractorStyle(style);
144 }
145 
146 template<unsigned DIM>
148 {
149 
150 }
151 
152 template<unsigned DIM>
153 boost::shared_ptr<PartActorGenerator<DIM> > MicrovesselVtkScene<DIM>::GetPartActorGenerator()
154 {
155  return mpPartGenerator;
156 }
157 
158 template<unsigned DIM>
159 boost::shared_ptr<DiscreteContinuumMeshActorGenerator<DIM> > MicrovesselVtkScene<DIM>::GetDiscreteContinuumMeshActorGenerator()
160 {
162 }
163 
164 template<unsigned DIM>
165 boost::shared_ptr<RegularGridActorGenerator<DIM> > MicrovesselVtkScene<DIM>::GetRegularGridActorGenerator()
166 {
167  return mpGridGenerator;
168 }
169 
170 template<unsigned DIM>
171 boost::shared_ptr<VesselNetworkActorGenerator<DIM> > MicrovesselVtkScene<DIM>::GetVesselNetworkActorGenerator()
172 {
173  return mpNetworkGenerator;
174 }
175 
176 template<unsigned DIM>
177 boost::shared_ptr<CellPopulationActorGenerator<DIM> > MicrovesselVtkScene<DIM>::GetCellPopulationActorGenerator()
178 {
180 }
181 
182 template<unsigned DIM>
184 {
185  if(!mHasStarted)
186  {
187  Start();
188  }
189 
190  vtkSmartPointer<vtkActorCollection> p_actors = mpRenderer->GetActors();
191  vtkActor *p_actor;
192  for( p_actors->InitTraversal(); (p_actor = p_actors->GetNextItem())!=NULL; )
193  {
194  mpRenderer->RemoveActor(p_actor);
195  }
196  mpRenderer->Clear();
197 
199  {
201  }
202  if(mpPartGenerator)
203  {
204  mpPartGenerator->AddActor(mpRenderer);
205  }
207  {
208  mpNetworkGenerator->AddActor(mpRenderer);
209  }
211  {
213  }
214  if(mpGridGenerator)
215  {
216  mpGridGenerator->AddActor(mpRenderer);
217  }
218  mpRenderer->ResetCamera();
219 
220  if(mIncludeAxes)
221  {
222  vtkSmartPointer<vtkCubeAxesActor> p_cubeAxesActor = vtkSmartPointer<vtkCubeAxesActor>::New();
223  p_cubeAxesActor->SetBounds(mpRenderer->ComputeVisiblePropBounds());
224  p_cubeAxesActor->SetCamera(mpRenderer->GetActiveCamera());
225  p_cubeAxesActor->GetTitleTextProperty(0)->SetColor(0.0, 0.0, 0.0);
226  p_cubeAxesActor->GetLabelTextProperty(0)->SetColor(0.0, 0.0, 0.0);
227  p_cubeAxesActor->GetTitleTextProperty(1)->SetColor(0.0, 0.0, 0.0);
228  p_cubeAxesActor->GetLabelTextProperty(1)->SetColor(0.0, 0.0, 0.0);
229  p_cubeAxesActor->GetTitleTextProperty(2)->SetColor(0.0, 0.0, 0.0);
230  p_cubeAxesActor->GetLabelTextProperty(2)->SetColor(0.0, 0.0, 0.0);
231  p_cubeAxesActor->GetXAxesLinesProperty()->SetColor(0.0, 0.0, 0.0);
232  p_cubeAxesActor->GetYAxesLinesProperty()->SetColor(0.0, 0.0, 0.0);
233  p_cubeAxesActor->GetZAxesLinesProperty()->SetColor(0.0, 0.0, 0.0);
234  p_cubeAxesActor->DrawXGridlinesOff();
235  p_cubeAxesActor->DrawYGridlinesOff();
236  p_cubeAxesActor->DrawZGridlinesOff();
237  p_cubeAxesActor->XAxisMinorTickVisibilityOff();
238  p_cubeAxesActor->YAxisMinorTickVisibilityOff();
239  p_cubeAxesActor->ZAxisMinorTickVisibilityOff();
240  mpRenderer->AddActor(p_cubeAxesActor);
241  }
242 
243  if(mSaveAsImages)
244  {
245  mpRenderWindow->SetOffScreenRendering(1);
246  mpRenderWindow->Render();
247  mWindowToImageFilter->Modified();
248  vtkSmartPointer<vtkPNGWriter> p_writer = vtkSmartPointer<vtkPNGWriter>::New();
249  p_writer->SetWriteToMemory(1);
250  p_writer->SetInputConnection(mWindowToImageFilter->GetOutputPort());
251  if(!mOutputFilePath.empty())
252  {
253  p_writer->SetWriteToMemory(0);
254  p_writer->SetFileName((mOutputFilePath+"_"+boost::lexical_cast<std::string>(time_step)+".png").c_str());
255  p_writer->Write();
256  }
257  }
258  #if VTK_MAJOR_VERSION > 5
259  if(mSaveAsAnimation)
260  {
261  if(!mSaveAsImages)
262  {
263  mpRenderWindow->SetOffScreenRendering(1);
264  mpRenderWindow->Render();
265  mWindowToImageFilter->Modified();
266  }
267  mAnimationWriter->Write();
268  }
269  #endif
270  if(mIsInteractive)
271  {
272  mpRenderWindow->SetOffScreenRendering(0);
273  mpRenderWindow->Render();
274  }
275 }
276 
277 template<unsigned DIM>
278 void MicrovesselVtkScene<DIM>::SetOutputFilePath(const std::string& rPath)
279 {
280  mOutputFilePath = rPath;
281 }
282 
283 template<unsigned DIM>
284 void MicrovesselVtkScene<DIM>::SetSaveAsAnimation(bool saveAsAnimation)
285 {
286  mSaveAsAnimation = saveAsAnimation;
287 }
288 
289 template<unsigned DIM>
290 void MicrovesselVtkScene<DIM>::SetSaveAsImages(bool saveAsImages)
291 {
292  mSaveAsImages = saveAsImages;
293 }
294 
295 template<unsigned DIM>
296 void MicrovesselVtkScene<DIM>::SetIsInteractive(bool isInteractive)
297 {
298  mIsInteractive = isInteractive;
299 }
300 
301 template<unsigned DIM>
302 void MicrovesselVtkScene<DIM>::SetPart(boost::shared_ptr<Part<DIM> > pPart)
303 {
304  mpPartGenerator->SetPart(pPart);
305 }
306 
307 template<unsigned DIM>
308 void MicrovesselVtkScene<DIM>::SetVesselNetwork(boost::shared_ptr<VesselNetwork<DIM> > pNetwork)
309 {
310  mpNetworkGenerator->SetVesselNetwork(pNetwork);
311 }
312 
313 template<unsigned DIM>
314 void MicrovesselVtkScene<DIM>::SetCellPopulation(boost::shared_ptr<AbstractCellPopulation<DIM> > pCellPopulation)
315 {
316  mpCellPopulationGenerator->SetCellPopulation(pCellPopulation);
317 }
318 
319 template<unsigned DIM>
320 void MicrovesselVtkScene<DIM>::SetRegularGrid(boost::shared_ptr<RegularGrid<DIM> > pGrid)
321 {
322  mpGridGenerator->SetRegularGrid(pGrid);
323 }
324 
325 template<unsigned DIM>
327 {
328  mpDiscreteContinuumMeshGenerator->SetDiscreteContinuumMesh(pMesh);
329 }
330 
331 template<unsigned DIM>
333 {
334  #if VTK_MAJOR_VERSION > 5
336  {
337  mAnimationWriter->End();
338  }
339  #endif
340 }
341 
342 template<unsigned DIM>
344 {
345  mpRenderer->ResetCamera();
346  if(DIM==3)
347  {
348  mpRenderer->GetActiveCamera()->Azimuth(45.0);
349  }
350 
352  {
353  mpRenderWindow->SetOffScreenRendering(1);
355  mWindowToImageFilter->Update();
356  }
357 
358  if(mSaveAsAnimation)
359  {
360  #if VTK_MAJOR_VERSION > 5
361  mAnimationWriter->SetInputConnection(mWindowToImageFilter->GetOutputPort());
362  mAnimationWriter->SetFileName((mOutputFilePath+".ogg").c_str());
363  mAnimationWriter->SetRate(1.0);
364  mAnimationWriter->Start();
365  #endif
366  }
367 
368  mHasStarted = true;
369  ResetRenderer();
370 
371  if(mIsInteractive)
372  {
373  mpRenderWindowInteractor->Initialize();
374  }
375 
376 }
377 
378 template<unsigned DIM>
380 {
381  mpRenderWindowInteractor->Start();
382 }
383 
384 template class MicrovesselVtkScene<2>;
385 template class MicrovesselVtkScene<3>;
vtkSmartPointer< vtkWindowToImageFilter > mWindowToImageFilter
vtkSmartPointer< vtkRenderWindowInteractor > mpRenderWindowInteractor
void SetOutputFilePath(const std::string &rPath)
boost::shared_ptr< VesselNetworkActorGenerator< DIM > > mpNetworkGenerator
vtkSmartPointer< vtkRenderer > mpRenderer
boost::shared_ptr< DiscreteContinuumMeshActorGenerator< DIM > > mpDiscreteContinuumMeshGenerator
void SetCellPopulation(boost::shared_ptr< AbstractCellPopulation< DIM > > pCellPopulation)
vtkSmartPointer< vtkRenderWindow > mpRenderWindow
boost::shared_ptr< CellPopulationActorGenerator< DIM > > mpCellPopulationGenerator
boost::shared_ptr< PartActorGenerator< DIM > > mpPartGenerator
void ResetRenderer(unsigned timeStep=0)
Definition: Part.hpp:60
boost::shared_ptr< RegularGridActorGenerator< DIM > > mpGridGenerator