rewrote cloud widget in vtk filters style
Conflicts: modules/viz/src/clouds.cpp
This commit is contained in:
parent
159de9cc47
commit
7dbf6bc8df
@ -59,103 +59,10 @@ namespace cv
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// Point Cloud Widget implementation
|
||||
|
||||
namespace cv { namespace viz { namespace
|
||||
{
|
||||
struct CloudUtils
|
||||
{
|
||||
static inline vtkSmartPointer<vtkPolyData> create(const Mat &cloud, vtkIdType &nr_points)
|
||||
{
|
||||
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
|
||||
vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
|
||||
|
||||
polydata->SetVerts(vertices);
|
||||
|
||||
vtkSmartPointer<vtkPoints> points = polydata->GetPoints();
|
||||
vtkSmartPointer<vtkIdTypeArray> initcells;
|
||||
nr_points = cloud.total();
|
||||
|
||||
if (!points)
|
||||
{
|
||||
points = vtkSmartPointer<vtkPoints>::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<float>(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<double>(points);
|
||||
Vec3d *data_end = NanFilter::copy(cloud, data_beg, cloud);
|
||||
nr_points = data_end - data_beg;
|
||||
}
|
||||
points->SetNumberOfPoints(nr_points);
|
||||
|
||||
// Update cells
|
||||
vtkSmartPointer<vtkIdTypeArray> cells = vertices->GetData();
|
||||
// If no init cells and cells has not been initialized...
|
||||
if (!cells)
|
||||
cells = vtkSmartPointer<vtkIdTypeArray>::New();
|
||||
|
||||
// If we have less values then we need to recreate the array
|
||||
if (cells->GetNumberOfTuples() < nr_points)
|
||||
{
|
||||
cells = vtkSmartPointer<vtkIdTypeArray>::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<vtkIdTypeArray>::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<vtkPolyData> polydata = CloudUtils::create(cloud, nr_points);
|
||||
|
||||
// Filter colors
|
||||
Vec3b* colors_data = new Vec3b[nr_points];
|
||||
NanFilter::copyColor(colors, colors_data, cloud);
|
||||
|
||||
vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::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<vtkCloudColorMatSource> cloud_source = vtkSmartPointer<vtkCloudColorMatSource>::New();
|
||||
cloud_source->SetCloud(cloud);
|
||||
cloud_source->SetColors(colors, cloud);
|
||||
|
||||
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::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<vtkLODActor> actor = vtkSmartPointer<vtkLODActor>::New();
|
||||
actor->SetNumberOfCloudPoints(int(std::max<vtkIdType>(1, polydata->GetNumberOfPoints() / 10)));
|
||||
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::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<vtkPolyData> polydata = CloudUtils::create(cloud, nr_points);
|
||||
vtkSmartPointer<vtkCloudMatSource> cloud_source = vtkSmartPointer<vtkCloudMatSource>::New();
|
||||
cloud_source->SetCloud(cloud);
|
||||
|
||||
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::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<vtkLODActor> actor = vtkSmartPointer<vtkLODActor>::New();
|
||||
actor->SetNumberOfCloudPoints(int(std::max<vtkIdType>(1, polydata->GetNumberOfPoints() / 10)));
|
||||
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::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)
|
||||
|
@ -114,6 +114,10 @@
|
||||
#include <vtkPolyDataAlgorithm.h>
|
||||
#include <vtkMergeFilter.h>
|
||||
|
||||
#include <vtk/vtkCloudMatSource.h>
|
||||
#include <vtk/vtkColorMatSource.h>
|
||||
#include <vtk/vtkCloudColorMatSource.h>
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/viz.hpp>
|
||||
#include <opencv2/viz/widget_accessor.hpp>
|
||||
|
168
modules/viz/src/vtk/vtkCloudColorMatSource.cpp
Normal file
168
modules/viz/src/vtk/vtkCloudColorMatSource.cpp
Normal file
@ -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<typename _Tp>
|
||||
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<float >(cloud, VTK_FLOAT)
|
||||
: filterNanCopy<double>(cloud, VTK_DOUBLE);
|
||||
|
||||
vertices = vtkSmartPointer<vtkCellArray>::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<float, IsNotNan>(colors, cloud_mask);
|
||||
else if (cloud_mask.depth() == CV_64F)
|
||||
filterNanCopy<double, IsNotNan>(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<typename _Tp>
|
||||
int cv::viz::vtkCloudColorMatSource::filterNanCopy(const Mat& source, int dataType)
|
||||
{
|
||||
CV_DbgAssert(DataType<_Tp>::depth == source.depth());
|
||||
points = vtkSmartPointer<vtkPoints>::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<typename _Msk, class _NanPred>
|
||||
void cv::viz::vtkCloudColorMatSource::filterNanCopy(const Mat& colors, const Mat& mask)
|
||||
{
|
||||
Mat buffer(colors.size(), CV_8UC3);
|
||||
Vec3b* pos = buffer.ptr<Vec3b>();
|
||||
|
||||
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<unsigned char>(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>();
|
||||
Vec3b* array = new Vec3b[total];
|
||||
std::copy(buffer.ptr<Vec3b>(), pos, array);
|
||||
|
||||
scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
||||
scalars->SetName("colors");
|
||||
scalars->SetNumberOfComponents(3);
|
||||
scalars->SetNumberOfTuples(total);
|
||||
scalars->SetArray(array->val, total * 3, 0);
|
||||
}
|
||||
|
||||
|
88
modules/viz/src/vtk/vtkCloudColorMatSource.h
Normal file
88
modules/viz/src/vtk/vtkCloudColorMatSource.h
Normal file
@ -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 <opencv2/core/mat.hpp>
|
||||
#include <vtkPolyDataAlgorithm.h>
|
||||
#include <vtkSmartPointer.h>
|
||||
#include <vtkPoints.h>
|
||||
#include <vtkCellArray.h>
|
||||
|
||||
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<vtkPoints> points;
|
||||
vtkSmartPointer<vtkCellArray> vertices;
|
||||
vtkSmartPointer<vtkUnsignedCharArray> scalars;
|
||||
private:
|
||||
vtkCloudColorMatSource(const vtkCloudColorMatSource&); // Not implemented.
|
||||
void operator=(const vtkCloudColorMatSource&); // Not implemented.
|
||||
|
||||
template<typename _Tp> int filterNanCopy(const Mat& source, int dataType);
|
||||
|
||||
template<typename _Msk, class _NanPred>
|
||||
void filterNanCopy(const Mat& colors, const Mat& mask);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
105
modules/viz/src/vtk/vtkCloudMatSource.cpp
Normal file
105
modules/viz/src/vtk/vtkCloudMatSource.cpp
Normal file
@ -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<float >(cloud, VTK_FLOAT)
|
||||
: filterNanCopy<double>(cloud, VTK_DOUBLE);
|
||||
|
||||
vertices = vtkSmartPointer<vtkCellArray>::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<typename _Tp>
|
||||
int cv::viz::vtkCloudMatSource::filterNanCopy(const Mat& source, int dataType)
|
||||
{
|
||||
CV_DbgAssert(DataType<_Tp>::depth == source.depth());
|
||||
points = vtkSmartPointer<vtkPoints>::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;
|
||||
}
|
||||
|
||||
|
83
modules/viz/src/vtk/vtkCloudMatSource.h
Normal file
83
modules/viz/src/vtk/vtkCloudMatSource.h
Normal file
@ -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 <opencv2/core/mat.hpp>
|
||||
#include <vtkPolyDataAlgorithm.h>
|
||||
#include <vtkSmartPointer.h>
|
||||
#include <vtkPoints.h>
|
||||
#include <vtkCellArray.h>
|
||||
|
||||
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<vtkPoints> points;
|
||||
vtkSmartPointer<vtkCellArray> vertices;
|
||||
private:
|
||||
vtkCloudMatSource(const vtkCloudMatSource&); // Not implemented.
|
||||
void operator=(const vtkCloudMatSource&); // Not implemented.
|
||||
|
||||
template<typename _Tp> int filterNanCopy(const Mat& source, int dataType);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
132
modules/viz/src/vtk/vtkColorMatSource.cpp
Normal file
132
modules/viz/src/vtk/vtkColorMatSource.cpp
Normal file
@ -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<typename _Tp>
|
||||
bool operator()(const _Tp* data) const
|
||||
{
|
||||
return !isNan(data[0]) && !isNan(data[1]) && !isNan(data[2]);
|
||||
}
|
||||
};
|
||||
|
||||
struct AllOk
|
||||
{
|
||||
template<typename _Tp>
|
||||
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<float, IsNotNan>(colors, mask);
|
||||
else if (!mask.empty() && mask.depth() == CV_64F)
|
||||
filterNanCopy<double, IsNotNan>(colors, mask);
|
||||
else /* mask.empty() */
|
||||
filterNanCopy<double, AllOk>(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<typename _Msk, class _NanPred>
|
||||
void cv::viz::vtkColorMatSource::filterNanCopy(const Mat& colors, const Mat& cloud_mask)
|
||||
{
|
||||
Mat buffer(colors.size(), CV_8UC3);
|
||||
Vec3b* pos = buffer.ptr<Vec3b>();
|
||||
|
||||
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<unsigned char>(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>();
|
||||
Vec3b* array = new Vec3b[total];
|
||||
std::copy(buffer.ptr<Vec3b>(), pos, array);
|
||||
|
||||
scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
||||
scalars->SetName("colors");
|
||||
scalars->SetNumberOfComponents(3);
|
||||
scalars->SetNumberOfTuples(total);
|
||||
scalars->SetArray(array->val, total * 3, 0);
|
||||
}
|
82
modules/viz/src/vtk/vtkColorMatSource.h
Normal file
82
modules/viz/src/vtk/vtkColorMatSource.h
Normal file
@ -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 <opencv2/core/mat.hpp>
|
||||
#include <vtkPolyDataAlgorithm.h>
|
||||
#include <vtkSmartPointer.h>
|
||||
|
||||
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<vtkUnsignedCharArray> scalars;
|
||||
private:
|
||||
vtkColorMatSource(const vtkColorMatSource&); // Not implemented.
|
||||
void operator=(const vtkColorMatSource&); // Not implemented.
|
||||
|
||||
template<typename _Msk, class _NanPred>
|
||||
void filterNanCopy(const Mat& colors, const Mat& mask);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user