precomp.hpp 12.1 KB
Newer Older
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
// Authors:
//  * Ozan Tonkal, ozantonkal@gmail.com
//  * Anatoly Baksheev, Itseez Inc.  myname.mysurname <> mycompany.com
//
//M*/

#ifndef __OPENCV_VIZ_PRECOMP_HPP__
#define __OPENCV_VIZ_PRECOMP_HPP__

#include <map>
#include <ctime>
#include <list>
#include <vector>
#include <iomanip>
#include <limits>

56
#include <vtkVersionMacros.h>
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
#include <vtkAppendPolyData.h>
#include <vtkAssemblyPath.h>
#include <vtkCellData.h>
#include <vtkLineSource.h>
#include <vtkPropPicker.h>
#include <vtkSmartPointer.h>
#include <vtkDataSet.h>
#include <vtkPolygon.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDiskSource.h>
#include <vtkPlaneSource.h>
#include <vtkSphereSource.h>
#include <vtkArrowSource.h>
#include <vtkOutlineSource.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkTubeFilter.h>
#include <vtkCubeSource.h>
#include <vtkAxes.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkCellArray.h>
#include <vtkCommand.h>
#include <vtkPLYReader.h>
#include <vtkPolyLine.h>
#include <vtkVectorText.h>
#include <vtkFollower.h>
#include <vtkInteractorStyle.h>
#include <vtkUnsignedCharArray.h>
#include <vtkRendererCollection.h>
#include <vtkPNGWriter.h>
#include <vtkWindowToImageFilter.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkPlanes.h>
#include <vtkImageFlip.h>
#include <vtkRenderWindow.h>
#include <vtkTextProperty.h>
#include <vtkProperty2D.h>
#include <vtkLODActor.h>
#include <vtkActor.h>
#include <vtkTextActor.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkMath.h>
#include <vtkExtractEdges.h>
#include <vtkFrustumSource.h>
#include <vtkTexture.h>
#include <vtkTextureMapToPlane.h>
#include <vtkPolyDataNormals.h>
#include <vtkAlgorithmOutput.h>
#include <vtkImageMapper.h>
#include <vtkPoints.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkObjectFactory.h>
#include <vtkPolyDataAlgorithm.h>
#include <vtkMergeFilter.h>
#include <vtkErrorCode.h>
#include <vtkPLYWriter.h>
#include <vtkSTLWriter.h>
#include <vtkPLYReader.h>
#include <vtkOBJReader.h>
#include <vtkSTLReader.h>
#include <vtkPNGReader.h>
#include <vtkOBJExporter.h>
#include <vtkVRMLExporter.h>
#include <vtkTensorGlyph.h>
#include <vtkImageAlgorithm.h>
#include <vtkTransformFilter.h>
#include <vtkConeSource.h>
#include <vtkElevationFilter.h>
#include <vtkColorTransferFunction.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkLight.h>
#include "vtkCallbackCommand.h"

#if !defined(_WIN32) || defined(__CYGWIN__)
# include <unistd.h> /* unlink */
#else
# include <io.h> /* unlink */
#endif

#include "vtk/vtkOBJWriter.h"
#include "vtk/vtkXYZWriter.h"
#include "vtk/vtkXYZReader.h"
#include "vtk/vtkCloudMatSink.h"
#include "vtk/vtkCloudMatSource.h"
#include "vtk/vtkTrajectorySource.h"
#include "vtk/vtkImageMatSource.h"


#include <opencv2/core.hpp>
#include <opencv2/viz.hpp>
#include <opencv2/viz/widget_accessor.hpp>
#include <opencv2/core/utility.hpp>


namespace cv
{
    namespace viz
    {
        typedef std::map<String, vtkSmartPointer<vtkProp> > WidgetActorMap;

        struct VizMap
        {
            typedef std::map<String, Viz3d> type;
            typedef type::iterator iterator;

            type m;
            ~VizMap();
            void replace_clear();
        };

        class VizStorage
        {
        public:
            static void unregisterAll();

            //! window names automatically have Viz - prefix even though not provided by the users
            static String generateWindowName(const String &window_name);

        private:
            VizStorage(); // Static

            static void add(const Viz3d& window);
            static Viz3d& get(const String &window_name);
            static void remove(const String &window_name);
            static bool windowExists(const String &window_name);
            static void removeUnreferenced();

            static VizMap storage;
            friend class Viz3d;

            static VizStorage init;
        };

        template<typename _Tp> inline _Tp normalized(const _Tp& v) { return v * 1/norm(v); }

        template<typename _Tp> inline bool isNan(const _Tp* data)
        {
            return isNan(data[0]) || isNan(data[1]) || isNan(data[2]);
        }

        inline vtkSmartPointer<vtkActor> getActor(const Widget3D& widget)
        {
            return vtkActor::SafeDownCast(WidgetAccessor::getProp(widget));
        }

