Open Source Computer Vision Library https://opencv.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.0 KiB

#include "precomp.hpp"
///////////////////////////////////////////////////////////////////////////////////////////////
/// widget implementation
12 years ago
class cv::viz::Widget::Impl
{
public:
12 years ago
vtkSmartPointer<vtkProp> prop;
int ref_counter;
12 years ago
Impl() : prop(0) {}
};
12 years ago
cv::viz::Widget::Widget() : impl_(0)
{
create();
}
12 years ago
cv::viz::Widget::Widget(const Widget &other) : impl_(other.impl_)
{
if (impl_) CV_XADD(&impl_->ref_counter, 1);
}
12 years ago
cv::viz::Widget& cv::viz::Widget::operator=(const Widget &other)
{
if (this != &other)
{
release();
impl_ = other.impl_;
if (impl_) CV_XADD(&impl_->ref_counter, 1);
}
return *this;
}
12 years ago
cv::viz::Widget::~Widget()
{
release();
}
12 years ago
void cv::viz::Widget::create()
{
if (impl_) release();
impl_ = new Impl();
impl_->ref_counter = 1;
}
12 years ago
void cv::viz::Widget::release()
{
if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1)
{
delete impl_;
impl_ = 0;
}
}
cv::viz::Widget cv::viz::Widget::fromPlyFile(const String &file_name)
{
vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New ();
reader->SetFileName (file_name.c_str ());
vtkSmartPointer<vtkDataSet> data = reader->GetOutput();
CV_Assert(data);
vtkSmartPointer<vtkLODActor> actor = vtkSmartPointer<vtkLODActor>::New();
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput (data);
vtkSmartPointer<vtkDataArray> scalars = data->GetPointData ()->GetScalars ();
if (scalars)
{
cv::Vec3d minmax(scalars->GetRange());
mapper->SetScalarRange(minmax.val);
mapper->SetScalarModeToUsePointData ();
// interpolation OFF, if data is a vtkPolyData that contains only vertices, ON for anything else.
vtkPolyData* polyData = vtkPolyData::SafeDownCast (data);
bool interpolation = (polyData && polyData->GetNumberOfCells () != polyData->GetNumberOfVerts ());
mapper->SetInterpolateScalarsBeforeMapping (interpolation);
mapper->ScalarVisibilityOn ();
}
mapper->ImmediateModeRenderingOff ();
actor->SetNumberOfCloudPoints (int (std::max<vtkIdType> (1, data->GetNumberOfPoints () / 10)));
actor->GetProperty ()->SetInterpolationToFlat ();
actor->GetProperty ()->BackfaceCullingOn ();
actor->SetMapper (mapper);
Widget widget;
widget.impl_->prop = actor;
return widget;
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// widget accessor implementaion
12 years ago
vtkSmartPointer<vtkProp> cv::viz::WidgetAccessor::getProp(const Widget& widget)
{
12 years ago
return widget.impl_->prop;
}
12 years ago
void cv::viz::WidgetAccessor::setProp(Widget& widget, vtkSmartPointer<vtkProp> prop)
{
12 years ago
widget.impl_->prop = prop;
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// widget3D implementation
12 years ago
struct cv::viz::Widget3D::MatrixConverter
{
12 years ago
static Matx44f convertToMatx(const vtkSmartPointer<vtkMatrix4x4>& vtk_matrix)
{
12 years ago
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;
}
12 years ago
static vtkSmartPointer<vtkMatrix4x4> convertToVtkMatrix (const 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;
}
};
12 years ago
void cv::viz::Widget3D::setPose(const Affine3f &pose)
{
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix);
actor->SetUserMatrix (matrix);
actor->Modified ();
}
12 years ago
void cv::viz::Widget3D::updatePose(const Affine3f &pose)
{
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix();
if (!matrix)
{
setPose(pose);
return ;
}
Matx44f matrix_cv = MatrixConverter::convertToMatx(matrix);
Affine3f updated_pose = pose * Affine3f(matrix_cv);
matrix = MatrixConverter::convertToVtkMatrix(updated_pose.matrix);
actor->SetUserMatrix (matrix);
actor->Modified ();
}
12 years ago
cv::Affine3f cv::viz::Widget3D::getPose() const
{
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix();
Matx44f matrix_cv = MatrixConverter::convertToMatx(matrix);
return Affine3f(matrix_cv);
}
12 years ago
void cv::viz::Widget3D::setColor(const Color &color)
{
// Cast to actor instead of prop3d since prop3d doesn't provide getproperty
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
Color c = vtkcolor(color);
actor->GetMapper ()->ScalarVisibilityOff ();
actor->GetProperty ()->SetColor (c.val);
actor->GetProperty ()->SetEdgeColor (c.val);
actor->Modified ();
}
12 years ago
template<> cv::viz::Widget3D cv::viz::Widget::cast<cv::viz::Widget3D>()
{
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
Widget3D widget;
WidgetAccessor::setProp(widget, actor);
return widget;
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// widget2D implementation
12 years ago
void cv::viz::Widget2D::setColor(const Color &color)
{
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
Color c = vtkcolor(color);
actor->GetProperty ()->SetColor (c.val);
actor->Modified ();
12 years ago
}
12 years ago
template<> cv::viz::Widget2D cv::viz::Widget::cast<cv::viz::Widget2D>()
{
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
CV_Assert(actor);
Widget2D widget;
WidgetAccessor::setProp(widget, actor);
return widget;
}