Commit 653eda45 authored by ozantonkal's avatar ozantonkal

text widget implementation

parent c4f4727e
...@@ -22,6 +22,7 @@ namespace temp_viz ...@@ -22,6 +22,7 @@ namespace temp_viz
typedef cv::Matx33d Matx33d; typedef cv::Matx33d Matx33d;
typedef cv::Affine3f Affine3f; typedef cv::Affine3f Affine3f;
typedef cv::Affine3d Affine3d; typedef cv::Affine3d Affine3d;
typedef cv::Point2i Point2i;
typedef cv::Point3f Point3f; typedef cv::Point3f Point3f;
typedef cv::Point3d Point3d; typedef cv::Point3d Point3d;
typedef cv::Matx44d Matx44d; typedef cv::Matx44d Matx44d;
......
...@@ -12,6 +12,6 @@ namespace temp_viz ...@@ -12,6 +12,6 @@ namespace temp_viz
//It is indended for those users who want to develop own widgets system using VTK library API. //It is indended for those users who want to develop own widgets system using VTK library API.
struct CV_EXPORTS WidgetAccessor struct CV_EXPORTS WidgetAccessor
{ {
static vtkSmartPointer<vtkLODActor> getActor(const Widget &widget); static vtkSmartPointer<vtkProp> getActor(const Widget &widget);
}; };
} }
...@@ -22,13 +22,17 @@ namespace temp_viz ...@@ -22,13 +22,17 @@ namespace temp_viz
void setPose(const Affine3f &pose); void setPose(const Affine3f &pose);
void updatePose(const Affine3f &pose); void updatePose(const Affine3f &pose);
Affine3f getPose() const; Affine3f getPose() const;
protected:
Widget(bool text_widget);
private: private:
class Impl; class Impl;
Impl* impl_; Impl* impl_;
void create(); void create();
void release(); void release();
void create(bool text_widget);
friend struct WidgetAccessor; friend struct WidgetAccessor;
}; };
...@@ -85,5 +89,12 @@ namespace temp_viz ...@@ -85,5 +89,12 @@ namespace temp_viz
public: public:
CoordinateSystemWidget(double scale, const Affine3f& affine); CoordinateSystemWidget(double scale, const Affine3f& affine);
}; };
class CV_EXPORTS TextWidget : public Widget
{
public:
TextWidget(const String &text, const Point2i &pos, int font_size = 10, const Color &color = Color::white());
// TODO Overload setColor method, and hide setPose, updatePose, getPose methods
};
} }
...@@ -12,7 +12,7 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C ...@@ -12,7 +12,7 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(line->GetOutput ()); mapper->SetInput(line->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
setColor(color); setColor(color);
...@@ -20,13 +20,13 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C ...@@ -20,13 +20,13 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C
void temp_viz::LineWidget::setLineWidth(float line_width) void temp_viz::LineWidget::setLineWidth(float line_width)
{ {
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->GetProperty()->SetLineWidth(line_width); actor->GetProperty()->SetLineWidth(line_width);
} }
float temp_viz::LineWidget::getLineWidth() float temp_viz::LineWidget::getLineWidth()
{ {
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
return actor->GetProperty()->GetLineWidth(); return actor->GetProperty()->GetLineWidth();
} }
...@@ -43,7 +43,7 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color ...@@ -43,7 +43,7 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(plane->GetOutput ()); mapper->SetInput(plane->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
actor->SetScale(size); actor->SetScale(size);
...@@ -64,7 +64,7 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double ...@@ -64,7 +64,7 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(plane->GetOutput ()); mapper->SetInput(plane->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
actor->SetScale(size); actor->SetScale(size);
...@@ -87,7 +87,7 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f &center, float radius, in ...@@ -87,7 +87,7 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f &center, float radius, in
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(sphere->GetOutput ()); mapper->SetInput(sphere->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
setColor(color); setColor(color);
...@@ -149,7 +149,7 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const ...@@ -149,7 +149,7 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(transformPD->GetOutput ()); mapper->SetInput(transformPD->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
setColor(color); setColor(color);
...@@ -178,7 +178,7 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, ...@@ -178,7 +178,7 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius,
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(tf->GetOutput ()); mapper->SetInput(tf->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
setColor(color); setColor(color);
...@@ -202,7 +202,7 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3 ...@@ -202,7 +202,7 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(tuber->GetOutput ()); mapper->SetInput(tuber->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
setColor(color); setColor(color);
...@@ -219,7 +219,7 @@ temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, c ...@@ -219,7 +219,7 @@ temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, c
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New (); vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
mapper->SetInput(cube->GetOutput ()); mapper->SetInput(cube->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
setColor(color); setColor(color);
...@@ -256,7 +256,7 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff ...@@ -256,7 +256,7 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff
mapper->SetScalarModeToUsePointData (); mapper->SetScalarModeToUsePointData ();
mapper->SetInput(axes_tubes->GetOutput ()); mapper->SetInput(axes_tubes->GetOutput ());
vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this); vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetMapper(mapper); actor->SetMapper(mapper);
cv::Vec3d t = affine.translation(); cv::Vec3d t = affine.translation();
...@@ -273,3 +273,19 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff ...@@ -273,3 +273,19 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff
actor->SetOrientation(0,0,0); actor->SetOrientation(0,0,0);
actor->RotateWXYZ(r_angle*180/CV_PI,rvec[0], rvec[1], rvec[2]); actor->RotateWXYZ(r_angle*180/CV_PI,rvec[0], rvec[1], rvec[2]);
} }
temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int font_size, const Color &color) : Widget(true)
{
vtkSmartPointer<vtkTextActor> actor = vtkTextActor::SafeDownCast(WidgetAccessor::getActor(*this));
actor->SetPosition (pos.x, pos.y);
actor->SetInput (text.c_str ());
vtkSmartPointer<vtkTextProperty> tprop = actor->GetTextProperty ();
tprop->SetFontSize (font_size);
tprop->SetFontFamilyToArial ();
tprop->SetJustificationToLeft ();
tprop->BoldOn ();
Color c = vtkcolor(color);
tprop->SetColor (c.val);
}
...@@ -874,12 +874,15 @@ void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget ...@@ -874,12 +874,15 @@ void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget
removeActorFromRenderer(wam_itr->second.actor); removeActorFromRenderer(wam_itr->second.actor);
} }
// Get the actor and set the user matrix // Get the actor and set the user matrix
vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget)); vtkSmartPointer<vtkLODActor> actor;
vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix); if (actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget)))
actor->SetUserMatrix (matrix); {
actor->Modified(); vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix);
renderer_->AddActor(actor); actor->SetUserMatrix (matrix);
(*widget_actor_map_)[id].actor = actor; actor->Modified();
}
renderer_->AddActor(WidgetAccessor::getActor(widget));
(*widget_actor_map_)[id].actor = WidgetAccessor::getActor(widget);
} }
bool temp_viz::Viz3d::VizImpl::removeWidget(const String &id) bool temp_viz::Viz3d::VizImpl::removeWidget(const String &id)
......
...@@ -3,34 +3,45 @@ ...@@ -3,34 +3,45 @@
class temp_viz::Widget::Impl class temp_viz::Widget::Impl
{ {
public: public:
vtkSmartPointer<vtkLODActor> actor; vtkSmartPointer<vtkProp> actor;
int ref_counter; int ref_counter;
Impl() : actor(vtkSmartPointer<vtkLODActor>::New()) {} Impl() : actor(vtkSmartPointer<vtkLODActor>::New()) {}
Impl(bool text_widget)
{
if (text_widget)
actor = vtkSmartPointer<vtkTextActor>::New();
else
actor = vtkSmartPointer<vtkLeaderActor2D>::New();
}
void setColor(const Color& color) void setColor(const Color& color)
{ {
vtkSmartPointer<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
Color c = vtkcolor(color); Color c = vtkcolor(color);
actor->GetMapper ()->ScalarVisibilityOff (); lod_actor->GetMapper ()->ScalarVisibilityOff ();
actor->GetProperty ()->SetColor (c.val); lod_actor->GetProperty ()->SetColor (c.val);
actor->GetProperty ()->SetEdgeColor (c.val); lod_actor->GetProperty ()->SetEdgeColor (c.val);
actor->GetProperty ()->SetAmbient (0.8); lod_actor->GetProperty ()->SetAmbient (0.8);
actor->GetProperty ()->SetDiffuse (0.8); lod_actor->GetProperty ()->SetDiffuse (0.8);
actor->GetProperty ()->SetSpecular (0.8); lod_actor->GetProperty ()->SetSpecular (0.8);
actor->GetProperty ()->SetLighting (0); lod_actor->GetProperty ()->SetLighting (0);
actor->Modified (); lod_actor->Modified ();
} }
void setPose(const Affine3f& pose) void setPose(const Affine3f& pose)
{ {
vtkSmartPointer<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix); vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix);
actor->SetUserMatrix (matrix); lod_actor->SetUserMatrix (matrix);
actor->Modified (); lod_actor->Modified ();
} }
void updatePose(const Affine3f& pose) void updatePose(const Affine3f& pose)
{ {
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix(); vtkSmartPointer<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = lod_actor->GetUserMatrix();
if (!matrix) if (!matrix)
{ {
setPose(pose); setPose(pose);
...@@ -41,13 +52,14 @@ public: ...@@ -41,13 +52,14 @@ public:
Affine3f updated_pose = pose * Affine3f(matrix_cv); Affine3f updated_pose = pose * Affine3f(matrix_cv);
matrix = convertToVtkMatrix(updated_pose.matrix); matrix = convertToVtkMatrix(updated_pose.matrix);
actor->SetUserMatrix (matrix); lod_actor->SetUserMatrix (matrix);
actor->Modified (); lod_actor->Modified ();
} }
Affine3f getPose() const Affine3f getPose() const
{ {
vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix(); vtkSmartPointer<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
vtkSmartPointer<vtkMatrix4x4> matrix = lod_actor->GetUserMatrix();
Matx44f matrix_cv = convertToMatx(matrix); Matx44f matrix_cv = convertToMatx(matrix);
return Affine3f(matrix_cv); return Affine3f(matrix_cv);
} }
...@@ -77,7 +89,7 @@ protected: ...@@ -77,7 +89,7 @@ protected:
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
/// stream accessor implementaion /// stream accessor implementaion
vtkSmartPointer<vtkLODActor> temp_viz::WidgetAccessor::getActor(const Widget& widget) vtkSmartPointer<vtkProp> temp_viz::WidgetAccessor::getActor(const Widget& widget)
{ {
return widget.impl_->actor; return widget.impl_->actor;
} }
...@@ -90,6 +102,11 @@ temp_viz::Widget::Widget() : impl_(0) ...@@ -90,6 +102,11 @@ temp_viz::Widget::Widget() : impl_(0)
create(); create();
} }
temp_viz::Widget::Widget(bool text_widget) : impl_(0)
{
create(text_widget);
}
temp_viz::Widget::Widget(const Widget& other) : impl_(other.impl_) temp_viz::Widget::Widget(const Widget& other) : impl_(other.impl_)
{ {
if (impl_) if (impl_)
...@@ -140,3 +157,11 @@ void temp_viz::Widget::release() ...@@ -140,3 +157,11 @@ void temp_viz::Widget::release()
} }
} }
void temp_viz::Widget::create(bool text_widget)
{
if (impl_)
release();
impl_ = new Impl(text_widget);
impl_->ref_counter = 1;
}
...@@ -98,6 +98,7 @@ TEST(Viz_viz3d, accuracy) ...@@ -98,6 +98,7 @@ TEST(Viz_viz3d, accuracy)
temp_viz::CylinderWidget cyw(cv::Point3f(0,0,0), cv::Point3f(-1,-1,-1), 0.5, 30, temp_viz::Color(0,255,0)); temp_viz::CylinderWidget cyw(cv::Point3f(0,0,0), cv::Point3f(-1,-1,-1), 0.5, 30, temp_viz::Color(0,255,0));
temp_viz::CubeWidget cuw(cv::Point3f(-2,-2,-2), cv::Point3f(-1,-1,-1), temp_viz::Color(0,0,255)); temp_viz::CubeWidget cuw(cv::Point3f(-2,-2,-2), cv::Point3f(-1,-1,-1), temp_viz::Color(0,0,255));
temp_viz::CoordinateSystemWidget csw(1.0f, cv::Affine3f::Identity()); temp_viz::CoordinateSystemWidget csw(1.0f, cv::Affine3f::Identity());
temp_viz::TextWidget tw("TEST", cv::Point2i(100,100), 20);
// v.showWidget("line", lw); // v.showWidget("line", lw);
v.showWidget("plane", pw); v.showWidget("plane", pw);
...@@ -107,6 +108,7 @@ TEST(Viz_viz3d, accuracy) ...@@ -107,6 +108,7 @@ TEST(Viz_viz3d, accuracy)
// v.showWidget("cylinder", cyw); // v.showWidget("cylinder", cyw);
// v.showWidget("cube", cuw); // v.showWidget("cube", cuw);
v.showWidget("coordinateSystem", csw); v.showWidget("coordinateSystem", csw);
v.showWidget("text",tw);
temp_viz::LineWidget lw2 = lw; temp_viz::LineWidget lw2 = lw;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment