utils.cpp
Go to the documentation of this file.
1 /*
2  * ar_marker_processor.hpp
3  *
4  * utility functions
5  *
6  * LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
7  *
8  * Created on: Apr 6, 2013
9  * Author: jorge
10  * Modified on: Dec, 2013
11  * Jihoon Lee
12  *
13  */
14 
16 
17 namespace yocs {
18 
19 bool ARMarkerTracking::spotted(double younger_than,
20  const ar_track_alvar_msgs::AlvarMarkers& including,
21  const ar_track_alvar_msgs::AlvarMarkers& excluding,
22  ar_track_alvar_msgs::AlvarMarkers& spotted)
23 {
24  if (spotted_markers_.markers.size() == 0)
25  return false;
26 
27  if ((ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec() >= younger_than)
28  {
29  return false;
30  }
31 
32  spotted.header = spotted_markers_.header;
33  spotted.markers.clear();
34  for (unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
35  {
36  if ((included(spotted_markers_.markers[i].id, including) == true) &&
37  (excluded(spotted_markers_.markers[i].id, excluding) == true))
38  {
39  spotted.markers.push_back(spotted_markers_.markers[i]);
40  }
41  }
42 
43  return (spotted.markers.size() > 0);
44 }
45 
46 bool ARMarkerTracking::closest(const ar_track_alvar_msgs::AlvarMarkers& including, const ar_track_alvar_msgs::AlvarMarkers& excluding, ar_track_alvar_msgs::AlvarMarker& closest)
47 {
48  double closest_dist = std::numeric_limits<double>::max();
49  for (unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
50  {
51  if ((included(spotted_markers_.markers[i].id, including) == true) &&
52  (excluded(spotted_markers_.markers[i].id, excluding) == true))
53  {
54  double d = mtk::distance2D(spotted_markers_.markers[i].pose.pose.position);
55  if (d < closest_dist)
56  {
57  closest_dist = d;
58  closest = spotted_markers_.markers[i];
59  }
60  }
61  }
62 
63  return (closest_dist < std::numeric_limits<double>::max());
64 }
65 
66 bool ARMarkerTracking::spotted(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers& spotted)
67 {
68  if (spotted_markers_.markers.size() == 0)
69  return false;
70 
71  if ((ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec() >= younger_than)
72  {
73  // We must check the timestamp from an element in the markers list, as the one on message's header is always zero!
74  // WARNING: parameter younger_than must be high enough, as ar_track_alvar publish at Kinect rate but only updates
75  // timestamps about every 0.1 seconds (and now we can set it to run slower, as frequency is a dynamic parameter!)
76  ROS_WARN("Spotted markers too old: %f >= %f", (ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec(), younger_than);
77  return false;
78  }
79 
80  spotted.header = spotted_markers_.header;
81  spotted.markers.clear();
82  for (unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
83  {
84  if (tracked_markers_[spotted_markers_.markers[i].id].confidence >= min_confidence)
85  {
86  spotted.markers.push_back(spotted_markers_.markers[i]);
87  }
88  }
89 
90  return (spotted.markers.size() > 0);
91 }
92 
93 bool ARMarkerTracking::spotted(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers& excluding, ar_track_alvar_msgs::AlvarMarkers& spotted)
94 {
95  if (spotted_markers_.markers.size() == 0)
96  return false;
97 
98  if ((ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec() >= younger_than)
99  {
100  // We must check the timestamp from an element in the markers list, as the one on message's header is always zero!
101  // WARNING: parameter younger_than must be high enough, as ar_track_alvar publish at Kinect rate but only updates
102  // timestamps about every 0.1 seconds (and now we can set it to run slower, as frequency is a dynamic parameter!)
103  ROS_WARN("Spotted markers too old: %f >= %f", (ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec(), younger_than);
104  return false;
105  }
106 
107  spotted.header = spotted_markers_.header;
108  spotted.markers.clear();
109  for(unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
110  {
111  ar_track_alvar_msgs::AlvarMarker m = spotted_markers_.markers[i];
112 
113  if ((tracked_markers_[m.id].confidence >= min_confidence) &&(excluded(m.id, excluding)))
114  {
115  spotted.markers.push_back(m);
116  }
117  }
118 
119  return (spotted.markers.size() > 0);
120 }
121 
122 bool ARMarkerTracking::closest(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarker& closest)
123 {
124  ar_track_alvar_msgs::AlvarMarkers spotted_markers;
125  if (spotted(younger_than, min_confidence, spotted_markers) == false)
126  return false;
127 
128  double closest_dist = std::numeric_limits<double>::max();
129  for (unsigned int i = 0; i < spotted_markers.markers.size(); i++)
130  {
131  double d = mtk::distance2D(spotted_markers.markers[i].pose.pose.position);
132  if (d < closest_dist)
133  {
134  closest_dist = d;
135  closest = spotted_markers.markers[i];
136  }
137  }
138 
139  return (closest_dist < std::numeric_limits<double>::max());
140 }
141 
142 bool ARMarkerTracking::closest(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers& excluding, ar_track_alvar_msgs::AlvarMarker& closest)
143 {
144  ar_track_alvar_msgs::AlvarMarkers spotted_markers;
145  if (spotted(younger_than, min_confidence, excluding, spotted_markers) == false)
146  return false;
147 
148  double closest_dist = std::numeric_limits<double>::max();
149  for (unsigned int i = 0; i < spotted_markers.markers.size(); i++)
150  {
151  double d = mtk::distance2D(spotted_markers.markers[i].pose.pose.position);
152  if (d < closest_dist)
153  {
154  closest_dist = d;
155  closest = spotted_markers.markers[i];
156  }
157  }
158 
159  return (closest_dist < std::numeric_limits<double>::max());
160 }
161 
162 
163 // check if the given id ar_marker is in the list. If yes, return the full ar marker data
164 bool ARMarkerTracking::included(const uint32_t id, const ar_track_alvar_msgs::AlvarMarkers& v, ar_track_alvar_msgs::AlvarMarker* e)
165 {
166  for (unsigned int i = 0; i < v.markers.size(); i++)
167  {
168  if (id == v.markers[i].id)
169  {
170  if (e != NULL)
171  *e = v.markers[i];
172 
173  return true;
174  }
175  }
176 
177  return false;
178 }
179 
180 // Check if the given id is in the list of ar markers
181 bool ARMarkerTracking::excluded(const uint32_t id, const ar_track_alvar_msgs::AlvarMarkers& v)
182 {
183  for (unsigned int i = 0; i < v.markers.size(); i++)
184  {
185  if (id == v.markers[i].id)
186  return false;
187  }
188 
189  return true;
190 }
191 
192 } // yocs
d
ar_track_alvar_msgs::AlvarMarkers spotted_markers_
Definition: tracking.hpp:175
std::vector< TrackedMarker > tracked_markers_
Definition: tracking.hpp:174
#define ROS_WARN(...)
double distance2D(double ax, double ay, double bx, double by)
bool spotted(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers &spotted_markers)
Definition: utils.cpp:66
bool included(const uint32_t id, const ar_track_alvar_msgs::AlvarMarkers &v, ar_track_alvar_msgs::AlvarMarker *e=NULL)
Definition: utils.cpp:164
static Time now()
bool excluded(const uint32_t id, const ar_track_alvar_msgs::AlvarMarkers &v)
Definition: utils.cpp:181
bool closest(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarker &closest_marker)
Definition: utils.cpp:122


yocs_ar_marker_tracking
Author(s): Daniel Stonier, Jorge Santos
autogenerated on Mon Jun 10 2019 15:53:43