Skip to content

Commit ea9589e

Browse files
larshgjanekT
andauthored
point and area picking improvement for cloud names (#5476)
* point and area picking improvement for cloud names * Change to index_t for remaining int indexes / point counts. * Added simple area callback to pcd_viewer. * Comment unused variable. * Apply clang-tidy suggestions. * Fix more indices types. * Removed nb_points. Replaced static_cast<pcl::index_t>(-1) with pcl::UNAVAILABLE. Co-authored-by: janekT <j.trochta@gmail.com>
1 parent 0cbaf1e commit ea9589e

File tree

4 files changed

+153
-66
lines changed

4 files changed

+153
-66
lines changed

tools/pcd_viewer.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ printHelp (int, char **argv)
136136
print_info ("\n");
137137
print_info (" -use_point_picking = enable the usage of picking points on screen (default "); print_value ("disabled"); print_info (")\n");
138138
print_info ("\n");
139+
print_info (" -use_area_picking = enable the usage of area picking points on screen (default "); print_value("disabled"); print_info(")\n");
140+
print_info ("\n");
139141
print_info (" -optimal_label_colors = maps existing labels to the optimal sequential glasbey colors, label_ids will not be mapped to fixed colors (default "); print_value ("disabled"); print_info (")\n");
140142
print_info ("\n");
141143
print_info (" -edl = Enable Eye-Dome Lighting rendering, to improve depth perception. (default: "); print_value ("disabled"); print_info (")\n");
@@ -152,6 +154,18 @@ pcl::search::KdTree<pcl::PointXYZ> search;
152154
pcl::PCLPointCloud2::Ptr cloud;
153155
pcl::PointCloud<pcl::PointXYZ>::Ptr xyzcloud;
154156

157+
void
158+
area_callback(const pcl::visualization::AreaPickingEvent& event, void* /*cookie*/)
159+
{
160+
const auto names = event.getCloudNames();
161+
162+
for (const std::string& name : names) {
163+
const pcl::Indices indices = event.getPointsIndices(name);
164+
165+
PCL_INFO("Picked %d points from %s \n", indices.size(), name.c_str());
166+
}
167+
}
168+
155169
void
156170
pp_callback (const pcl::visualization::PointPickingEvent& event, void* cookie)
157171
{
@@ -284,6 +298,10 @@ main (int argc, char** argv)
284298
if (use_pp)
285299
print_highlight ("Point picking enabled.\n");
286300

301+
bool use_ap = pcl::console::find_switch(argc, argv, "-use_area_picking");
302+
if (use_ap)
303+
print_highlight("Area picking enabled. \n");
304+
287305
bool use_optimal_l_colors = pcl::console::find_switch (argc, argv, "-optimal_label_colors");
288306
if (use_optimal_l_colors)
289307
print_highlight ("Optimal glasbey colors are being assigned to existing labels.\nNote: No static mapping between label ids and colors\n");
@@ -489,6 +507,9 @@ main (int argc, char** argv)
489507
if (use_pp) // Only enable the point picking callback if the command line parameter is enabled
490508
p->registerPointPickingCallback (&pp_callback, static_cast<void*> (&cloud));
491509

510+
if (use_ap)
511+
p->registerAreaPickingCallback(&area_callback);
512+
492513
if (useEDLRendering)
493514
p->enableEDLRendering();
494515

visualization/include/pcl/visualization/area_picking_event.h

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
#include <pcl/pcl_macros.h>
4242

43+
#include <map>
44+
4345
namespace pcl
4446
{
4547
namespace visualization
@@ -48,27 +50,58 @@ namespace pcl
4850
class PCL_EXPORTS AreaPickingEvent
4951
{
5052
public:
51-
AreaPickingEvent (int nb_points, const pcl::Indices& indices)
52-
: nb_points_ (nb_points)
53-
, indices_ (indices)
53+
AreaPickingEvent (std::map<std::string, pcl::Indices> cloud_indices)
54+
: cloud_indices_ (std::move(cloud_indices))
5455
{}
5556

56-
/** \brief For situations where a whole are is selected, return the points indices.
57+
PCL_DEPRECATED(1,16,"This constructor is deprecated!")
58+
AreaPickingEvent(int /*nb_points*/, const pcl::Indices& indices)
59+
: AreaPickingEvent ({{"",indices}}) {}
60+
61+
/** \brief For situations where a whole area is selected, return the points indices.
5762
* \param[out] indices indices of the points under the area selected by user.
5863
* \return true, if the area selected by the user contains points, false otherwise
5964
*/
6065
inline bool
6166
getPointsIndices (pcl::Indices& indices) const
6267
{
63-
if (nb_points_ <= 0)
68+
if (cloud_indices_.empty())
6469
return (false);
65-
indices = indices_;
70+
71+
for (const auto& i : cloud_indices_)
72+
indices.insert(indices.cend (), i.second.cbegin (), i.second.cend ());
73+
6674
return (true);
6775
}
76+
/** \brief For situations where a whole area is selected, return the names
77+
* of the selected point clouds.
78+
* \return The names of selected point clouds
79+
*/
80+
inline std::vector<std::string>
81+
getCloudNames () const
82+
{
83+
std::vector<std::string> names;
84+
for (const auto& i : cloud_indices_)
85+
names.push_back (i.first);
86+
return names;
87+
}
88+
/** \brief For situations where a whole area is selected, return the points indices
89+
* for a given point cloud
90+
* \param[in] name of selected clouds.
91+
* \return The indices for the selected cloud.
92+
*/
93+
inline Indices
94+
getPointsIndices (const std::string& name) const
95+
{
96+
const auto cloud = cloud_indices_.find (name);
97+
if(cloud == cloud_indices_.cend ())
98+
return Indices ();
99+
100+
return cloud->second;
101+
}
68102

69103
private:
70-
int nb_points_;
71-
pcl::Indices indices_;
104+
std::map<std::string, pcl::Indices> cloud_indices_;
72105
};
73106
} //namespace visualization
74107
} //namespace pcl

