Home | History | Annotate | Download | only in vtk
      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 //  * Anatoly Baksheev, Itseez Inc.  myname.mysurname <> mycompany.com
     42 //
     43 //M*/
     44 
     45 #include "precomp.hpp"
     46 
     47 namespace cv { namespace viz
     48 {
     49     vtkStandardNewMacro(vtkCloudMatSink);
     50 }}
     51 
     52 cv::viz::vtkCloudMatSink::vtkCloudMatSink() {}
     53 cv::viz::vtkCloudMatSink::~vtkCloudMatSink() {}
     54 
     55 void cv::viz::vtkCloudMatSink::SetOutput(OutputArray _cloud, OutputArray _colors, OutputArray _normals, OutputArray _tcoords)
     56 {
     57     cloud = _cloud;
     58     colors = _colors;
     59     normals = _normals;
     60     tcoords = _tcoords;
     61 }
     62 
     63 void cv::viz::vtkCloudMatSink::WriteData()
     64 {
     65     vtkPolyData *input = this->GetInput();
     66     if (!input)
     67         return;
     68 
     69     vtkSmartPointer<vtkPoints> points_Data = input->GetPoints();
     70 
     71     if (cloud.needed() && points_Data)
     72     {
     73         int vtktype = points_Data->GetDataType();
     74         CV_Assert(vtktype == VTK_FLOAT || vtktype == VTK_DOUBLE);
     75 
     76         cloud.create(1, points_Data->GetNumberOfPoints(), vtktype == VTK_FLOAT ? CV_32FC3 : CV_64FC3);
     77         Vec3d *ddata = cloud.getMat().ptr<Vec3d>();
     78         Vec3f *fdata = cloud.getMat().ptr<Vec3f>();
     79 
     80         if (cloud.depth() == CV_32F)
     81             for(size_t i = 0; i < cloud.total(); ++i)
     82                 *fdata++ = Vec3d(points_Data->GetPoint((vtkIdType)i));
     83 
     84         if (cloud.depth() == CV_64F)
     85             for(size_t i = 0; i < cloud.total(); ++i)
     86                 *ddata++ = Vec3d(points_Data->GetPoint((vtkIdType)i));
     87     }
     88     else
     89         cloud.release();
     90 
     91     vtkSmartPointer<vtkDataArray> scalars_data = input->GetPointData() ? input->GetPointData()->GetScalars() : 0;
     92 
     93     if (colors.needed() && scalars_data)
     94     {
     95         int channels = scalars_data->GetNumberOfComponents();
     96         int vtktype = scalars_data->GetDataType();
     97 
     98         CV_Assert((channels == 3 || channels == 4) && "Only 3- or 4-channel color data support is implemented");
     99         CV_Assert(cloud.total() == (size_t)scalars_data->GetNumberOfTuples());
    100 
    101         Mat buffer(cloud.size(), CV_64FC(channels));
    102         Vec3d *cptr = buffer.ptr<Vec3d>();
    103         for(size_t i = 0; i < buffer.total(); ++i)
    104             *cptr++ = Vec3d(scalars_data->GetTuple((vtkIdType)i));
    105 
    106         buffer.convertTo(colors, CV_8U, vtktype == VTK_FLOAT || VTK_FLOAT == VTK_DOUBLE ?  255.0 : 1.0);
    107     }
    108     else
    109         colors.release();
    110 
    111     vtkSmartPointer<vtkDataArray> normals_data = input->GetPointData() ? input->GetPointData()->GetNormals() : 0;
    112 
    113     if (normals.needed() && normals_data)
    114     {
    115         int channels = normals_data->GetNumberOfComponents();
    116         int vtktype = normals_data->GetDataType();
    117 
    118         CV_Assert((vtktype == VTK_FLOAT || VTK_FLOAT == VTK_DOUBLE) && (channels == 3 || channels == 4));
    119         CV_Assert(cloud.total() == (size_t)normals_data->GetNumberOfTuples());
    120 
    121         Mat buffer(cloud.size(), CV_64FC(channels));
    122         Vec3d *cptr = buffer.ptr<Vec3d>();
    123         for(size_t i = 0; i < buffer.total(); ++i)
    124             *cptr++ = Vec3d(normals_data->GetTuple((vtkIdType)i));
    125 
    126         buffer.convertTo(normals, vtktype == VTK_FLOAT ? CV_32F : CV_64F);
    127     }
    128     else
    129         normals.release();
    130 
    131     vtkSmartPointer<vtkDataArray> coords_data = input->GetPointData() ? input->GetPointData()->GetTCoords() : 0;
    132 
    133     if (tcoords.needed() && coords_data)
    134     {
    135         int vtktype = coords_data->GetDataType();
    136 
    137         CV_Assert(vtktype == VTK_FLOAT || VTK_FLOAT == VTK_DOUBLE);
    138         CV_Assert(cloud.total() == (size_t)coords_data->GetNumberOfTuples());
    139 
    140         Mat buffer(cloud.size(), CV_64FC2);
    141         Vec2d *cptr = buffer.ptr<Vec2d>();
    142         for(size_t i = 0; i < buffer.total(); ++i)
    143             *cptr++ = Vec2d(coords_data->GetTuple((vtkIdType)i));
    144 
    145         buffer.convertTo(tcoords, vtktype == VTK_FLOAT ? CV_32F : CV_64F);
    146 
    147     }
    148     else
    149         tcoords.release();
    150 }
    151 
    152 void cv::viz::vtkCloudMatSink::PrintSelf(ostream& os, vtkIndent indent)
    153 {
    154   Superclass::PrintSelf(os, indent);
    155   os << indent << "Cloud: " << cloud.needed() << "\n";
    156   os << indent << "Colors: " << colors.needed() << "\n";
    157   os << indent << "Normals: " << normals.needed() << "\n";
    158 }
    159 
    160 int cv::viz::vtkCloudMatSink::FillInputPortInformation(int, vtkInformation *info)
    161 {
    162     info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
    163     return 1;
    164 }
    165 
    166 vtkPolyData* cv::viz::vtkCloudMatSink::GetInput()
    167 {
    168     return vtkPolyData::SafeDownCast(this->Superclass::GetInput());
    169 }
    170 
    171 vtkPolyData* cv::viz::vtkCloudMatSink::GetInput(int port)
    172 {
    173     return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port));
    174 }
    175