utils.cpp
Go to the documentation of this file.
00001 /*
00002  * ar_marker_processor.hpp
00003  *
00004  *  utility functions
00005  *
00006  *  LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
00007  *
00008  *  Created on: Apr 6, 2013
00009  *      Author: jorge
00010  *  Modified on: Dec, 2013 
00011  *      Jihoon Lee          
00012  *
00013  */
00014 
00015 #include "yocs_ar_marker_tracking/tracking.hpp"
00016 
00017 namespace yocs {
00018 
00019 bool ARMarkerTracking::spotted(double younger_than,
00020                            const ar_track_alvar_msgs::AlvarMarkers& including,
00021                            const ar_track_alvar_msgs::AlvarMarkers& excluding,
00022                                   ar_track_alvar_msgs::AlvarMarkers& spotted)
00023 {
00024   if (spotted_markers_.markers.size() == 0)
00025     return false;
00026 
00027   if ((ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec() >= younger_than)
00028   {
00029     return false;
00030   }
00031 
00032   spotted.header = spotted_markers_.header;
00033   spotted.markers.clear();
00034   for (unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
00035   {
00036     if ((included(spotted_markers_.markers[i].id, including) == true) &&
00037         (excluded(spotted_markers_.markers[i].id, excluding) == true))
00038     {
00039       spotted.markers.push_back(spotted_markers_.markers[i]);
00040     }
00041   }
00042 
00043   return (spotted.markers.size() > 0);
00044 }
00045 
00046 bool ARMarkerTracking::closest(const ar_track_alvar_msgs::AlvarMarkers& including, const ar_track_alvar_msgs::AlvarMarkers& excluding, ar_track_alvar_msgs::AlvarMarker& closest)
00047 {
00048   double closest_dist = std::numeric_limits<double>::max();
00049   for (unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
00050   {
00051     if ((included(spotted_markers_.markers[i].id, including) == true) &&
00052         (excluded(spotted_markers_.markers[i].id, excluding) == true))
00053     {
00054       double d = mtk::distance2D(spotted_markers_.markers[i].pose.pose.position);
00055       if (d < closest_dist)
00056       {
00057         closest_dist = d;
00058         closest = spotted_markers_.markers[i];
00059       }
00060     }
00061   }
00062 
00063   return (closest_dist < std::numeric_limits<double>::max());
00064 }
00065 
00066 bool ARMarkerTracking::spotted(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers& spotted)
00067 {
00068   if (spotted_markers_.markers.size() == 0)
00069     return false;
00070 
00071   if ((ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec() >= younger_than)
00072   {
00073     // We must check the timestamp from an element in the markers list, as the one on message's header is always zero!
00074     // WARNING: parameter younger_than must be high enough, as ar_track_alvar publish at Kinect rate but only updates
00075     // timestamps about every 0.1 seconds (and now we can set it to run slower, as frequency is a dynamic parameter!)
00076     ROS_WARN("Spotted markers too old:   %f  >=  %f",   (ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec(), younger_than);
00077     return false;
00078   }
00079 
00080   spotted.header = spotted_markers_.header;
00081   spotted.markers.clear();
00082   for (unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
00083   {
00084     if (tracked_markers_[spotted_markers_.markers[i].id].confidence >= min_confidence)
00085     {
00086       spotted.markers.push_back(spotted_markers_.markers[i]);
00087     }
00088   }
00089 
00090   return (spotted.markers.size() > 0);
00091 }
00092 
00093 bool ARMarkerTracking::spotted(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers& excluding, ar_track_alvar_msgs::AlvarMarkers& spotted)
00094 {
00095   if (spotted_markers_.markers.size() == 0)
00096     return false;
00097 
00098   if ((ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec() >= younger_than)
00099   {
00100     // We must check the timestamp from an element in the markers list, as the one on message's header is always zero!
00101     // WARNING: parameter younger_than must be high enough, as ar_track_alvar publish at Kinect rate but only updates
00102     // timestamps about every 0.1 seconds (and now we can set it to run slower, as frequency is a dynamic parameter!)
00103     ROS_WARN("Spotted markers too old:   %f  >=  %f",   (ros::Time::now() - spotted_markers_.markers[0].header.stamp).toSec(), younger_than);
00104     return false;
00105   }
00106   
00107   spotted.header = spotted_markers_.header;
00108   spotted.markers.clear();
00109   for(unsigned int i = 0; i < spotted_markers_.markers.size(); i++)
00110   {
00111     ar_track_alvar_msgs::AlvarMarker m = spotted_markers_.markers[i];
00112 
00113     if ((tracked_markers_[m.id].confidence >= min_confidence) &&(excluded(m.id, excluding)))
00114     {
00115       spotted.markers.push_back(m);
00116     }
00117   }
00118 
00119   return (spotted.markers.size() > 0);
00120 }
00121 
00122 bool ARMarkerTracking::closest(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarker& closest)
00123 {
00124   ar_track_alvar_msgs::AlvarMarkers spotted_markers;
00125   if (spotted(younger_than, min_confidence, spotted_markers) == false)
00126     return false;
00127 
00128   double closest_dist = std::numeric_limits<double>::max();
00129   for (unsigned int i = 0; i < spotted_markers.markers.size(); i++)
00130   {
00131     double d = mtk::distance2D(spotted_markers.markers[i].pose.pose.position);
00132     if (d < closest_dist)
00133     {
00134       closest_dist = d;
00135       closest = spotted_markers.markers[i];
00136     }
00137   }
00138 
00139   return (closest_dist < std::numeric_limits<double>::max());
00140 }
00141 
00142 bool ARMarkerTracking::closest(double younger_than, double min_confidence, ar_track_alvar_msgs::AlvarMarkers& excluding, ar_track_alvar_msgs::AlvarMarker& closest)
00143 {
00144   ar_track_alvar_msgs::AlvarMarkers spotted_markers;
00145   if (spotted(younger_than, min_confidence, excluding, spotted_markers) == false)
00146     return false;
00147 
00148   double closest_dist = std::numeric_limits<double>::max();
00149   for (unsigned int i = 0; i < spotted_markers.markers.size(); i++)
00150   {
00151     double d = mtk::distance2D(spotted_markers.markers[i].pose.pose.position);
00152     if (d < closest_dist)
00153     {
00154       closest_dist = d;
00155       closest = spotted_markers.markers[i];
00156     }
00157   }
00158 
00159   return (closest_dist < std::numeric_limits<double>::max());
00160 }
00161 
00162 
00163 // check if the given id ar_marker is in the list. If yes, return the full ar marker data
00164 bool ARMarkerTracking::included(const uint32_t id, const ar_track_alvar_msgs::AlvarMarkers& v, ar_track_alvar_msgs::AlvarMarker* e)
00165 {
00166   for (unsigned int i = 0; i < v.markers.size(); i++)
00167   {
00168     if (id == v.markers[i].id)
00169     {
00170       if (e != NULL)
00171         *e = v.markers[i];
00172 
00173       return true;
00174     }
00175   }
00176 
00177   return false;
00178 }
00179 
00180 // Check if the given id is in the list of ar markers
00181 bool ARMarkerTracking::excluded(const uint32_t id, const ar_track_alvar_msgs::AlvarMarkers& v)
00182 {
00183   for (unsigned int i = 0; i < v.markers.size(); i++)
00184   {
00185     if (id == v.markers[i].id)
00186       return false;
00187   }
00188 
00189   return true;
00190 }
00191 
00192 } // yocs 


yocs_ar_marker_tracking
Author(s): Daniel Stonier, Jorge Santos
autogenerated on Thu Jun 6 2019 21:47:27