visualization/include/pcl/visualization/point_picking_event.h

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@
3838

3939
#pragma once
4040

41+
#include <pcl/pcl_base.h>
4142
#include <pcl/pcl_macros.h>
4243
#include <pcl/types.h> // for pcl::Indices
44+
#include <pcl/visualization/common/actor_map.h>
4345

4446
#include <vtkCommand.h>
47+
#include <vtkActor.h>
48+
49+
#include <map>
50+
#include <vector>
51+
52+
4553
class vtkRenderWindowInteractor;
4654

4755
namespace pcl
@@ -55,38 +63,39 @@ namespace pcl
5563
{
5664
return (new PointPickingCallback);
5765
}
58-
59-
PointPickingCallback () : x_ (0), y_ (0), z_ (0), idx_ (-1), pick_first_ (false) {}
6066

6167
/** \brief Empty destructor */
6268
~PointPickingCallback () override = default;
6369

6470
void
6571
Execute (vtkObject *caller, unsigned long eventid, void*) override;
6672

67-
int
73+
pcl::index_t
6874
performSinglePick (vtkRenderWindowInteractor *iren);
6975

70-
int
76+
pcl::index_t
7177
performSinglePick (vtkRenderWindowInteractor *iren, float &x, float &y, float &z);
7278

73-
int
74-
performAreaPick (vtkRenderWindowInteractor *iren, pcl::Indices &indices) const;
79+
pcl::index_t
80+
performAreaPick (vtkRenderWindowInteractor *iren,
81+
CloudActorMapPtr cam_ptr,
82+
std::map<std::string, pcl::Indices>& cloud_indices) const;
83+
7584

7685
private:
77-
float x_, y_, z_;
78-
int idx_;
79-
bool pick_first_;
86+
float x_{0}, y_{0}, z_{0};
87+
pcl::index_t idx_{pcl::UNAVAILABLE};
88+
bool pick_first_{false};
89+
const vtkActor* actor_{nullptr};
8090
};
8191

8292
/** /brief Class representing 3D point picking events. */
8393
class PCL_EXPORTS PointPickingEvent
8494
{
8595
public:
86-
PointPickingEvent (int idx) : idx_ (idx), idx2_ (-1), x_ (), y_ (), z_ (), x2_ (), y2_ (), z2_ () {}
87-
PointPickingEvent (int idx, float x, float y, float z) : idx_ (idx), idx2_ (-1), x_ (x), y_ (y), z_ (z), x2_ (), y2_ (), z2_ () {}
88-
89-
PointPickingEvent (int idx1, int idx2, float x1, float y1, float z1, float x2, float y2, float z2) :
96+
PointPickingEvent (pcl::index_t idx) : PointPickingEvent ( idx, -1,-1, -1) {}
97+
PointPickingEvent (pcl::index_t idx, float x, float y, float z, const std::string& name = "") : idx_ (idx), idx2_ (-1), x_ (x), y_ (y), z_ (z), x2_ (), y2_ (), z2_ (), name_ (name) {}
98+
PointPickingEvent (pcl::index_t idx1, pcl::index_t idx2, float x1, float y1, float z1, float x2, float y2, float z2) :
9099
idx_ (idx1), idx2_ (idx2), x_ (x1), y_ (y1), z_ (z1), x2_ (x2), y2_ (y2), z2_ (z2)
91100
{}
92101

@@ -97,7 +106,7 @@ namespace pcl
97106
* cloud for the correct index. An example of how to do this can be found in the pp_callback function in
98107
* visualization/tools/pcd_viewer.cpp
99108
*/
100-
inline int
109+
inline pcl::index_t
101110
getPointIndex () const
102111
{
103112
return (idx_);
@@ -126,7 +135,7 @@ namespace pcl
126135
inline bool
127136
getPoints (float &x1, float &y1, float &z1, float &x2, float &y2, float &z2) const
128137
{
129-
if (idx2_ == -1)
138+
if (idx2_ == pcl::UNAVAILABLE)
130139
return (false);
131140
x1 = x_; y1 = y_; z1 = z_;
132141
x2 = x2_; y2 = y2_; z2 = z2_;
@@ -144,20 +153,27 @@ namespace pcl
144153
* visualization/tools/pcd_viewer.cpp
145154
*/
146155
inline bool
147-
getPointIndices (int &index_1, int &index_2) const
156+
getPointIndices(pcl::index_t& index_1, pcl::index_t& index_2) const
148157
{
149-
if (idx2_ == -1)
158+
if (idx2_ == pcl::UNAVAILABLE)
150159
return (false);
151160
index_1 = idx_;
152161
index_2 = idx2_;
153162
return (true);
154163
}
155164

165+
/** \brief Get name of selected cloud.
166+
* \return name of the cloud selected by the user
167+
*/
168+
inline const std::string&
169+
getCloudName () const { return name_; }
170+
156171
private:
157-
int idx_, idx2_;
172+
pcl::index_t idx_, idx2_;
158173

159174
float x_, y_, z_;
160175
float x2_, y2_, z2_;
176+
std::string name_;
161177
};
162178
} //namespace visualization
163179
} //namespace pcl

0 commit comments

Comments
 (0)