From d0ad8a879b37c21bafe82dafc6d45ac67c0f2efd Mon Sep 17 00:00:00 2001 From: nizar-sallem Date: Mon, 13 Jan 2014 14:42:47 +0100 Subject: [PATCH 1/2] Add: new PCLContextItem Markers to mark several points at once --- .../pcl/visualization/vtk/pcl_context_item.h | 12 +++++++ visualization/src/vtk/pcl_context_item.cpp | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/visualization/include/pcl/visualization/vtk/pcl_context_item.h b/visualization/include/pcl/visualization/vtk/pcl_context_item.h index 437ca0986c5..9078fc5cc5c 100644 --- a/visualization/include/pcl/visualization/vtk/pcl_context_item.h +++ b/visualization/include/pcl/visualization/vtk/pcl_context_item.h @@ -156,6 +156,18 @@ namespace pcl virtual void set (float x, float y, const std::string& _text); std::string text; }; + + struct PCL_EXPORTS Markers : public Points + { + vtkTypeMacro (Markers, Points); + static Markers *New (); + virtual bool Paint (vtkContext2D *painter); + void setSize (float _size) { size = _size; } + void setPointColors (unsigned char r, unsigned char g, unsigned char b); + void setPointColors (unsigned char rgb[3]); + float size; + unsigned char point_colors[3]; + }; } } } diff --git a/visualization/src/vtk/pcl_context_item.cpp b/visualization/src/vtk/pcl_context_item.cpp index a119fb54afb..21a48528e7d 100644 --- a/visualization/src/vtk/pcl_context_item.cpp +++ b/visualization/src/vtk/pcl_context_item.cpp @@ -63,6 +63,7 @@ namespace pcl vtkStandardNewMacro (Points); vtkStandardNewMacro (Polygon); vtkStandardNewMacro (Text); + vtkStandardNewMacro (Markers); } } } @@ -233,6 +234,37 @@ pcl::visualization::context_items::Text::Paint (vtkContext2D *painter) return (true); } +/////////////////////////////////////////////////////////////////////////////////////////// +void +pcl::visualization::context_items::Markers::setPointColors (unsigned char r, unsigned char g, unsigned char b) +{ + point_colors[0] = r; point_colors[1] = g; point_colors[2] = b; +} + +/////////////////////////////////////////////////////////////////////////////////////////// +void +pcl::visualization::context_items::Markers::setPointColors (unsigned char rgb[3]) +{ + memcpy (point_colors, rgb, 3 * sizeof (unsigned char)); +} + +/////////////////////////////////////////////////////////////////////////////////////////// +bool +pcl::visualization::context_items::Markers::Paint (vtkContext2D *painter) +{ + int nb_points (params.size () / 2); + if (size <= 0) + size = 2.3 * painter->GetPen ()->GetWidth (); + + painter->GetPen ()->SetWidth (size); + painter->GetPen ()->SetColor (colors); + painter->DrawPointSprites (0, ¶ms[0], nb_points); + painter->GetPen ()->SetWidth (1); + painter->GetPen ()->SetColor (point_colors); + painter->DrawPointSprites (0, ¶ms[0], nb_points); + return (true); +} + /////////////////////////////////////////////////////////////////////////////////////////// pcl::visualization::PCLContextImageItem::PCLContextImageItem () { From c1c7c9f875fbaf417437778c163d542066b98eb1 Mon Sep 17 00:00:00 2001 From: nizar-sallem Date: Mon, 13 Jan 2014 14:46:15 +0100 Subject: [PATCH 2/2] Add: method to mark a list of points at once This is faster then looping on markPoint since all the markers are placed in the same actor and rendered only once. --- .../include/pcl/visualization/image_viewer.h | 12 ++++++ visualization/src/image_viewer.cpp | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/visualization/include/pcl/visualization/image_viewer.h b/visualization/include/pcl/visualization/image_viewer.h index 65ed38e90a5..c3ac657b122 100644 --- a/visualization/include/pcl/visualization/image_viewer.h +++ b/visualization/include/pcl/visualization/image_viewer.h @@ -429,6 +429,18 @@ namespace pcl markPoint (size_t u, size_t v, Vector3ub fg_color, Vector3ub bg_color = red_color, double radius = 3.0, const std::string &layer_id = "points", double opacity = 1.0); + /** \brief Sets the pixel at coordinates(u,v) to color while setting the neighborhood to another + * \param[in] uv the u/x, v/y coordinate of the pixels to be marked + * \param[in] fg_color the pixel color + * \param[in] bg_color the neighborhood color + * \param[in] size edge of the square surrounding each pixel + * \param[in] layer_id the name of the layer (default: "markers") + * \param[in] opacity the opacity of the layer (default: 1.0) + */ + void + markPoints (const std::vector& uv, Vector3ub fg_color, Vector3ub bg_color = red_color, double size = 3.0, + const std::string &layer_id = "markers", double opacity = 1.0); + /** \brief Set the window title name * \param[in] name the window title */ diff --git a/visualization/src/image_viewer.cpp b/visualization/src/image_viewer.cpp index eaf1af59b1f..75365306b58 100644 --- a/visualization/src/image_viewer.cpp +++ b/visualization/src/image_viewer.cpp @@ -927,6 +927,48 @@ pcl::visualization::ImageViewer::markPoint ( am_it->actor->GetScene ()->AddItem (point); } +////////////////////////////////////////////////////////////////////////////////////////// +void +pcl::visualization::ImageViewer::markPoints ( + const std::vector& uv, Vector3ub fg_color, Vector3ub bg_color, double size, + const std::string &layer_id, double opacity) +{ + if (uv.size () == 0) + return; + + // Check to see if this ID entry already exists (has it been already added to the visualizer?) + LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id)); + if (am_it == layer_map_.end ()) + { + PCL_DEBUG ("[pcl::visualization::ImageViewer::markPoint] No layer with ID='%s' found. Creating new one...\n", layer_id.c_str ()); + am_it = createLayer (layer_id, getSize ()[0] - 1, getSize ()[1] - 1, opacity, false); +#if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION > 10)) + interactor_style_->adjustCamera (ren_); +#endif + } + + vtkSmartPointer markers = vtkSmartPointer::New (); + markers->setOpacity (opacity); + std::vector points (uv.size ()); + std::size_t nb_points = points.size () / 2; + for (std::size_t i = 0; i < nb_points; ++i) + { + std::size_t ii = 2*i; + points[ii] = static_cast (uv[ii]); +#if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION > 10)) + points[ii] = static_cast (uv[ii+1]); +#else + points[ii+1] = static_cast (getSize ()[1] - uv[ii+1]); +#endif + } + + markers->set (points); + markers->setSize (size); + markers->setColors (bg_color[0], bg_color[1], bg_color[2]); + markers->setPointColors (fg_color[0], fg_color[1], fg_color[2]); + am_it->actor->GetScene ()->AddItem (markers); +} + ////////////////////////////////////////////////////////////////////////////////////////// void pcl::visualization::ImageViewer::render ()