From 26a68232078eee2a66c265561f4d9c3f211ce43e Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Fri, 21 Jun 2013 12:48:27 +0200 Subject: [PATCH] showLine implementation --- modules/viz/include/opencv2/viz/viz3d.hpp | 2 + modules/viz/src/q/viz3d_impl.hpp | 3 +- modules/viz/src/viz3d.cpp | 5 +++ modules/viz/src/viz3d_impl.cpp | 49 ++++++++++++++--------- modules/viz/test/test_viz3d.cpp | 7 ++-- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index d7e47074dd..ab76302cbe 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -31,6 +31,8 @@ namespace temp_viz void showPointCloud(const String& id, InputArray cloud, const Color& color, const Affine3f& pose = Affine3f::Identity()); bool addPointCloudNormals (const Mat &cloud, const Mat& normals, int level = 100, float scale = 0.02f, const String &id = "cloud"); + + void showLine(const String &id, const Point3f &pt1, const Point3f &pt2, const Color &color); bool addPlane (const ModelCoefficients &coefficients, const String &id = "plane"); bool addPlane (const ModelCoefficients &coefficients, double x, double y, double z, const String &id = "plane"); diff --git a/modules/viz/src/q/viz3d_impl.hpp b/modules/viz/src/q/viz3d_impl.hpp index 7afa86a7ac..af43a9b503 100644 --- a/modules/viz/src/q/viz3d_impl.hpp +++ b/modules/viz/src/q/viz3d_impl.hpp @@ -137,9 +137,10 @@ public: // This tends to close the window... interactor_->TerminateApp (); } + + void showLine (const String &id, const cv::Point3f &pt1, const cv::Point3f &pt2, const Color &color); bool addPolygon(const cv::Mat& cloud, const Color& color, const std::string &id = "polygon"); - bool addLine (const cv::Point3f &pt1, const cv::Point3f &pt2, const Color& color, const std::string &id = "line"); bool addArrow (const cv::Point3f &pt1, const cv::Point3f &pt2, const Color& color, bool display_length, const std::string &id = "arrow"); bool addArrow (const cv::Point3f &pt1, const cv::Point3f &pt2, const Color& color_line, const Color& color_text, const std::string &id = "arrow"); bool addSphere (const cv::Point3f ¢er, float radius, const Color& color, const std::string &id = "sphere"); diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index dfade9926d..7e0d324dff 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -78,6 +78,11 @@ void temp_viz::Viz3d::spinOnce (int time, bool force_redraw) impl_->spinOnce(time, force_redraw); } +void temp_viz::Viz3d::showLine(const String &id, const Point3f &pt1, const Point3f &pt2, const Color &color) +{ + impl_->showLine(id, pt1, pt2, color); +} + bool temp_viz::Viz3d::addPlane (const ModelCoefficients &coefficients, const String &id) { return impl_->addPlane(coefficients, id); diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index 2ee07ffe71..8fc4d632de 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -266,33 +266,42 @@ bool temp_viz::Viz3d::VizImpl::addPointCloudNormals (const cv::Mat &cloud, const } -//////////////////////////////////////////////////////////////////////////////////////////// -bool temp_viz::Viz3d::VizImpl::addLine (const cv::Point3f &pt1, const cv::Point3f &pt2, const Color& color, const std::string &id) +void temp_viz::Viz3d::VizImpl::showLine (const String &id, const cv::Point3f &pt1, const cv::Point3f &pt2, const Color &color) { - // Check to see if this ID entry already exists (has it been already added to the visualizer?) + // Check if this Id already exists ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - return std::cout << "[addLine] A shape with id <" << id << "> already exists! Please choose a different id and retry." << std::endl, false; + bool exists = (am_it != shape_actor_map_->end()); + + // If it exists just update + if (exists) + { + vtkSmartPointer actor = vtkLODActor::SafeDownCast (am_it->second); + reinterpret_cast(actor->GetMapper ())->SetInput(createLine(pt1,pt2)); + Color c = vtkcolor(color); + actor->GetProperty ()->SetColor (c.val); + actor->GetMapper ()->ScalarVisibilityOff (); + actor->Modified (); + } + else + { + // Create new line + vtkSmartPointer data = createLine (pt1, pt2); - vtkSmartPointer data = createLine (pt1, pt2); + // Create an Actor + vtkSmartPointer actor; + createActorFromVTKDataSet (data, actor); + actor->GetProperty ()->SetRepresentationToWireframe (); - // Create an Actor - vtkSmartPointer actor; - createActorFromVTKDataSet (data, actor); - actor->GetProperty ()->SetRepresentationToWireframe (); + Color c = vtkcolor(color); + actor->GetProperty ()->SetColor (c.val); + actor->GetMapper ()->ScalarVisibilityOff (); + renderer_->AddActor (actor); - Color c = vtkcolor(color); - actor->GetProperty ()->SetColor (c.val); - actor->GetMapper ()->ScalarVisibilityOff (); - renderer_->AddActor (actor); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = actor; - return (true); + // Save the pointer/ID pair to the global actor map + (*shape_actor_map_)[id] = actor; + } } - - bool temp_viz::Viz3d::VizImpl::addPolygonMesh (const Mesh3d& mesh, const Mat& mask, const std::string &id) { CV_Assert(mesh.cloud.type() == CV_32FC3 && mesh.cloud.rows == 1 && !mesh.polygons.empty ()); diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index 5c9141f988..e74ee1301d 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -86,18 +86,19 @@ TEST(Viz_viz3d, accuracy) float pos_x = 0.0f; float pos_y = 0.0f; float pos_z = 0.0f; - temp_viz::Mesh3d::Ptr mesh = temp_viz::mesh_load("d:/horse.ply"); - v.addPolygonMesh(*mesh, "pq"); +// temp_viz::Mesh3d::Ptr mesh = temp_viz::mesh_load("d:/horse.ply"); +// v.addPolygonMesh(*mesh, "pq"); int col_blue = 0; int col_green = 0; int col_red = 0; - + while(!v.wasStopped()) { // Creating new point cloud with id cloud1 cv::Affine3f cloudPosition(angle_x, angle_y, angle_z, cv::Vec3f(pos_x, pos_y, pos_z)); v.showPointCloud("cloud1", cloud, temp_viz::Color(col_blue, col_green, col_red), cloudPosition); + v.showLine("line1", cv::Point3f(0.0,0.0,0.0), cv::Point3f(pos_x, pos_y, pos_z) , temp_viz::Color(255-col_blue, 255-col_green, 255-col_red)); angle_x += 0.1f; angle_y -= 0.1f;