From 7dbf6bc8df019240ff6f07bcb52e4d078c9a087c Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Thu, 12 Dec 2013 23:21:55 +0400 Subject: [PATCH] rewrote cloud widget in vtk filters style Conflicts: modules/viz/src/clouds.cpp --- modules/viz/src/clouds.cpp | 153 ++-------------- modules/viz/src/precomp.hpp | 4 + .../viz/src/vtk/vtkCloudColorMatSource.cpp | 168 ++++++++++++++++++ modules/viz/src/vtk/vtkCloudColorMatSource.h | 88 +++++++++ modules/viz/src/vtk/vtkCloudMatSource.cpp | 105 +++++++++++ modules/viz/src/vtk/vtkCloudMatSource.h | 83 +++++++++ modules/viz/src/vtk/vtkColorMatSource.cpp | 132 ++++++++++++++ modules/viz/src/vtk/vtkColorMatSource.h | 82 +++++++++ 8 files changed, 675 insertions(+), 140 deletions(-) create mode 100644 modules/viz/src/vtk/vtkCloudColorMatSource.cpp create mode 100644 modules/viz/src/vtk/vtkCloudColorMatSource.h create mode 100644 modules/viz/src/vtk/vtkCloudMatSource.cpp create mode 100644 modules/viz/src/vtk/vtkCloudMatSource.h create mode 100644 modules/viz/src/vtk/vtkColorMatSource.cpp create mode 100644 modules/viz/src/vtk/vtkColorMatSource.h diff --git a/modules/viz/src/clouds.cpp b/modules/viz/src/clouds.cpp index ec83676eef..3d8105c3f8 100644 --- a/modules/viz/src/clouds.cpp +++ b/modules/viz/src/clouds.cpp @@ -59,103 +59,10 @@ namespace cv /////////////////////////////////////////////////////////////////////////////////////////////// /// Point Cloud Widget implementation -namespace cv { namespace viz { namespace -{ - struct CloudUtils - { - static inline vtkSmartPointer create(const Mat &cloud, vtkIdType &nr_points) - { - vtkSmartPointer polydata = vtkSmartPointer::New(); - vtkSmartPointer vertices = vtkSmartPointer::New(); - - polydata->SetVerts(vertices); - - vtkSmartPointer points = polydata->GetPoints(); - vtkSmartPointer initcells; - nr_points = cloud.total(); - - if (!points) - { - points = vtkSmartPointer::New(); - if (cloud.depth() == CV_32F) - points->SetDataTypeToFloat(); - else if (cloud.depth() == CV_64F) - points->SetDataTypeToDouble(); - polydata->SetPoints(points); - } - points->SetNumberOfPoints(nr_points); - - if (cloud.depth() == CV_32F) - { - // Get a pointer to the beginning of the data array - Vec3f *data_beg = vtkpoints_data(points); - Vec3f *data_end = NanFilter::copy(cloud, data_beg, cloud); - nr_points = data_end - data_beg; - } - else if (cloud.depth() == CV_64F) - { - // Get a pointer to the beginning of the data array - Vec3d *data_beg = vtkpoints_data(points); - Vec3d *data_end = NanFilter::copy(cloud, data_beg, cloud); - nr_points = data_end - data_beg; - } - points->SetNumberOfPoints(nr_points); - - // Update cells - vtkSmartPointer cells = vertices->GetData(); - // If no init cells and cells has not been initialized... - if (!cells) - cells = vtkSmartPointer::New(); - - // If we have less values then we need to recreate the array - if (cells->GetNumberOfTuples() < nr_points) - { - cells = vtkSmartPointer::New(); - - // If init cells is given, and there's enough data in it, use it - if (initcells && initcells->GetNumberOfTuples() >= nr_points) - { - cells->DeepCopy(initcells); - cells->SetNumberOfComponents(2); - cells->SetNumberOfTuples(nr_points); - } - else - { - // If the number of tuples is still too small, we need to recreate the array - cells->SetNumberOfComponents(2); - cells->SetNumberOfTuples(nr_points); - vtkIdType *cell = cells->GetPointer(0); - // Fill it with 1s - std::fill(cell, cell + nr_points * 2, 1); - cell++; - for (vtkIdType i = 0; i < nr_points; ++i, cell += 2) - *cell = i; - // Save the results in initcells - initcells = vtkSmartPointer::New(); - initcells->DeepCopy(cells); - } - } - else - { - // The assumption here is that the current set of cells has more data than needed - cells->SetNumberOfComponents(2); - cells->SetNumberOfTuples(nr_points); - } - - // Set the cells and the vertices - vertices->SetCells(nr_points, cells); - return polydata; - } - }; -}}} - - cv::viz::WCloud::WCloud(InputArray _cloud, InputArray _colors) { Mat cloud = _cloud.getMat(); Mat colors = _colors.getMat(); - CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); - CV_Assert(colors.depth() == CV_8U && cloud.size() == colors.size()); if (cloud.isContinuous() && colors.isContinuous()) { @@ -163,41 +70,18 @@ cv::viz::WCloud::WCloud(InputArray _cloud, InputArray _colors) colors = colors.reshape(colors.channels(), 1); } - vtkIdType nr_points; - vtkSmartPointer polydata = CloudUtils::create(cloud, nr_points); - - // Filter colors - Vec3b* colors_data = new Vec3b[nr_points]; - NanFilter::copyColor(colors, colors_data, cloud); - - vtkSmartPointer scalars = vtkSmartPointer::New(); - scalars->SetNumberOfComponents(3); - scalars->SetNumberOfTuples(nr_points); - scalars->SetArray(colors_data->val, 3 * nr_points, 0); - - // Assign the colors - polydata->GetPointData()->SetScalars(scalars); + vtkSmartPointer cloud_source = vtkSmartPointer::New(); + cloud_source->SetCloud(cloud); + cloud_source->SetColors(colors, cloud); vtkSmartPointer mapper = vtkSmartPointer::New(); -#if VTK_MAJOR_VERSION <= 5 - mapper->SetInput(polydata); -#else - mapper->SetInputData(polydata); -#endif - - Vec2d minmax(scalars->GetRange()); - mapper->SetScalarRange(minmax.val); + mapper->SetInputConnection(cloud_source->GetOutputPort()); mapper->SetScalarModeToUsePointData(); - - bool interpolation = (polydata && polydata->GetNumberOfCells() != polydata->GetNumberOfVerts()); - - mapper->SetInterpolateScalarsBeforeMapping(interpolation); + mapper->ImmediateModeRenderingOff(); + mapper->SetScalarRange(0, 255); mapper->ScalarVisibilityOn(); - mapper->ImmediateModeRenderingOff(); - - vtkSmartPointer actor = vtkSmartPointer::New(); - actor->SetNumberOfCloudPoints(int(std::max(1, polydata->GetNumberOfPoints() / 10))); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->GetProperty()->SetInterpolationToFlat(); actor->GetProperty()->BackfaceCullingOn(); actor->SetMapper(mapper); @@ -208,27 +92,16 @@ cv::viz::WCloud::WCloud(InputArray _cloud, InputArray _colors) cv::viz::WCloud::WCloud(InputArray _cloud, const Color &color) { Mat cloud = _cloud.getMat(); - CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); - vtkIdType nr_points; - vtkSmartPointer polydata = CloudUtils::create(cloud, nr_points); + vtkSmartPointer cloud_source = vtkSmartPointer::New(); + cloud_source->SetCloud(cloud); vtkSmartPointer mapper = vtkSmartPointer::New(); -#if VTK_MAJOR_VERSION <= 5 - mapper->SetInput(polydata); -#else - mapper->SetInputData(polydata); -#endif - - bool interpolation = (polydata && polydata->GetNumberOfCells() != polydata->GetNumberOfVerts()); - - mapper->SetInterpolateScalarsBeforeMapping(interpolation); + mapper->SetInputConnection(cloud_source->GetOutputPort()); + mapper->ImmediateModeRenderingOff(); mapper->ScalarVisibilityOff(); - mapper->ImmediateModeRenderingOff(); - - vtkSmartPointer actor = vtkSmartPointer::New(); - actor->SetNumberOfCloudPoints(int(std::max(1, polydata->GetNumberOfPoints() / 10))); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->GetProperty()->SetInterpolationToFlat(); actor->GetProperty()->BackfaceCullingOn(); actor->SetMapper(mapper); @@ -435,8 +308,8 @@ void cv::viz::WCloudCollection::addCloud(InputArray _cloud, InputArray _colors, vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert("Incompatible widget type." && actor); - CloudCollectionUtils::createMapper(actor, transform_filter->GetOutput()); + } void cv::viz::WCloudCollection::addCloud(InputArray _cloud, const Color &color, const Affine3f &pose) diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index 24b7e3f97e..7741dc7df5 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -114,6 +114,10 @@ #include #include +#include +#include +#include + #include #include #include diff --git a/modules/viz/src/vtk/vtkCloudColorMatSource.cpp b/modules/viz/src/vtk/vtkCloudColorMatSource.cpp new file mode 100644 index 0000000000..8979873eb3 --- /dev/null +++ b/modules/viz/src/vtk/vtkCloudColorMatSource.cpp @@ -0,0 +1,168 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#include "precomp.hpp" + +namespace cv { namespace viz +{ + vtkStandardNewMacro(vtkCloudColorMatSource); + + struct IsNotNan + { + template + bool operator()(const _Tp* data) const + { + return !isNan(data[0]) && !isNan(data[1]) && !isNan(data[2]); + } + }; +}} + +cv::viz::vtkCloudColorMatSource::vtkCloudColorMatSource() { SetNumberOfInputPorts(0); } +cv::viz::vtkCloudColorMatSource::~vtkCloudColorMatSource() {} + +void cv::viz::vtkCloudColorMatSource::SetCloud(const Mat& cloud) +{ + CV_Assert(cloud.depth() == CV_32F || cloud.depth() == CV_64F); + CV_Assert(cloud.channels() == 3 || cloud.channels() == 4); + + int total = cloud.depth() == CV_32F ? filterNanCopy(cloud, VTK_FLOAT) + : filterNanCopy(cloud, VTK_DOUBLE); + + vertices = vtkSmartPointer::New(); + vertices->Allocate(vertices->EstimateSize(1, total)); + vertices->InsertNextCell(total); + for(int i = 0; i < total; ++i) + vertices->InsertCellPoint(i); +} + +void cv::viz::vtkCloudColorMatSource::SetColors(const Mat &colors, const Mat &cloud_mask) +{ + CV_Assert(colors.depth() == CV_8U && colors.channels() <= 4 && colors.channels() != 2); + CV_Assert(cloud_mask.depth() == CV_32F || cloud_mask.depth() == CV_64F); + CV_Assert(colors.size() == cloud_mask.size()); + + if (cloud_mask.depth() == CV_32F) + filterNanCopy(colors, cloud_mask); + else if (cloud_mask.depth() == CV_64F) + filterNanCopy(colors, cloud_mask); +} + +int cv::viz::vtkCloudColorMatSource::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *outputVector) +{ + vtkInformation *outInfo = outputVector->GetInformationObject(0); + vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())); + + output->SetPoints(points); + output->SetVerts(vertices); + output->GetPointData()->SetScalars(scalars); + return 1; +} + +template +int cv::viz::vtkCloudColorMatSource::filterNanCopy(const Mat& source, int dataType) +{ + CV_DbgAssert(DataType<_Tp>::depth == source.depth()); + points = vtkSmartPointer::New(); + points->SetDataType(dataType); + points->Allocate(source.total()); + points->SetNumberOfPoints(source.total()); + + int cn = source.channels(); + int total = 0; + for (int y = 0; y < source.rows; ++y) + { + const _Tp* srow = source.ptr<_Tp>(y); + const _Tp* send = srow + source.cols * cn; + + for (; srow != send; srow += cn) + if (!isNan(srow[0]) && !isNan(srow[1]) && !isNan(srow[2])) + points->SetPoint(total++, srow); + } + points->SetNumberOfPoints(total); + points->Squeeze(); + return total; +} + +template +void cv::viz::vtkCloudColorMatSource::filterNanCopy(const Mat& colors, const Mat& mask) +{ + Mat buffer(colors.size(), CV_8UC3); + Vec3b* pos = buffer.ptr(); + + int s_chs = colors.channels(); + int m_chs = mask.channels(); + + _NanPred pred; + + for (int y = 0; y < colors.rows; ++y) + { + const unsigned char* srow = colors.ptr(y); + const unsigned char* send = srow + colors.cols * colors.channels(); + const _Msk* mrow = mask.empty() ? 0 : mask.ptr<_Msk>(y); + + if (colors.channels() == 1) + { + for (; srow != send; srow += s_chs, mrow += m_chs) + if (pred(mrow)) + *pos++ = Vec3b(srow[0], srow[0], srow[0]); + } + else + for (; srow != send; srow += s_chs, mrow += m_chs) + if (pred(mrow)) + *pos++ = Vec3b(srow[2], srow[1], srow[0]); + + } + + int total = pos - buffer.ptr(); + Vec3b* array = new Vec3b[total]; + std::copy(buffer.ptr(), pos, array); + + scalars = vtkSmartPointer::New(); + scalars->SetName("colors"); + scalars->SetNumberOfComponents(3); + scalars->SetNumberOfTuples(total); + scalars->SetArray(array->val, total * 3, 0); +} + + diff --git a/modules/viz/src/vtk/vtkCloudColorMatSource.h b/modules/viz/src/vtk/vtkCloudColorMatSource.h new file mode 100644 index 0000000000..0aa5b87a3e --- /dev/null +++ b/modules/viz/src/vtk/vtkCloudColorMatSource.h @@ -0,0 +1,88 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#ifndef __vtkCloudColorMatSource_h +#define __vtkCloudColorMatSource_h + +#include +#include +#include +#include +#include + +namespace cv +{ + namespace viz + { + class vtkCloudColorMatSource : public vtkPolyDataAlgorithm + { + public: + static vtkCloudColorMatSource *New(); + vtkTypeMacro(vtkCloudColorMatSource,vtkPolyDataAlgorithm); + + virtual void SetCloud(const Mat& cloud); + virtual void SetColors(const Mat &colors, const Mat &cloud_mask); + + protected: + vtkCloudColorMatSource(); + ~vtkCloudColorMatSource(); + + int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *); + + vtkSmartPointer points; + vtkSmartPointer vertices; + vtkSmartPointer scalars; + private: + vtkCloudColorMatSource(const vtkCloudColorMatSource&); // Not implemented. + void operator=(const vtkCloudColorMatSource&); // Not implemented. + + template int filterNanCopy(const Mat& source, int dataType); + + template + void filterNanCopy(const Mat& colors, const Mat& mask); + }; + } +} + +#endif diff --git a/modules/viz/src/vtk/vtkCloudMatSource.cpp b/modules/viz/src/vtk/vtkCloudMatSource.cpp new file mode 100644 index 0000000000..a27653a4f6 --- /dev/null +++ b/modules/viz/src/vtk/vtkCloudMatSource.cpp @@ -0,0 +1,105 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#include "precomp.hpp" + +namespace cv { namespace viz +{ + vtkStandardNewMacro(vtkCloudMatSource); +}} + +cv::viz::vtkCloudMatSource::vtkCloudMatSource() { SetNumberOfInputPorts(0); } +cv::viz::vtkCloudMatSource::~vtkCloudMatSource() {} + +void cv::viz::vtkCloudMatSource::SetCloud(const Mat& cloud) +{ + CV_Assert(cloud.depth() == CV_32F || cloud.depth() == CV_64F); + CV_Assert(cloud.channels() == 3 || cloud.channels() == 4); + + int total = cloud.depth() == CV_32F ? filterNanCopy(cloud, VTK_FLOAT) + : filterNanCopy(cloud, VTK_DOUBLE); + + vertices = vtkSmartPointer::New(); + vertices->Allocate(vertices->EstimateSize(1, total)); + vertices->InsertNextCell(total); + for(int i = 0; i < total; ++i) + vertices->InsertCellPoint(i); +} + +int cv::viz::vtkCloudMatSource::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *outputVector) +{ + vtkInformation *outInfo = outputVector->GetInformationObject(0); + vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())); + + output->SetPoints(points); + output->SetVerts(vertices); + return 1; +} + +template +int cv::viz::vtkCloudMatSource::filterNanCopy(const Mat& source, int dataType) +{ + CV_DbgAssert(DataType<_Tp>::depth == source.depth()); + points = vtkSmartPointer::New(); + points->SetDataType(dataType); + points->Allocate(source.total()); + points->SetNumberOfPoints(source.total()); + + int cn = source.channels(); + int total = 0; + for (int y = 0; y < source.rows; ++y) + { + const _Tp* srow = source.ptr<_Tp>(y); + const _Tp* send = srow + source.cols * cn; + + for (; srow != send; srow += cn) + if (!isNan(srow[0]) && !isNan(srow[1]) && !isNan(srow[2])) + points->SetPoint(total++, srow); + } + points->SetNumberOfPoints(total); + points->Squeeze(); + return total; +} + + diff --git a/modules/viz/src/vtk/vtkCloudMatSource.h b/modules/viz/src/vtk/vtkCloudMatSource.h new file mode 100644 index 0000000000..6a8dbebd16 --- /dev/null +++ b/modules/viz/src/vtk/vtkCloudMatSource.h @@ -0,0 +1,83 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#ifndef __vtkCloudMatSource_h +#define __vtkCloudMatSource_h + +#include +#include +#include +#include +#include + +namespace cv +{ + namespace viz + { + class vtkCloudMatSource : public vtkPolyDataAlgorithm + { + public: + static vtkCloudMatSource *New(); + vtkTypeMacro(vtkCloudMatSource,vtkPolyDataAlgorithm); + + virtual void SetCloud(const Mat& cloud); + + protected: + vtkCloudMatSource(); + ~vtkCloudMatSource(); + + int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *); + + vtkSmartPointer points; + vtkSmartPointer vertices; + private: + vtkCloudMatSource(const vtkCloudMatSource&); // Not implemented. + void operator=(const vtkCloudMatSource&); // Not implemented. + + template int filterNanCopy(const Mat& source, int dataType); + }; + } +} + +#endif diff --git a/modules/viz/src/vtk/vtkColorMatSource.cpp b/modules/viz/src/vtk/vtkColorMatSource.cpp new file mode 100644 index 0000000000..aa09a2d49d --- /dev/null +++ b/modules/viz/src/vtk/vtkColorMatSource.cpp @@ -0,0 +1,132 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#include "precomp.hpp" + +namespace cv { namespace viz +{ + vtkStandardNewMacro(vtkColorMatSource); + + struct IsNotNan + { + template + bool operator()(const _Tp* data) const + { + return !isNan(data[0]) && !isNan(data[1]) && !isNan(data[2]); + } + }; + + struct AllOk + { + template + bool operator()(const _Tp*) const { return true; } + }; +}} + +cv::viz::vtkColorMatSource::vtkColorMatSource() { SetNumberOfInputPorts(0); } +cv::viz::vtkColorMatSource::~vtkColorMatSource() {} + +void cv::viz::vtkColorMatSource::SetColors(const Mat &colors, const Mat& mask) +{ + CV_Assert(colors.depth() == CV_8U && colors.channels() <= 4 && colors.channels() != 2); + CV_Assert(mask.empty() || mask.depth() == CV_32F || mask.depth() == CV_64F); + + if (!mask.empty() && mask.depth() == CV_32F) + filterNanCopy(colors, mask); + else if (!mask.empty() && mask.depth() == CV_64F) + filterNanCopy(colors, mask); + else /* mask.empty() */ + filterNanCopy(colors, mask); +} + +int cv::viz::vtkColorMatSource::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *outputVector) +{ + vtkInformation *outInfo = outputVector->GetInformationObject(0); + vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())); + + output->GetPointData()->SetScalars(scalars); + + return 1; +} + +template +void cv::viz::vtkColorMatSource::filterNanCopy(const Mat& colors, const Mat& cloud_mask) +{ + Mat buffer(colors.size(), CV_8UC3); + Vec3b* pos = buffer.ptr(); + + int s_chs = colors.channels(); + int m_chs = cloud_mask.channels(); + + _NanPred pred; + + for (int y = 0; y < colors.rows; ++y) + { + const unsigned char* srow = colors.ptr(y); + const unsigned char* send = srow + colors.cols * colors.channels(); + const _Msk* mrow = cloud_mask.empty() ? 0 : cloud_mask.ptr<_Msk>(y); + + if (colors.channels() == 1) + { + for (; srow != send; srow += s_chs, mrow += m_chs) + if (pred(mrow)) + *pos++ = Vec3b(srow[0], srow[0], srow[0]); + } + else + for (; srow != send; srow += s_chs, mrow += m_chs) + if (pred(mrow)) + *pos++ = Vec3b(srow[2], srow[1], srow[0]); + + } + + int total = pos - buffer.ptr(); + Vec3b* array = new Vec3b[total]; + std::copy(buffer.ptr(), pos, array); + + scalars = vtkSmartPointer::New(); + scalars->SetName("colors"); + scalars->SetNumberOfComponents(3); + scalars->SetNumberOfTuples(total); + scalars->SetArray(array->val, total * 3, 0); +} diff --git a/modules/viz/src/vtk/vtkColorMatSource.h b/modules/viz/src/vtk/vtkColorMatSource.h new file mode 100644 index 0000000000..d1650faad8 --- /dev/null +++ b/modules/viz/src/vtk/vtkColorMatSource.h @@ -0,0 +1,82 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#ifndef __vtkColorMatSource_h +#define __vtkColorMatSource_h + +#include +#include +#include + +namespace cv +{ + namespace viz + { + class vtkColorMatSource : public vtkPolyDataAlgorithm + { + public: + static vtkColorMatSource *New(); + vtkTypeMacro(vtkColorMatSource,vtkPolyDataAlgorithm); + + virtual void SetColors(const Mat &colors, const Mat &cloud_mask = Mat()); + + + protected: + vtkColorMatSource(); + ~vtkColorMatSource(); + + int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *); + + vtkSmartPointer scalars; + private: + vtkColorMatSource(const vtkColorMatSource&); // Not implemented. + void operator=(const vtkColorMatSource&); // Not implemented. + + template + void filterNanCopy(const Mat& colors, const Mat& mask); + }; + } +} + +#endif