2013-07-04 17:54:00 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2013-07-09 16:21:57 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// widget implementation
|
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
class cv::viz::Widget::Impl
|
2013-07-04 17:54:00 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-09 23:35:26 +08:00
|
|
|
vtkSmartPointer<vtkProp> prop;
|
2013-08-29 03:27:30 +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-04 17:54:00 +08:00
|
|
|
|
2013-08-29 03:27:30 +08:00
|
|
|
cv::viz::Widget::Widget() : impl_(0)
|
|
|
|
{
|
|
|
|
create();
|
|
|
|
}
|
|
|
|
|
|
|
|
cv::viz::Widget::Widget(const Widget& other) : impl_(other.impl_)
|
|
|
|
{
|
|
|
|
if (impl_) CV_XADD(&impl_->ref_counter, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
cv::viz::Widget::~Widget()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::viz::Widget::create()
|
|
|
|
{
|
|
|
|
if (impl_) release();
|
|
|
|
impl_ = new Impl();
|
|
|
|
impl_->ref_counter = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::viz::Widget::release()
|
|
|
|
{
|
|
|
|
if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1)
|
|
|
|
{
|
|
|
|
delete impl_;
|
|
|
|
impl_ = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 17:54:00 +08:00
|
|
|
|
2013-08-23 01:42:19 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-24 17:49:56 +08:00
|
|
|
void cv::viz::Widget::setRenderingProperty(int property, double value)
|
|
|
|
{
|
|
|
|
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
|
|
|
CV_Assert(actor);
|
|
|
|
|
|
|
|
switch (property)
|
|
|
|
{
|
|
|
|
case VIZ_POINT_SIZE:
|
|
|
|
{
|
|
|
|
actor->GetProperty ()->SetPointSize (float (value));
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_OPACITY:
|
|
|
|
{
|
|
|
|
actor->GetProperty ()->SetOpacity (value);
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Turn on/off flag to control whether data is rendered using immediate
|
|
|
|
// mode or note. Immediate mode rendering tends to be slower but it can
|
|
|
|
// handle larger datasets. The default value is immediate mode off. If you
|
|
|
|
// are having problems rendering a large dataset you might want to consider
|
|
|
|
// using immediate more rendering.
|
|
|
|
case VIZ_IMMEDIATE_RENDERING:
|
|
|
|
{
|
|
|
|
actor->GetMapper ()->SetImmediateModeRendering (int (value));
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_LINE_WIDTH:
|
|
|
|
{
|
|
|
|
actor->GetProperty ()->SetLineWidth (float (value));
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
CV_Assert("setPointCloudRenderingProperties: Unknown property");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double cv::viz::Widget::getRenderingProperty(int property) const
|
|
|
|
{
|
|
|
|
vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
|
|
|
CV_Assert(actor);
|
|
|
|
|
|
|
|
double value = 0.0;
|
|
|
|
switch (property)
|
|
|
|
{
|
|
|
|
case VIZ_POINT_SIZE:
|
|
|
|
{
|
|
|
|
value = actor->GetProperty ()->GetPointSize ();
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_OPACITY:
|
|
|
|
{
|
|
|
|
value = actor->GetProperty ()->GetOpacity ();
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_LINE_WIDTH:
|
|
|
|
{
|
|
|
|
value = actor->GetProperty ()->GetLineWidth ();
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_FONT_SIZE:
|
|
|
|
{
|
|
|
|
vtkTextActor* text_actor = vtkTextActor::SafeDownCast (actor);
|
|
|
|
vtkSmartPointer<vtkTextProperty> tprop = text_actor->GetTextProperty ();
|
|
|
|
tprop->SetFontSize (int (value));
|
|
|
|
text_actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_REPRESENTATION:
|
|
|
|
{
|
|
|
|
switch (int (value))
|
|
|
|
{
|
|
|
|
case REPRESENTATION_POINTS: actor->GetProperty ()->SetRepresentationToPoints (); break;
|
|
|
|
case REPRESENTATION_WIREFRAME: actor->GetProperty ()->SetRepresentationToWireframe (); break;
|
|
|
|
case REPRESENTATION_SURFACE: actor->GetProperty ()->SetRepresentationToSurface (); break;
|
|
|
|
}
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIZ_SHADING:
|
|
|
|
{
|
|
|
|
switch (int (value))
|
|
|
|
{
|
|
|
|
case SHADING_FLAT: actor->GetProperty ()->SetInterpolationToFlat (); break;
|
|
|
|
case SHADING_GOURAUD:
|
|
|
|
{
|
|
|
|
if (!actor->GetMapper ()->GetInput ()->GetPointData ()->GetNormals ())
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New ();
|
|
|
|
normals->SetInput (actor->GetMapper ()->GetInput ());
|
|
|
|
normals->Update ();
|
|
|
|
vtkDataSetMapper::SafeDownCast (actor->GetMapper ())->SetInput (normals->GetOutput ());
|
|
|
|
}
|
|
|
|
actor->GetProperty ()->SetInterpolationToGouraud ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SHADING_PHONG:
|
|
|
|
{
|
|
|
|
if (!actor->GetMapper ()->GetInput ()->GetPointData ()->GetNormals ())
|
|
|
|
{
|
|
|
|
vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New ();
|
|
|
|
normals->SetInput (actor->GetMapper ()->GetInput ());
|
|
|
|
normals->Update ();
|
|
|
|
vtkDataSetMapper::SafeDownCast (actor->GetMapper ())->SetInput (normals->GetOutput ());
|
|
|
|
}
|
|
|
|
actor->GetProperty ()->SetInterpolationToPhong ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actor->Modified ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
CV_Assert("getPointCloudRenderingProperties: Unknown property");
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-07-04 17:54:00 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
2013-07-09 17:07:21 +08:00
|
|
|
/// widget accessor implementaion
|
2013-07-04 17:54:00 +08:00
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
vtkSmartPointer<vtkProp> cv::viz::WidgetAccessor::getProp(const Widget& widget)
|
2013-07-04 17:54:00 +08:00
|
|
|
{
|
2013-07-09 23:35:26 +08:00
|
|
|
return widget.impl_->prop;
|
2013-07-04 17:54:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
void cv::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-04 17:54:00 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
2013-07-09 16:21:57 +08:00
|
|
|
/// widget3D implementation
|
2013-07-04 17:54:00 +08:00
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
struct cv::viz::Widget3D::MatrixConverter
|
2013-07-04 17:54:00 +08:00
|
|
|
{
|
2013-07-13 22:01:56 +08:00
|
|
|
static Matx44f convertToMatx(const vtkSmartPointer<vtkMatrix4x4>& vtk_matrix)
|
2013-07-09 16:21:57 +08:00
|
|
|
{
|
2013-07-13 22:01:56 +08:00
|
|
|
Matx44f m;
|
2013-07-09 16:21:57 +08:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
for (int k = 0; k < 4; k++)
|
|
|
|
m(i, k) = vtk_matrix->GetElement (i, k);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
static vtkSmartPointer<vtkMatrix4x4> convertToVtkMatrix (const Matx44f& m)
|
2013-07-09 16:21:57 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2013-07-04 17:54:00 +08:00
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
void cv::viz::Widget3D::setPose(const Affine3f &pose)
|
2013-07-04 17:54:00 +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 = convertToVtkMatrix(pose.matrix);
|
|
|
|
actor->SetUserMatrix (matrix);
|
|
|
|
actor->Modified ();
|
2013-07-04 17:54:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
void cv::viz::Widget3D::updatePose(const Affine3f &pose)
|
2013-07-04 17:54:00 +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();
|
|
|
|
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-04 17:54:00 +08:00
|
|
|
|
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-04 17:54:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
cv::Affine3f cv::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-04 17:54:00 +08:00
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
void cv::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->Modified ();
|
2013-07-04 20:57:49 +08:00
|
|
|
}
|
2013-07-09 20:12:49 +08:00
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
template<> cv::viz::Widget3D cv::viz::Widget::cast<cv::viz::Widget3D>()
|
2013-07-10 00:02:43 +08:00
|
|
|
{
|
|
|
|
vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this));
|
|
|
|
CV_Assert(actor);
|
|
|
|
|
|
|
|
Widget3D widget;
|
|
|
|
WidgetAccessor::setProp(widget, actor);
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:12:49 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// widget2D implementation
|
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
void cv::viz::Widget2D::setColor(const Color &color)
|
2013-07-09 20:28:55 +08:00
|
|
|
{
|
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
|
|
|
}
|
2013-07-10 15:55:42 +08:00
|
|
|
|
2013-07-13 22:01:56 +08:00
|
|
|
template<> cv::viz::Widget2D cv::viz::Widget::cast<cv::viz::Widget2D>()
|
2013-07-10 15:55:42 +08:00
|
|
|
{
|
|
|
|
vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this));
|
|
|
|
CV_Assert(actor);
|
|
|
|
|
|
|
|
Widget2D widget;
|
|
|
|
WidgetAccessor::setProp(widget, actor);
|
|
|
|
return widget;
|
|
|
|
}
|