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, ozantonkal@gmail.com 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 <vtkTextureMapToPlane.h> 108 #include <vtkPolyDataNormals.h> 109 #include <vtkAlgorithmOutput.h> 110 #include <vtkImageMapper.h> 111 #include <vtkPoints.h> 112 #include <vtkInformation.h> 113 #include <vtkInformationVector.h> 114 #include <vtkObjectFactory.h> 115 #include <vtkPolyDataAlgorithm.h> 116 #include <vtkMergeFilter.h> 117 #include <vtkErrorCode.h> 118 #include <vtkPLYWriter.h> 119 #include <vtkSTLWriter.h> 120 #include <vtkPLYReader.h> 121 #include <vtkOBJReader.h> 122 #include <vtkSTLReader.h> 123 #include <vtkPNGReader.h> 124 #include <vtkOBJExporter.h> 125 #include <vtkVRMLExporter.h> 126 #include <vtkTensorGlyph.h> 127 #include <vtkImageAlgorithm.h> 128 #include <vtkTransformFilter.h> 129 #include <vtkConeSource.h> 130 #include <vtkElevationFilter.h> 131 #include <vtkColorTransferFunction.h> 132 #include <vtkStreamingDemandDrivenPipeline.h> 133 #include "vtkCallbackCommand.h" 134 135 #if !defined(_WIN32) || defined(__CYGWIN__) 136 # include <unistd.h> /* unlink */ 137 #else 138 # include <io.h> /* unlink */ 139 #endif 140 141 #include <vtk/vtkOBJWriter.h> 142 #include <vtk/vtkXYZWriter.h> 143 #include <vtk/vtkXYZReader.h> 144 #include <vtk/vtkCloudMatSink.h> 145 #include <vtk/vtkCloudMatSource.h> 146 #include <vtk/vtkTrajectorySource.h> 147 #include <vtk/vtkImageMatSource.h> 148 149 150 #include <opencv2/core.hpp> 151 #include <opencv2/viz.hpp> 152 #include <opencv2/viz/widget_accessor.hpp> 153 #include <opencv2/core/utility.hpp> 154 155 156 namespace cv 157 { 158 namespace viz 159 { 160 typedef std::map<String, vtkSmartPointer<vtkProp> > WidgetActorMap; 161 162 struct VizMap 163 { 164 typedef std::map<String, Viz3d> type; 165 typedef type::iterator iterator; 166 167 type m; 168 ~VizMap(); 169 void replace_clear(); 170 }; 171 172 class VizStorage 173 { 174 public: 175 static void unregisterAll(); 176 177 //! window names automatically have Viz - prefix even though not provided by the users 178 static String generateWindowName(const String &window_name); 179 180 private: 181 VizStorage(); // Static 182 183 static void add(const Viz3d& window); 184 static Viz3d& get(const String &window_name); 185 static void remove(const String &window_name); 186 static bool windowExists(const String &window_name); 187 static void removeUnreferenced(); 188 189 static VizMap storage; 190 friend class Viz3d; 191 192 static VizStorage init; 193 }; 194 normalized(const _Tp & v)195 template<typename _Tp> inline _Tp normalized(const _Tp& v) { return v * 1/norm(v); } 196 isNan(const _Tp * data)197 template<typename _Tp> inline bool isNan(const _Tp* data) 198 { 199 return isNan(data[0]) || isNan(data[1]) || isNan(data[2]); 200 } 201 getActor(const Widget3D & widget)202 inline vtkSmartPointer<vtkActor> getActor(const Widget3D& widget) 203 { 204 return vtkActor::SafeDownCast(WidgetAccessor::getProp(widget)); 205 } 206 getPolyData(const Widget3D & widget)207 inline vtkSmartPointer<vtkPolyData> getPolyData(const Widget3D& widget) 208 { 209 vtkSmartPointer<vtkMapper> mapper = getActor(widget)->GetMapper(); 210 return vtkPolyData::SafeDownCast(mapper->GetInput()); 211 } 212 vtkmatrix(const cv::Matx44d & matrix)213 inline vtkSmartPointer<vtkMatrix4x4> vtkmatrix(const cv::Matx44d &matrix) 214 { 215 vtkSmartPointer<vtkMatrix4x4> vtk_matrix = vtkSmartPointer<vtkMatrix4x4>::New(); 216 vtk_matrix->DeepCopy(matrix.val); 217 return vtk_matrix; 218 } 219 vtkcolor(const Color & color)220 inline Color vtkcolor(const Color& color) 221 { 222 Color scaled_color = color * (1.0/255.0); 223 std::swap(scaled_color[0], scaled_color[2]); 224 return scaled_color; 225 } 226 get_random_vec(double from=-10.0,double to=10.0)227 inline Vec3d get_random_vec(double from = -10.0, double to = 10.0) 228 { 229 RNG& rng = theRNG(); 230 return Vec3d(rng.uniform(from, to), rng.uniform(from, to), rng.uniform(from, to)); 231 } 232 233 struct VtkUtils 234 { 235 template<class Filter> SetInputDatacv::viz::VtkUtils236 static void SetInputData(vtkSmartPointer<Filter> filter, vtkPolyData* polydata) 237 { 238 #if VTK_MAJOR_VERSION <= 5 239 filter->SetInput(polydata); 240 #else 241 filter->SetInputData(polydata); 242 #endif 243 } 244 template<class Filter> SetSourceDatacv::viz::VtkUtils245 static void SetSourceData(vtkSmartPointer<Filter> filter, vtkPolyData* polydata) 246 { 247 #if VTK_MAJOR_VERSION <= 5 248 filter->SetSource(polydata); 249 #else 250 filter->SetSourceData(polydata); 251 #endif 252 } 253 254 template<class Filter> SetInputDatacv::viz::VtkUtils255 static void SetInputData(vtkSmartPointer<Filter> filter, vtkImageData* polydata) 256 { 257 #if VTK_MAJOR_VERSION <= 5 258 filter->SetInput(polydata); 259 #else 260 filter->SetInputData(polydata); 261 #endif 262 } 263 264 template<class Filter> AddInputDatacv::viz::VtkUtils265 static void AddInputData(vtkSmartPointer<Filter> filter, vtkPolyData *polydata) 266 { 267 #if VTK_MAJOR_VERSION <= 5 268 filter->AddInput(polydata); 269 #else 270 filter->AddInputData(polydata); 271 #endif 272 } 273 FillScalarscv::viz::VtkUtils274 static vtkSmartPointer<vtkUnsignedCharArray> FillScalars(size_t size, const Color& color) 275 { 276 Vec3b rgb = Vec3d(color[2], color[1], color[0]); 277 Vec3b* color_data = new Vec3b[size]; 278 std::fill(color_data, color_data + size, rgb); 279 280 vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New(); 281 scalars->SetName("Colors"); 282 scalars->SetNumberOfComponents(3); 283 scalars->SetNumberOfTuples((vtkIdType)size); 284 scalars->SetArray(color_data->val, (vtkIdType)(size * 3), 0); 285 return scalars; 286 } 287 FillScalarscv::viz::VtkUtils288 static vtkSmartPointer<vtkPolyData> FillScalars(vtkSmartPointer<vtkPolyData> polydata, const Color& color) 289 { 290 return polydata->GetPointData()->SetScalars(FillScalars(polydata->GetNumberOfPoints(), color)), polydata; 291 } 292 ComputeNormalscv::viz::VtkUtils293 static vtkSmartPointer<vtkPolyData> ComputeNormals(vtkSmartPointer<vtkPolyData> polydata) 294 { 295 vtkSmartPointer<vtkPolyDataNormals> normals_generator = vtkSmartPointer<vtkPolyDataNormals>::New(); 296 normals_generator->ComputePointNormalsOn(); 297 normals_generator->ComputeCellNormalsOff(); 298 normals_generator->SetFeatureAngle(0.1); 299 normals_generator->SetSplitting(0); 300 normals_generator->SetConsistency(1); 301 normals_generator->SetAutoOrientNormals(0); 302 normals_generator->SetFlipNormals(0); 303 normals_generator->SetNonManifoldTraversal(1); 304 VtkUtils::SetInputData(normals_generator, polydata); 305 normals_generator->Update(); 306 return normals_generator->GetOutput(); 307 } 308 TransformPolydatacv::viz::VtkUtils309 static vtkSmartPointer<vtkPolyData> TransformPolydata(vtkSmartPointer<vtkAlgorithmOutput> algorithm_output_port, const Affine3d& pose) 310 { 311 vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New(); 312 transform->SetMatrix(vtkmatrix(pose.matrix)); 313 314 vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New(); 315 transform_filter->SetTransform(transform); 316 transform_filter->SetInputConnection(algorithm_output_port); 317 transform_filter->Update(); 318 return transform_filter->GetOutput(); 319 } 320 TransformPolydatacv::viz::VtkUtils321 static vtkSmartPointer<vtkPolyData> TransformPolydata(vtkSmartPointer<vtkPolyData> polydata, const Affine3d& pose) 322 { 323 vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New(); 324 transform->SetMatrix(vtkmatrix(pose.matrix)); 325 326 vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New(); 327 VtkUtils::SetInputData(transform_filter, polydata); 328 transform_filter->SetTransform(transform); 329 transform_filter->Update(); 330 return transform_filter->GetOutput(); 331 } 332 }; 333 334 vtkSmartPointer<vtkRenderWindowInteractor> vtkCocoaRenderWindowInteractorNew(); 335 } 336 } 337 338 #include "vtk/vtkVizInteractorStyle.hpp" 339 #include "vizimpl.hpp" 340 341 #endif 342