File: TestPDFExporterContextPolyData.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (223 lines) | stat: -rw-r--r-- 7,703 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestPDFExporterContextPolyData.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkAbstractMapper.h"
#include "vtkAxis.h"
#include "vtkBandedPolyDataContourFilter.h"
#include "vtkBoundingBox.h"
#include "vtkCellData.h"
#include "vtkContextScene.h"
#include "vtkContextView.h"
#include "vtkFeatureEdges.h"
#include "vtkFloatArray.h"
#include "vtkInteractiveArea.h"
#include "vtkInteractorStyle.h"
#include "vtkLookupTable.h"
#include "vtkNew.h"
#include "vtkPDFExporter.h"
#include "vtkPen.h"
#include "vtkPointData.h"
#include "vtkPolyDataConnectivityFilter.h"
#include "vtkPolyDataItem.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkTestUtilities.h"
#include "vtkTestingInteractor.h"
#include "vtkTextProperty.h"
#include "vtkXMLPolyDataReader.h"

namespace
{

//------------------------------------------------------------------------------
vtkSmartPointer<vtkXMLPolyDataReader> ReadUVCDATPolyData(int argc, char* argv[])
{
  const char* fileName = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/isofill_0.vtp");
  vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
  reader->SetFileName(fileName);
  reader->Update();

  delete[] fileName;

  return reader;
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkPolyDataItem> CreateMapItem(int argc, char* argv[])
{
  vtkSmartPointer<vtkXMLPolyDataReader> reader = ReadUVCDATPolyData(argc, argv);
  vtkPolyData* poly = reader->GetOutput();

  // Select point/cell data
  double range[2];
  int scalarMode = VTK_SCALAR_MODE_USE_POINT_DATA;

  vtkDataArray* activeData = scalarMode == VTK_SCALAR_MODE_USE_POINT_DATA
    ? poly->GetPointData()->GetScalars()
    : poly->GetCellData()->GetScalars();
  activeData->GetRange(range, 0);

  // Map scalars
  vtkLookupTable* colorLut = activeData->GetLookupTable();
  if (!colorLut)
  {
    activeData->CreateDefaultLookupTable();
    colorLut = activeData->GetLookupTable();
    colorLut->SetAlpha(1.0);
    colorLut->SetRange(range[0], range[1]);
  }
  vtkUnsignedCharArray* mappedColors = colorLut->MapScalars(activeData, VTK_COLOR_MODE_DEFAULT, 0);

  // Setup item
  vtkSmartPointer<vtkPolyDataItem> polyItem = vtkSmartPointer<vtkPolyDataItem>::New();
  polyItem->SetPolyData(poly);
  polyItem->SetScalarMode(scalarMode);
  polyItem->SetMappedColors(mappedColors);
  mappedColors->Delete();

  return polyItem;
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkPolyDataItem> CreateContourItem(int argc, char* argv[])
{
  vtkSmartPointer<vtkXMLPolyDataReader> reader = ReadUVCDATPolyData(argc, argv);
  vtkNew<vtkBandedPolyDataContourFilter> contour;
  contour->SetInputConnection(reader->GetOutputPort());
  contour->GenerateValues(20, 6, 40);
  contour->ClippingOn();
  contour->SetClipTolerance(0.);
  contour->Update();

  vtkNew<vtkPolyDataConnectivityFilter> connectivity;
  connectivity->SetInputConnection(contour->GetOutputPort());
  connectivity->SetExtractionModeToAllRegions();
  connectivity->ColorRegionsOn();
  connectivity->Update();

  vtkNew<vtkPolyDataConnectivityFilter> extract;
  extract->SetInputConnection(connectivity->GetOutputPort());
  extract->ScalarConnectivityOn();
  extract->SetScalarRange(6, 58);

  vtkNew<vtkFeatureEdges> edge;
  edge->SetInputConnection(extract->GetOutputPort());
  edge->BoundaryEdgesOn();
  edge->FeatureEdgesOff();
  edge->ManifoldEdgesOff();
  edge->NonManifoldEdgesOff();
  edge->Update();

  // Select point/cell data
  double range[2];
  int scalarMode = VTK_SCALAR_MODE_USE_CELL_DATA;

  vtkPolyData* poly = edge->GetOutput();
  vtkDataArray* activeData = scalarMode == VTK_SCALAR_MODE_USE_POINT_DATA
    ? poly->GetPointData()->GetScalars()
    : poly->GetCellData()->GetScalars();
  activeData->GetRange(range, 0);

  // Map scalars
  vtkLookupTable* colorLut = activeData->GetLookupTable();
  if (!colorLut)
  {
    activeData->CreateDefaultLookupTable();
    colorLut = activeData->GetLookupTable();
    colorLut->SetAlpha(1.0);
    colorLut->SetRange(range[0], range[1]);
  }
  vtkUnsignedCharArray* mappedColors = colorLut->MapScalars(activeData, VTK_COLOR_MODE_DEFAULT, 0);

  // Setup item
  vtkSmartPointer<vtkPolyDataItem> polyItem = vtkSmartPointer<vtkPolyDataItem>::New();
  polyItem->SetPolyData(poly);
  polyItem->SetScalarMode(scalarMode);
  polyItem->SetMappedColors(mappedColors);
  mappedColors->Delete();

  return polyItem;
}

} // end anon namespace

/**
 * Tests vtkPolyDataItem and shows its usage with an example. vtkPolyDataItem
 * renders vtkPolyData primitives into a vtkContextScene directly (without the
 * need of a vtkMapper).
 */

///////////////////////////////////////////////////////////////////////////////
int TestPDFExporterContextPolyData(int argc, char* argv[])
{
  // Set up a 2D context view, context test object and add it to the scene
  vtkNew<vtkContextView> view;
  view->GetRenderer()->SetBackground(0.3, 0.3, 0.3);
  view->GetRenderWindow()->SetSize(600, 400);
  view->GetRenderWindow()->SetMultiSamples(0);
  view->GetInteractor()->Initialize();
  view->GetInteractor()->GetInteractorStyle()->SetCurrentRenderer(view->GetRenderer());

  // Create the container item that handles view transform (aspect, interaction,
  // etc.)
  vtkNew<vtkInteractiveArea> area;

  vtkSmartPointer<vtkPolyDataItem> mapItem = CreateMapItem(argc, argv);
  vtkSmartPointer<vtkPolyDataItem> contourItem = CreateContourItem(argc, argv);
  area->GetDrawAreaItem()->AddItem(mapItem);
  area->GetDrawAreaItem()->AddItem(contourItem);

  vtkBoundingBox bounds(mapItem->GetPolyData()->GetBounds());
  area->SetDrawAreaBounds(
    vtkRectd(bounds.GetBound(0), bounds.GetBound(2), bounds.GetLength(0), bounds.GetLength(1)));
  area->SetFixedAspect(bounds.GetLength(0) / bounds.GetLength(1));

  area->GetAxis(vtkAxis::BOTTOM)->SetTitle("X Axis");
  area->GetAxis(vtkAxis::LEFT)->SetTitle("Y Axis");
  area->GetAxis(vtkAxis::TOP)->SetVisible(false);
  area->GetAxis(vtkAxis::RIGHT)->SetVisible(false);
  for (int i = 0; i < 4; ++i)
  {
    vtkAxis* axis = area->GetAxis(static_cast<vtkAxis::Location>(i));
    axis->GetLabelProperties()->SetColor(.6, .6, .9);
    axis->GetTitleProperties()->SetColor(.6, .6, .9);
    axis->GetPen()->SetColor(.6 * 255, .6 * 255, .9 * 255, 255);
    axis->GetGridPen()->SetColor(.6 * 255, .6 * 255, .9 * 255, 128);
  }

  // Turn off the color buffer
  view->GetScene()->SetUseBufferId(false);
  view->GetScene()->AddItem(area);
  view->Render();

  std::string filename =
    vtkTestingInteractor::TempDirectory + std::string("/TestPDFExporterContextPolyData.pdf");

  vtkNew<vtkPDFExporter> exp;
  exp->SetRenderWindow(view->GetRenderWindow());
  exp->SetFileName(filename.c_str());
  exp->Write();

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(view->GetRenderWindow());
  view->GetRenderWindow()->SetMultiSamples(0);
  view->GetRenderWindow()->GetInteractor()->Initialize();
  view->GetRenderWindow()->GetInteractor()->Start();

  return EXIT_SUCCESS;
}