        inline vtkSmartPointer<vtkPolyData> getPolyData(const Widget3D& widget)
        {
            vtkSmartPointer<vtkMapper> mapper = getActor(widget)->GetMapper();
            return vtkPolyData::SafeDownCast(mapper->GetInput());
        }

        inline vtkSmartPointer<vtkMatrix4x4> vtkmatrix(const cv::Matx44d &matrix)
        {
            vtkSmartPointer<vtkMatrix4x4> vtk_matrix = vtkSmartPointer<vtkMatrix4x4>::New();
            vtk_matrix->DeepCopy(matrix.val);
            return vtk_matrix;
        }

        inline Color vtkcolor(const Color& color)
        {
            Color scaled_color = color * (1.0/255.0);
            std::swap(scaled_color[0], scaled_color[2]);
            return scaled_color;
        }

        inline Vec3d get_random_vec(double from = -10.0, double to = 10.0)
        {
            RNG& rng = theRNG();
            return Vec3d(rng.uniform(from, to), rng.uniform(from, to), rng.uniform(from, to));
        }

        struct VtkUtils
        {
            template<class Filter>
            static void SetInputData(vtkSmartPointer<Filter> filter, vtkPolyData* polydata)
            {
            #if VTK_MAJOR_VERSION <= 5
                filter->SetInput(polydata);
            #else
                filter->SetInputData(polydata);
            #endif
            }
            template<class Filter>
            static void SetSourceData(vtkSmartPointer<Filter> filter, vtkPolyData* polydata)
            {
            #if VTK_MAJOR_VERSION <= 5
                filter->SetSource(polydata);
            #else
                filter->SetSourceData(polydata);
            #endif
            }

            template<class Filter>
            static void SetInputData(vtkSmartPointer<Filter> filter, vtkImageData* polydata)
            {
            #if VTK_MAJOR_VERSION <= 5
                filter->SetInput(polydata);
            #else
                filter->SetInputData(polydata);
            #endif
            }

            template<class Filter>
            static void AddInputData(vtkSmartPointer<Filter> filter, vtkPolyData *polydata)
            {
            #if VTK_MAJOR_VERSION <= 5
                filter->AddInput(polydata);
            #else
                filter->AddInputData(polydata);
            #endif
            }

            static vtkSmartPointer<vtkUnsignedCharArray> FillScalars(size_t size, const Color& color)
            {
                Vec3b rgb = Vec3d(color[2], color[1], color[0]);
                Vec3b* color_data = new Vec3b[size];
                std::fill(color_data, color_data + size, rgb);

                vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
                scalars->SetName("Colors");
                scalars->SetNumberOfComponents(3);
                scalars->SetNumberOfTuples((vtkIdType)size);
                scalars->SetArray(color_data->val, (vtkIdType)(size * 3), 0, vtkUnsignedCharArray::VTK_DATA_ARRAY_DELETE);
                return scalars;
            }

            static vtkSmartPointer<vtkPolyData> FillScalars(vtkSmartPointer<vtkPolyData> polydata, const Color& color)
            {
                return polydata->GetPointData()->SetScalars(FillScalars(polydata->GetNumberOfPoints(), color)), polydata;
            }

            static vtkSmartPointer<vtkPolyData> ComputeNormals(vtkSmartPointer<vtkPolyData> polydata)
            {
                vtkSmartPointer<vtkPolyDataNormals> normals_generator = vtkSmartPointer<vtkPolyDataNormals>::New();
                normals_generator->ComputePointNormalsOn();
                normals_generator->ComputeCellNormalsOff();
                normals_generator->SetFeatureAngle(0.1);
                normals_generator->SetSplitting(0);
                normals_generator->SetConsistency(1);
                normals_generator->SetAutoOrientNormals(0);
                normals_generator->SetFlipNormals(0);
                normals_generator->SetNonManifoldTraversal(1);
                VtkUtils::SetInputData(normals_generator, polydata);
                normals_generator->Update();
                return normals_generator->GetOutput();
            }

            static vtkSmartPointer<vtkPolyData> TransformPolydata(vtkSmartPointer<vtkAlgorithmOutput> algorithm_output_port, const Affine3d& pose)
            {
                vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
                transform->SetMatrix(vtkmatrix(pose.matrix));

                vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
                transform_filter->SetTransform(transform);
                transform_filter->SetInputConnection(algorithm_output_port);
                transform_filter->Update();
                return transform_filter->GetOutput();
            }

            static vtkSmartPointer<vtkPolyData> TransformPolydata(vtkSmartPointer<vtkPolyData> polydata, const Affine3d& pose)
            {
                vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
                transform->SetMatrix(vtkmatrix(pose.matrix));

                vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
                VtkUtils::SetInputData(transform_filter, polydata);
                transform_filter->SetTransform(transform);
                transform_filter->Update();
                return transform_filter->GetOutput();
            }
        };

        vtkSmartPointer<vtkRenderWindowInteractor> vtkCocoaRenderWindowInteractorNew();
    }
}

#include "vtk/vtkVizInteractorStyle.hpp"
#include "vizimpl.hpp"

#endif