opencv/modules/viz/src/widget.cpp

205 lines
5.5 KiB
C++
Raw Normal View History

#include "precomp.hpp"
2013-07-09 16:21:57 +08:00
///////////////////////////////////////////////////////////////////////////////////////////////
/// widget implementation
class temp_viz::Widget::Impl
{
public:
2013-07-09 23:35:26 +08:00
vtkSmartPointer<vtkProp> prop;
2013-07-09 17:07:21 +08:00
int ref_counter;
2013-07-08 16:41:42 +08:00
2013-07-09 23:35:26 +08:00
Impl() : prop(0) {}
2013-07-09 16:21:57 +08:00
};
2013-07-09 16:21:57 +08:00
temp_viz::Widget::Widget() : impl_(0)
{
2013-07-09 17:07:21 +08:00
create();
2013-07-09 16:21:57 +08:00
}
2013-07-09 17:07:21 +08:00
temp_viz::Widget::Widget(const Widget &other) : impl_(other.impl_)
2013-07-09 16:21:57 +08:00
{
2013-07-09 17:07:21 +08:00
if (impl_) CV_XADD(&impl_->ref_counter, 1);
2013-07-09 16:21:57 +08:00
}
temp_viz::Widget& temp_viz::Widget::operator=(const Widget &other)
2013-07-09 16:21:57 +08:00
{
if (this != &other)
{
2013-07-09 17:07:21 +08:00
release();
2013-07-09 16:21:57 +08:00
impl_ = other.impl_;
2013-07-09 17:07:21 +08:00
if (impl_) CV_XADD(&impl_->ref_counter, 1);
}
2013-07-09 16:21:57 +08:00
return *this;
}
2013-07-09 16:21:57 +08:00
temp_viz::Widget::~Widget()
{
2013-07-09 17:07:21 +08:00
release();
}
void temp_viz::Widget::create()
{
if (impl_) release();
impl_ = new Impl();
impl_->ref_counter = 1;
}
void temp_viz::Widget::release()
{
if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1)
{
2013-07-09 16:21:57 +08:00
delete impl_;
impl_ = 0;
}
2013-07-09 16:21:57 +08:00
}
///////////////////////////////////////////////////////////////////////////////////////////////
2013-07-09 17:07:21 +08:00
/// widget accessor implementaion
2013-07-09 21:18:44 +08:00
vtkSmartPointer<vtkProp> temp_viz::WidgetAccessor::getProp(const Widget& widget)
{
2013-07-09 23:35:26 +08:00
return widget.impl_->prop;
}
2013-07-09 23:35:26 +08:00
void temp_viz::WidgetAccessor::setProp(Widget& widget, vtkSmartPointer<vtkProp> prop)
2013-07-09 16:21:57 +08:00
{
2013-07-09 23:35:26 +08:00
widget.impl_->prop = prop;
2013-07-09 16:21:57 +08:00
}
///////////////////////////////////////////////////////////////////////////////////////////////
2013-07-09 16:21:57 +08:00
/// widget3D implementation
2013-07-09 16:21:57 +08:00
struct temp_viz::Widget3D::MatrixConverter
{
2013-07-09 16:21:57 +08:00
static cv::Matx44f convertToMatx(const vtkSmartPointer<vtkMatrix4x4>& vtk_matrix)
{
cv::Matx44f m;
for (int i = 0; i < 4; i++)
for (int k = 0; k < 4; k++)
m(i, k) = vtk_matrix->GetElement (i, k);
return m;
}
static vtkSmartPointer<vtkMatrix4x4> convertToVtkMatrix (const cv::Matx44f& m)
{
vtkSmartPointer<vtkMatrix4x4> vtk_matrix = vtkSmartPointer<vtkMatrix4x4>::New ();
for (int i = 0; i < 4; i++)
for (int k = 0; k < 4; k++)
vtk_matrix->SetElement(i, k, m(i, k));
return vtk_matrix;
}
};
temp_viz::Widget3D::Widget3D(const Widget& other) : Widget(other)
{
// Check if other's actor is castable to vtkProp3D
2013-07-09 21:18:44 +08:00
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(other));
CV_Assert(actor);
}
temp_viz::Widget3D& temp_viz::Widget3D::operator =(const Widget &other)
{
// Check if other's actor is castable to vtkProp3D
2013-07-09 21:18:44 +08:00
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(other));
CV_Assert(actor);
Widget::operator=(other);
return *this;
}
2013-07-09 16:21:57 +08:00
void temp_viz::Widget3D::setPose(const Affine3f &pose)
{
2013-07-09 21:18:44 +08:00
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
2013-07-09 16:21:57 +08:00
CV_Assert(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix);
actor->SetUserMatrix (matrix);
actor->Modified ();
}
2013-07-09 16:21:57 +08:00
void temp_viz::Widget3D::updatePose(const Affine3f &pose)
{
2013-07-09 21:18:44 +08:00
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
2013-07-09 16:21:57 +08:00
CV_Assert(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix();
if (!matrix)
2013-07-04 20:57:49 +08:00
{
2013-07-09 16:21:57 +08:00
setPose(pose);
return ;
2013-07-04 20:57:49 +08:00
}
2013-07-09 16:21:57 +08:00
Matx44f matrix_cv = MatrixConverter::convertToMatx(matrix);
2013-07-09 16:21:57 +08:00
Affine3f updated_pose = pose * Affine3f(matrix_cv);
matrix = MatrixConverter::convertToVtkMatrix(updated_pose.matrix);
2013-07-04 20:57:49 +08:00
2013-07-09 16:21:57 +08:00
actor->SetUserMatrix (matrix);
actor->Modified ();
}
2013-07-09 16:21:57 +08:00
temp_viz::Affine3f temp_viz::Widget3D::getPose() const
2013-07-04 20:57:49 +08:00
{
2013-07-09 21:18:44 +08:00
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
2013-07-09 16:21:57 +08:00
CV_Assert(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix();
Matx44f matrix_cv = MatrixConverter::convertToMatx(matrix);
return Affine3f(matrix_cv);
2013-07-04 20:57:49 +08:00
}
2013-07-09 16:21:57 +08:00
void temp_viz::Widget3D::setColor(const Color &color)
2013-07-04 20:57:49 +08:00
{
2013-07-09 16:21:57 +08:00
// Cast to actor instead of prop3d since prop3d doesn't provide getproperty
2013-07-09 21:18:44 +08:00
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
2013-07-09 16:21:57 +08:00
CV_Assert(actor);
Color c = vtkcolor(color);
actor->GetMapper ()->ScalarVisibilityOff ();
actor->GetProperty ()->SetColor (c.val);
actor->GetProperty ()->SetEdgeColor (c.val);
actor->GetProperty ()->SetAmbient (0.8);
actor->GetProperty ()->SetDiffuse (0.8);
actor->GetProperty ()->SetSpecular (0.8);
actor->GetProperty ()->SetLighting (0);
actor->Modified ();
2013-07-04 20:57:49 +08:00
}
template<> temp_viz::Widget3D temp_viz::Widget::cast<temp_viz::Widget3D>()
{
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
Widget3D widget;
WidgetAccessor::setProp(widget, actor);
return widget;
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// widget2D implementation
temp_viz::Widget2D::Widget2D(const Widget &other) : Widget(other)
{
// Check if other's actor is castable to vtkActor2D
2013-07-09 21:18:44 +08:00
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(other));
CV_Assert(actor);
}
temp_viz::Widget2D& temp_viz::Widget2D::operator=(const Widget &other)
{
// Check if other's actor is castable to vtkActor2D
2013-07-09 21:18:44 +08:00
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(other));
CV_Assert(actor);
Widget::operator=(other);
return *this;
2013-07-09 20:28:55 +08:00
}
void temp_viz::Widget2D::setColor(const Color &color)
{
2013-07-09 21:18:44 +08:00
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
2013-07-09 20:28:55 +08:00
CV_Assert(actor);
Color c = vtkcolor(color);
actor->GetProperty ()->SetColor (c.val);
actor->Modified ();
2013-07-09 23:35:26 +08:00
}