DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
vtkPolyDataViewer.h
Go to the documentation of this file.
1 
11 #ifndef __vtkPolyDataViewer_h
12 #define __vtkPolyDataViewer_h
13 
14 #include <vtkObject.h>
15 #include <vtkPolyData.h>
16 #include <vtkPointData.h>
17 #include <vtkLookupTable.h>
18 #include <vtkSmartPointer.h>
19 #include <vtkRenderer.h>
20 #include <vtkNew.h>
21 #include <vtkProperty.h>
22 
23 #include <vtkCamera.h>
24 #include <vtkPolyDataMapper.h>
25 #include <vtkPolyDataWriter.h>
26 #include <vtkWindowToImageFilter.h>
27 #include <vtkPNGWriter.h>
28 #include <vtkLODActor.h>
29 #include <vtkPolyDataNormals.h>
30 #include <vtkObjectFactory.h>
31 
32 
33 #include <vtkRenderWindow.h>
34 #include <vtkRenderWindowInteractor.h>
35 
36 #include "utlCoreMacro.h"
37 
38 namespace vtk
39 {
40 
42 inline vtkSmartPointer<vtkLODActor>
43 vtkPolyDataToActor(const vtkSmartPointer<vtkPolyData>& mesh, const double opacity=1.0, const std::vector<double>& hueRange={0.6667,0.0}, bool useNormal=true, const std::vector<double>& scalarRange={-1.0,-1.0}, bool lighting=true )
44  {
45  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
46 
47  if (useNormal)
48  {
49  vtkSmartPointer<vtkPolyDataNormals> polyDataNormals = vtkPolyDataNormals::New();
50  polyDataNormals->SetInputData(mesh);
51  mapper->SetInputConnection(polyDataNormals->GetOutputPort());
52  }
53  else
54  mapper->SetInputData(mesh);
55 
56  // lut when scalar is used for colors
57  if (mesh->GetPointData()->GetScalars() && mesh->GetPointData()->GetScalars()->GetNumberOfComponents()==1)
58  {
59  vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
60  double* valueRange = mesh->GetScalarRange();
61  double rangeUsed[2];
62  rangeUsed[0] = std::fabs(scalarRange[0]+1.0) < 1.0e-8 ? valueRange[0] : scalarRange[0];
63  rangeUsed[1] = std::fabs(scalarRange[1]+1.0) < 1.0e-8 ? valueRange[1] : scalarRange[1];
64  // utlPrintVar(true, rangeUsed[0], rangeUsed[1]);
65  lut->SetTableRange(rangeUsed[0], rangeUsed[1]);
66  lut->SetHueRange(hueRange[0],hueRange[1]);
67  lut->SetRampToLinear();
68  lut->Build();
69 
70  mapper->SetLookupTable(lut);
71  mapper->SetScalarRange(rangeUsed[0], rangeUsed[1]);
72  }
73 
74  vtkSmartPointer<vtkLODActor> actor = vtkSmartPointer<vtkLODActor>::New();
75  actor->SetMapper(mapper);
76 
77  actor->GetProperty()->SetLighting(lighting);
78  actor->GetProperty()->SetOpacity(opacity);
79 
80  return actor;
81  }
82 
89 class vtkPolyDataViewer : public vtkObject
90  {
91 public:
92  static vtkPolyDataViewer *New();
93  vtkTypeMacro(vtkPolyDataViewer, vtkObject);
94 
96 
98 
99  vtkSetMacro (UseNormal, bool);
100  vtkGetMacro (UseNormal, bool);
101 
102  vtkSetMacro (Zoom, double);
103  vtkGetMacro (Zoom, double);
104 
105  vtkSetMacro (Lighting, bool);
106  vtkGetMacro (Lighting, bool);
107 
108  vtkSetVector2Macro(Angle, double);
109  vtkGetVector2Macro(Angle, double);
110 
113 
115  void Add(const vtkSmartPointer<vtkPolyData>& mesh, const double opacity=1.0);
116 
118  void Add(const vtkSmartPointer<vtkPolyData>& mesh, const double opacity, const std::vector<double>& hueRange, bool useNormal, const std::vector<double>& scalarRange={-1.0,-1.0}, bool lighting=true );
119 
120  void Add(const vtkSmartPointer<vtkLODActor>& actor)
121  {
122  Renderer->AddActor(actor);
123  }
124 
125  void SetBackground(double r, double g, double b)
126  {
127  Renderer->SetBackground(r,g,b);
128  }
129 
130  void Show();
131 
132  void SavePNG(const std::string& pngfile);
133 
134 protected:
135 
138 
139 
140  void SetCamara();
141 
142  void RenderWindowUpdate();
143 
144 private:
145 
146  vtkSmartPointer<vtkRenderer> Renderer = vtkSmartPointer<vtkRenderer>::New();
147 
148  vtkSmartPointer<vtkRenderWindow> RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
149  vtkSmartPointer<vtkRenderWindowInteractor> Interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
150 
152  double ScalarRange[2]={-1.0,-1.0};
153 
154  bool UseNormal=true;
155 
156  bool Lighting=true;
157 
158  int WindowSize[2]={600,600};
159 
160  double Angle[2]={0.0,0.0};
161 
162  double HueRange[2]={0.6667,0.0};
163 
164  double Zoom=1.0;
165 
166  };
167 
169 
171 inline void
172 VisualizePolyDataWithScalarRange (const std::vector<vtkPolyData*>& meshes, const std::vector<double>& opacity, const std::vector<double>& scalarRange={-1.0,-1.0}, const std::vector<double>& hueRange={0.6667,0.0}, const std::vector<double>& angle={0.0,0.0}, const std::vector<int>& windowSize={600,600}, const bool useNormal=true, const bool lighting=true, const double zoom=1.0, const std::string& pngfile="", const std::vector<double>& bgColor={0,0,0} )
173 {
174  vtkSmartPointer<vtkPolyDataViewer> viewer = vtkPolyDataViewer::New();
175 
176  viewer->SetScalarRange(scalarRange[0], scalarRange[1]);
177  viewer->SetHueRange(hueRange[0], hueRange[1]);
178  viewer->SetAngle(angle[0], angle[1]);
179  viewer->SetWindowSize(windowSize[0], windowSize[1]);
180  viewer->SetUseNormal(useNormal);
181  viewer->SetLighting(lighting);
182  viewer->SetZoom(zoom);
183  viewer->SetBackground(bgColor[0], bgColor[1], bgColor[2]);
184 
185  utlException(meshes.size()!=opacity.size(), "meshes and opacity should have the same size");
186 
187  for ( int i = 0; i < meshes.size(); ++i )
188  {
189  if (meshes[i])
190  viewer->Add(meshes[i], opacity[i]);
191  }
192 
193  if (pngfile=="")
194  viewer->Show();
195  else
196  viewer->SavePNG(pngfile);
197 }
198 
200 inline void
201 VisualizePolyDataWithScalarRange ( vtkPolyData* mesh, const std::vector<double>& scalarRange={-1.0,-1.0}, const std::vector<double>& hueRange={0.6667,0.0}, const std::vector<double>& angle={0.0,0.0}, const std::vector<int>& windowSize={600,600}, const bool useNormal=true, const bool lighting=true, const double zoom=1.0, const std::string& pngfile="", const std::vector<double>& bgColor={0,0,0} )
202 {
203  vtkSmartPointer<vtkPolyDataViewer> viewer = vtkPolyDataViewer::New();
204 
205  viewer->SetScalarRange(scalarRange[0], scalarRange[1]);
206  viewer->SetHueRange(hueRange[0], hueRange[1]);
207  viewer->SetAngle(angle[0], angle[1]);
208  viewer->SetWindowSize(windowSize[0], windowSize[1]);
209  viewer->SetUseNormal(useNormal);
210  viewer->SetLighting(lighting);
211  viewer->SetZoom(zoom);
212  viewer->SetBackground(bgColor[0], bgColor[1], bgColor[2]);
213 
214  viewer->Add(mesh);
215 
216  if (pngfile=="")
217  viewer->Show();
218  else
219  viewer->SavePNG(pngfile);
220 }
221 
223 inline void
224 VisualizePolyData ( vtkPolyData* mesh, const std::vector<double>& angle={0.0,0.0}, const std::vector<int>& windowSize={600,600}, const bool useNormal=true, const bool lighting=true, const double zoom=1.0, const std::string& pngfile="", const std::vector<double>& bgColor={0,0,0} )
225 {
226  VisualizePolyDataWithScalarRange(mesh, {-1.0,-1.0}, {0.6667,0.0}, angle, windowSize, useNormal, lighting, zoom, pngfile, bgColor);
227 }
228 
229 }
230 
231 
232 #if !defined(__vtkPolyDataViewer_hxx)
233 #include "vtkPolyDataViewer.hxx"
234 #endif
235 
236 #endif
vtkSetMacro(UseNormal, bool)
Created "06-08-2017.
vtkSmartPointer< vtkRenderWindowInteractor > Interactor
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
vtkStandardNewMacro(vtkPolyDataViewer)
vtkTypeMacro(vtkPolyDataViewer, vtkObject)
void Add(const vtkSmartPointer< vtkLODActor > &actor)
static vtkPolyDataViewer * New()
void SavePNG(const std::string &pngfile)
helper class to visualize vtkPolyData.
void VisualizePolyData(vtkPolyData *mesh, const std::vector< double > &angle={0.0, 0.0}, const std::vector< int > &windowSize={600, 600}, const bool useNormal=true, const bool lighting=true, const double zoom=1.0, const std::string &pngfile="", const std::vector< double > &bgColor={0, 0, 0})
vtkGetMacro(UseNormal, bool)
void VisualizePolyDataWithScalarRange(const std::vector< vtkPolyData * > &meshes, const std::vector< double > &opacity, const std::vector< double > &scalarRange={-1.0,-1.0}, const std::vector< double > &hueRange={0.6667, 0.0}, const std::vector< double > &angle={0.0, 0.0}, const std::vector< int > &windowSize={600, 600}, const bool useNormal=true, const bool lighting=true, const double zoom=1.0, const std::string &pngfile="", const std::vector< double > &bgColor={0, 0, 0})
void Add(const vtkSmartPointer< vtkPolyData > &mesh, const double opacity=1.0)
vtkSmartPointer< vtkLODActor > vtkPolyDataToActor(const vtkSmartPointer< vtkPolyData > &mesh, const double opacity=1.0, const std::vector< double > &hueRange={0.6667, 0.0}, bool useNormal=true, const std::vector< double > &scalarRange={-1.0,-1.0}, bool lighting=true)
vtkGetVector2Macro(Angle, double)
macros for utlCore
void SetBackground(double r, double g, double b)
vtkSmartPointer< vtkRenderer > Renderer
vtkSmartPointer< vtkRenderWindow > RenderWindow
vtkSetVector2Macro(ScalarRange, double)