Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "ros/console.h"
00033
00034 #include "industrial_utils/utils.h"
00035 #include "industrial_robot_client/utils.h"
00036
00037 #include <cmath>
00038 #include <iostream>
00039
00040 namespace industrial_robot_client
00041 {
00042 namespace utils
00043 {
00044
00045 bool isWithinRange(const std::vector<double> & lhs, const std::vector<double> & rhs, double full_range)
00046 {
00047 bool rtn = false;
00048
00049 if (lhs.size() != rhs.size())
00050 {
00051 ROS_ERROR_STREAM(__FUNCTION__ << "::lhs size: " << lhs.size() << " does not match rhs size: " << rhs.size());
00052 rtn = false;
00053 }
00054 else
00055 {
00056
00057 double half_range = full_range / 2.0;
00058 rtn = true;
00059
00060
00061 for (size_t i = 0; i < lhs.size(); ++i)
00062 {
00063 if (fabs(lhs[i] - rhs[i]) > fabs(half_range))
00064 {
00065 rtn = false;
00066 break;
00067 }
00068 }
00069
00070 }
00071
00072 return rtn;
00073 }
00074
00075 bool mapInsert(const std::string & key, double value, std::map<std::string, double> & mappings)
00076 {
00077 bool rtn = false;
00078
00079 std::pair<std::map<std::string, double>::iterator, bool> insert_rtn;
00080
00081 insert_rtn = mappings.insert(std::make_pair(key, value));
00082
00083
00084 if (!insert_rtn.second)
00085 {
00086 ROS_ERROR_STREAM(__FUNCTION__ << "::Failed to insert item into map with key: " << key);
00087 rtn = false;
00088 }
00089 else
00090 {
00091 rtn = true;
00092 }
00093 return rtn;
00094
00095 }
00096
00097 bool toMap(const std::vector<std::string> & keys, const std::vector<double> & values,
00098 std::map<std::string, double> & mappings)
00099 {
00100 bool rtn;
00101
00102 mappings.clear();
00103
00104 if (keys.size() == values.size())
00105 {
00106 rtn = true;
00107
00108 for (size_t i = 0; i < keys.size(); ++i)
00109 {
00110 rtn = mapInsert(keys[i], values[i], mappings);
00111 if (!rtn)
00112 {
00113 break;
00114 }
00115 }
00116
00117 }
00118 else
00119 {
00120 ROS_ERROR_STREAM(__FUNCTION__ << "::keys size: " << keys.size()
00121 << " does not match values size: " << values.size());
00122
00123 rtn = false;
00124 }
00125
00126 return rtn;
00127 }
00128
00129 bool isWithinRange(const std::vector<std::string> & keys, const std::map<std::string, double> & lhs,
00130 const std::map<std::string, double> & rhs, double full_range)
00131 {
00132 bool rtn = false;
00133
00134 if ((keys.size() != rhs.size()) || (keys.size() != lhs.size()))
00135 {
00136 ROS_ERROR_STREAM(__FUNCTION__ << "::Size mistmatch ::lhs size: " << lhs.size() <<
00137 " rhs size: " << rhs.size() << " key size: " << keys.size());
00138
00139 rtn = false;
00140 }
00141 else
00142 {
00143
00144 double half_range = full_range / 2.0;
00145 rtn = true;
00146
00147
00148 for (size_t i = 0; i < keys.size(); ++i)
00149 {
00150 if (fabs(lhs.at(keys[i]) - rhs.at(keys[i])) > fabs(half_range))
00151 {
00152 rtn = false;
00153 break;
00154 }
00155 }
00156
00157 }
00158
00159 return rtn;
00160 }
00161
00162 bool isWithinRange(const std::vector<std::string> & lhs_keys, const std::vector<double> & lhs_values,
00163 const std::vector<std::string> & rhs_keys, const std::vector<double> & rhs_values, double full_range)
00164 {
00165 bool rtn = false;
00166 std::map<std::string, double> lhs_map;
00167 std::map<std::string, double> rhs_map;
00168 if (industrial_utils::isSimilar(lhs_keys, rhs_keys))
00169 {
00170 if (toMap(lhs_keys, lhs_values, lhs_map) && toMap(rhs_keys, rhs_values, rhs_map))
00171 {
00172 rtn = isWithinRange(lhs_keys, lhs_map, rhs_map, full_range);
00173 }
00174 }
00175 else
00176 {
00177 ROS_ERROR_STREAM(__FUNCTION__ << "::Key vectors are not similar");
00178 rtn = false;
00179 }
00180 return rtn;
00181 }
00182
00183 }
00184 }