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 #include <swri_route_util/route.h>
00030 #include <swri_route_util/route_point.h>
00031 #include <boost/make_shared.hpp>
00032
00033 namespace mnm = marti_nav_msgs;
00034
00035 namespace swri_route_util
00036 {
00037 Route::Route()
00038 {
00039 }
00040
00041
00042 static
00043 void pointFromMsg(RoutePoint &dst, const marti_nav_msgs::RoutePoint &src)
00044 {
00045 dst.setPose(src.pose);
00046 dst.setId(src.id);
00047
00048 for (auto const &prop : src.properties) {
00049 dst.setProperty(prop.key, prop.value);
00050 }
00051 }
00052
00053 Route::Route(const mnm::Route &msg)
00054 {
00055 header = msg.header;
00056
00057 points.resize(msg.route_points.size());
00058 for (size_t i = 0; i < points.size(); ++i) {
00059 pointFromMsg(points[i], msg.route_points[i]);
00060 }
00061
00062 for (auto const &prop : msg.properties) {
00063 setProperty(prop.key, prop.value);
00064 }
00065
00066 rebuildPointIndex();
00067 }
00068
00069
00070 static
00071 void msgFromPoint(marti_nav_msgs::RoutePoint &dst, const RoutePoint &src)
00072 {
00073 dst.pose = src.poseMsg();
00074 dst.id = src.id();
00075
00076 std::vector<std::string> names = src.getPropertyNames();
00077 dst.properties.resize(names.size());
00078 for (size_t i = 0; i < names.size(); ++i) {
00079 dst.properties[i].key = names[i];
00080 dst.properties[i].value = src.getProperty(names[i]);
00081 }
00082 }
00083
00084 void Route::toMsg(mnm::Route &msg) const
00085 {
00086 msg.header = header;
00087
00088 msg.route_points.resize(points.size());
00089 for (size_t i = 0; i < points.size(); ++i) {
00090 msgFromPoint(msg.route_points[i], points[i]);
00091 }
00092
00093 std::vector<std::string> names = getPropertyNames();
00094 msg.properties.resize(names.size());
00095 for (size_t i = 0; i < names.size(); ++i) {
00096 msg.properties[i].key = names[i];
00097 msg.properties[i].value = getProperty(names[i]);
00098 }
00099 }
00100
00101 mnm::RoutePtr Route::toMsgPtr() const
00102 {
00103 mnm::RoutePtr ptr = boost::make_shared<mnm::Route>();
00104 toMsg(*ptr);
00105 return ptr;
00106 }
00107
00108 bool Route::valid() const
00109 {
00110 return !points.empty();
00111 }
00112
00113 bool Route::findPointId(size_t &index, const std::string &id) const
00114 {
00115 if (point_index_.count(id)) {
00116 size_t i = point_index_.at(id);
00117 if (i < points.size() && points[i].id() == id) {
00118
00119 index = i;
00120 return true;
00121 }
00122
00123
00124 }
00125
00126
00127
00128
00129 rebuildPointIndex();
00130
00131 if (point_index_.count(id)) {
00132
00133
00134
00135 index = point_index_.at(id);
00136 return true;
00137 }
00138
00139
00140 return false;
00141 }
00142
00143 bool Route::findPointIdConst(size_t &index, const std::string &id) const
00144 {
00145 if (point_index_.count(id)) {
00146 size_t i = point_index_.at(id);
00147 if (i < points.size() && points[i].id() == id) {
00148
00149 index = i;
00150 return true;
00151 }
00152
00153 }
00154
00155 return false;
00156 }
00157
00158 std::vector<std::string> Route::getPropertyNames() const
00159 {
00160 std::vector<std::string> names;
00161 names.push_back("name");
00162 names.push_back("guid");
00163
00164 for (auto const &it : properties_) {
00165 names.push_back(it.first);
00166 }
00167
00168 return names;
00169 }
00170
00171 std::string Route::getProperty(const std::string &name) const
00172 {
00173 if (name == "name") {
00174 return name_;
00175 } else if (name == "guid") {
00176 return guid_;
00177 } else if (properties_.count(name)) {
00178 return properties_.at(name);
00179 } else {
00180 return "";
00181 }
00182 }
00183
00184 bool Route::hasProperty(const std::string &name) const
00185 {
00186 if (name == "name") {
00187 return true;
00188 } else if (name == "guid") {
00189 return true;
00190 } else {
00191 return properties_.count(name) > 0;
00192 }
00193 }
00194
00195 void Route::setProperty(const std::string &name, const std::string &value)
00196 {
00197 if (name == "name") {
00198 name_ = value;
00199 } else if (name == "guid") {
00200 guid_ = value;
00201 } else {
00202 properties_[name] = value;
00203 }
00204 }
00205
00206 void Route::deleteProperty(const std::string &name)
00207 {
00208
00209
00210
00211
00212
00213 properties_.erase(name);
00214 }
00215
00216 std::string Route::name() const
00217 {
00218 return name_;
00219 }
00220
00221 void Route::setName(const std::string &name)
00222 {
00223 name_ = name;
00224 }
00225
00226 std::string Route::guid() const
00227 {
00228 return guid_;
00229 }
00230
00231 void Route::setGuid(const std::string &guid)
00232 {
00233 guid_ = guid;
00234 }
00235
00236 void Route::rebuildPointIndex() const
00237 {
00238
00239 point_index_.clear();
00240 for (size_t i = 0; i < points.size(); ++i) {
00241 point_index_[points[i].id()] = i;
00242 }
00243
00244 if (point_index_.size() != points.size()) {
00245 ROS_ERROR("Route points do not have unique IDs. This will likely cause problems.");
00246 }
00247 }
00248 }