Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/viz/src/widget.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
// Authors:
41
// * Ozan Tonkal, [email protected]
42
// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com
43
//
44
//M*/
45
46
#include "precomp.hpp"
47
48
///////////////////////////////////////////////////////////////////////////////////////////////
49
/// widget implementation
50
51
class cv::viz::Widget::Impl
52
{
53
public:
54
vtkSmartPointer<vtkProp> prop;
55
Impl() : prop(0) {}
56
};
57
58
cv::viz::Widget::Widget() : impl_( new Impl() ) { }
59
60
cv::viz::Widget::Widget(const Widget& other) : impl_( new Impl() )
61
{
62
if (other.impl_ && other.impl_->prop)
63
impl_->prop = other.impl_->prop;
64
}
65
66
cv::viz::Widget& cv::viz::Widget::operator=(const Widget& other)
67
{
68
if (!impl_)
69
impl_ = new Impl();
70
71
if (other.impl_)
72
impl_->prop = other.impl_->prop;
73
return *this;
74
}
75
76
cv::viz::Widget::~Widget()
77
{
78
if (impl_)
79
{
80
delete impl_;
81
impl_ = 0;
82
}
83
}
84
85
cv::viz::Widget cv::viz::Widget::fromPlyFile(const String &file_name)
86
{
87
CV_Assert(vtkPLYReader::CanReadFile(file_name.c_str()));
88
89
vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
90
reader->SetFileName(file_name.c_str());
91
92
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
93
mapper->SetInputConnection( reader->GetOutputPort() );
94
#if VTK_MAJOR_VERSION < 8
95
mapper->ImmediateModeRenderingOff();
96
#endif
97
98
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
99
actor->GetProperty()->SetInterpolationToFlat();
100
actor->GetProperty()->BackfaceCullingOn();
101
actor->SetMapper(mapper);
102
103
Widget widget;
104
WidgetAccessor::setProp(widget, actor);
105
return widget;
106
}
107
108
void cv::viz::Widget::setRenderingProperty(int property, double value)
109
{
110
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
111
CV_Assert("Widget type is not supported." && actor);
112
113
switch (property)
114
{
115
case POINT_SIZE: actor->GetProperty()->SetPointSize(float(value)); break;
116
case OPACITY: actor->GetProperty()->SetOpacity(value); break;
117
case LINE_WIDTH: actor->GetProperty()->SetLineWidth(float(value)); break;
118
#if VTK_MAJOR_VERSION < 8
119
case IMMEDIATE_RENDERING: actor->GetMapper()->SetImmediateModeRendering(int(value)); break;
120
#else
121
case IMMEDIATE_RENDERING: std::cerr << "this property has no effect" << std::endl; break;
122
#endif
123
case AMBIENT: actor->GetProperty()->SetAmbient(float(value)); break;
124
case LIGHTING:
125
{
126
if (value == 0)
127
actor->GetProperty()->LightingOff();
128
else
129
actor->GetProperty()->LightingOn();
130
break;
131
}
132
case FONT_SIZE:
133
{
134
vtkTextActor* text_actor = vtkTextActor::SafeDownCast(actor);
135
CV_Assert("Widget does not have text content." && text_actor);
136
text_actor->GetTextProperty()->SetFontSize(int(value));
137
break;
138
}
139
case REPRESENTATION:
140
{
141
switch (int(value))
142
{
143
case REPRESENTATION_POINTS: actor->GetProperty()->SetRepresentationToPoints(); break;
144
case REPRESENTATION_WIREFRAME: actor->GetProperty()->SetRepresentationToWireframe(); break;
145
case REPRESENTATION_SURFACE: actor->GetProperty()->SetRepresentationToSurface(); break;
146
}
147
break;
148
}
149
case SHADING:
150
{
151
switch (int(value))
152
{
153
case SHADING_FLAT: actor->GetProperty()->SetInterpolationToFlat(); break;
154
case SHADING_GOURAUD:
155
{
156
if (!actor->GetMapper()->GetInput()->GetPointData()->GetNormals())
157
{
158
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
159
CV_Assert("Can't set shading property for such type of widget" && mapper);
160
161
vtkSmartPointer<vtkPolyData> with_normals = VtkUtils::ComputeNormals(mapper->GetInput());
162
VtkUtils::SetInputData(mapper, with_normals);
163
}
164
actor->GetProperty()->SetInterpolationToGouraud();
165
break;
166
}
167
case SHADING_PHONG:
168
{
169
if (!actor->GetMapper()->GetInput()->GetPointData()->GetNormals())
170
{
171
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
172
CV_Assert("Can't set shading property for such type of widget" && mapper);
173
174
vtkSmartPointer<vtkPolyData> with_normals = VtkUtils::ComputeNormals(mapper->GetInput());
175
VtkUtils::SetInputData(mapper, with_normals);
176
}
177
actor->GetProperty()->SetInterpolationToPhong();
178
break;
179
}
180
}
181
break;
182
}
183
default:
184
CV_Assert("setRenderingProperty: Unknown property");
185
}
186
actor->Modified();
187
}
188
189
double cv::viz::Widget::getRenderingProperty(int property) const
190
{
191
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
192
CV_Assert("Widget type is not supported." && actor);
193
194
double value = 0.0;
195
switch (property)
196
{
197
case POINT_SIZE: value = actor->GetProperty()->GetPointSize(); break;
198
case OPACITY: value = actor->GetProperty()->GetOpacity(); break;
199
case LINE_WIDTH: value = actor->GetProperty()->GetLineWidth(); break;
200
#if VTK_MAJOR_VERSION < 8
201
case IMMEDIATE_RENDERING: value = actor->GetMapper()->GetImmediateModeRendering(); break;
202
#else
203
case IMMEDIATE_RENDERING: std::cerr << "this property has no effect" << std::endl; break;
204
#endif
205
case AMBIENT: value = actor->GetProperty()->GetAmbient(); break;
206
case LIGHTING: value = actor->GetProperty()->GetLighting(); break;
207
case FONT_SIZE:
208
{
209
vtkTextActor* text_actor = vtkTextActor::SafeDownCast(actor);
210
CV_Assert("Widget does not have text content." && text_actor);
211
value = text_actor->GetTextProperty()->GetFontSize();;
212
break;
213
}
214
case REPRESENTATION:
215
{
216
switch (actor->GetProperty()->GetRepresentation())
217
{
218
case VTK_POINTS: value = REPRESENTATION_POINTS; break;
219
case VTK_WIREFRAME: value = REPRESENTATION_WIREFRAME; break;
220
case VTK_SURFACE: value = REPRESENTATION_SURFACE; break;
221
}
222
break;
223
}
224
case SHADING:
225
{
226
switch (actor->GetProperty()->GetInterpolation())
227
{
228
case VTK_FLAT: value = SHADING_FLAT; break;
229
case VTK_GOURAUD: value = SHADING_GOURAUD; break;
230
case VTK_PHONG: value = SHADING_PHONG; break;
231
}
232
break;
233
}
234
default:
235
CV_Assert("getRenderingProperty: Unknown property");
236
}
237
return value;
238
}
239
240
///////////////////////////////////////////////////////////////////////////////////////////////
241
/// widget accessor implementation
242
243
vtkSmartPointer<vtkProp> cv::viz::WidgetAccessor::getProp(const Widget& widget)
244
{
245
return widget.impl_->prop;
246
}
247
248
void cv::viz::WidgetAccessor::setProp(Widget& widget, vtkSmartPointer<vtkProp> prop)
249
{
250
widget.impl_->prop = prop;
251
}
252
253
///////////////////////////////////////////////////////////////////////////////////////////////
254
/// widget3D implementation
255
256
void cv::viz::Widget3D::setPose(const Affine3d &pose)
257
{
258
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
259
CV_Assert("Widget is not 3D." && actor);
260
261
vtkSmartPointer<vtkMatrix4x4> matrix = vtkmatrix(pose.matrix);
262
actor->SetUserMatrix(matrix);
263
actor->Modified();
264
}
265
266
void cv::viz::Widget3D::updatePose(const Affine3d &pose)
267
{
268
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
269
CV_Assert("Widget is not 3D." && actor);
270
271
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix();
272
if (!matrix)
273
{
274
setPose(pose);
275
return;
276
}
277
278
Affine3d updated_pose = pose * Affine3d(*matrix->Element);
279
matrix = vtkmatrix(updated_pose.matrix);
280
281
actor->SetUserMatrix(matrix);
282
actor->Modified();
283
}
284
285
cv::Affine3d cv::viz::Widget3D::getPose() const
286
{
287
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
288
CV_Assert("Widget is not 3D." && actor);
289
if (!actor->GetUserMatrix())
290
{
291
return Affine3d(); // empty user matrix, return an identity transform.
292
}
293
return Affine3d(*actor->GetUserMatrix()->Element);
294
}
295
296
void cv::viz::Widget3D::applyTransform(const Affine3d &transform)
297
{
298
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
299
CV_Assert("Widget is not 3D actor." && actor);
300
301
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
302
CV_Assert("Widget doesn't have a polydata mapper" && mapper);
303
304
mapper->Update(); // #10945
305
VtkUtils::SetInputData(mapper, VtkUtils::TransformPolydata(mapper->GetInput(), transform));
306
mapper->Update();
307
}
308
309
void cv::viz::Widget3D::setColor(const Color &color)
310
{
311
// Cast to actor instead of prop3d since prop3d doesn't provide getproperty
312
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
313
CV_Assert("Widget type is not supported." && actor);
314
315
Color c = vtkcolor(color);
316
actor->GetMapper()->ScalarVisibilityOff();
317
actor->GetProperty()->SetColor(c.val);
318
actor->GetProperty()->SetEdgeColor(c.val);
319
actor->Modified();
320
}
321
322
template<> cv::viz::Widget3D cv::viz::Widget::cast<cv::viz::Widget3D>()
323
{
324
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
325
CV_Assert("Widget cannot be cast." && actor);
326
327
Widget3D widget;
328
WidgetAccessor::setProp(widget, actor);
329
return widget;
330
}
331
332
///////////////////////////////////////////////////////////////////////////////////////////////
333
/// widget2D implementation
334
335
void cv::viz::Widget2D::setColor(const Color &color)
336
{
337
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
338
CV_Assert("Widget type is not supported." && actor);
339
Color c = vtkcolor(color);
340
actor->GetProperty()->SetColor(c.val);
341
actor->Modified();
342
}
343
344
template<> cv::viz::Widget2D cv::viz::Widget::cast<cv::viz::Widget2D>()
345
{
346
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
347
CV_Assert("Widget cannot be cast." && actor);
348
349
Widget2D widget;
350
WidgetAccessor::setProp(widget, actor);
351
return widget;
352
}
353
354