query.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2015-2019 Autoware Foundation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Authors: Simon Thompson, Ryohsuke Mitsudome
  17. */
  18. #include <ros/ros.h>
  19. #include <Eigen/Eigen>
  20. #include <lanelet2_extension/utility/message_conversion.h>
  21. #include <lanelet2_extension/utility/query.h>
  22. #include <set>
  23. #include <string>
  24. #include <vector>
  25. namespace lanelet
  26. {
  27. namespace utils
  28. {
  29. // returns all lanelets in laneletLayer - don't know how to convert
  30. // PrimitveLayer<Lanelets> -> std::vector<Lanelets>
  31. lanelet::ConstLanelets query::laneletLayer(const lanelet::LaneletMapPtr ll_map)
  32. {
  33. lanelet::ConstLanelets lanelets;
  34. if (!ll_map)
  35. {
  36. ROS_WARN("No map received!");
  37. return lanelets;
  38. }
  39. for (auto li = ll_map->laneletLayer.begin(); li != ll_map->laneletLayer.end(); li++)
  40. {
  41. lanelets.push_back(*li);
  42. }
  43. return lanelets;
  44. }
  45. lanelet::ConstLanelets query::subtypeLanelets(const lanelet::ConstLanelets lls, const char subtype[])
  46. {
  47. lanelet::ConstLanelets subtype_lanelets;
  48. for (auto li = lls.begin(); li != lls.end(); li++)
  49. {
  50. lanelet::ConstLanelet ll = *li;
  51. if (ll.hasAttribute(lanelet::AttributeName::Subtype))
  52. {
  53. lanelet::Attribute attr = ll.attribute(lanelet::AttributeName::Subtype);
  54. if (attr.value() == subtype)
  55. {
  56. subtype_lanelets.push_back(ll);
  57. }
  58. }
  59. }
  60. return subtype_lanelets;
  61. }
  62. lanelet::ConstLanelets query::crosswalkLanelets(const lanelet::ConstLanelets lls)
  63. {
  64. return (query::subtypeLanelets(lls, lanelet::AttributeValueString::Crosswalk));
  65. }
  66. lanelet::ConstLanelets query::roadLanelets(const lanelet::ConstLanelets lls)
  67. {
  68. return (query::subtypeLanelets(lls, lanelet::AttributeValueString::Road));
  69. }
  70. std::vector<lanelet::TrafficLightConstPtr> query::trafficLights(const lanelet::ConstLanelets lanelets)
  71. {
  72. std::vector<lanelet::TrafficLightConstPtr> tl_reg_elems;
  73. for (auto i = lanelets.begin(); i != lanelets.end(); i++)
  74. {
  75. lanelet::ConstLanelet ll = *i;
  76. std::vector<lanelet::TrafficLightConstPtr> ll_tl_re = ll.regulatoryElementsAs<lanelet::TrafficLight>();
  77. // insert unique tl into array
  78. for (auto tli = ll_tl_re.begin(); tli != ll_tl_re.end(); tli++)
  79. {
  80. lanelet::TrafficLightConstPtr tl_ptr = *tli;
  81. lanelet::Id id = tl_ptr->id();
  82. bool unique_id = true;
  83. for (auto ii = tl_reg_elems.begin(); ii != tl_reg_elems.end(); ii++)
  84. {
  85. if (id == (*ii)->id())
  86. {
  87. unique_id = false;
  88. break;
  89. }
  90. }
  91. if (unique_id)
  92. {
  93. tl_reg_elems.push_back(tl_ptr);
  94. }
  95. }
  96. }
  97. return tl_reg_elems;
  98. }
  99. std::vector<lanelet::AutowareTrafficLightConstPtr> query::autowareTrafficLights(const lanelet::ConstLanelets lanelets)
  100. {
  101. std::vector<lanelet::AutowareTrafficLightConstPtr> tl_reg_elems;
  102. for (auto i = lanelets.begin(); i != lanelets.end(); i++)
  103. {
  104. lanelet::ConstLanelet ll = *i;
  105. std::vector<lanelet::AutowareTrafficLightConstPtr> ll_tl_re =
  106. ll.regulatoryElementsAs<lanelet::autoware::AutowareTrafficLight>();
  107. // insert unique tl into array
  108. for (auto tli = ll_tl_re.begin(); tli != ll_tl_re.end(); tli++)
  109. {
  110. lanelet::AutowareTrafficLightConstPtr tl_ptr = *tli;
  111. lanelet::Id id = tl_ptr->id();
  112. bool unique_id = true;
  113. for (auto ii = tl_reg_elems.begin(); ii != tl_reg_elems.end(); ii++)
  114. {
  115. if (id == (*ii)->id())
  116. {
  117. unique_id = false;
  118. break;
  119. }
  120. }
  121. if (unique_id)
  122. tl_reg_elems.push_back(tl_ptr);
  123. }
  124. }
  125. return tl_reg_elems;
  126. }
  127. // return all stop lines and ref lines from a given set of lanelets
  128. std::vector<lanelet::ConstLineString3d> query::getTrafficLightStopLines(const lanelet::ConstLanelets lanelets)
  129. {
  130. std::vector<lanelet::ConstLineString3d> stoplines;
  131. for (auto lli = lanelets.begin(); lli != lanelets.end(); lli++)
  132. {
  133. std::vector<lanelet::ConstLineString3d> ll_stoplines;
  134. ll_stoplines = query::getTrafficLightStopLines(*lli);
  135. stoplines.insert(stoplines.end(), ll_stoplines.begin(), ll_stoplines.end());
  136. }
  137. return stoplines;
  138. }
  139. // return all stop and ref lines from a given lanelet
  140. std::vector<lanelet::ConstLineString3d> query::getTrafficLightStopLines(const lanelet::ConstLanelet ll)
  141. {
  142. std::vector<lanelet::ConstLineString3d> stoplines;
  143. // find stop lines referenced by traffic lights
  144. std::vector<std::shared_ptr<const lanelet::TrafficLight> > traffic_light_reg_elems =
  145. ll.regulatoryElementsAs<const lanelet::TrafficLight>();
  146. // lanelet has a traffic light elem element
  147. for (const auto reg_elem : traffic_light_reg_elems)
  148. {
  149. lanelet::Optional<lanelet::ConstLineString3d> traffic_light_stopline_opt = reg_elem->stopLine();
  150. if (!!traffic_light_stopline_opt)
  151. {
  152. stoplines.push_back(traffic_light_stopline_opt.get());
  153. }
  154. }
  155. return stoplines;
  156. }
  157. std::vector<lanelet::ConstLineString3d> query::getStopSignStopLines(const lanelet::ConstLanelets lanelets,
  158. const std::string& stop_sign_id)
  159. {
  160. std::vector<lanelet::ConstLineString3d> all_stoplines;
  161. std::vector<lanelet::ConstLineString3d> traffic_sign_stoplines;
  162. std::vector<lanelet::ConstLineString3d> right_of_way_stoplines;
  163. std::vector<lanelet::ConstLineString3d> all_way_stop_stoplines;
  164. traffic_sign_stoplines = getTrafficSignStopLines(lanelets, stop_sign_id);
  165. right_of_way_stoplines = getRightOfWayStopLines(lanelets);
  166. all_way_stop_stoplines = getAllWayStopStopLines(lanelets);
  167. all_stoplines.reserve(traffic_sign_stoplines.size() + right_of_way_stoplines.size() + all_way_stop_stoplines.size());
  168. all_stoplines.insert(all_stoplines.end(), traffic_sign_stoplines.begin(), traffic_sign_stoplines.end());
  169. all_stoplines.insert(all_stoplines.end(), right_of_way_stoplines.begin(), right_of_way_stoplines.end());
  170. all_stoplines.insert(all_stoplines.end(), all_way_stop_stoplines.begin(), all_way_stop_stoplines.end());
  171. return all_stoplines;
  172. }
  173. std::vector<lanelet::ConstLineString3d> query::getTrafficSignStopLines(const lanelet::ConstLanelets lanelets,
  174. const std::string& stop_sign_id)
  175. {
  176. std::vector<lanelet::ConstLineString3d> stoplines;
  177. std::set<lanelet::Id> checklist;
  178. for (const auto& ll : lanelets)
  179. {
  180. // find stop lines referenced by traffic signs
  181. std::vector<std::shared_ptr<const lanelet::TrafficSign> > traffic_sign_reg_elems =
  182. ll.regulatoryElementsAs<const lanelet::TrafficSign>();
  183. if (traffic_sign_reg_elems.size() > 0)
  184. {
  185. // lanelet has a traffic sign reg elem - can have multiple ref lines (but
  186. // stop sign should have 1
  187. for (const auto& ts : traffic_sign_reg_elems)
  188. {
  189. // skip if traffic sign is not stop sign
  190. if (ts->type() != stop_sign_id)
  191. {
  192. continue;
  193. }
  194. lanelet::ConstLineStrings3d traffic_sign_stoplines = ts->refLines();
  195. // only add new items
  196. if (traffic_sign_stoplines.size() > 0)
  197. {
  198. auto id = traffic_sign_stoplines.front().id();
  199. if (checklist.find(id) == checklist.end())
  200. {
  201. checklist.insert(id);
  202. stoplines.push_back(traffic_sign_stoplines.front());
  203. }
  204. }
  205. }
  206. }
  207. }
  208. return stoplines;
  209. }
  210. std::vector<lanelet::ConstLineString3d> query::getRightOfWayStopLines(const lanelet::ConstLanelets lanelets)
  211. {
  212. std::vector<lanelet::ConstLineString3d> stoplines;
  213. for (const auto& ll : lanelets)
  214. {
  215. // find stop lines referenced by RightOfWay reg. elems.
  216. std::vector<std::shared_ptr<const lanelet::RightOfWay> > right_of_way_reg_elems =
  217. ll.regulatoryElementsAs<const lanelet::RightOfWay>();
  218. for (const auto reg_elem : right_of_way_reg_elems)
  219. {
  220. if (reg_elem->getManeuver(ll) == lanelet::ManeuverType::Yield)
  221. {
  222. // lanelet has a yield reg. elem.
  223. lanelet::Optional<lanelet::ConstLineString3d> row_stopline_opt = reg_elem->stopLine();
  224. if (!!row_stopline_opt)
  225. {
  226. stoplines.push_back(row_stopline_opt.get());
  227. }
  228. }
  229. }
  230. }
  231. return stoplines;
  232. }
  233. std::vector<lanelet::ConstLineString3d> query::getAllWayStopStopLines(const lanelet::ConstLanelets lanelets)
  234. {
  235. std::vector<lanelet::ConstLineString3d> stoplines;
  236. for (const auto& ll : lanelets)
  237. {
  238. // Get every AllWayStop reg. elem. that this lanelet references.
  239. std::vector<std::shared_ptr<const lanelet::AllWayStop>> all_way_stop_reg_elems =
  240. ll.regulatoryElementsAs<const lanelet::AllWayStop>();
  241. for (const auto reg_elem : all_way_stop_reg_elems)
  242. {
  243. // Only get the stopline for this lanelet
  244. lanelet::Optional<lanelet::ConstLineString3d> stopline = reg_elem->getStopLine(ll);
  245. if (!!stopline)
  246. {
  247. stoplines.push_back(stopline.get());
  248. }
  249. }
  250. }
  251. return stoplines;
  252. }
  253. } // namespace utils
  254. } // namespace lanelet