VTKFIG  0.20.20181114
Easy VTK based in situ visualization
vtkfigScalarView.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 
39  ScalarView::ScalarView(): Figure()
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 
53  {
54  state.warp_ztran=val;
55  warp_transform->Identity();
56  warp_transform->Translate(0,0,state.warp_ztran);
57  }
58 
60  {
61  state.elevation_scale=val;
62  warp_transform->Identity();
63  warp_transform->Translate(0,0,state.warp_ztran);
64  }
65 
66 
69 
70  template <class DATA, class FILTER>
72  {
73 
75 
77  //renderer->GetActiveCamera()->SetParallelProjection(1);
78 
79  auto values=vtkDoubleArray::SafeDownCast(DATA::SafeDownCast(data_producer->GetOutputDataObject(0))->GetPointData()->GetAbstractArray(dataname.c_str()));
80 
81  vtkSmartPointer<vtkExtractCells> subgrid;
82  if (celllist)
83  {
84  subgrid=vtkSmartPointer<vtkExtractCells>::New();
85  subgrid->SetInputConnection(data_producer->GetOutputPort());
86  subgrid->SetCellList(celllist);
87  }
88 
89 
90  auto scalar = vtkSmartPointer<vtkAssignAttribute>::New();
91  scalar->Assign(dataname.c_str(),vtkDataSetAttributes::SCALARS,vtkAssignAttribute::POINT_DATA);
92  if (celllist)
93  scalar->SetInputConnection(subgrid->GetOutputPort());
94  else
95  scalar->SetInputConnection(data_producer->GetOutputPort());
96 
97  auto geometry=vtkSmartPointer<FILTER>::New();
98  geometry->SetInputConnection(scalar->GetOutputPort());
99 
100 
101 
102  auto transgeometry=vtkSmartPointer<vtkTransformPolyDataFilter>::New();
103  transgeometry->SetInputConnection(geometry->GetOutputPort());
104  transgeometry->SetTransform(transform);
105 
109  vtkSmartPointer<vtkTransformPolyDataFilter> transallgeometry;
110  if (celllist)
111  {
112  auto allgeometry=vtkSmartPointer<FILTER>::New();
113  allgeometry->SetInputConnection(data_producer->GetOutputPort());
114 
115  transallgeometry=vtkSmartPointer<vtkTransformPolyDataFilter>::New();
116  transallgeometry->SetTransform(transform);
117  transallgeometry->SetInputConnection(allgeometry->GetOutputPort());
118  }
119  else
120  transallgeometry=transgeometry;
121 
122  if (true) // Elevation
123  {
124  auto wtransgeometry=vtkSmartPointer<vtkTransformPolyDataFilter>::New();
125  wtransgeometry->SetInputConnection(transgeometry->GetOutputPort());
126  wtransgeometry->SetTransform(warp_transform);
127 
128  auto elevation = vtkSmartPointer<vtkWarpScalar>::New();
129  elevation->SetInputConnection(wtransgeometry->GetOutputPort());
130  elevation->SetScaleFactor(state.elevation_scale*0.5/(state.real_vmax-state.real_vmin));
131  vtkSmartPointer<vtkPolyDataMapper> wmapper = vtkSmartPointer<vtkPolyDataMapper>::New();
132  wmapper->SetInputConnection(elevation->GetOutputPort());
133  vtkSmartPointer<vtkActor> wplot = vtkSmartPointer<vtkActor>::New();
134  wmapper->UseLookupTableScalarRangeOn();
135  wmapper->SetLookupTable(elevation_lut);
136  elevation_plot->SetMapper(wmapper);
137  elevation_plot->SetVisibility(state.show_elevation);
139  }
140 
141 
142  if (true)
143  {
144  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
145  mapper->SetInputConnection(transgeometry->GetOutputPort());
146 
147 // mapper->InterpolateScalarsBeforeMappingOn();
148  mapper->UseLookupTableScalarRangeOn();
149  mapper->SetLookupTable(surface_lut);
150 #ifdef VTK_HAS_MAPPER_IMMEDIATE_RENDERING_ON
151  mapper->ImmediateModeRenderingOn();
152 #endif
153  surface_plot->SetMapper(mapper);
154  surface_plot->SetVisibility(state.show_surface);
156 
157  if (state.show_surface_colorbar)
158  {
159  auto cbar=BuildColorBar(mapper);
160  cbar->SetLabelFormat(state.surface_colorbar_label_format);
161  cbar->SetNumberOfLabels(state.surface_colorbar_num_labels);
163  }
164  }
165 
166 
167  if (true)
168  {
169 
170  isoline_filter->SetInputConnection(transgeometry->GetOutputPort());
171 
172  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
173  mapper->SetInputConnection(isoline_filter->GetOutputPort());
174  //mapper->UseLookupTableScalarRangeOn();
175  //mapper->SetLookupTable(contour_lut);
176  mapper->ScalarVisibilityOff();
177 
178  isoline_plot->SetMapper(mapper);
179  isoline_plot->GetProperty()->SetColor(0,0,0);
180  isoline_plot->GetProperty()->SetLineWidth(state.isoline_width);
181  isoline_plot->SetVisibility(state.show_isolines);
183 
184  // if (state.show_contour_colorbar)
185  // Figure::RTAddActor2D(BuildColorBar(mapper));
186 
187  // if (state.show_slider)
188  // AddSlider(interactor,renderer);
189 
190  }
191 
192 
193  }
194 
195 
198 
199  template <class DATA,class FILTER>
201  {
202  RTCalcTransform();
203 
204 
205 
206  vtkSmartPointer<vtkExtractCells> subgrid;
207  if (celllist)
208  {
209  subgrid=vtkSmartPointer<vtkExtractCells>::New();
210  subgrid->SetInputConnection(data_producer->GetOutputPort());
211  subgrid->SetCellList(celllist);
212  }
213 
214 
215 
216  auto scalar = vtkSmartPointer<vtkAssignAttribute>::New();
217  scalar->Assign(dataname.c_str(),vtkDataSetAttributes::SCALARS,vtkAssignAttribute::POINT_DATA);
218  if (celllist)
219  scalar->SetInputConnection(subgrid->GetOutputPort());
220  else
221  scalar->SetInputConnection(data_producer->GetOutputPort());
222 
223 
224 
225 
226  auto transgeometry=vtkSmartPointer<vtkTransformFilter>::New();
227  transgeometry->SetInputConnection(scalar->GetOutputPort());
228  transgeometry->SetTransform(transform);
229 
230  vtkSmartPointer<vtkTransformFilter> transallgeometry;
231  if (celllist)
232  {
233  auto allgeometry=vtkSmartPointer<FILTER>::New();
234  allgeometry->SetInputConnection(data_producer->GetOutputPort());
235 
236  transallgeometry=vtkSmartPointer<vtkTransformFilter>::New();
237  transallgeometry->SetTransform(transform);
238  transallgeometry->SetInputConnection(allgeometry->GetOutputPort());
239  }
240  else
241  transallgeometry=transgeometry;
242 
243 
244 
245 
246  planeX->SetOrigin(trans_center);
247  planeY->SetOrigin(trans_center);
248  planeZ->SetOrigin(trans_center);
249 
250  planecutX->SetInputConnection(transgeometry->GetOutputPort());
251  planecutX->SetCutFunction(planeX);
252  planecutX->SetNumberOfContours(0);
253 
254 
255  planecutY->SetInputConnection(transgeometry->GetOutputPort());
256  planecutY->SetCutFunction(planeY);
257  planecutY->SetNumberOfContours(0);
258 
259  planecutZ->SetInputConnection(transgeometry->GetOutputPort());
260  planecutZ->SetCutFunction(planeZ);
261  planecutZ->SetNumberOfContours(1);
262  planecutZ->SetValue(0,0.0);
263 
264 
265  vtkSmartPointer<vtkClipPolyData> clipgeometry=0;
266  if (false) // plot complete outline surface
267  {
268 
269  clipgeometry=vtkSmartPointer<vtkClipPolyData>::New();
270  auto sfilter=vtkSmartPointer<FILTER>::New();
271  sfilter->SetInputConnection(transgeometry->GetOutputPort());
272  clipgeometry->SetInputConnection(sfilter->GetOutputPort());
273  clipgeometry->SetClipFunction(planeZ);
274  clipgeometry->SetInsideOut(1);
275 
276  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
277 
278 
279  mapper->SetInputConnection(clipgeometry->GetOutputPort());
280  mapper->UseLookupTableScalarRangeOn();
281  mapper->SetLookupTable(surface_lut);
282  auto splot=vtkSmartPointer<vtkActor>::New();
283  splot->GetProperty()->SetOpacity(1.0);
284  splot->SetMapper(mapper);
286 
287  }
288 
289 
290 
291  auto xyz = vtkSmartPointer<vtkAppendPolyData>::New();
292  xyz->SetUserManagedInputs(1);
293  if (clipgeometry)
294  xyz->SetNumberOfInputs(4);
295  else
296  xyz->SetNumberOfInputs(3);
297 
298  xyz->SetInputConnectionByNumber(0,planecutX->GetOutputPort());
299  xyz->SetInputConnectionByNumber(1,planecutY->GetOutputPort());
300  xyz->SetInputConnectionByNumber(2,planecutZ->GetOutputPort());
301  if (clipgeometry)
302  xyz->SetInputConnectionByNumber(3,clipgeometry->GetOutputPort());
303 
304 
305  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
306  mapper->SetInputConnection(xyz->GetOutputPort());
307  mapper->UseLookupTableScalarRangeOn();
308  mapper->SetLookupTable(surface_lut);
309 
310  vtkSmartPointer<vtkActor> plot = vtkSmartPointer<vtkActor>::New();
311  plot->GetProperty()->SetOpacity(1);
312  plot->SetMapper(mapper);
313  Figure::RTAddActor(plot);
314 
316 
317 
318 
319  if (true)
320  {
321 
322  isoline_filter->SetInputConnection(xyz->GetOutputPort());
323 
324  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
325  mapper->SetInputConnection(isoline_filter->GetOutputPort());
326  // mapper->UseLookupTableScalarRangeOn();
327  // mapper->SetLookupTable(contour_lut);
328  mapper->ScalarVisibilityOff();
329 
330  isoline_plot->GetProperty()->SetOpacity(1.0);
331  isoline_plot->GetProperty()->SetColor(0,0,0);
332  isoline_plot->GetProperty()->SetLineWidth(state.isoline_width);
333  isoline_plot->SetMapper(mapper);
334  isoline_plot->SetVisibility(state.show_isolines);
336  }
337 
338 
339  if (true)
340  {
341 
342  isosurface_filter->SetInputConnection(transgeometry->GetOutputPort());
343 
344  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
345  mapper->SetInputConnection(isosurface_filter->GetOutputPort());
346  mapper->UseLookupTableScalarRangeOn();
347  mapper->SetLookupTable(surface_lut);
348 
349  isosurface_plot->GetProperty()->SetOpacity(0.3);
350 
351  isosurface_plot->SetMapper(mapper);
352  isosurface_plot->SetVisibility(state.show_isosurfaces);
354 
355  }
356 
357 
358  }
359 
360 
364  {
365 
367  {
368  if (state.spacedim==2)
369  this->RTBuildVTKPipeline2D<vtkUnstructuredGrid,vtkGeometryFilter>();
370  else
371  this->RTBuildVTKPipeline3D<vtkUnstructuredGrid,vtkGeometryFilter>();
372  }
373  else if (state.datatype==DataSet::DataType::RectilinearGrid)
374  {
375  if (state.spacedim==2)
376  this->RTBuildVTKPipeline2D<vtkRectilinearGrid,vtkRectilinearGridGeometryFilter>();
377  else
378  this->RTBuildVTKPipeline3D<vtkRectilinearGrid,vtkRectilinearGridGeometryFilter>();
379  }
380  }
381 
382 
383 
384 
387 
388  void ScalarView::ServerRTSend(vtkSmartPointer<internals::Communicator> communicator)
389  {
390  if (state.surface_rgbtab_modified)
391  {
392  SendRGBTable(communicator, surface_rgbtab);
393  state.surface_rgbtab_modified=false;
394  }
395  }
396 
397  void ScalarView::ClientMTReceive(vtkSmartPointer<internals::Communicator> communicator)
398  {
399 
400  if (state.surface_rgbtab_modified)
401  {
402  RGBTable new_rgbtab;
403  ReceiveRGBTable(communicator, new_rgbtab);
404  SetSurfaceRGBTable(new_rgbtab,state.surface_rgbtab_size);
405  }
406 
407 
408  }
409 
410 }
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
vtkSmartPointer< vtkCutter > planecutX
Cutters for plane sections.
Definition: vtkfigFigure.h:203
void ClientMTReceive(vtkSmartPointer< internals::Communicator > communicator) override final
Receive data from server.
void SetElevationScale(double val)
Change scale for elevation plot.
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
vtkSmartPointer< vtkTransform > warp_transform
Items for elevation plot.
Definition: vtkfigFigure.h:233
void SetElevationZShift(double val)
Change zshift for elevation plot.