visualization.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 <visualization_msgs/Marker.h>
  20. #include <visualization_msgs/MarkerArray.h>
  21. #include <Eigen/Eigen>
  22. #include <string>
  23. #include <vector>
  24. #include <lanelet2_extension/utility/message_conversion.h>
  25. #include <lanelet2_extension/utility/query.h>
  26. #include <lanelet2_extension/visualization/visualization.h>
  27. #include <amathutils_lib/amathutils.hpp>
  28. namespace
  29. {
  30. void adjacentPoints(const int i, const int N, const geometry_msgs::Polygon poly, geometry_msgs::Point32* p0,
  31. geometry_msgs::Point32* p1, geometry_msgs::Point32* p2)
  32. {
  33. if (p0 == nullptr || p1 == nullptr || p2 == nullptr)
  34. {
  35. ROS_ERROR_STREAM(__FUNCTION__ << ": either p0, p1, or p2 is null pointer!");
  36. return;
  37. }
  38. *p1 = poly.points[i];
  39. if (i == 0)
  40. *p0 = poly.points[N - 1];
  41. else
  42. *p0 = poly.points[i - 1];
  43. if (i < N - 1)
  44. *p2 = poly.points[i + 1];
  45. else
  46. *p2 = poly.points[0];
  47. }
  48. double hypot(const geometry_msgs::Point32& p0, const geometry_msgs::Point32& p1)
  49. {
  50. return (sqrt(pow((p1.x - p0.x), 2.0) + pow((p1.y - p0.y), 2.0)));
  51. }
  52. bool isAttributeValue(const lanelet::ConstPoint3d p, const std::string attr_str, const std::string value_str)
  53. {
  54. lanelet::Attribute attr = p.attribute(attr_str);
  55. if (attr.value().compare(value_str) == 0)
  56. return true;
  57. return false;
  58. }
  59. bool isLaneletAttributeValue(const lanelet::ConstLanelet ll, const std::string attr_str, const std::string value_str)
  60. {
  61. lanelet::Attribute attr = ll.attribute(attr_str);
  62. if (attr.value().compare(value_str) == 0)
  63. return true;
  64. return false;
  65. }
  66. void lightAsMarker(lanelet::ConstPoint3d p, visualization_msgs::Marker* marker, const std::string ns)
  67. {
  68. if (marker == nullptr)
  69. {
  70. ROS_ERROR_STREAM(__FUNCTION__ << ": marker is null pointer!");
  71. return;
  72. }
  73. marker->header.frame_id = "map";
  74. marker->header.stamp = ros::Time();
  75. marker->ns = ns;
  76. marker->id = p.id();
  77. marker->type = visualization_msgs::Marker::SPHERE;
  78. marker->pose.position.x = p.x();
  79. marker->pose.position.y = p.y();
  80. marker->pose.position.z = p.z();
  81. float s = 0.3;
  82. marker->scale.x = s;
  83. marker->scale.y = s;
  84. marker->scale.z = s;
  85. marker->color.r = 0.0f;
  86. marker->color.g = 0.0f;
  87. marker->color.b = 0.0f;
  88. marker->color.a = 1.0f;
  89. if (isAttributeValue(p, "color", "red"))
  90. marker->color.r = 1.0f;
  91. else if (isAttributeValue(p, "color", "green"))
  92. marker->color.g = 1.0f;
  93. else if (isAttributeValue(p, "color", "yellow"))
  94. {
  95. marker->color.r = 1.0f;
  96. marker->color.g = 1.0f;
  97. }
  98. else
  99. {
  100. marker->color.r = 1.0f;
  101. marker->color.g = 1.0f;
  102. marker->color.b = 1.0f;
  103. }
  104. marker->lifetime = ros::Duration();
  105. }
  106. void laneletDirectionAsMarker(const lanelet::ConstLanelet ll, visualization_msgs::Marker* marker, const int id,
  107. const std::string ns)
  108. {
  109. if (marker == nullptr)
  110. {
  111. ROS_ERROR_STREAM(__FUNCTION__ << ": marker is null pointer!");
  112. return;
  113. }
  114. marker->header.frame_id = "map";
  115. marker->header.stamp = ros::Time();
  116. marker->ns = ns;
  117. marker->id = id;
  118. marker->type = visualization_msgs::Marker::TRIANGLE_LIST;
  119. lanelet::BasicPoint3d pt[3];
  120. pt[0].x() = 0.0;
  121. pt[0].y() = -0.3;
  122. pt[0].z() = 0.0;
  123. pt[1].x() = 0.0;
  124. pt[1].y() = 0.3;
  125. pt[1].z() = 0;
  126. pt[2].x() = 1.0;
  127. pt[2].y() = 0.0;
  128. pt[2].z() = 0;
  129. lanelet::BasicPoint3d pc, pc2;
  130. lanelet::ConstLineString3d center_ls = ll.centerline();
  131. float s = 1.0;
  132. marker->lifetime = ros::Duration();
  133. marker->pose.position.x = 0.0; // p.x();
  134. marker->pose.position.y = 0.0; // p.y();
  135. marker->pose.position.z = 0.0; // p.z();
  136. marker->scale.x = s;
  137. marker->scale.y = s;
  138. marker->scale.z = s;
  139. marker->color.r = 1.0f;
  140. marker->color.g = 1.0f;
  141. marker->color.b = 1.0f;
  142. marker->color.a = 1.0f;
  143. lanelet::Attribute attr = ll.attribute("turn_direction");
  144. double turn_dir = 0;
  145. std_msgs::ColorRGBA c;
  146. c.r = 0.0;
  147. c.g = 0.0;
  148. c.b = 1.0;
  149. c.a = 0.6;
  150. if (isLaneletAttributeValue(ll, "turn_direction", "right"))
  151. {
  152. turn_dir = -M_PI / 2.0;
  153. c.r = 1.0;
  154. c.g = 0.0;
  155. c.b = 1.0;
  156. }
  157. else if (isLaneletAttributeValue(ll, "turn_direction", "left"))
  158. {
  159. turn_dir = M_PI / 2.0;
  160. c.r = 0.0;
  161. c.g = 1.0;
  162. c.b = 1.0;
  163. }
  164. for (int ci = 0; ci < center_ls.size() - 1;)
  165. {
  166. pc = center_ls[ci];
  167. if (center_ls.size() > 1)
  168. pc2 = center_ls[ci + 1];
  169. else
  170. return;
  171. double heading = atan2(pc2.y() - pc.y(), pc2.x() - pc.x());
  172. lanelet::BasicPoint3d pt_tf[3];
  173. Eigen::Vector3d axis(0, 0, 1);
  174. Eigen::Transform<double, 3, Eigen::Affine> t(Eigen::AngleAxis<double>(heading, axis));
  175. for (int i = 0; i < 3; i++)
  176. pt_tf[i] = t * pt[i] + pc;
  177. geometry_msgs::Point gp[3];
  178. for (int i = 0; i < 3; i++)
  179. {
  180. std_msgs::ColorRGBA cn = c;
  181. gp[i].x = pt_tf[i].x();
  182. gp[i].y = pt_tf[i].y();
  183. gp[i].z = pt_tf[i].z();
  184. marker->points.push_back(gp[i]);
  185. marker->colors.push_back(cn);
  186. }
  187. ci = ci + 1;
  188. }
  189. }
  190. bool isReflexAngle(const geometry_msgs::Point32& a, const geometry_msgs::Point32& o, const geometry_msgs::Point32& b)
  191. {
  192. // angle aob is reflex in counterclock wise direction if point b is on the right side of vector oa
  193. // which can be found out by calculating cross product of oa and ob
  194. return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x) < 0;
  195. }
  196. bool isWithinTriangle(const geometry_msgs::Point32& a, const geometry_msgs::Point32& b, const geometry_msgs::Point32& c,
  197. const geometry_msgs::Point32& p)
  198. {
  199. // point p in within triangle if p is on the same side
  200. const double side1 = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x); // a to b
  201. const double side2 = (c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x); // b to c
  202. const double side3 = (a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x); // c to a
  203. return (side1 > 0.0 && side2 > 0.0 && side3 > 0.0) || (side1 < 0.0 && side2 < 0.0 && side3 < 0.0);
  204. }
  205. } // anonymous namespace
  206. namespace lanelet
  207. {
  208. void visualization::lanelet2Triangle(const lanelet::ConstLanelet& ll, std::vector<geometry_msgs::Polygon>* triangles)
  209. {
  210. if (triangles == nullptr)
  211. {
  212. ROS_ERROR_STREAM(__FUNCTION__ << ": triangles is null pointer!");
  213. return;
  214. }
  215. triangles->clear();
  216. geometry_msgs::Polygon ll_poly;
  217. lanelet2Polygon(ll, &ll_poly);
  218. polygon2Triangle(ll_poly, triangles);
  219. }
  220. void visualization::polygon2Triangle(const geometry_msgs::Polygon& polygon,
  221. std::vector<geometry_msgs::Polygon>* triangles)
  222. {
  223. geometry_msgs::Polygon poly = polygon;
  224. // ear clipping: find smallest internal angle in polygon
  225. int N = poly.points.size();
  226. // array of angles for each vertex
  227. std::vector<bool> is_reflex_angle;
  228. is_reflex_angle.assign(N, false);
  229. for (int i = 0; i < N; i++)
  230. {
  231. geometry_msgs::Point32 p0, p1, p2;
  232. adjacentPoints(i, N, poly, &p0, &p1, &p2);
  233. is_reflex_angle.at(i) = isReflexAngle(p0, p1, p2);
  234. }
  235. // start ear clipping
  236. while (N >= 3)
  237. {
  238. int clipped_vertex = -1;
  239. for (int i = 0; i < N; i++)
  240. {
  241. bool is_reflex = is_reflex_angle.at(i);
  242. if (!is_reflex)
  243. {
  244. geometry_msgs::Point32 p0, p1, p2;
  245. adjacentPoints(i, N, poly, &p0, &p1, &p2);
  246. int j_begin = (i + 2) % N;
  247. int j_end = (i - 1 + N) % N;
  248. bool is_ear = true;
  249. for (int j = j_begin; j != j_end; j = (j + 1) % N)
  250. {
  251. if (isWithinTriangle(p0, p1, p2, poly.points.at(j)))
  252. {
  253. is_ear = false;
  254. break;
  255. }
  256. }
  257. if (is_ear)
  258. {
  259. clipped_vertex = i;
  260. break;
  261. }
  262. }
  263. }
  264. if (clipped_vertex < 0 || clipped_vertex >= N)
  265. {
  266. ROS_ERROR("Could not find valid vertex for ear clipping triangulation. Triangulation result might be invalid");
  267. clipped_vertex = 0;
  268. }
  269. // create triangle
  270. geometry_msgs::Point32 p0, p1, p2;
  271. adjacentPoints(clipped_vertex, N, poly, &p0, &p1, &p2);
  272. geometry_msgs::Polygon triangle;
  273. triangle.points.push_back(p0);
  274. triangle.points.push_back(p1);
  275. triangle.points.push_back(p2);
  276. triangles->push_back(triangle);
  277. // remove vertex of center of angle
  278. auto it = poly.points.begin();
  279. std::advance(it, clipped_vertex);
  280. poly.points.erase(it);
  281. // remove from angle list
  282. auto it_angle = is_reflex_angle.begin();
  283. std::advance(it_angle, clipped_vertex);
  284. is_reflex_angle.erase(it_angle);
  285. // update angle list
  286. N = poly.points.size();
  287. if (clipped_vertex == N)
  288. {
  289. clipped_vertex = 0;
  290. }
  291. adjacentPoints(clipped_vertex, N, poly, &p0, &p1, &p2);
  292. is_reflex_angle.at(clipped_vertex) = isReflexAngle(p0, p1, p2);
  293. int i_prev = (clipped_vertex == 0) ? N - 1 : clipped_vertex - 1;
  294. adjacentPoints(i_prev, N, poly, &p0, &p1, &p2);
  295. is_reflex_angle.at(i_prev) = isReflexAngle(p0, p1, p2);
  296. }
  297. }
  298. void visualization::lanelet2Polygon(const lanelet::ConstLanelet& ll, geometry_msgs::Polygon* polygon)
  299. {
  300. if (polygon == nullptr)
  301. {
  302. ROS_ERROR_STREAM(__FUNCTION__ << ": polygon is null pointer!");
  303. return;
  304. }
  305. lanelet::CompoundPolygon3d ll_poly = ll.polygon3d();
  306. polygon->points.clear();
  307. polygon->points.reserve(ll_poly.size());
  308. for (const auto& pt : ll_poly)
  309. {
  310. geometry_msgs::Point32 pt32;
  311. utils::conversion::toGeomMsgPt32(pt.basicPoint(), &pt32);
  312. polygon->points.push_back(pt32);
  313. }
  314. }
  315. visualization_msgs::MarkerArray visualization::laneletDirectionAsMarkerArray(const lanelet::ConstLanelets lanelets)
  316. {
  317. visualization_msgs::MarkerArray marker_array;
  318. int ll_dir_count = 0;
  319. for (auto lli = lanelets.begin(); lli != lanelets.end(); lli++)
  320. {
  321. lanelet::ConstLanelet ll = *lli;
  322. // bool road_found = false;
  323. if (ll.hasAttribute(std::string("turn_direction")))
  324. {
  325. lanelet::Attribute attr = ll.attribute("turn_direction");
  326. visualization_msgs::Marker marker;
  327. laneletDirectionAsMarker(ll, &marker, ll_dir_count, "lanelet direction");
  328. marker_array.markers.push_back(marker);
  329. ll_dir_count++;
  330. }
  331. }
  332. return (marker_array);
  333. }
  334. visualization_msgs::MarkerArray visualization::autowareTrafficLightsAsMarkerArray(
  335. const std::vector<lanelet::AutowareTrafficLightConstPtr> tl_reg_elems, const std_msgs::ColorRGBA c,
  336. const ros::Duration duration, const double scale)
  337. {
  338. visualization_msgs::MarkerArray tl_marker_array;
  339. int tl_count = 0;
  340. for (auto tli = tl_reg_elems.begin(); tli != tl_reg_elems.end(); tli++)
  341. {
  342. lanelet::ConstLineStrings3d light_bulbs;
  343. lanelet::AutowareTrafficLightConstPtr tl = *tli;
  344. const auto lights = tl->trafficLights();
  345. for (const auto& lsp : lights)
  346. {
  347. if (lsp.isLineString()) // traffic ligths can either polygons or
  348. { // linestrings
  349. lanelet::ConstLineString3d ls = static_cast<lanelet::ConstLineString3d>(lsp);
  350. visualization_msgs::Marker marker;
  351. visualization::trafficLight2TriangleMarker(ls, &marker, "traffic_light_triangle", c, duration, scale);
  352. tl_marker_array.markers.push_back(marker);
  353. }
  354. }
  355. light_bulbs = tl->lightBulbs();
  356. for (auto ls : light_bulbs)
  357. {
  358. lanelet::ConstLineString3d l = static_cast<lanelet::ConstLineString3d>(ls);
  359. for (auto pt : l)
  360. {
  361. if (pt.hasAttribute("color"))
  362. {
  363. visualization_msgs::Marker marker;
  364. lightAsMarker(pt, &marker, "traffic_light");
  365. tl_marker_array.markers.push_back(marker);
  366. tl_count++;
  367. }
  368. }
  369. }
  370. }
  371. return (tl_marker_array);
  372. }
  373. visualization_msgs::MarkerArray
  374. visualization::lineStringsAsMarkerArray(const std::vector<lanelet::ConstLineString3d> line_strings,
  375. const std::string name_space, const std_msgs::ColorRGBA c, const double lss)
  376. {
  377. visualization_msgs::MarkerArray ls_marker_array;
  378. for (auto i = line_strings.begin(); i != line_strings.end(); i++)
  379. {
  380. lanelet::ConstLineString3d ls = *i;
  381. visualization_msgs::Marker ls_marker;
  382. visualization::lineString2Marker(ls, &ls_marker, "map", name_space, c, 0.2);
  383. ls_marker_array.markers.push_back(ls_marker);
  384. }
  385. return (ls_marker_array);
  386. }
  387. visualization_msgs::MarkerArray visualization::laneletsBoundaryAsMarkerArray(const lanelet::ConstLanelets& lanelets,
  388. const std_msgs::ColorRGBA c,
  389. const bool viz_centerline)
  390. {
  391. double lss = 0.2; // line string size
  392. visualization_msgs::MarkerArray marker_array;
  393. for (auto li = lanelets.begin(); li != lanelets.end(); li++)
  394. {
  395. lanelet::ConstLanelet lll = *li;
  396. lanelet::ConstLineString3d left_ls = lll.leftBound();
  397. lanelet::ConstLineString3d right_ls = lll.rightBound();
  398. lanelet::ConstLineString3d center_ls = lll.centerline();
  399. visualization_msgs::Marker left_line_strip, right_line_strip, center_line_strip;
  400. visualization::lineString2Marker(left_ls, &left_line_strip, "map", "left_lane_bound", c, lss);
  401. visualization::lineString2Marker(right_ls, &right_line_strip, "map", "right_lane_bound", c, lss);
  402. marker_array.markers.push_back(left_line_strip);
  403. marker_array.markers.push_back(right_line_strip);
  404. if (viz_centerline)
  405. {
  406. visualization::lineString2Marker(center_ls, &center_line_strip, "map", "center_lane_line", c, lss * 0.5);
  407. marker_array.markers.push_back(center_line_strip);
  408. }
  409. }
  410. return marker_array;
  411. }
  412. visualization_msgs::MarkerArray visualization::trafficLightsAsTriangleMarkerArray(
  413. const std::vector<lanelet::TrafficLightConstPtr> tl_reg_elems, const std_msgs::ColorRGBA c,
  414. const ros::Duration duration, const double scale)
  415. {
  416. // convert to to an array of linestrings and publish as marker array using
  417. // exisitng function
  418. int tl_count = 0;
  419. std::vector<lanelet::ConstLineString3d> line_strings;
  420. visualization_msgs::MarkerArray marker_array;
  421. for (auto tli = tl_reg_elems.begin(); tli != tl_reg_elems.end(); tli++)
  422. {
  423. lanelet::TrafficLightConstPtr tl = *tli;
  424. lanelet::LineString3d ls;
  425. auto lights = tl->trafficLights();
  426. for (auto lsp : lights)
  427. {
  428. if (lsp.isLineString()) // traffic ligths can either polygons or
  429. { // linestrings
  430. lanelet::ConstLineString3d ls = static_cast<lanelet::ConstLineString3d>(lsp);
  431. visualization_msgs::Marker marker;
  432. visualization::trafficLight2TriangleMarker(ls, &marker, "traffic_light_triangle", c, duration, scale);
  433. marker_array.markers.push_back(marker);
  434. tl_count++;
  435. }
  436. }
  437. }
  438. return (marker_array);
  439. }
  440. visualization_msgs::MarkerArray visualization::laneletsAsTriangleMarkerArray(const std::string ns,
  441. const lanelet::ConstLanelets& lanelets,
  442. const std_msgs::ColorRGBA c)
  443. {
  444. visualization_msgs::MarkerArray marker_array;
  445. visualization_msgs::Marker marker;
  446. marker.header.frame_id = "map";
  447. marker.header.stamp = ros::Time();
  448. marker.ns = ns;
  449. marker.id = 0;
  450. marker.type = visualization_msgs::Marker::TRIANGLE_LIST;
  451. marker.lifetime = ros::Duration();
  452. marker.pose.position.x = 0.0; // p.x();
  453. marker.pose.position.y = 0.0; // p.y();
  454. marker.pose.position.z = 0.0; // p.z();
  455. marker.scale.x = 1.0;
  456. marker.scale.y = 1.0;
  457. marker.scale.z = 1.0;
  458. marker.color.r = 1.0f;
  459. marker.color.g = 1.0f;
  460. marker.color.b = 1.0f;
  461. marker.color.a = 1.0f;
  462. for (auto ll : lanelets)
  463. {
  464. std::vector<geometry_msgs::Polygon> triangles;
  465. lanelet2Triangle(ll, &triangles);
  466. for (auto tri : triangles)
  467. {
  468. geometry_msgs::Point tri0[3];
  469. for (int i = 0; i < 3; i++)
  470. {
  471. utils::conversion::toGeomMsgPt(tri.points[i], &tri0[i]);
  472. marker.points.push_back(tri0[i]);
  473. marker.colors.push_back(c);
  474. }
  475. }
  476. }
  477. if (!marker.points.empty())
  478. {
  479. marker_array.markers.push_back(marker);
  480. }
  481. return (marker_array);
  482. }
  483. void visualization::trafficLight2TriangleMarker(const lanelet::ConstLineString3d ls, visualization_msgs::Marker* marker,
  484. const std::string ns, const std_msgs::ColorRGBA cl,
  485. const ros::Duration duration, const double scale)
  486. {
  487. if (marker == nullptr)
  488. {
  489. ROS_ERROR_STREAM(__FUNCTION__ << ": marker is null pointer!");
  490. return;
  491. }
  492. marker->header.frame_id = "map";
  493. marker->header.stamp = ros::Time();
  494. marker->ns = ns;
  495. marker->id = ls.id();
  496. marker->type = visualization_msgs::Marker::TRIANGLE_LIST;
  497. marker->lifetime = duration;
  498. marker->pose.position.x = 0.0; // p.x();
  499. marker->pose.position.y = 0.0; // p.y();
  500. marker->pose.position.z = 0.0; // p.z();
  501. marker->scale.x = 1.0;
  502. marker->scale.y = 1.0;
  503. marker->scale.z = 1.0;
  504. marker->color.r = 1.0f;
  505. marker->color.g = 1.0f;
  506. marker->color.b = 1.0f;
  507. marker->color.a = 1.0f;
  508. double h = 0.7;
  509. if (ls.hasAttribute("height"))
  510. {
  511. lanelet::Attribute attr = ls.attribute("height");
  512. h = std::stod(attr.value());
  513. }
  514. // construct triangles and add to marker
  515. // define polygon of traffic light border
  516. Eigen::Vector3d v[4];
  517. v[0] << ls.front().x(), ls.front().y(), ls.front().z();
  518. v[1] << ls.back().x(), ls.back().y(), ls.back().z();
  519. v[2] << ls.back().x(), ls.back().y(), ls.back().z() + h;
  520. v[3] << ls.front().x(), ls.front().y(), ls.front().z() + h;
  521. Eigen::Vector3d c = (v[0] + v[1] + v[2] + v[3]) / 4;
  522. if (scale > 0.0 && scale != 1.0)
  523. {
  524. for (int i = 0; i < 4; i++)
  525. {
  526. v[i] = (v[i] - c) * scale + c;
  527. }
  528. }
  529. geometry_msgs::Point tri0[3];
  530. utils::conversion::toGeomMsgPt(v[0], &tri0[0]);
  531. utils::conversion::toGeomMsgPt(v[1], &tri0[1]);
  532. utils::conversion::toGeomMsgPt(v[2], &tri0[2]);
  533. geometry_msgs::Point tri1[3];
  534. utils::conversion::toGeomMsgPt(v[0], &tri1[0]);
  535. utils::conversion::toGeomMsgPt(v[2], &tri1[1]);
  536. utils::conversion::toGeomMsgPt(v[3], &tri1[2]);
  537. for (int i = 0; i < 3; i++)
  538. {
  539. marker->points.push_back(tri0[i]);
  540. marker->colors.push_back(cl);
  541. }
  542. for (int i = 0; i < 3; i++)
  543. {
  544. marker->points.push_back(tri1[i]);
  545. marker->colors.push_back(cl);
  546. }
  547. }
  548. void visualization::lineString2Marker(const lanelet::ConstLineString3d ls, visualization_msgs::Marker* line_strip,
  549. const std::string frame_id, const std::string ns, const std_msgs::ColorRGBA c,
  550. const float lss)
  551. {
  552. if (line_strip == nullptr)
  553. {
  554. ROS_ERROR_STREAM(__FUNCTION__ << ": line_strip is null pointer!");
  555. return;
  556. }
  557. line_strip->header.frame_id = frame_id;
  558. line_strip->header.stamp = ros::Time();
  559. line_strip->ns = ns;
  560. line_strip->action = visualization_msgs::Marker::ADD;
  561. line_strip->pose.orientation.w = 1.0;
  562. line_strip->id = ls.id();
  563. line_strip->type = visualization_msgs::Marker::LINE_STRIP;
  564. line_strip->scale.x = lss;
  565. line_strip->color = c;
  566. // fill out lane line
  567. for (auto i = ls.begin(); i != ls.end(); i++)
  568. {
  569. geometry_msgs::Point p;
  570. p.x = (*i).x();
  571. p.y = (*i).y();
  572. p.z = (*i).z();
  573. line_strip->points.push_back(p);
  574. }
  575. }
  576. } // namespace lanelet