Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/viz/src/precomp.hpp
16339 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
#ifndef __OPENCV_VIZ_PRECOMP_HPP__
47
#define __OPENCV_VIZ_PRECOMP_HPP__
48
49
#include <map>
50
#include <ctime>
51
#include <list>
52
#include <vector>
53
#include <iomanip>
54
#include <limits>
55
56
#include <vtkAppendPolyData.h>
57
#include <vtkAssemblyPath.h>
58
#include <vtkCellData.h>
59
#include <vtkLineSource.h>
60
#include <vtkPropPicker.h>
61
#include <vtkSmartPointer.h>
62
#include <vtkDataSet.h>
63
#include <vtkPolygon.h>
64
#include <vtkUnstructuredGrid.h>
65
#include <vtkDiskSource.h>
66
#include <vtkPlaneSource.h>
67
#include <vtkSphereSource.h>
68
#include <vtkArrowSource.h>
69
#include <vtkOutlineSource.h>
70
#include <vtkTransform.h>
71
#include <vtkTransformPolyDataFilter.h>
72
#include <vtkTubeFilter.h>
73
#include <vtkCubeSource.h>
74
#include <vtkAxes.h>
75
#include <vtkFloatArray.h>
76
#include <vtkDoubleArray.h>
77
#include <vtkPointData.h>
78
#include <vtkPolyData.h>
79
#include <vtkPolyDataMapper.h>
80
#include <vtkDataSetMapper.h>
81
#include <vtkCellArray.h>
82
#include <vtkCommand.h>
83
#include <vtkPLYReader.h>
84
#include <vtkPolyLine.h>
85
#include <vtkVectorText.h>
86
#include <vtkFollower.h>
87
#include <vtkInteractorStyle.h>
88
#include <vtkUnsignedCharArray.h>
89
#include <vtkRendererCollection.h>
90
#include <vtkPNGWriter.h>
91
#include <vtkWindowToImageFilter.h>
92
#include <vtkInteractorStyleTrackballCamera.h>
93
#include <vtkProperty.h>
94
#include <vtkCamera.h>
95
#include <vtkPlanes.h>
96
#include <vtkImageFlip.h>
97
#include <vtkRenderWindow.h>
98
#include <vtkTextProperty.h>
99
#include <vtkProperty2D.h>
100
#include <vtkLODActor.h>
101
#include <vtkActor.h>
102
#include <vtkTextActor.h>
103
#include <vtkRenderWindowInteractor.h>
104
#include <vtkMath.h>
105
#include <vtkExtractEdges.h>
106
#include <vtkFrustumSource.h>
107
#include <vtkTexture.h>
108
#include <vtkTextureMapToPlane.h>
109
#include <vtkPolyDataNormals.h>
110
#include <vtkAlgorithmOutput.h>
111
#include <vtkImageMapper.h>
112
#include <vtkPoints.h>
113
#include <vtkInformation.h>
114
#include <vtkInformationVector.h>
115
#include <vtkObjectFactory.h>
116
#include <vtkPolyDataAlgorithm.h>
117
#include <vtkMergeFilter.h>
118
#include <vtkErrorCode.h>
119
#include <vtkPLYWriter.h>
120
#include <vtkSTLWriter.h>
121
#include <vtkPLYReader.h>
122
#include <vtkOBJReader.h>
123
#include <vtkSTLReader.h>
124
#include <vtkPNGReader.h>
125
#include <vtkOBJExporter.h>
126
#include <vtkVRMLExporter.h>
127
#include <vtkTensorGlyph.h>
128
#include <vtkImageAlgorithm.h>
129
#include <vtkTransformFilter.h>
130
#include <vtkConeSource.h>
131
#include <vtkElevationFilter.h>
132
#include <vtkColorTransferFunction.h>
133
#include <vtkStreamingDemandDrivenPipeline.h>
134
#include <vtkLight.h>
135
#include "vtkCallbackCommand.h"
136
137
#if !defined(_WIN32) || defined(__CYGWIN__)
138
# include <unistd.h> /* unlink */
139
#else
140
# include <io.h> /* unlink */
141
#endif
142
143
#include "vtk/vtkOBJWriter.h"
144
#include "vtk/vtkXYZWriter.h"
145
#include "vtk/vtkXYZReader.h"
146
#include "vtk/vtkCloudMatSink.h"
147
#include "vtk/vtkCloudMatSource.h"
148
#include "vtk/vtkTrajectorySource.h"
149
#include "vtk/vtkImageMatSource.h"
150
151
152
#include <opencv2/core.hpp>
153
#include <opencv2/viz.hpp>
154
#include <opencv2/viz/widget_accessor.hpp>
155
#include <opencv2/core/utility.hpp>
156
157
158
namespace cv
159
{
160
namespace viz
161
{
162
typedef std::map<String, vtkSmartPointer<vtkProp> > WidgetActorMap;
163
164
struct VizMap
165
{
166
typedef std::map<String, Viz3d> type;
167
typedef type::iterator iterator;
168
169
type m;
170
~VizMap();
171
void replace_clear();
172
};
173
174
class VizStorage
175
{
176
public:
177
static void unregisterAll();
178
179
//! window names automatically have Viz - prefix even though not provided by the users
180
static String generateWindowName(const String &window_name);
181
182
private:
183
VizStorage(); // Static
184
185
static void add(const Viz3d& window);
186
static Viz3d& get(const String &window_name);
187
static void remove(const String &window_name);
188
static bool windowExists(const String &window_name);
189
static void removeUnreferenced();
190
191
static VizMap storage;
192
friend class Viz3d;
193
194
static VizStorage init;
195
};
196
197
template<typename _Tp> inline _Tp normalized(const _Tp& v) { return v * 1/norm(v); }
198
199
template<typename _Tp> inline bool isNan(const _Tp* data)
200
{
201
return isNan(data[0]) || isNan(data[1]) || isNan(data[2]);
202
}
203
204
inline vtkSmartPointer<vtkActor> getActor(const Widget3D& widget)
205
{
206
return vtkActor::SafeDownCast(WidgetAccessor::getProp(widget));
207
}
208
209
inline vtkSmartPointer<vtkPolyData> getPolyData(const Widget3D& widget)
210
{
211
vtkSmartPointer<vtkMapper> mapper = getActor(widget)->GetMapper();
212
return vtkPolyData::SafeDownCast(mapper->GetInput());
213
}
214
215
inline vtkSmartPointer<vtkMatrix4x4> vtkmatrix(const cv::Matx44d &matrix)
216
{
217
vtkSmartPointer<vtkMatrix4x4> vtk_matrix = vtkSmartPointer<vtkMatrix4x4>::New();
218
vtk_matrix->DeepCopy(matrix.val);
219
return vtk_matrix;
220
}
221
222
inline Color vtkcolor(const Color& color)
223
{
224
Color scaled_color = color * (1.0/255.0);
225
std::swap(scaled_color[0], scaled_color[2]);
226
return scaled_color;
227
}
228
229
inline Vec3d get_random_vec(double from = -10.0, double to = 10.0)
230
{
231
RNG& rng = theRNG();
232
return Vec3d(rng.uniform(from, to), rng.uniform(from, to), rng.uniform(from, to));
233
}
234
235
struct VtkUtils
236
{
237
template<class Filter>
238
static void SetInputData(vtkSmartPointer<Filter> filter, vtkPolyData* polydata)
239
{
240
#if VTK_MAJOR_VERSION <= 5
241
filter->SetInput(polydata);
242
#else
243
filter->SetInputData(polydata);
244
#endif
245
}
246
template<class Filter>
247
static void SetSourceData(vtkSmartPointer<Filter> filter, vtkPolyData* polydata)
248
{
249
#if VTK_MAJOR_VERSION <= 5
250
filter->SetSource(polydata);
251
#else
252
filter->SetSourceData(polydata);
253
#endif
254
}
255
256
template<class Filter>
257
static void SetInputData(vtkSmartPointer<Filter> filter, vtkImageData* polydata)
258
{
259
#if VTK_MAJOR_VERSION <= 5
260
filter->SetInput(polydata);
261
#else
262
filter->SetInputData(polydata);
263
#endif
264
}
265
266
template<class Filter>
267
static void AddInputData(vtkSmartPointer<Filter> filter, vtkPolyData *polydata)
268
{
269
#if VTK_MAJOR_VERSION <= 5
270
filter->AddInput(polydata);
271
#else
272
filter->AddInputData(polydata);
273
#endif
274
}
275
276
static vtkSmartPointer<vtkUnsignedCharArray> FillScalars(size_t size, const Color& color)
277
{
278
Vec3b rgb = Vec3d(color[2], color[1], color[0]);
279
Vec3b* color_data = new Vec3b[size];
280
std::fill(color_data, color_data + size, rgb);
281
282
vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
283
scalars->SetName("Colors");
284
scalars->SetNumberOfComponents(3);
285
scalars->SetNumberOfTuples((vtkIdType)size);
286
scalars->SetArray(color_data->val, (vtkIdType)(size * 3), 0, vtkUnsignedCharArray::VTK_DATA_ARRAY_DELETE);
287
return scalars;
288
}
289
290
static vtkSmartPointer<vtkPolyData> FillScalars(vtkSmartPointer<vtkPolyData> polydata, const Color& color)
291
{
292
return polydata->GetPointData()->SetScalars(FillScalars(polydata->GetNumberOfPoints(), color)), polydata;
293
}
294
295
static vtkSmartPointer<vtkPolyData> ComputeNormals(vtkSmartPointer<vtkPolyData> polydata)
296
{
297
vtkSmartPointer<vtkPolyDataNormals> normals_generator = vtkSmartPointer<vtkPolyDataNormals>::New();
298
normals_generator->ComputePointNormalsOn();
299
normals_generator->ComputeCellNormalsOff();
300
normals_generator->SetFeatureAngle(0.1);
301
normals_generator->SetSplitting(0);
302
normals_generator->SetConsistency(1);
303
normals_generator->SetAutoOrientNormals(0);
304
normals_generator->SetFlipNormals(0);
305
normals_generator->SetNonManifoldTraversal(1);
306
VtkUtils::SetInputData(normals_generator, polydata);
307
normals_generator->Update();
308
return normals_generator->GetOutput();
309
}
310
311
static vtkSmartPointer<vtkPolyData> TransformPolydata(vtkSmartPointer<vtkAlgorithmOutput> algorithm_output_port, const Affine3d& pose)
312
{
313
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
314
transform->SetMatrix(vtkmatrix(pose.matrix));
315
316
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
317
transform_filter->SetTransform(transform);
318
transform_filter->SetInputConnection(algorithm_output_port);
319
transform_filter->Update();
320
return transform_filter->GetOutput();
321
}
322
323
static vtkSmartPointer<vtkPolyData> TransformPolydata(vtkSmartPointer<vtkPolyData> polydata, const Affine3d& pose)
324
{
325
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
326
transform->SetMatrix(vtkmatrix(pose.matrix));
327
328
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
329
VtkUtils::SetInputData(transform_filter, polydata);
330
transform_filter->SetTransform(transform);
331
transform_filter->Update();
332
return transform_filter->GetOutput();
333
}
334
};
335
336
vtkSmartPointer<vtkRenderWindowInteractor> vtkCocoaRenderWindowInteractorNew();
337
}
338
}
339
340
#include "vtk/vtkVizInteractorStyle.hpp"
341
#include "vizimpl.hpp"
342
343
#endif
344
345