2013-09-08 21:20:02 +08:00
|
|
|
/*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
|
|
|
|
//
|
|
|
|
// OpenCV Viz module is complete rewrite of
|
|
|
|
// PCL visualization module (www.pointclouds.org)
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2013-07-04 17:54:00 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2013-07-15 18:02:20 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
namespace viz
|
|
|
|
{
|
|
|
|
template<typename _Tp> Vec<_Tp, 3>* vtkpoints_data(vtkSmartPointer<vtkPoints>& points);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 21:15:20 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// line widget implementation
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WLine::WLine(const Point3f &pt1, const Point3f &pt2, const Color &color)
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-07-04 17:54:00 +08:00
|
|
|
vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
line->SetPoint1(pt1.x, pt1.y, pt1.z);
|
|
|
|
line->SetPoint2(pt2.x, pt2.y, pt2.z);
|
|
|
|
line->Update();
|
2013-07-04 17:54:00 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(line->GetOutputPort());
|
2013-07-04 17:54:00 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 17:54:00 +08:00
|
|
|
actor->SetMapper(mapper);
|
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 17:54:00 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
2013-07-04 21:05:05 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WLine cv::viz::Widget::cast<cv::viz::WLine>()
|
2013-07-10 00:02:43 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WLine&>(widget);
|
2013-07-10 00:02:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 21:15:20 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// plane widget implementation
|
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
namespace cv { namespace viz { namespace
|
2013-07-22 20:34:44 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
struct PlaneUtils
|
2013-07-22 20:34:44 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
template<typename _Tp>
|
|
|
|
static vtkSmartPointer<vtkTransformPolyDataFilter> setSize(const Vec<_Tp, 3> ¢er, vtkSmartPointer<vtkAlgorithmOutput> poly_data_port, double size)
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->PreMultiply();
|
|
|
|
transform->Translate(center[0], center[1], center[2]);
|
|
|
|
transform->Scale(size, size, size);
|
|
|
|
transform->Translate(-center[0], -center[1], -center[2]);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
|
|
|
transform_filter->SetInputConnection(poly_data_port);
|
|
|
|
transform_filter->SetTransform(transform);
|
|
|
|
transform_filter->Update();
|
|
|
|
|
|
|
|
return transform_filter;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}}}
|
2013-07-22 20:34:44 +08:00
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WPlane::WPlane(const Vec4f& coefs, float size, const Color &color)
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
|
|
|
|
plane->SetNormal(coefs[0], coefs[1], coefs[2]);
|
2013-07-13 22:01:56 +08:00
|
|
|
double norm = cv::norm(Vec3f(coefs.val));
|
2013-09-08 21:20:02 +08:00
|
|
|
plane->Push(-coefs[3] / norm);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 20:34:44 +08:00
|
|
|
Vec3d p_center;
|
|
|
|
plane->GetOrigin(p_center.val);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-12-01 19:25:44 +08:00
|
|
|
mapper->SetInputConnection(PlaneUtils::setSize(p_center, plane->GetOutputPort(), size)->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 21:15:20 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 21:15:20 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WPlane::WPlane(const Vec4f& coefs, const Point3f& pt, float size, const Color &color)
|
2013-07-04 21:15:20 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
|
2013-07-13 22:01:56 +08:00
|
|
|
Point3f coefs3(coefs[0], coefs[1], coefs[2]);
|
2013-09-08 21:20:02 +08:00
|
|
|
double norm_sqr = 1.0 / coefs3.dot(coefs3);
|
2013-07-04 21:15:20 +08:00
|
|
|
plane->SetNormal(coefs[0], coefs[1], coefs[2]);
|
|
|
|
|
|
|
|
double t = coefs3.dot(pt) + coefs[3];
|
2013-07-13 22:01:56 +08:00
|
|
|
Vec3f p_center = pt - coefs3 * t * norm_sqr;
|
2013-09-08 21:20:02 +08:00
|
|
|
plane->SetCenter(p_center[0], p_center[1], p_center[2]);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-12-01 19:25:44 +08:00
|
|
|
mapper->SetInputConnection(PlaneUtils::setSize(p_center, plane->GetOutputPort(), size)->GetOutputPort());
|
2013-07-04 21:15:20 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 21:15:20 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 21:32:06 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WPlane cv::viz::Widget::cast<cv::viz::WPlane>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WPlane&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 21:32:06 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// sphere widget implementation
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WSphere::WSphere(const Point3f ¢er, float radius, int sphere_resolution, const Color &color)
|
2013-07-04 21:32:06 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
|
|
|
|
sphere->SetRadius(radius);
|
|
|
|
sphere->SetCenter(center.x, center.y, center.z);
|
|
|
|
sphere->SetPhiResolution(sphere_resolution);
|
|
|
|
sphere->SetThetaResolution(sphere_resolution);
|
|
|
|
sphere->LatLongTessellationOff();
|
|
|
|
sphere->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(sphere->GetOutputPort());
|
2013-07-04 21:32:06 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 21:32:06 +08:00
|
|
|
actor->SetMapper(mapper);
|
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 21:15:20 +08:00
|
|
|
setColor(color);
|
2013-07-04 21:54:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WSphere cv::viz::Widget::cast<cv::viz::WSphere>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WSphere&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 21:54:46 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// arrow widget implementation
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WArrow::WArrow(const Point3f& pt1, const Point3f& pt2, float thickness, const Color &color)
|
2013-07-04 21:54:46 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkArrowSource> arrowSource = vtkSmartPointer<vtkArrowSource>::New();
|
2013-07-15 21:14:13 +08:00
|
|
|
arrowSource->SetShaftRadius(thickness);
|
|
|
|
// The thickness and radius of the tip are adjusted based on the thickness of the arrow
|
|
|
|
arrowSource->SetTipRadius(thickness * 3.0);
|
|
|
|
arrowSource->SetTipLength(thickness * 10.0);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-04 21:54:46 +08:00
|
|
|
float startPoint[3], endPoint[3];
|
|
|
|
startPoint[0] = pt1.x;
|
|
|
|
startPoint[1] = pt1.y;
|
|
|
|
startPoint[2] = pt1.z;
|
|
|
|
endPoint[0] = pt2.x;
|
|
|
|
endPoint[1] = pt2.y;
|
|
|
|
endPoint[2] = pt2.z;
|
|
|
|
float normalizedX[3], normalizedY[3], normalizedZ[3];
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-04 21:54:46 +08:00
|
|
|
// The X axis is a vector from start to end
|
|
|
|
vtkMath::Subtract(endPoint, startPoint, normalizedX);
|
|
|
|
float length = vtkMath::Norm(normalizedX);
|
|
|
|
vtkMath::Normalize(normalizedX);
|
|
|
|
|
|
|
|
// The Z axis is an arbitrary vecotr cross X
|
|
|
|
float arbitrary[3];
|
|
|
|
arbitrary[0] = vtkMath::Random(-10,10);
|
|
|
|
arbitrary[1] = vtkMath::Random(-10,10);
|
|
|
|
arbitrary[2] = vtkMath::Random(-10,10);
|
|
|
|
vtkMath::Cross(normalizedX, arbitrary, normalizedZ);
|
|
|
|
vtkMath::Normalize(normalizedZ);
|
|
|
|
|
|
|
|
// The Y axis is Z cross X
|
|
|
|
vtkMath::Cross(normalizedZ, normalizedX, normalizedY);
|
|
|
|
vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
|
|
|
|
|
|
|
|
// Create the direction cosine matrix
|
|
|
|
matrix->Identity();
|
|
|
|
for (unsigned int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
matrix->SetElement(i, 0, normalizedX[i]);
|
|
|
|
matrix->SetElement(i, 1, normalizedY[i]);
|
|
|
|
matrix->SetElement(i, 2, normalizedZ[i]);
|
2013-09-18 19:50:55 +08:00
|
|
|
}
|
2013-07-04 21:54:46 +08:00
|
|
|
|
|
|
|
// Apply the transforms
|
2013-07-05 18:13:23 +08:00
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
2013-07-04 21:54:46 +08:00
|
|
|
transform->Translate(startPoint);
|
|
|
|
transform->Concatenate(matrix);
|
|
|
|
transform->Scale(length, length, length);
|
|
|
|
|
|
|
|
// Transform the polydata
|
2013-07-05 18:13:23 +08:00
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> transformPD = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
2013-07-04 21:54:46 +08:00
|
|
|
transformPD->SetTransform(transform);
|
|
|
|
transformPD->SetInputConnection(arrowSource->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(transformPD->GetOutputPort());
|
2013-07-04 21:54:46 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 21:54:46 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 21:54:46 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WArrow cv::viz::Widget::cast<cv::viz::WArrow>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WArrow&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 21:54:46 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// circle widget implementation
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCircle::WCircle(const Point3f& pt, float radius, float thickness, const Color& color)
|
2013-07-04 21:54:46 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkDiskSource> disk = vtkSmartPointer<vtkDiskSource>::New();
|
2013-07-04 21:54:46 +08:00
|
|
|
// Maybe the resolution should be lower e.g. 50 or 25
|
2013-09-08 21:20:02 +08:00
|
|
|
disk->SetCircumferentialResolution(50);
|
|
|
|
disk->SetInnerRadius(radius - thickness);
|
|
|
|
disk->SetOuterRadius(radius + thickness);
|
2013-07-04 21:54:46 +08:00
|
|
|
|
|
|
|
// Set the circle origin
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
t->Identity();
|
|
|
|
t->Translate(pt.x, pt.y, pt.z);
|
2013-07-04 21:54:46 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
|
|
|
tf->SetTransform(t);
|
|
|
|
tf->SetInputConnection(disk->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(tf->GetOutputPort());
|
2013-07-04 21:54:46 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 21:54:46 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 21:54:46 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WCircle cv::viz::Widget::cast<cv::viz::WCircle>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WCircle&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 22:44:41 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// cylinder widget implementation
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCylinder::WCylinder(const Point3f& pt_on_axis, const Point3f& axis_direction, float radius, int numsides, const Color &color)
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-07-13 22:01:56 +08:00
|
|
|
const Point3f pt2 = pt_on_axis + axis_direction;
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
|
|
|
|
line->SetPoint1(pt_on_axis.x, pt_on_axis.y, pt_on_axis.z);
|
|
|
|
line->SetPoint2(pt2.x, pt2.y, pt2.z);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkTubeFilter> tuber = vtkSmartPointer<vtkTubeFilter>::New();
|
|
|
|
tuber->SetInputConnection(line->GetOutputPort());
|
|
|
|
tuber->SetRadius(radius);
|
|
|
|
tuber->SetNumberOfSides(numsides);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(tuber->GetOutputPort());
|
2013-07-04 22:44:41 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 22:44:41 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 22:44:41 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
2013-07-04 22:59:11 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WCylinder cv::viz::Widget::cast<cv::viz::WCylinder>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WCylinder&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 22:59:11 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// cylinder widget implementation
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WCube::WCube(const Point3f& pt_min, const Point3f& pt_max, bool wire_frame, const Color &color)
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-07-15 22:47:19 +08:00
|
|
|
if (wire_frame)
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkOutlineSource> cube = vtkSmartPointer<vtkOutlineSource>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
cube->SetBounds(pt_min.x, pt_max.x, pt_min.y, pt_max.y, pt_min.z, pt_max.z);
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(cube->GetOutputPort());
|
2013-07-15 22:47:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
|
|
|
|
cube->SetBounds(pt_min.x, pt_max.x, pt_min.y, pt_max.y, pt_min.z, pt_max.z);
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(cube->GetOutputPort());
|
2013-07-15 22:47:19 +08:00
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 22:59:11 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 22:59:11 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
2013-07-04 23:19:06 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WCube cv::viz::Widget::cast<cv::viz::WCube>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WCube&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 23:19:06 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// coordinate system widget implementation
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCoordinateSystem::WCoordinateSystem(float scale)
|
2013-07-04 23:19:06 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
|
|
|
|
axes->SetOrigin(0, 0, 0);
|
|
|
|
axes->SetScaleFactor(scale);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkFloatArray> axes_colors = vtkSmartPointer<vtkFloatArray>::New();
|
|
|
|
axes_colors->Allocate(6);
|
|
|
|
axes_colors->InsertNextValue(0.0);
|
|
|
|
axes_colors->InsertNextValue(0.0);
|
|
|
|
axes_colors->InsertNextValue(0.5);
|
|
|
|
axes_colors->InsertNextValue(0.5);
|
|
|
|
axes_colors->InsertNextValue(1.0);
|
|
|
|
axes_colors->InsertNextValue(1.0);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkPolyData> axes_data = axes->GetOutput();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_data->Update();
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
axes->Update();
|
|
|
|
#endif
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_data->GetPointData()->SetScalars(axes_colors);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkTubeFilter> axes_tubes = vtkSmartPointer<vtkTubeFilter>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_tubes->SetInput(axes_data);
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
axes_tubes->SetInputData(axes_data);
|
|
|
|
#endif
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_tubes->SetRadius(axes->GetScaleFactor() / 50.0);
|
|
|
|
axes_tubes->SetNumberOfSides(6);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
mapper->SetScalarModeToUsePointData();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(axes_tubes->GetOutputPort());
|
2013-07-04 23:19:06 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-04 23:19:06 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-04 23:19:06 +08:00
|
|
|
}
|
2013-07-08 16:41:42 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WCoordinateSystem cv::viz::Widget::cast<cv::viz::WCoordinateSystem>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WCoordinateSystem&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// polyline widget implementation
|
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
namespace cv { namespace viz { namespace
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
struct PolyLineUtils
|
2013-07-10 18:51:17 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
template<typename _Tp>
|
|
|
|
static void copy(const Mat& source, Vec<_Tp, 3> *output, vtkSmartPointer<vtkPolyLine> polyLine)
|
2013-07-10 18:51:17 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
int s_chs = source.channels();
|
2013-07-10 18:51:17 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
for (int y = 0, id = 0; y < source.rows; ++y)
|
2013-07-10 18:51:17 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
const _Tp* srow = source.ptr<_Tp>(y);
|
|
|
|
|
|
|
|
for (int x = 0; x < source.cols; ++x, srow += s_chs, ++id)
|
|
|
|
{
|
|
|
|
*output++ = Vec<_Tp, 3>(srow);
|
|
|
|
polyLine->GetPointIds()->SetId(id,id);
|
|
|
|
}
|
2013-07-10 18:51:17 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-01 19:25:44 +08:00
|
|
|
};
|
|
|
|
}}}
|
2013-07-10 18:51:17 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WPolyLine::WPolyLine(InputArray _pointData, const Color &color)
|
2013-07-10 18:51:17 +08:00
|
|
|
{
|
|
|
|
Mat pointData = _pointData.getMat();
|
|
|
|
CV_Assert(pointData.type() == CV_32FC3 || pointData.type() == CV_32FC4 || pointData.type() == CV_64FC3 || pointData.type() == CV_64FC4);
|
2013-09-18 19:50:55 +08:00
|
|
|
vtkIdType nr_points = pointData.total();
|
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
|
|
|
|
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
|
|
|
|
vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
if (pointData.depth() == CV_32F)
|
|
|
|
points->SetDataTypeToFloat();
|
|
|
|
else
|
|
|
|
points->SetDataTypeToDouble();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
points->SetNumberOfPoints(nr_points);
|
|
|
|
polyLine->GetPointIds()->SetNumberOfIds(nr_points);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
if (pointData.depth() == CV_32F)
|
|
|
|
{
|
|
|
|
// Get a pointer to the beginning of the data array
|
|
|
|
Vec3f *data_beg = vtkpoints_data<float>(points);
|
2013-12-01 19:25:44 +08:00
|
|
|
PolyLineUtils::copy(pointData, data_beg, polyLine);
|
2013-07-10 18:51:17 +08:00
|
|
|
}
|
|
|
|
else if (pointData.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
// Get a pointer to the beginning of the data array
|
|
|
|
Vec3d *data_beg = vtkpoints_data<double>(points);
|
2013-12-01 19:25:44 +08:00
|
|
|
PolyLineUtils::copy(pointData, data_beg, polyLine);
|
2013-07-10 18:51:17 +08:00
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
|
|
|
|
cells->InsertNextCell(polyLine);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
polyData->SetPoints(points);
|
|
|
|
polyData->SetLines(cells);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-10 18:51:17 +08:00
|
|
|
mapper->SetInput(polyData);
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
mapper->SetInputData(polyData);
|
|
|
|
#endif
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 18:51:17 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WPolyLine cv::viz::Widget::cast<cv::viz::WPolyLine>()
|
2013-07-10 18:51:17 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WPolyLine&>(widget);
|
2013-07-10 18:51:17 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 21:34:19 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// grid widget implementation
|
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
namespace cv { namespace viz { namespace
|
2013-07-24 16:13:01 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
struct GridUtils
|
2013-07-24 16:13:01 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
static vtkSmartPointer<vtkPolyData> createGrid(const Vec2i &dimensions, const Vec2d &spacing)
|
|
|
|
{
|
|
|
|
// Create the grid using image data
|
|
|
|
vtkSmartPointer<vtkImageData> grid = vtkSmartPointer<vtkImageData>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
// Add 1 to dimensions because in ImageData dimensions is the number of lines
|
|
|
|
// - however here it means number of cells
|
|
|
|
grid->SetDimensions(dimensions[0]+1, dimensions[1]+1, 1);
|
|
|
|
grid->SetSpacing(spacing[0], spacing[1], 0.);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
// Set origin of the grid to be the middle of the grid
|
|
|
|
grid->SetOrigin(dimensions[0] * spacing[0] * (-0.5), dimensions[1] * spacing[1] * (-0.5), 0);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
// Extract the edges so we have the grid
|
|
|
|
vtkSmartPointer<vtkExtractEdges> filter = vtkSmartPointer<vtkExtractEdges>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-12-01 19:25:44 +08:00
|
|
|
filter->SetInputConnection(grid->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
2013-12-01 19:25:44 +08:00
|
|
|
filter->SetInputData(grid);
|
2013-09-09 00:55:41 +08:00
|
|
|
#endif
|
2013-12-01 19:25:44 +08:00
|
|
|
filter->Update();
|
|
|
|
return filter->GetOutput();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}}}
|
2013-07-24 16:13:01 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WGrid::WGrid(const Vec2i &dimensions, const Vec2d &spacing, const Color &color)
|
2013-07-10 21:34:19 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
vtkSmartPointer<vtkPolyData> grid = GridUtils::createGrid(dimensions, spacing);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-24 16:13:01 +08:00
|
|
|
mapper->SetInputConnection(grid->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
mapper->SetInputData(grid);
|
|
|
|
#endif
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-24 16:02:33 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-24 16:02:33 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WGrid::WGrid(const Vec4f &coefs, const Vec2i &dimensions, const Vec2d &spacing, const Color &color)
|
2013-07-24 16:02:33 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
vtkSmartPointer<vtkPolyData> grid = GridUtils::createGrid(dimensions, spacing);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-24 16:02:33 +08:00
|
|
|
// Estimate the transform to set the normal based on the coefficients
|
|
|
|
Vec3f normal(coefs[0], coefs[1], coefs[2]);
|
|
|
|
Vec3f up_vector(0.0f, 1.0f, 0.0f); // Just set as default
|
|
|
|
double push_distance = -coefs[3]/cv::norm(Vec3f(coefs.val));
|
|
|
|
Vec3f u,v,n;
|
|
|
|
n = normalize(normal);
|
|
|
|
u = normalize(up_vector.cross(n));
|
|
|
|
v = n.cross(u);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-24 16:02:33 +08:00
|
|
|
vtkSmartPointer<vtkMatrix4x4> mat_trans = vtkSmartPointer<vtkMatrix4x4>::New();
|
|
|
|
mat_trans->SetElement(0,0,u[0]);
|
|
|
|
mat_trans->SetElement(0,1,u[1]);
|
|
|
|
mat_trans->SetElement(0,2,u[2]);
|
|
|
|
mat_trans->SetElement(1,0,v[0]);
|
|
|
|
mat_trans->SetElement(1,1,v[1]);
|
|
|
|
mat_trans->SetElement(1,2,v[2]);
|
|
|
|
mat_trans->SetElement(2,0,n[0]);
|
|
|
|
mat_trans->SetElement(2,1,n[1]);
|
|
|
|
mat_trans->SetElement(2,2,n[2]);
|
|
|
|
// Inverse rotation (orthogonal, so just take transpose)
|
|
|
|
mat_trans->Transpose();
|
|
|
|
mat_trans->SetElement(0,3,n[0] * push_distance);
|
|
|
|
mat_trans->SetElement(1,3,n[1] * push_distance);
|
|
|
|
mat_trans->SetElement(2,3,n[2] * push_distance);
|
|
|
|
mat_trans->SetElement(3,3,1);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-24 16:02:33 +08:00
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->PreMultiply();
|
|
|
|
transform->SetMatrix(mat_trans);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-24 16:02:33 +08:00
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
|
|
|
transform_filter->SetTransform(transform);
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-24 16:13:01 +08:00
|
|
|
transform_filter->SetInputConnection(grid->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
transform_filter->SetInputData(grid);
|
|
|
|
#endif
|
2013-07-24 16:02:33 +08:00
|
|
|
transform_filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-07-24 16:13:01 +08:00
|
|
|
mapper->SetInputConnection(transform_filter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 21:34:19 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 21:34:19 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-13 23:08:25 +08:00
|
|
|
setColor(color);
|
2013-07-10 21:34:19 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WGrid cv::viz::Widget::cast<cv::viz::WGrid>()
|
2013-07-10 21:34:19 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WGrid&>(widget);
|
2013-07-10 21:34:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 22:34:47 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// text3D widget implementation
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WText3D::WText3D(const String &text, const Point3f &position, float text_scale, bool face_camera, const Color &color)
|
2013-07-10 22:34:47 +08:00
|
|
|
{
|
2013-09-08 18:19:14 +08:00
|
|
|
vtkSmartPointer<vtkVectorText> textSource = vtkSmartPointer<vtkVectorText>::New();
|
|
|
|
textSource->SetText(text.c_str());
|
|
|
|
textSource->Update();
|
2013-07-10 22:34:47 +08:00
|
|
|
|
2013-09-08 18:19:14 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
mapper->SetInputConnection(textSource->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 18:19:14 +08:00
|
|
|
if (face_camera)
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkFollower> actor = vtkSmartPointer<vtkFollower>::New();
|
|
|
|
actor->SetMapper(mapper);
|
|
|
|
actor->SetPosition(position.x, position.y, position.z);
|
|
|
|
actor->SetScale(text_scale);
|
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
|
|
|
actor->SetPosition(position.x, position.y, position.z);
|
|
|
|
actor->SetScale(text_scale);
|
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 22:34:47 +08:00
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
void cv::viz::WText3D::setText(const String &text)
|
2013-07-10 22:34:47 +08:00
|
|
|
{
|
|
|
|
vtkFollower *actor = vtkFollower::SafeDownCast(WidgetAccessor::getProp(*this));
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support text." && actor);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 22:34:47 +08:00
|
|
|
// Update text source
|
|
|
|
vtkPolyDataMapper *mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
|
|
|
|
vtkVectorText * textSource = vtkVectorText::SafeDownCast(mapper->GetInputConnection(0,0)->GetProducer());
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support text." && textSource);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 22:34:47 +08:00
|
|
|
textSource->SetText(text.c_str());
|
|
|
|
textSource->Update();
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::String cv::viz::WText3D::getText() const
|
2013-07-10 22:34:47 +08:00
|
|
|
{
|
|
|
|
vtkFollower *actor = vtkFollower::SafeDownCast(WidgetAccessor::getProp(*this));
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support text." && actor);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 22:34:47 +08:00
|
|
|
vtkPolyDataMapper *mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
|
|
|
|
vtkVectorText * textSource = vtkVectorText::SafeDownCast(mapper->GetInputConnection(0,0)->GetProducer());
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support text." && textSource);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-10 22:34:47 +08:00
|
|
|
return textSource->GetText();
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WText3D cv::viz::Widget::cast<cv::viz::WText3D>()
|
2013-07-10 22:34:47 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WText3D&>(widget);
|
2013-07-10 22:34:47 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 21:12:50 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// text widget implementation
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WText::WText(const String &text, const Point2i &pos, int font_size, const Color &color)
|
2013-07-08 16:41:42 +08:00
|
|
|
{
|
2013-07-09 21:18:44 +08:00
|
|
|
vtkSmartPointer<vtkTextActor> actor = vtkSmartPointer<vtkTextActor>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
actor->SetPosition(pos.x, pos.y);
|
|
|
|
actor->SetInput(text.c_str());
|
2013-07-08 16:41:42 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkTextProperty> tprop = actor->GetTextProperty();
|
|
|
|
tprop->SetFontSize(font_size);
|
|
|
|
tprop->SetFontFamilyToArial();
|
|
|
|
tprop->SetJustificationToLeft();
|
|
|
|
tprop->BoldOn();
|
2013-07-08 16:41:42 +08:00
|
|
|
|
|
|
|
Color c = vtkcolor(color);
|
2013-09-08 21:20:02 +08:00
|
|
|
tprop->SetColor(c.val);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-09 21:18:44 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
2013-07-08 16:41:42 +08:00
|
|
|
}
|
2013-07-08 21:12:50 +08:00
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WText cv::viz::Widget::cast<cv::viz::WText>()
|
2013-07-09 20:12:49 +08:00
|
|
|
{
|
2013-07-10 15:55:42 +08:00
|
|
|
Widget2D widget = this->cast<Widget2D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WText&>(widget);
|
2013-07-09 20:12:49 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
void cv::viz::WText::setText(const String &text)
|
2013-07-09 21:18:44 +08:00
|
|
|
{
|
|
|
|
vtkTextActor *actor = vtkTextActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support text." && actor);
|
2013-07-09 21:18:44 +08:00
|
|
|
actor->SetInput(text.c_str());
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::String cv::viz::WText::getText() const
|
2013-07-09 21:18:44 +08:00
|
|
|
{
|
|
|
|
vtkTextActor *actor = vtkTextActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support text." && actor);
|
2013-07-09 21:18:44 +08:00
|
|
|
return actor->GetInput();
|
|
|
|
}
|
2013-07-17 17:35:14 +08:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// image overlay widget implementation
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WImageOverlay::WImageOverlay(const Mat &image, const Rect &rect)
|
2013-07-17 17:35:14 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Create the vtk image and set its parameters based on input image
|
|
|
|
vtkSmartPointer<vtkImageData> vtk_image = vtkSmartPointer<vtkImageData>::New();
|
2013-07-24 16:32:21 +08:00
|
|
|
ConvertToVtkImage::convert(image, vtk_image);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Need to flip the image as the coordinates are different in OpenCV and VTK
|
|
|
|
vtkSmartPointer<vtkImageFlip> flipFilter = vtkSmartPointer<vtkImageFlip>::New();
|
|
|
|
flipFilter->SetFilteredAxis(1); // Vertical flip
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->SetInputConnection(vtk_image->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
flipFilter->SetInputData(vtk_image);
|
|
|
|
#endif
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:12:22 +08:00
|
|
|
// Scale the image based on the Rect
|
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->Scale(double(image.cols)/rect.width,double(image.rows)/rect.height,1.0);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:12:22 +08:00
|
|
|
vtkSmartPointer<vtkImageReslice> image_reslice = vtkSmartPointer<vtkImageReslice>::New();
|
|
|
|
image_reslice->SetResliceTransform(transform);
|
|
|
|
image_reslice->SetInputConnection(flipFilter->GetOutputPort());
|
|
|
|
image_reslice->SetOutputDimensionality(2);
|
|
|
|
image_reslice->InterpolateOn();
|
2013-09-18 19:50:55 +08:00
|
|
|
image_reslice->AutoCropOutputOn();
|
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
vtkSmartPointer<vtkImageMapper> imageMapper = vtkSmartPointer<vtkImageMapper>::New();
|
2013-07-22 22:12:22 +08:00
|
|
|
imageMapper->SetInputConnection(image_reslice->GetOutputPort());
|
2013-07-17 17:35:14 +08:00
|
|
|
imageMapper->SetColorWindow(255); // OpenCV color
|
2013-09-18 19:50:55 +08:00
|
|
|
imageMapper->SetColorLevel(127.5);
|
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
vtkSmartPointer<vtkActor2D> actor = vtkSmartPointer<vtkActor2D>::New();
|
|
|
|
actor->SetMapper(imageMapper);
|
2013-07-22 22:12:22 +08:00
|
|
|
actor->SetPosition(rect.x, rect.y);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
void cv::viz::WImageOverlay::setImage(const Mat &image)
|
2013-07-17 17:35:14 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support overlay image." && actor);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
vtkImageMapper *mapper = vtkImageMapper::SafeDownCast(actor->GetMapper());
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support overlay image." && mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Create the vtk image and set its parameters based on input image
|
|
|
|
vtkSmartPointer<vtkImageData> vtk_image = vtkSmartPointer<vtkImageData>::New();
|
2013-07-24 16:32:21 +08:00
|
|
|
ConvertToVtkImage::convert(image, vtk_image);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Need to flip the image as the coordinates are different in OpenCV and VTK
|
|
|
|
vtkSmartPointer<vtkImageFlip> flipFilter = vtkSmartPointer<vtkImageFlip>::New();
|
|
|
|
flipFilter->SetFilteredAxis(1); // Vertical flip
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->SetInputConnection(vtk_image->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
flipFilter->SetInputData(vtk_image);
|
|
|
|
#endif
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
mapper->SetInputConnection(flipFilter->GetOutputPort());
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WImageOverlay cv::viz::Widget::cast<cv::viz::WImageOverlay>()
|
2013-07-17 17:35:14 +08:00
|
|
|
{
|
|
|
|
Widget2D widget = this->cast<Widget2D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WImageOverlay&>(widget);
|
2013-07-17 17:35:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// image 3D widget implementation
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WImage3D::WImage3D(const Mat &image, const Size &size)
|
2013-07-17 17:35:14 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Create the vtk image and set its parameters based on input image
|
|
|
|
vtkSmartPointer<vtkImageData> vtk_image = vtkSmartPointer<vtkImageData>::New();
|
2013-07-24 16:32:21 +08:00
|
|
|
ConvertToVtkImage::convert(image, vtk_image);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Need to flip the image as the coordinates are different in OpenCV and VTK
|
|
|
|
vtkSmartPointer<vtkImageFlip> flipFilter = vtkSmartPointer<vtkImageFlip>::New();
|
|
|
|
flipFilter->SetFilteredAxis(1); // Vertical flip
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->SetInputConnection(vtk_image->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
flipFilter->SetInputData(vtk_image);
|
|
|
|
#endif
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
Vec3d plane_center(size.width * 0.5, size.height * 0.5, 0.0);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
|
|
|
|
plane->SetCenter(plane_center[0], plane_center[1], plane_center[2]);
|
|
|
|
plane->SetNormal(0.0, 0.0, 1.0);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->PreMultiply();
|
|
|
|
transform->Translate(plane_center[0], plane_center[1], plane_center[2]);
|
|
|
|
transform->Scale(size.width, size.height, 1.0);
|
|
|
|
transform->Translate(-plane_center[0], -plane_center[1], -plane_center[2]);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
|
|
|
transform_filter->SetTransform(transform);
|
|
|
|
transform_filter->SetInputConnection(plane->GetOutputPort());
|
|
|
|
transform_filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
// Apply the texture
|
|
|
|
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
|
|
|
|
texture->SetInputConnection(flipFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkSmartPointer<vtkTextureMapToPlane> texturePlane = vtkSmartPointer<vtkTextureMapToPlane>::New();
|
|
|
|
texturePlane->SetInputConnection(transform_filter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
|
|
|
planeMapper->SetInputConnection(texturePlane->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(planeMapper);
|
|
|
|
actor->SetTexture(texture);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
cv::viz::WImage3D::WImage3D(const Vec3f &position, const Vec3f &normal, const Vec3f &up_vector, const Mat &image, const Size &size)
|
2013-07-22 23:18:19 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
// Create the vtk image and set its parameters based on input image
|
|
|
|
vtkSmartPointer<vtkImageData> vtk_image = vtkSmartPointer<vtkImageData>::New();
|
2013-07-24 16:32:21 +08:00
|
|
|
ConvertToVtkImage::convert(image, vtk_image);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
// Need to flip the image as the coordinates are different in OpenCV and VTK
|
|
|
|
vtkSmartPointer<vtkImageFlip> flipFilter = vtkSmartPointer<vtkImageFlip>::New();
|
|
|
|
flipFilter->SetFilteredAxis(1); // Vertical flip
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-22 23:18:19 +08:00
|
|
|
flipFilter->SetInputConnection(vtk_image->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
flipFilter->SetInputData(vtk_image);
|
|
|
|
#endif
|
2013-07-22 23:18:19 +08:00
|
|
|
flipFilter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
|
|
|
|
plane->SetCenter(0.0, 0.0, 0.0);
|
2013-09-18 19:50:55 +08:00
|
|
|
plane->SetNormal(0.0, 0.0, 1.0);
|
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
// Compute the transformation matrix for drawing the camera frame in a scene
|
|
|
|
Vec3f u,v,n;
|
|
|
|
n = normalize(normal);
|
|
|
|
u = normalize(up_vector.cross(n));
|
|
|
|
v = n.cross(u);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
vtkSmartPointer<vtkMatrix4x4> mat_trans = vtkSmartPointer<vtkMatrix4x4>::New();
|
|
|
|
mat_trans->SetElement(0,0,u[0]);
|
|
|
|
mat_trans->SetElement(0,1,u[1]);
|
|
|
|
mat_trans->SetElement(0,2,u[2]);
|
|
|
|
mat_trans->SetElement(1,0,v[0]);
|
|
|
|
mat_trans->SetElement(1,1,v[1]);
|
|
|
|
mat_trans->SetElement(1,2,v[2]);
|
|
|
|
mat_trans->SetElement(2,0,n[0]);
|
|
|
|
mat_trans->SetElement(2,1,n[1]);
|
|
|
|
mat_trans->SetElement(2,2,n[2]);
|
|
|
|
// Inverse rotation (orthogonal, so just take transpose)
|
2013-09-18 19:50:55 +08:00
|
|
|
mat_trans->Transpose();
|
2013-07-22 23:18:19 +08:00
|
|
|
// Then translate the coordinate frame to camera position
|
|
|
|
mat_trans->SetElement(0,3,position[0]);
|
|
|
|
mat_trans->SetElement(1,3,position[1]);
|
|
|
|
mat_trans->SetElement(2,3,position[2]);
|
|
|
|
mat_trans->SetElement(3,3,1);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
// Apply the texture
|
|
|
|
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
|
|
|
|
texture->SetInputConnection(flipFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
vtkSmartPointer<vtkTextureMapToPlane> texturePlane = vtkSmartPointer<vtkTextureMapToPlane>::New();
|
|
|
|
texturePlane->SetInputConnection(plane->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
// Apply the transform after texture mapping
|
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->PreMultiply();
|
|
|
|
transform->SetMatrix(mat_trans);
|
|
|
|
transform->Scale(size.width, size.height, 1.0);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
|
|
|
transform_filter->SetTransform(transform);
|
|
|
|
transform_filter->SetInputConnection(texturePlane->GetOutputPort());
|
|
|
|
transform_filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
|
|
|
planeMapper->SetInputConnection(transform_filter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(planeMapper);
|
|
|
|
actor->SetTexture(texture);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 23:18:19 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
void cv::viz::WImage3D::setImage(const Mat &image)
|
2013-07-17 17:35:14 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
2013-08-31 17:30:37 +08:00
|
|
|
CV_Assert("This widget does not support 3D image." && actor);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Create the vtk image and set its parameters based on input image
|
|
|
|
vtkSmartPointer<vtkImageData> vtk_image = vtkSmartPointer<vtkImageData>::New();
|
2013-07-24 16:32:21 +08:00
|
|
|
ConvertToVtkImage::convert(image, vtk_image);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-17 17:35:14 +08:00
|
|
|
// Need to flip the image as the coordinates are different in OpenCV and VTK
|
|
|
|
vtkSmartPointer<vtkImageFlip> flipFilter = vtkSmartPointer<vtkImageFlip>::New();
|
|
|
|
flipFilter->SetFilteredAxis(1); // Vertical flip
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->SetInputConnection(vtk_image->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
flipFilter->SetInputData(vtk_image);
|
|
|
|
#endif
|
2013-07-17 17:35:14 +08:00
|
|
|
flipFilter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
// Apply the texture
|
|
|
|
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
|
|
|
|
texture->SetInputConnection(flipFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 22:48:17 +08:00
|
|
|
actor->SetTexture(texture);
|
2013-07-17 17:35:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WImage3D cv::viz::Widget::cast<cv::viz::WImage3D>()
|
2013-07-17 17:35:14 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WImage3D&>(widget);
|
2013-07-18 16:39:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// camera position widget implementation
|
2013-07-18 23:08:58 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
namespace cv { namespace viz { namespace
|
2013-09-08 19:06:40 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
struct CameraPositionUtils
|
2013-09-08 19:06:40 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
static void projectImage(float fovy, float far_end_height, const Mat &image,
|
|
|
|
double scale, const Color &color, vtkSmartPointer<vtkActor> actor)
|
|
|
|
{
|
|
|
|
// Create a camera
|
|
|
|
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
|
|
|
|
float aspect_ratio = float(image.cols)/float(image.rows);
|
|
|
|
|
|
|
|
// Create the vtk image
|
|
|
|
vtkSmartPointer<vtkImageData> vtk_image = vtkSmartPointer<vtkImageData>::New();
|
|
|
|
ConvertToVtkImage::convert(image, vtk_image);
|
|
|
|
|
|
|
|
// Adjust a pixel of the vtk_image
|
|
|
|
vtk_image->SetScalarComponentFromDouble(0, image.rows-1, 0, 0, color[2]);
|
|
|
|
vtk_image->SetScalarComponentFromDouble(0, image.rows-1, 0, 1, color[1]);
|
|
|
|
vtk_image->SetScalarComponentFromDouble(0, image.rows-1, 0, 2, color[0]);
|
|
|
|
|
|
|
|
// Need to flip the image as the coordinates are different in OpenCV and VTK
|
|
|
|
vtkSmartPointer<vtkImageFlip> flipFilter = vtkSmartPointer<vtkImageFlip>::New();
|
|
|
|
flipFilter->SetFilteredAxis(1); // Vertical flip
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-12-01 19:25:44 +08:00
|
|
|
flipFilter->SetInputConnection(vtk_image->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
2013-12-01 19:25:44 +08:00
|
|
|
flipFilter->SetInputData(vtk_image);
|
2013-09-09 00:55:41 +08:00
|
|
|
#endif
|
2013-12-01 19:25:44 +08:00
|
|
|
flipFilter->Update();
|
|
|
|
|
|
|
|
Vec3d plane_center(0.0, 0.0, scale);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
|
|
|
|
plane->SetCenter(plane_center[0], plane_center[1], plane_center[2]);
|
|
|
|
plane->SetNormal(0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->PreMultiply();
|
|
|
|
transform->Translate(plane_center[0], plane_center[1], plane_center[2]);
|
|
|
|
transform->Scale(far_end_height*aspect_ratio, far_end_height, 1.0);
|
|
|
|
transform->RotateY(180.0);
|
|
|
|
transform->Translate(-plane_center[0], -plane_center[1], -plane_center[2]);
|
|
|
|
|
|
|
|
// Apply the texture
|
|
|
|
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
|
|
|
|
texture->SetInputConnection(flipFilter->GetOutputPort());
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkTextureMapToPlane> texturePlane = vtkSmartPointer<vtkTextureMapToPlane>::New();
|
|
|
|
texturePlane->SetInputConnection(plane->GetOutputPort());
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> transform_filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
|
|
|
transform_filter->SetTransform(transform);
|
|
|
|
transform_filter->SetInputConnection(texturePlane->GetOutputPort());
|
|
|
|
transform_filter->Update();
|
|
|
|
|
|
|
|
// Create frustum
|
|
|
|
camera->SetViewAngle(fovy);
|
|
|
|
camera->SetPosition(0.0,0.0,0.0);
|
|
|
|
camera->SetViewUp(0.0,1.0,0.0);
|
|
|
|
camera->SetFocalPoint(0.0,0.0,1.0);
|
|
|
|
camera->SetClippingRange(0.01, scale);
|
|
|
|
|
|
|
|
double planesArray[24];
|
|
|
|
camera->GetFrustumPlanes(aspect_ratio, planesArray);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();
|
|
|
|
planes->SetFrustumPlanes(planesArray);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkFrustumSource> frustumSource =
|
|
|
|
vtkSmartPointer<vtkFrustumSource>::New();
|
|
|
|
frustumSource->SetPlanes(planes);
|
|
|
|
frustumSource->Update();
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkExtractEdges> filter = vtkSmartPointer<vtkExtractEdges>::New();
|
|
|
|
filter->SetInputConnection(frustumSource->GetOutputPort());
|
|
|
|
filter->Update();
|
|
|
|
|
|
|
|
// Frustum needs to be textured or else it can't be combined with image
|
|
|
|
vtkSmartPointer<vtkTextureMapToPlane> frustum_texture = vtkSmartPointer<vtkTextureMapToPlane>::New();
|
|
|
|
frustum_texture->SetInputConnection(filter->GetOutputPort());
|
|
|
|
// Texture mapping with only one pixel from the image to have constant color
|
|
|
|
frustum_texture->SetSRange(0.0, 0.0);
|
|
|
|
frustum_texture->SetTRange(0.0, 0.0);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
|
|
|
appendFilter->AddInputConnection(frustum_texture->GetOutputPort());
|
|
|
|
appendFilter->AddInputConnection(transform_filter->GetOutputPort());
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
|
|
|
planeMapper->SetInputConnection(appendFilter->GetOutputPort());
|
|
|
|
|
|
|
|
actor->SetMapper(planeMapper);
|
|
|
|
actor->SetTexture(texture);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}}}
|
2013-09-08 19:06:40 +08:00
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCameraPosition::WCameraPosition(float scale)
|
2013-07-18 23:08:58 +08:00
|
|
|
{
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
|
|
|
|
axes->SetOrigin(0, 0, 0);
|
|
|
|
axes->SetScaleFactor(scale);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkFloatArray> axes_colors = vtkSmartPointer<vtkFloatArray>::New();
|
|
|
|
axes_colors->Allocate(6);
|
|
|
|
axes_colors->InsertNextValue(0.0);
|
|
|
|
axes_colors->InsertNextValue(0.0);
|
|
|
|
axes_colors->InsertNextValue(0.5);
|
|
|
|
axes_colors->InsertNextValue(0.5);
|
|
|
|
axes_colors->InsertNextValue(1.0);
|
|
|
|
axes_colors->InsertNextValue(1.0);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkPolyData> axes_data = axes->GetOutput();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_data->Update();
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
axes->Update();
|
|
|
|
#endif
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_data->GetPointData()->SetScalars(axes_colors);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkTubeFilter> axes_tubes = vtkSmartPointer<vtkTubeFilter>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_tubes->SetInput(axes_data);
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
axes_tubes->SetInputData(axes_data);
|
|
|
|
#endif
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_tubes->SetRadius(axes->GetScaleFactor() / 50.0);
|
|
|
|
axes_tubes->SetNumberOfSides(6);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 22:31:04 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
mapper->SetScalarModeToUsePointData();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(axes_tubes->GetOutputPort());
|
2013-07-18 23:08:58 +08:00
|
|
|
|
2013-12-09 00:58:55 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-07-18 23:08:58 +08:00
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCameraPosition::WCameraPosition(const Matx33f &K, float scale, const Color &color)
|
2013-07-18 23:08:58 +08:00
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
|
2013-07-18 23:46:08 +08:00
|
|
|
float f_x = K(0,0);
|
|
|
|
float f_y = K(1,1);
|
2013-07-18 23:08:58 +08:00
|
|
|
float c_y = K(1,2);
|
2013-07-18 23:46:08 +08:00
|
|
|
float aspect_ratio = f_y / f_x;
|
2013-07-18 23:08:58 +08:00
|
|
|
// Assuming that this is an ideal camera (c_y and c_x are at the center of the image)
|
2013-07-18 23:46:08 +08:00
|
|
|
float fovy = 2.0f * atan2(c_y,f_y) * 180 / CV_PI;
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
camera->SetViewAngle(fovy);
|
|
|
|
camera->SetPosition(0.0,0.0,0.0);
|
|
|
|
camera->SetViewUp(0.0,1.0,0.0);
|
|
|
|
camera->SetFocalPoint(0.0,0.0,1.0);
|
2013-07-20 00:30:12 +08:00
|
|
|
camera->SetClippingRange(0.01, scale);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
double planesArray[24];
|
|
|
|
camera->GetFrustumPlanes(aspect_ratio, planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();
|
|
|
|
planes->SetFrustumPlanes(planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
vtkSmartPointer<vtkFrustumSource> frustumSource =
|
|
|
|
vtkSmartPointer<vtkFrustumSource>::New();
|
|
|
|
frustumSource->SetPlanes(planes);
|
|
|
|
frustumSource->Update();
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkExtractEdges> filter = vtkSmartPointer<vtkExtractEdges>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
filter->SetInputConnection(frustumSource->GetOutputPort());
|
2013-07-18 23:08:58 +08:00
|
|
|
filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(filter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-18 23:08:58 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
setColor(color);
|
|
|
|
}
|
2013-07-20 00:30:12 +08:00
|
|
|
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCameraPosition::WCameraPosition(const Vec2f &fov, float scale, const Color &color)
|
2013-07-20 00:30:12 +08:00
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
camera->SetViewAngle(fov[1] * 180 / CV_PI); // Vertical field of view
|
|
|
|
camera->SetPosition(0.0,0.0,0.0);
|
|
|
|
camera->SetViewUp(0.0,1.0,0.0);
|
|
|
|
camera->SetFocalPoint(0.0,0.0,1.0);
|
|
|
|
camera->SetClippingRange(0.01, scale);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:23:24 +08:00
|
|
|
double aspect_ratio = tan(fov[0] * 0.5) / tan(fov[1] * 0.5);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
double planesArray[24];
|
2013-07-25 16:23:24 +08:00
|
|
|
camera->GetFrustumPlanes(aspect_ratio, planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();
|
|
|
|
planes->SetFrustumPlanes(planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
vtkSmartPointer<vtkFrustumSource> frustumSource =
|
|
|
|
vtkSmartPointer<vtkFrustumSource>::New();
|
|
|
|
frustumSource->SetPlanes(planes);
|
|
|
|
frustumSource->Update();
|
|
|
|
|
|
|
|
// Extract the edges so we have the grid
|
|
|
|
vtkSmartPointer<vtkExtractEdges> filter = vtkSmartPointer<vtkExtractEdges>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
filter->SetInputConnection(frustumSource->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
filter->Update();
|
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(filter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-20 00:30:12 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
setColor(color);
|
|
|
|
}
|
2013-07-22 18:53:19 +08:00
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCameraPosition::WCameraPosition(const Matx33f &K, const Mat &image, float scale, const Color &color)
|
2013-07-23 21:43:23 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
|
|
|
float f_y = K(1,1);
|
|
|
|
float c_y = K(1,2);
|
|
|
|
// Assuming that this is an ideal camera (c_y and c_x are at the center of the image)
|
2013-09-08 19:06:40 +08:00
|
|
|
float fovy = 2.0f * atan2(c_y,f_y) * 180.0f / CV_PI;
|
2013-07-23 21:43:23 +08:00
|
|
|
float far_end_height = 2.0f * c_y * scale / f_y;
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 19:06:40 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-12-01 19:25:44 +08:00
|
|
|
CameraPositionUtils::projectImage(fovy, far_end_height, image, scale, color, actor);
|
2013-09-08 19:06:40 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-11-18 18:50:05 +08:00
|
|
|
cv::viz::WCameraPosition::WCameraPosition(const Vec2f &fov, const Mat &image, float scale, const Color &color)
|
2013-09-08 19:06:40 +08:00
|
|
|
{
|
|
|
|
CV_Assert(!image.empty() && image.depth() == CV_8U);
|
|
|
|
float fovy = fov[1] * 180.0f / CV_PI;
|
|
|
|
float far_end_height = 2.0 * scale * tan(fov[1] * 0.5);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-23 21:43:23 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
2013-12-01 19:25:44 +08:00
|
|
|
CameraPositionUtils::projectImage(fovy, far_end_height, image, scale, color, actor);
|
2013-07-23 21:43:23 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
template<> cv::viz::WCameraPosition cv::viz::Widget::cast<cv::viz::WCameraPosition>()
|
2013-07-25 16:23:24 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-09-15 22:26:53 +08:00
|
|
|
return static_cast<WCameraPosition&>(widget);
|
2013-07-25 16:23:24 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 18:53:19 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// trajectory widget implementation
|
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
namespace cv { namespace viz { namespace
|
2013-07-22 21:03:28 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
struct TrajectoryUtils
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
static void applyPath(vtkSmartPointer<vtkPolyData> poly_data, vtkSmartPointer<vtkAppendPolyData> append_filter, const std::vector<Affine3f> &path)
|
2013-07-22 21:03:28 +08:00
|
|
|
{
|
2013-12-01 19:25:44 +08:00
|
|
|
vtkIdType nr_points = path.size();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
for (vtkIdType i = 0; i < nr_points; ++i)
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkPolyData> new_data = vtkSmartPointer<vtkPolyData>::New();
|
|
|
|
new_data->DeepCopy(poly_data);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
// Transform the default coordinate frame
|
|
|
|
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
|
|
|
|
transform->PreMultiply();
|
|
|
|
vtkSmartPointer<vtkMatrix4x4> mat_trans = vtkSmartPointer<vtkMatrix4x4>::New();
|
|
|
|
mat_trans = convertToVtkMatrix(path[i].matrix);
|
|
|
|
transform->SetMatrix(mat_trans);
|
|
|
|
|
|
|
|
vtkSmartPointer<vtkTransformPolyDataFilter> filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-12-01 19:25:44 +08:00
|
|
|
filter->SetInput(new_data);
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
2013-12-01 19:25:44 +08:00
|
|
|
filter->SetInputData(new_data);
|
2013-09-09 00:55:41 +08:00
|
|
|
#endif
|
2013-12-01 19:25:44 +08:00
|
|
|
filter->SetTransform(transform);
|
|
|
|
filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
append_filter->AddInputConnection(filter->GetOutputPort());
|
|
|
|
}
|
2013-07-22 21:03:28 +08:00
|
|
|
}
|
2013-12-01 19:25:44 +08:00
|
|
|
};
|
|
|
|
}}}
|
2013-07-22 21:03:28 +08:00
|
|
|
|
2013-12-08 22:07:16 +08:00
|
|
|
cv::viz::WTrajectory::WTrajectory(const std::vector<Affine3f> &path, int display_mode, float scale, const Color &color)
|
2013-07-22 18:53:19 +08:00
|
|
|
{
|
2013-07-25 16:56:54 +08:00
|
|
|
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 22:08:23 +08:00
|
|
|
// Bitwise and with 3 in order to limit the domain to 2 bits
|
2013-12-08 21:12:02 +08:00
|
|
|
if ((~display_mode & 3) ^ WTrajectory::PATH)
|
2013-07-22 18:53:19 +08:00
|
|
|
{
|
2013-07-25 16:56:54 +08:00
|
|
|
// Create a poly line along the path
|
2013-09-18 19:50:55 +08:00
|
|
|
vtkIdType nr_points = path.size();
|
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
|
|
|
|
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
|
|
|
|
vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
points->SetDataTypeToFloat();
|
|
|
|
points->SetNumberOfPoints(nr_points);
|
|
|
|
polyLine->GetPointIds()->SetNumberOfIds(nr_points);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
Vec3f *data_beg = vtkpoints_data<float>(points);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
for (vtkIdType i = 0; i < nr_points; ++i)
|
|
|
|
{
|
2013-07-29 17:01:59 +08:00
|
|
|
Vec3f cam_pose = path[i].translation();
|
|
|
|
*data_beg++ = cam_pose;
|
2013-07-25 16:56:54 +08:00
|
|
|
polyLine->GetPointIds()->SetId(i,i);
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
|
|
|
|
cells->InsertNextCell(polyLine);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
polyData->SetPoints(points);
|
|
|
|
polyData->SetLines(cells);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
// Set the color for polyData
|
|
|
|
vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
|
|
|
colors->SetNumberOfComponents(3);
|
2013-07-26 22:55:03 +08:00
|
|
|
colors->SetNumberOfTuples(nr_points);
|
|
|
|
colors->FillComponent(0, color[2]);
|
|
|
|
colors->FillComponent(1, color[1]);
|
|
|
|
colors->FillComponent(2, color[0]);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 16:56:54 +08:00
|
|
|
polyData->GetPointData()->SetScalars(colors);
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-07-25 16:56:54 +08:00
|
|
|
appendFilter->AddInputConnection(polyData->GetProducerPort());
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
appendFilter->AddInputData(polyData);
|
|
|
|
#endif
|
2013-07-25 16:56:54 +08:00
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-08 21:12:02 +08:00
|
|
|
if ((~display_mode & 3) ^ WTrajectory::FRAMES)
|
2013-07-22 18:53:19 +08:00
|
|
|
{
|
2013-07-25 16:56:54 +08:00
|
|
|
// Create frames and transform along the path
|
2013-07-22 21:03:28 +08:00
|
|
|
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
axes->SetOrigin(0, 0, 0);
|
|
|
|
axes->SetScaleFactor(scale);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkUnsignedCharArray> axes_colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
2013-07-22 21:03:28 +08:00
|
|
|
axes_colors->SetNumberOfComponents(3);
|
|
|
|
axes_colors->InsertNextTuple3(255,0,0);
|
|
|
|
axes_colors->InsertNextTuple3(255,0,0);
|
|
|
|
axes_colors->InsertNextTuple3(0,255,0);
|
|
|
|
axes_colors->InsertNextTuple3(0,255,0);
|
|
|
|
axes_colors->InsertNextTuple3(0,0,255);
|
|
|
|
axes_colors->InsertNextTuple3(0,0,255);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkPolyData> axes_data = axes->GetOutput();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_data->Update();
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
axes->Update();
|
|
|
|
#endif
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_data->GetPointData()->SetScalars(axes_colors);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-08 21:20:02 +08:00
|
|
|
vtkSmartPointer<vtkTubeFilter> axes_tubes = vtkSmartPointer<vtkTubeFilter>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_tubes->SetInput(axes_data);
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
axes_tubes->SetInputData(axes_data);
|
|
|
|
#endif
|
2013-09-08 21:20:02 +08:00
|
|
|
axes_tubes->SetRadius(axes->GetScaleFactor() / 50.0);
|
|
|
|
axes_tubes->SetNumberOfSides(6);
|
2013-07-22 21:03:28 +08:00
|
|
|
axes_tubes->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-12-01 19:25:44 +08:00
|
|
|
TrajectoryUtils::applyPath(axes_tubes->GetOutput(), appendFilter, path);
|
2013-07-22 18:53:19 +08:00
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 18:53:19 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
mapper->SetScalarModeToUsePointData();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(appendFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 18:53:19 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 18:53:19 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:07:16 +08:00
|
|
|
template<> cv::viz::WTrajectory cv::viz::Widget::cast<cv::viz::WTrajectory>()
|
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
|
|
|
return static_cast<WTrajectory&>(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WTrajectoryFrustums widget implementation
|
|
|
|
|
|
|
|
cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(const std::vector<Affine3f> &path, const Matx33f &K, float scale, const Color &color)
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-07-22 20:11:06 +08:00
|
|
|
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
|
|
|
|
float f_x = K(0,0);
|
|
|
|
float f_y = K(1,1);
|
|
|
|
float c_y = K(1,2);
|
|
|
|
float aspect_ratio = f_y / f_x;
|
|
|
|
// Assuming that this is an ideal camera (c_y and c_x are at the center of the image)
|
|
|
|
float fovy = 2.0f * atan2(c_y,f_y) * 180 / CV_PI;
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 20:11:06 +08:00
|
|
|
camera->SetViewAngle(fovy);
|
|
|
|
camera->SetPosition(0.0,0.0,0.0);
|
|
|
|
camera->SetViewUp(0.0,1.0,0.0);
|
|
|
|
camera->SetFocalPoint(0.0,0.0,1.0);
|
|
|
|
camera->SetClippingRange(0.01, scale);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 20:11:06 +08:00
|
|
|
double planesArray[24];
|
|
|
|
camera->GetFrustumPlanes(aspect_ratio, planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 21:03:28 +08:00
|
|
|
vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();
|
|
|
|
planes->SetFrustumPlanes(planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 21:03:28 +08:00
|
|
|
vtkSmartPointer<vtkFrustumSource> frustumSource = vtkSmartPointer<vtkFrustumSource>::New();
|
|
|
|
frustumSource->SetPlanes(planes);
|
|
|
|
frustumSource->Update();
|
|
|
|
|
|
|
|
// Extract the edges
|
|
|
|
vtkSmartPointer<vtkExtractEdges> filter = vtkSmartPointer<vtkExtractEdges>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
filter->SetInputConnection(frustumSource->GetOutputPort());
|
2013-07-22 21:03:28 +08:00
|
|
|
filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
2013-12-01 19:25:44 +08:00
|
|
|
TrajectoryUtils::applyPath(filter->GetOutput(), appendFilter, path);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(appendFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:07:16 +08:00
|
|
|
cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(const std::vector<Affine3f> &path, const Vec2f &fov, float scale, const Color &color)
|
2013-07-25 21:08:45 +08:00
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
camera->SetViewAngle(fov[1] * 180 / CV_PI); // Vertical field of view
|
|
|
|
camera->SetPosition(0.0,0.0,0.0);
|
|
|
|
camera->SetViewUp(0.0,1.0,0.0);
|
|
|
|
camera->SetFocalPoint(0.0,0.0,1.0);
|
|
|
|
camera->SetClippingRange(0.01, scale);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
double aspect_ratio = tan(fov[0] * 0.5) / tan(fov[1] * 0.5);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
double planesArray[24];
|
|
|
|
camera->GetFrustumPlanes(aspect_ratio, planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();
|
|
|
|
planes->SetFrustumPlanes(planesArray);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
vtkSmartPointer<vtkFrustumSource> frustumSource = vtkSmartPointer<vtkFrustumSource>::New();
|
|
|
|
frustumSource->SetPlanes(planes);
|
|
|
|
frustumSource->Update();
|
|
|
|
|
|
|
|
// Extract the edges
|
|
|
|
vtkSmartPointer<vtkExtractEdges> filter = vtkSmartPointer<vtkExtractEdges>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
filter->SetInputConnection(frustumSource->GetOutputPort());
|
2013-07-25 21:08:45 +08:00
|
|
|
filter->Update();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-25 21:08:45 +08:00
|
|
|
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
2013-12-01 19:25:44 +08:00
|
|
|
TrajectoryUtils::applyPath(filter->GetOutput(), appendFilter, path);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 20:11:06 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(appendFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 20:11:06 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-07-22 20:11:06 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
setColor(color);
|
|
|
|
}
|
2013-07-25 16:23:24 +08:00
|
|
|
|
2013-12-08 22:07:16 +08:00
|
|
|
template<> cv::viz::WTrajectoryFrustums cv::viz::Widget::cast<cv::viz::WTrajectoryFrustums>()
|
2013-07-25 16:23:24 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-12-08 22:07:16 +08:00
|
|
|
return static_cast<WTrajectoryFrustums&>(widget);
|
2013-08-03 21:40:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
2013-12-08 22:07:16 +08:00
|
|
|
/// WTrajectorySpheres widget implementation
|
2013-08-03 21:40:36 +08:00
|
|
|
|
2013-12-08 21:12:02 +08:00
|
|
|
cv::viz::WTrajectorySpheres::WTrajectorySpheres(const std::vector<Affine3f> &path, float line_length, float init_sphere_radius, float sphere_radius,
|
2013-08-03 21:40:36 +08:00
|
|
|
const Color &line_color, const Color &sphere_color)
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
|
|
|
vtkIdType nr_poses = path.size();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
// Create color arrays
|
|
|
|
vtkSmartPointer<vtkUnsignedCharArray> line_scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
|
|
|
line_scalars->SetNumberOfComponents(3);
|
|
|
|
line_scalars->InsertNextTuple3(line_color[2], line_color[1], line_color[0]);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
// Create color array for sphere
|
|
|
|
vtkSphereSource * dummy_sphere = vtkSphereSource::New();
|
|
|
|
// Create the array for big sphere
|
|
|
|
dummy_sphere->SetRadius(init_sphere_radius);
|
|
|
|
dummy_sphere->Update();
|
|
|
|
vtkIdType nr_points = dummy_sphere->GetOutput()->GetNumberOfCells();
|
|
|
|
vtkSmartPointer<vtkUnsignedCharArray> sphere_scalars_init = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
|
|
|
sphere_scalars_init->SetNumberOfComponents(3);
|
|
|
|
sphere_scalars_init->SetNumberOfTuples(nr_points);
|
|
|
|
sphere_scalars_init->FillComponent(0, sphere_color[2]);
|
|
|
|
sphere_scalars_init->FillComponent(1, sphere_color[1]);
|
|
|
|
sphere_scalars_init->FillComponent(2, sphere_color[0]);
|
|
|
|
// Create the array for small sphere
|
|
|
|
dummy_sphere->SetRadius(sphere_radius);
|
|
|
|
dummy_sphere->Update();
|
|
|
|
nr_points = dummy_sphere->GetOutput()->GetNumberOfCells();
|
|
|
|
vtkSmartPointer<vtkUnsignedCharArray> sphere_scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
|
|
|
sphere_scalars->SetNumberOfComponents(3);
|
|
|
|
sphere_scalars->SetNumberOfTuples(nr_points);
|
|
|
|
sphere_scalars->FillComponent(0, sphere_color[2]);
|
|
|
|
sphere_scalars->FillComponent(1, sphere_color[1]);
|
|
|
|
sphere_scalars->FillComponent(2, sphere_color[0]);
|
2013-09-18 19:50:55 +08:00
|
|
|
dummy_sphere->Delete();
|
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
for (vtkIdType i = 0; i < nr_poses; ++i)
|
|
|
|
{
|
|
|
|
Point3f new_pos = path[i].translation();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
vtkSmartPointer<vtkSphereSource> sphere_source = vtkSmartPointer<vtkSphereSource>::New();
|
2013-09-08 21:20:02 +08:00
|
|
|
sphere_source->SetCenter(new_pos.x, new_pos.y, new_pos.z);
|
2013-08-03 21:40:36 +08:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
sphere_source->SetRadius(init_sphere_radius);
|
|
|
|
sphere_source->Update();
|
|
|
|
sphere_source->GetOutput()->GetCellData()->SetScalars(sphere_scalars_init);
|
|
|
|
appendFilter->AddInputConnection(sphere_source->GetOutputPort());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sphere_source->SetRadius(sphere_radius);
|
|
|
|
sphere_source->Update();
|
|
|
|
sphere_source->GetOutput()->GetCellData()->SetScalars(sphere_scalars);
|
|
|
|
appendFilter->AddInputConnection(sphere_source->GetOutputPort());
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
Affine3f relativeAffine = path[i].inv() * path[i-1];
|
|
|
|
Vec3f v = path[i].rotation() * relativeAffine.translation();
|
|
|
|
v = normalize(v) * line_length;
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
vtkSmartPointer<vtkLineSource> line_source = vtkSmartPointer<vtkLineSource>::New();
|
|
|
|
line_source->SetPoint1(new_pos.x + v[0], new_pos.y + v[1], new_pos.z + v[2]);
|
|
|
|
line_source->SetPoint2(new_pos.x, new_pos.y, new_pos.z);
|
|
|
|
line_source->Update();
|
|
|
|
line_source->GetOutput()->GetCellData()->SetScalars(line_scalars);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
appendFilter->AddInputConnection(line_source->GetOutputPort());
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
|
|
|
mapper->SetScalarModeToUseCellData();
|
2013-09-09 00:55:41 +08:00
|
|
|
mapper->SetInputConnection(appendFilter->GetOutputPort());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-08-03 21:40:36 +08:00
|
|
|
WidgetAccessor::setProp(*this, actor);
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:12:02 +08:00
|
|
|
template<> cv::viz::WTrajectorySpheres cv::viz::Widget::cast<cv::viz::WTrajectorySpheres>()
|
2013-08-03 21:40:36 +08:00
|
|
|
{
|
|
|
|
Widget3D widget = this->cast<Widget3D>();
|
2013-12-08 21:12:02 +08:00
|
|
|
return static_cast<WTrajectorySpheres&>(widget);
|
2013-09-08 21:20:02 +08:00
|
|
|
}
|