VTKFIG  0.20.20181114
Easy VTK based in situ visualization
vtkfigScalarView-2018-01-21-2258.cxx
Go to the documentation of this file.
1 #include <vtkSliderRepresentation2D.h>
2 #include <vtkProperty2D.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkTextProperty.h>
5 #include <vtkRectilinearGrid.h>
6 #include <vtkGeometryFilter.h>
7 #include <vtkRectilinearGridGeometryFilter.h>
8 #include <vtkUnstructuredGrid.h>
9 #include <vtkUnstructuredGridGeometryFilter.h>
10 #include <vtkPlane.h>
11 #include <vtkCutter.h>
12 #include <vtkImplicitBoolean.h>
13 #include <vtkOutlineFilter.h>
14 #include <vtkCubeAxesActor2D.h>
15 #include <vtkAppendPolyData.h>
16 #include <vtkAssignAttribute.h>
17 #include <vtkCamera.h>
18 #include <vtkTextActor.h>
19 #include <vtkCoordinate.h>
20 #include <vtkTransformPolyDataFilter.h>
21 #include <vtkTransformFilter.h>
22 #include <vtkClipPolyData.h>
23 #include <vtkWarpScalar.h>
24 #include <vtkExtractCells.h>
25 #include <vtkIdList.h>
26 
27 
28 #include "vtkfigScalarView.h"
29 #include "config.h"
30 
31 
32 namespace vtkfig
33 {
34 
35 
36 
40  {
41  }
42 
43 
44  void ScalarView::SetSurfaceRGBTable(RGBTable & tab, int tabsize)
45  {
46  state.surface_rgbtab_size=tabsize;
47  state.surface_rgbtab_modified=true;
48  surface_rgbtab=tab;
49  surface_lut=BuildLookupTable(tab,tabsize);
50  }
51 
54 
55  template <class DATA, class FILTER>
57  {
58 
60 
62  //renderer->GetActiveCamera()->SetParallelProjection(1);
63 
64  auto values=vtkDoubleArray::SafeDownCast(DATA::SafeDownCast(data_producer->GetOutputDataObject(0))->GetPointData()->GetAbstractArray(dataname.c_str()));
65 
66  vtkSmartPointer<vtkExtractCells> subgrid;
67  if (celllist)
68  {
69  subgrid=vtkSmartPointer<vtkExtractCells>::New();
70  subgrid->SetInputConnection(data_producer->GetOutputPort());
71  subgrid->SetCellList(celllist);
72  }
73 
74 
75  auto scalar = vtkSmartPointer<vtkAssignAttribute>::New();
76  scalar->Assign(dataname.c_str(),vtkDataSetAttributes::SCALARS,vtkAssignAttribute::POINT_DATA);
77  if (celllist)
78  scalar->SetInputConnection(subgrid->GetOutputPort());
79  else
80  scalar->SetInputConnection(data_producer->GetOutputPort());
81 
82  auto geometry=vtkSmartPointer<FILTER>::New();
83  geometry->SetInputConnection(scalar->GetOutputPort());
84 
85 
86 
87  auto transgeometry=vtkSmartPointer<vtkTransformPolyDataFilter>::New();
88  transgeometry->SetInputConnection(geometry->GetOutputPort());
89  transgeometry->SetTransform(transform);
90 
94  vtkSmartPointer<vtkTransformPolyDataFilter> transallgeometry;
95  if (celllist)
96  {
97  auto allgeometry=vtkSmartPointer<FILTER>::New();
98  allgeometry->SetInputConnection(data_producer->GetOutputPort());
99 
100  transallgeometry=vtkSmartPointer<vtkTransformPolyDataFilter>::New();
101  transallgeometry->SetTransform(transform);
102  transallgeometry->SetInputConnection(allgeometry->GetOutputPort());
103  }
104  else
105  transallgeometry=transgeometry;
106 
107  if (true) // Elevation
108  {
109  auto wtransform = vtkSmartPointer<vtkTransform>::New();
110  wtransform->Translate(0,0,0.5);
111  auto wtransgeometry=vtkSmartPointer<vtkTransformPolyDataFilter>::New();
112  wtransgeometry->SetInputConnection(transgeometry->GetOutputPort());
113  wtransgeometry->SetTransform(wtransform);
114 
115  auto elevation = vtkSmartPointer<vtkWarpScalar>::New();
116  elevation->SetInputConnection(wtransgeometry->GetOutputPort());
117  elevation->SetScaleFactor(0.5/(state.real_vmax-state.real_vmin));
118  vtkSmartPointer<vtkPolyDataMapper> wmapper = vtkSmartPointer<vtkPolyDataMapper>::New();
119  wmapper->SetInputConnection(elevation->GetOutputPort());
120  vtkSmartPointer<vtkActor> wplot = vtkSmartPointer<vtkActor>::New();
121  wmapper->UseLookupTableScalarRangeOn();
122  wmapper->SetLookupTable(elevation_lut);
123  elevation_plot->SetMapper(wmapper);
124  elevation_plot->SetVisibility(state.show_elevation);
126  }
127 
128 
129  if (true)
130  {
131  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
132  mapper->SetInputConnection(transgeometry->GetOutputPort());
133 
134 // mapper->InterpolateScalarsBeforeMappingOn();
135  mapper->UseLookupTableScalarRangeOn();
136  mapper->SetLookupTable(surface_lut);
137 #ifdef VTK_HAS_MAPPER_IMMEDIATE_RENDERING_ON
138  mapper->ImmediateModeRenderingOn();
139 #endif
140  surface_plot->SetMapper(mapper);
141  surface_plot->SetVisibility(state.show_surface);
143 
144  if (state.show_surface_colorbar)
145  {
146  auto cbar=BuildColorBar(mapper);
147  cbar->SetLabelFormat(state.surface_colorbar_label_format);
148  cbar->SetNumberOfLabels(state.surface_colorbar_num_labels);
150  }
151  }
152 
153 
154  if (true)
155  {
156 
157  isoline_filter->SetInputConnection(transgeometry->GetOutputPort());
158 
159  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
160  mapper->SetInputConnection(isoline_filter->GetOutputPort());
161  //mapper->UseLookupTableScalarRangeOn();
162  //mapper->SetLookupTable(contour_lut);
163  mapper->ScalarVisibilityOff();
164 
165  isoline_plot->SetMapper(mapper);
166  isoline_plot->GetProperty()->SetColor(0,0,0);
167  isoline_plot->GetProperty()->SetLineWidth(state.isoline_width);
168  isoline_plot->SetVisibility(state.show_isolines);
170 
171  // if (state.show_contour_colorbar)
172  // Figure::RTAddActor2D(BuildColorBar(mapper));
173 
174  // if (state.show_slider)
175  // AddSlider(interactor,renderer);
176 
177  }
178 
179 
180  }
181 
182 
185 
186  template <class DATA,class FILTER>
188  {
189  RTCalcTransform();
190 
191 
192 
193  vtkSmartPointer<vtkExtractCells> subgrid;
194  if (celllist)
195  {
196  subgrid=vtkSmartPointer<vtkExtractCells>::New();
197  subgrid->SetInputConnection(data_producer->GetOutputPort());
198  subgrid->SetCellList(celllist);
199  }
200 
201 
202 
203  auto scalar = vtkSmartPointer<vtkAssignAttribute>::New();
204  scalar->Assign(dataname.c_str(),vtkDataSetAttributes::SCALARS,vtkAssignAttribute::POINT_DATA);
205  if (celllist)
206  scalar->SetInputConnection(subgrid->GetOutputPort());
207  else
208  scalar->SetInputConnection(data_producer->GetOutputPort());
209 
210 
211 
212 
213  auto transgeometry=vtkSmartPointer<vtkTransformFilter>::New();
214  transgeometry->SetInputConnection(scalar->GetOutputPort());
215  transgeometry->SetTransform(transform);
216 
217  vtkSmartPointer<vtkTransformFilter> transallgeometry;
218  if (celllist)
219  {
220  auto allgeometry=vtkSmartPointer<FILTER>::New();
221  allgeometry->SetInputConnection(data_producer->GetOutputPort());
222 
223  transallgeometry=vtkSmartPointer<vtkTransformFilter>::New();
224  transallgeometry->SetTransform(transform);
225  transallgeometry->SetInputConnection(allgeometry->GetOutputPort());
226  }
227  else
228  transallgeometry=transgeometry;
229 
230 
231 
232 
233  planeX->SetOrigin(trans_center);
234  planeY->SetOrigin(trans_center);
235  planeZ->SetOrigin(trans_center);
236 
237  planecutX->SetInputConnection(transgeometry->GetOutputPort());
238  planecutX->SetCutFunction(planeX);
239  planecutX->SetNumberOfContours(0);
240 
241 
242  planecutY->SetInputConnection(transgeometry->GetOutputPort());
243  planecutY->SetCutFunction(planeY);
244  planecutY->SetNumberOfContours(0);
245 
246  planecutZ->SetInputConnection(transgeometry->GetOutputPort());
247  planecutZ->SetCutFunction(planeZ);
248  planecutZ->SetNumberOfContours(1);
249  planecutZ->SetValue(0,0.0);
250 
251 
252  vtkSmartPointer<vtkClipPolyData> clipgeometry=0;
253  if (false) // plot complete outline surface
254  {
255 
256  clipgeometry=vtkSmartPointer<vtkClipPolyData>::New();
257  auto sfilter=vtkSmartPointer<FILTER>::New();
258  sfilter->SetInputConnection(transgeometry->GetOutputPort());
259  clipgeometry->SetInputConnection(sfilter->GetOutputPort());
260  clipgeometry->SetClipFunction(planeZ);
261  clipgeometry->SetInsideOut(1);
262 
263  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
264 
265 
266  mapper->SetInputConnection(clipgeometry->GetOutputPort());
267  mapper->UseLookupTableScalarRangeOn();
268  mapper->SetLookupTable(surface_lut);
269  auto splot=vtkSmartPointer<vtkActor>::New();
270  splot->GetProperty()->SetOpacity(1.0);
271  splot->SetMapper(mapper);
273 
274  }
275 
276 
277 
278  auto xyz = vtkSmartPointer<vtkAppendPolyData>::New();
279  xyz->SetUserManagedInputs(1);
280  if (clipgeometry)
281  xyz->SetNumberOfInputs(4);
282  else
283  xyz->SetNumberOfInputs(3);
284 
285  xyz->SetInputConnectionByNumber(0,planecutX->GetOutputPort());
286  xyz->SetInputConnectionByNumber(1,planecutY->GetOutputPort());
287  xyz->SetInputConnectionByNumber(2,planecutZ->GetOutputPort());
288  if (clipgeometry)
289  xyz->SetInputConnectionByNumber(3,clipgeometry->GetOutputPort());
290 
291 
292  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
293  mapper->SetInputConnection(xyz->GetOutputPort());
294  mapper->UseLookupTableScalarRangeOn();
295  mapper->SetLookupTable(surface_lut);
296 
297  vtkSmartPointer<vtkActor> plot = vtkSmartPointer<vtkActor>::New();
298  plot->GetProperty()->SetOpacity(1);
299  plot->SetMapper(mapper);
300  Figure::RTAddActor(plot);
301 
303 
304 
305 
306  if (true)
307  {
308 
309  isoline_filter->SetInputConnection(xyz->GetOutputPort());
310 
311  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
312  mapper->SetInputConnection(isoline_filter->GetOutputPort());
313  // mapper->UseLookupTableScalarRangeOn();
314  // mapper->SetLookupTable(contour_lut);
315  mapper->ScalarVisibilityOff();
316 
317  isoline_plot->GetProperty()->SetOpacity(1.0);
318  isoline_plot->GetProperty()->SetColor(0,0,0);
319  isoline_plot->GetProperty()->SetLineWidth(state.isoline_width);
320  isoline_plot->SetMapper(mapper);
321  isoline_plot->SetVisibility(state.show_isolines);
323  }
324 
325 
326  if (true)
327  {
328 
329  isosurface_filter->SetInputConnection(transgeometry->GetOutputPort());
330 
331  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
332  mapper->SetInputConnection(isosurface_filter->GetOutputPort());
333  mapper->UseLookupTableScalarRangeOn();
334  mapper->SetLookupTable(surface_lut);
335 
336  isosurface_plot->GetProperty()->SetOpacity(0.3);
337 
338  isosurface_plot->SetMapper(mapper);
339  isosurface_plot->SetVisibility(state.show_isosurfaces);
341 
342  }
343 
344 
345  }
346 
347 
351  {
352 
354  {
355  if (state.spacedim==2)
356  this->RTBuildVTKPipeline2D<vtkUnstructuredGrid,vtkGeometryFilter>();
357  else
358  this->RTBuildVTKPipeline3D<vtkUnstructuredGrid,vtkGeometryFilter>();
359  }
360  else if (state.datatype==DataSet::DataType::RectilinearGrid)
361  {
362  if (state.spacedim==2)
363  this->RTBuildVTKPipeline2D<vtkRectilinearGrid,vtkRectilinearGridGeometryFilter>();
364  else
365  this->RTBuildVTKPipeline3D<vtkRectilinearGrid,vtkRectilinearGridGeometryFilter>();
366  }
367  }
368 
369 
370 
371 
374 
375  void ScalarView::ServerRTSend(vtkSmartPointer<internals::Communicator> communicator)
376  {
377  if (state.surface_rgbtab_modified)
378  {
379  SendRGBTable(communicator, surface_rgbtab);
380  state.surface_rgbtab_modified=false;
381  }
382  }
383 
384  void ScalarView::ClientMTReceive(vtkSmartPointer<internals::Communicator> communicator)
385  {
386 
387  if (state.surface_rgbtab_modified)
388  {
389  RGBTable new_rgbtab;
390  ReceiveRGBTable(communicator, new_rgbtab);
391  SetSurfaceRGBTable(new_rgbtab,state.surface_rgbtab_size);
392  }
393 
394 
395  }
396 
397 }
std::string dataname
Name of data item in data set.
Definition: vtkfigFigure.h:256
vtkSmartPointer< vtkCutter > planecutY
Definition: vtkfigFigure.h:204
void ServerRTSend(vtkSmartPointer< internals::Communicator > communicator) override final
Client-Server communication.
void RTAddActor2D(vtkSmartPointer< vtkActor2D > prop)
Add vtk Actor to renderer showing figure.
vtkSmartPointer< vtkIdList > celllist
Cell mask.
Definition: vtkfigFigure.h:259
Base class for all figures.
Definition: vtkfigFigure.h:52
vtkSmartPointer< vtkCutter > planecutX
Cutters for plane sections.
Definition: vtkfigFigure.h:203
void ClientMTReceive(vtkSmartPointer< internals::Communicator > communicator) override final
Receive data from server.
vtkSmartPointer< vtkActor > splot
Definition: vtkfigFigure.h:238
vtkSmartPointer< vtkContourFilter > isoline_filter
Definition: vtkfigFigure.h:224
vtkSmartPointer< vtkActor > surface_plot
Items for surface plot.
Definition: vtkfigFigure.h:230
static void ReceiveRGBTable(vtkSmartPointer< internals::Communicator > communicator, RGBTable &rgbtab)
Receive rgb table from client.
RGBTable surface_rgbtab
Definition: vtkfigFigure.h:266
vtkSmartPointer< vtkTrivialProducer > data_producer
Data producer for grid dataset.
Definition: vtkfigFigure.h:247
vtkSmartPointer< vtkTransform > transform
Definition: vtkfigFigure.h:200
void RTCalcTransform()
Calculate transformation to unit cube This shall be applied to all data.
vtkSmartPointer< vtkActor > isoline_plot
Items for isoline plot.
Definition: vtkfigFigure.h:223
vtkSmartPointer< vtkLookupTable > surface_lut
Color lookup table for surface plots.
Definition: vtkfigFigure.h:265
vtkSmartPointer< vtkLookupTable > elevation_lut
Color lookup table for contour plots vtkSmartPointer<vtkLookupTable> contour_lut; RGBTable contour_rg...
Definition: vtkfigFigure.h:303
vtkSmartPointer< vtkLookupTable > BuildLookupTable(RGBTable &xrgb, size_t size)
struct vtkfig::Figure::@0 state
figure state
static void SendRGBTable(vtkSmartPointer< internals::Communicator > communicator, RGBTable &rgbtab)
Send rgb table to client.
void SetSurfaceRGBTable(const V &tab, int lutsize)
Set Surface RGB table from vector.
vtkSmartPointer< vtkActor > isosurface_plot
Items for isosurface plot.
Definition: vtkfigFigure.h:219
vtkSmartPointer< vtkPlane > planeY
Definition: vtkfigFigure.h:209
std::vector< RGBPoint > RGBTable
Definition: vtkfigTools.h:26
void RTBuildVTKPipeline() override final
Generic access to filter.
void RTAddActor(vtkSmartPointer< vtkActor > prop)
Add vtk Actor to renderer showing figure.
vtkSmartPointer< vtkPlane > planeX
Plane equations for plane sections.
Definition: vtkfigFigure.h:208
vtkSmartPointer< vtkScalarBarActor > cbar
Definition: vtkfigFigure.h:293
vtkSmartPointer< vtkCutter > planecutZ
Definition: vtkfigFigure.h:205
vtkSmartPointer< vtkScalarBarActor > BuildColorBar(vtkSmartPointer< vtkPolyDataMapper > mapper, int irank=0)
Definition: vtkfigTools.cxx:88
vtkSmartPointer< vtkPlane > planeZ
Definition: vtkfigFigure.h:210
double trans_center[3]
Definition: vtkfigFigure.h:533
vtkSmartPointer< vtkActor > elevation_plot
Items for elevation plot.
Definition: vtkfigFigure.h:227
vtkSmartPointer< vtkContourFilter > isosurface_filter
Definition: vtkfigFigure.h:220