1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGL2PSLabeledDataMapper.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #include "vtkTestUtilities.h"
17 #include "vtkRegressionTestImage.h"
18 #include "vtkGL2PSExporter.h"
19 
20 #include "vtkActor.h"
21 #include "vtkActor2D.h"
22 #include "vtkCamera.h"
23 #include "vtkCellArray.h"
24 #include "vtkCellCenters.h"
25 #include "vtkIdFilter.h"
26 #include "vtkLabeledDataMapper.h"
27 #include "vtkNew.h"
28 #include "vtkPoints.h"
29 #include "vtkPolyData.h"
30 #include "vtkPolyDataMapper.h"
31 #include "vtkPolyDataMapper2D.h"
32 #include "vtkRenderer.h"
33 #include "vtkRenderWindow.h"
34 #include "vtkRenderWindowInteractor.h"
35 #include "vtkSelectVisiblePoints.h"
36 #include "vtkSphereSource.h"
37 #include "vtkTestingInteractor.h"
38 #include "vtkTextProperty.h"
39 
40 // This test is adapted from labeledMesh.py to test GL2PS exporting of selection
41 // labels.
TestGL2PSLabeledDataMapper(int,char * [])42 int TestGL2PSLabeledDataMapper(int, char *[] )
43 {
44   // Selection rectangle:
45   double xmin = 100.;
46   double xmax = 400.;
47   double ymin = 100.;
48   double ymax = 400.;
49 
50   vtkNew<vtkPoints> pts;
51   pts->InsertPoint(0, xmin, ymin, 0.);
52   pts->InsertPoint(1, xmax, ymin, 0.);
53   pts->InsertPoint(2, xmax, ymax, 0.);
54   pts->InsertPoint(3, xmin, ymax, 0.);
55 
56   vtkNew<vtkCellArray> rect;
57   rect->InsertNextCell(5);
58   rect->InsertCellPoint(0);
59   rect->InsertCellPoint(1);
60   rect->InsertCellPoint(2);
61   rect->InsertCellPoint(3);
62   rect->InsertCellPoint(0);
63 
64   vtkNew<vtkPolyData> selectRect;
65   selectRect->SetPoints(pts);
66   selectRect->SetLines(rect);
67 
68   vtkNew<vtkPolyDataMapper2D> rectMapper;
69   vtkNew<vtkActor2D> rectActor;
70   rectMapper->SetInputData(selectRect);
71   rectActor->SetMapper(rectMapper);
72 
73   // Create sphere
74   vtkNew<vtkSphereSource> sphere;
75   vtkNew<vtkPolyDataMapper> sphereMapper;
76   vtkNew<vtkActor> sphereActor;
77   sphereMapper->SetInputConnection(sphere->GetOutputPort());
78   sphereActor->SetMapper(sphereMapper);
79 
80   // Generate ids for labeling
81   vtkNew<vtkIdFilter> ids;
82   ids->SetInputConnection(sphere->GetOutputPort());
83   ids->PointIdsOn();
84   ids->CellIdsOn();
85   ids->FieldDataOn();
86 
87   // Create labels for points
88   vtkNew<vtkSelectVisiblePoints> visPts;
89   visPts->SetInputConnection(ids->GetOutputPort());
90   visPts->SelectionWindowOn();
91   visPts->SetSelection(static_cast<int>(xmin), static_cast<int>(xmax), static_cast<int>(ymin), static_cast<int>(ymax));
92 
93   vtkNew<vtkLabeledDataMapper> ldm;
94   ldm->SetInputConnection(visPts->GetOutputPort());
95   ldm->SetLabelModeToLabelFieldData();
96 
97   vtkNew<vtkActor2D> pointLabels;
98   pointLabels->SetMapper(ldm);
99 
100   // Create labels for cells:
101   vtkNew<vtkCellCenters> cc;
102   cc->SetInputConnection(ids->GetOutputPort());
103 
104   vtkNew<vtkSelectVisiblePoints> visCells;
105   visCells->SetInputConnection(cc->GetOutputPort());
106   visCells->SelectionWindowOn();
107   visCells->SetSelection(static_cast<int>(xmin), static_cast<int>(xmax), static_cast<int>(ymin), static_cast<int>(ymax));
108 
109   vtkNew<vtkLabeledDataMapper> cellMapper;
110   cellMapper->SetInputConnection(visCells->GetOutputPort());
111   cellMapper->SetLabelModeToLabelFieldData();
112   cellMapper->GetLabelTextProperty()->SetColor(0., 1., 0.);
113 
114   vtkNew<vtkActor2D> cellLabels;
115   cellLabels->SetMapper(cellMapper);
116 
117   // Rendering setup
118   vtkNew<vtkRenderer> ren;
119   visPts->SetRenderer(ren);
120   visCells->SetRenderer(ren);
121   ren->AddActor(sphereActor);
122   ren->AddActor2D(rectActor);
123   ren->AddActor2D(pointLabels);
124   ren->AddActor2D(cellLabels);
125   ren->SetBackground(1., 1., 1.);
126   ren->GetActiveCamera()->Zoom(.55);
127 
128   vtkNew<vtkRenderWindow> renWin;
129   vtkNew<vtkRenderWindowInteractor> iren;
130   iren->SetRenderWindow(renWin);
131   renWin->AddRenderer(ren);
132   renWin->SetMultiSamples(0);
133   renWin->SetSize(500, 500);
134   renWin->Render();
135 
136   vtkNew<vtkGL2PSExporter> exp;
137   exp->SetRenderWindow(renWin);
138   exp->SetFileFormatToPS();
139   exp->CompressOff();
140   exp->SetPS3Shading(0);
141   exp->SetSortToSimple();
142   exp->DrawBackgroundOn();
143   exp->Write3DPropsAsRasterImageOff();
144   exp->SetTextAsPath(true);
145 
146   std::string fileprefix = vtkTestingInteractor::TempDirectory +
147       std::string("/TestGL2PSLabeledDataMapper");
148 
149   exp->SetFilePrefix(fileprefix.c_str());
150   exp->Write();
151 
152   iren->Initialize();
153   iren->Start();
154 
155   return EXIT_SUCCESS;
156 }
157