Path: blob/master/modules/viz/src/vtk/vtkCloudMatSink.cpp
16358 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39// Authors:40// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com41//42//M*/4344#include "../precomp.hpp"4546namespace cv { namespace viz47{48vtkStandardNewMacro(vtkCloudMatSink);49}}5051cv::viz::vtkCloudMatSink::vtkCloudMatSink() {}52cv::viz::vtkCloudMatSink::~vtkCloudMatSink() {}5354void cv::viz::vtkCloudMatSink::SetOutput(OutputArray _cloud, OutputArray _colors, OutputArray _normals, OutputArray _tcoords)55{56cloud = _cloud;57colors = _colors;58normals = _normals;59tcoords = _tcoords;60}6162void cv::viz::vtkCloudMatSink::WriteData()63{64vtkPolyData *input = this->GetInput();65if (!input)66return;6768vtkSmartPointer<vtkPoints> points_Data = input->GetPoints();6970if (cloud.needed() && points_Data)71{72int vtktype = points_Data->GetDataType();73CV_Assert(vtktype == VTK_FLOAT || vtktype == VTK_DOUBLE);7475cloud.create(1, points_Data->GetNumberOfPoints(), vtktype == VTK_FLOAT ? CV_32FC3 : CV_64FC3);76Vec3d *ddata = cloud.getMat().ptr<Vec3d>();77Vec3f *fdata = cloud.getMat().ptr<Vec3f>();7879if (cloud.depth() == CV_32F)80for(size_t i = 0; i < cloud.total(); ++i)81*fdata++ = Vec3d(points_Data->GetPoint((vtkIdType)i));8283if (cloud.depth() == CV_64F)84for(size_t i = 0; i < cloud.total(); ++i)85*ddata++ = Vec3d(points_Data->GetPoint((vtkIdType)i));86}87else88cloud.release();8990vtkSmartPointer<vtkDataArray> scalars_data = input->GetPointData() ? input->GetPointData()->GetScalars() : 0;9192if (colors.needed() && scalars_data)93{94int channels = scalars_data->GetNumberOfComponents();95int vtktype = scalars_data->GetDataType();9697CV_Assert((channels == 3 || channels == 4) && "Only 3- or 4-channel color data support is implemented");98CV_Assert(cloud.total() == (size_t)scalars_data->GetNumberOfTuples());99100Mat buffer(cloud.size(), CV_64FC(channels));101Vec3d *cptr = buffer.ptr<Vec3d>();102for(size_t i = 0; i < buffer.total(); ++i)103*cptr++ = Vec3d(scalars_data->GetTuple((vtkIdType)i));104105buffer.convertTo(colors, CV_8U, vtktype == VTK_FLOAT || VTK_FLOAT == VTK_DOUBLE ? 255.0 : 1.0);106}107else108colors.release();109110vtkSmartPointer<vtkDataArray> normals_data = input->GetPointData() ? input->GetPointData()->GetNormals() : 0;111112if (normals.needed() && normals_data)113{114int channels = normals_data->GetNumberOfComponents();115int vtktype = normals_data->GetDataType();116117CV_Assert((vtktype == VTK_FLOAT || VTK_FLOAT == VTK_DOUBLE) && (channels == 3 || channels == 4));118CV_Assert(cloud.total() == (size_t)normals_data->GetNumberOfTuples());119120Mat buffer(cloud.size(), CV_64FC(channels));121Vec3d *cptr = buffer.ptr<Vec3d>();122for(size_t i = 0; i < buffer.total(); ++i)123*cptr++ = Vec3d(normals_data->GetTuple((vtkIdType)i));124125buffer.convertTo(normals, vtktype == VTK_FLOAT ? CV_32F : CV_64F);126}127else128normals.release();129130vtkSmartPointer<vtkDataArray> coords_data = input->GetPointData() ? input->GetPointData()->GetTCoords() : 0;131132if (tcoords.needed() && coords_data)133{134int vtktype = coords_data->GetDataType();135136CV_Assert(vtktype == VTK_FLOAT || VTK_FLOAT == VTK_DOUBLE);137CV_Assert(cloud.total() == (size_t)coords_data->GetNumberOfTuples());138139Mat buffer(cloud.size(), CV_64FC2);140Vec2d *cptr = buffer.ptr<Vec2d>();141for(size_t i = 0; i < buffer.total(); ++i)142*cptr++ = Vec2d(coords_data->GetTuple((vtkIdType)i));143144buffer.convertTo(tcoords, vtktype == VTK_FLOAT ? CV_32F : CV_64F);145146}147else148tcoords.release();149}150151void cv::viz::vtkCloudMatSink::PrintSelf(ostream& os, vtkIndent indent)152{153Superclass::PrintSelf(os, indent);154os << indent << "Cloud: " << cloud.needed() << "\n";155os << indent << "Colors: " << colors.needed() << "\n";156os << indent << "Normals: " << normals.needed() << "\n";157}158159int cv::viz::vtkCloudMatSink::FillInputPortInformation(int, vtkInformation *info)160{161info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");162return 1;163}164165vtkPolyData* cv::viz::vtkCloudMatSink::GetInput()166{167return vtkPolyData::SafeDownCast(this->Superclass::GetInput());168}169170vtkPolyData* cv::viz::vtkCloudMatSink::GetInput(int port)171{172return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port));173}174175176