-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathPlanes.cxx
More file actions
164 lines (139 loc) · 5.45 KB
/
Planes.cxx
File metadata and controls
164 lines (139 loc) · 5.45 KB
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
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkHull.h>
#include <vtkNamedColors.h>
#include <vtkPlanes.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>
#include <string>
#include <vector>
int main(int, char* [])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// These are the two methods we will use.
std::vector<std::string> titles;
titles.push_back("Using frustum planes");
titles.push_back("Using bounds");
std::vector<vtkSmartPointer<vtkPlanes> > planes;
for (std::vector<std::string>::size_type i = 0; i < titles.size(); ++i)
{
planes.push_back(vtkSmartPointer<vtkPlanes>::New());
}
// Using frustum planes.
vtkSmartPointer<vtkCamera> camera =
vtkSmartPointer<vtkCamera>::New();
double planesArray[24];
camera->GetFrustumPlanes(1, planesArray);
planes[0]->SetFrustumPlanes(planesArray);
// Using bounds.
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
double bounds[6];
sphereSource->GetOutput()->GetBounds(bounds);
planes[1]->SetBounds(bounds);
// At this point we have the planes created by both of the methods above.
// You can do whatever you want with them.
// For visualisation we will produce an n-sided convex hull
// and visualise it.
// Create a common text property.
vtkSmartPointer<vtkTextProperty> textProperty =
vtkSmartPointer<vtkTextProperty>::New();
textProperty->SetFontSize(16);
textProperty->SetJustificationToCentered();
// Create the render window and interactor.
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->SetWindowName("Planes");
vtkSmartPointer<vtkRenderWindowInteractor> iRen =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iRen->SetRenderWindow(renWin);
std::vector<vtkSmartPointer<vtkHull> > hulls;
std::vector<vtkSmartPointer<vtkPolyData> > pds;
std::vector<vtkSmartPointer<vtkPolyDataMapper> > mappers;
std::vector<vtkSmartPointer<vtkActor> > actors;
std::vector<vtkSmartPointer<vtkTextMapper> > textMappers;
std::vector<vtkSmartPointer<vtkActor2D> > textActors;
std::vector<vtkSmartPointer<vtkRenderer> > renderers;
for (std::vector<std::string>::size_type i = 0; i < titles.size(); ++i)
{
hulls.push_back(vtkSmartPointer<vtkHull>::New());
hulls[i]->SetPlanes(planes[i]);
pds.push_back(vtkSmartPointer<vtkPolyData>::New());
// To generate the convex hull we supply a vtkPolyData object and a bounding
// box.
// We define the bounding box to be where we expect the resulting polyhedron
// to lie.
// Make it a generous fit as it is only used to create the initial
// polygons that are eventually clipped.
hulls[i]->GenerateHull(pds[i], -200, 200, -200, 200, -200, 200);
mappers.push_back(vtkSmartPointer<vtkPolyDataMapper>::New());
mappers[i]->SetInputData(pds[i]);
actors.push_back(vtkSmartPointer<vtkActor>::New());
actors[i]->SetMapper(mappers[i]);
actors[i]->GetProperty()->SetColor(
colors->GetColor3d("Moccasin").GetData());
actors[i]->GetProperty()->SetSpecular(0.8);
actors[i]->GetProperty()->SetSpecularPower(30);
textMappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textMappers[i]->SetInput(titles[i].c_str());
textMappers[i]->SetTextProperty(textProperty);
textActors.push_back(vtkSmartPointer<vtkActor2D>::New());
textActors[i]->SetMapper(textMappers[i]);
textActors[i]->SetPosition(120, 16);
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
renderers[i]->AddActor(actors[i]);
renderers[i]->AddViewProp(textActors[i]);
renWin->AddRenderer(renderers[i]);
}
// Setup the viewports
const int xGridDimensions = 2;
const int yGridDimensions = 1;
const int rendererSize = 300;
renWin->SetSize(rendererSize * xGridDimensions,
rendererSize * yGridDimensions);
for (int row = 0; row < yGridDimensions; ++row)
{
for (int col = 0; col < xGridDimensions; ++col)
{
const int index = row * xGridDimensions + col;
// (xmin, ymin, xmax, ymax)
double viewport[4] = {
static_cast<double>(col) / xGridDimensions,
static_cast<double>(yGridDimensions - (row + 1)) / yGridDimensions,
static_cast<double>(col + 1) / xGridDimensions,
static_cast<double>(yGridDimensions - row) / yGridDimensions};
if (index > (actors.size() - 1))
{
// Add a renderer even if there is no actor.
// This makes the render window background all the same color.
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
ren->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
ren->SetViewport(viewport);
renWin->AddRenderer(ren);
continue;
}
renderers[index]->SetViewport(viewport);
renderers[index]->SetBackground(
colors->GetColor3d("DarkSlateGray").GetData());
renderers[index]->ResetCamera();
renderers[index]->GetActiveCamera()->Azimuth(30);
renderers[index]->GetActiveCamera()->Elevation(-30);
renderers[index]->ResetCameraClippingRange();
}
}
iRen->Initialize();
renWin->Render();
iRen->Start();
return EXIT_SUCCESS;
}