Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
marker.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
4
5#include <map>
6#include <type_traits>
7
11#include "rclcpp/clock.hpp"
12#include "rclcpp/rclcpp.hpp"
13#include "std_msgs/msg/color_rgba.hpp"
14#include "visualization_msgs/msg/marker.hpp"
15#include "visualization_msgs/msg/marker_array.hpp"
16
17using K = CGAL::Exact_predicates_inexact_constructions_kernel;
18using Point = K::Point_2;
20
21inline const std::map<std::string, std::array<float, 4>, std::less<>>& marker_color_map() {
22 static const std::map<std::string, std::array<float, 4>, std::less<>> map = {
23 {"blue", {0.0f, 0.0f, 1.0f, 1.0f}}, {"yellow", {1.0f, 1.0f, 0.0f, 1.0f}},
24 {"orange", {1.0f, 0.5f, 0.0f, 1.0f}}, {"red", {1.0f, 0.0f, 0.0f, 1.0f}},
25 {"green", {0.0f, 1.0f, 0.0f, 1.0f}}, {"white", {1.0f, 1.0f, 1.0f, 1.0f}},
26 {"grey", {0.5f, 0.5f, 0.5f, 1.0f}},
27 };
28 return map;
29}
30
31inline const std::map<std::string, int, std::less<>>& marker_shape_map() {
32 static const std::map<std::string, int, std::less<>> map = {
33 {"cylinder", visualization_msgs::msg::Marker::CYLINDER},
34 {"cube", visualization_msgs::msg::Marker::CUBE},
35 {"sphere", visualization_msgs::msg::Marker::SPHERE},
36 {"line", visualization_msgs::msg::Marker::LINE_STRIP},
37 {"cone", visualization_msgs::msg::Marker::MESH_RESOURCE},
38 {"arrow", visualization_msgs::msg::Marker::ARROW},
39 };
40 return map;
41}
42
50template <typename T, typename = void>
51struct HasPosition : public std::false_type {};
52
60template <typename T>
61struct HasPosition<T,
62 std::enable_if_t<std::is_arithmetic_v<decltype(std::declval<T>().position.x)> &&
63 std::is_arithmetic_v<decltype(std::declval<T>().position.y)>>>
64 : public std::true_type {};
65
78template <typename T>
79visualization_msgs::msg::MarkerArray marker_array_from_structure_array(
80 const std::vector<T>& structure_array, const std::string& name_space,
81 const std::string& frame_id, const std::string& color = "red",
82 const std::string& shape = "cylinder", float scale = 0.5,
83 int action = visualization_msgs::msg::Marker::MODIFY) {
84 static_assert(
86 "Template argument T must have a data member named 'position' with 'x' and 'y' sub-members");
87
88 visualization_msgs::msg::MarkerArray marker_array;
89 std::array<float, 4> color_array = marker_color_map().at(color);
90
91 for (size_t i = 0; i < structure_array.size(); ++i) {
92 visualization_msgs::msg::Marker marker;
93
94 marker.header.frame_id = frame_id;
95 marker.header.stamp = rclcpp::Clock().now();
96 marker.ns = name_space;
97 marker.id = static_cast<int>(i);
98 marker.type = marker_shape_map().at(shape);
99 marker.action = action;
100
101 marker.pose.orientation.x = 0.0;
102 marker.pose.orientation.y = 0.0;
103 marker.pose.orientation.z = 0.0;
104 marker.pose.orientation.w = 1.0;
105
106 marker.pose.position.x = structure_array[i].position.x;
107 marker.pose.position.y = structure_array[i].position.y;
108 marker.pose.position.z = 0;
109
110 marker.scale.x = scale;
111 marker.scale.y = scale;
112 marker.scale.z = scale;
113
114 marker.color.r = color_array[0];
115 marker.color.g = color_array[1];
116 marker.color.b = color_array[2];
117 marker.color.a = color_array[3];
118
119 /* path points in planning also use this and have no islarge.
120 if (shape == "cylinder") {
121 if (structure_array[i].is_large) {
122 marker.scale.x = 1.5 * scale;
123 marker.scale.y = 1.5 * scale;
124 marker.scale.z = 1.5 * scale;
125 }
126 }
127 */
128
129 if (shape == "cone") {
130 marker.pose.orientation.x = 0.7071; // Approximately sqrt(2)/2
131 marker.pose.orientation.y = 0.0;
132 marker.pose.orientation.z = 0.0;
133 marker.pose.orientation.w = -0.7071; // Approximately sqrt(2)/2
134 marker.scale.x = scale * 0.03;
135 marker.scale.y = scale * 0.03;
136 marker.scale.z = scale * 0.03;
137 marker.mesh_resource = "https://paginas.fe.up.pt/~up202109860/FormulaStudent/FSGConev2.obj";
138 }
139 marker_array.markers.push_back(marker);
140 }
141
142 return marker_array;
143}
144
157template <typename T>
158visualization_msgs::msg::Marker line_marker_from_structure_array(
159 const std::vector<T>& structure_array, const std::string& name_space,
160 const std::string& frame_id, const int id, const std::string& color = "red",
161 const std::string& shape = "line", float scale = 0.1f,
162 int action = visualization_msgs::msg::Marker::MODIFY) {
163 static_assert(
165 "Template argument T must have a data member named 'position' with 'x' and 'y' sub-members");
166
167 std::array<float, 4> color_array = marker_color_map().at(color);
168
169 visualization_msgs::msg::Marker marker;
170
171 marker.header.frame_id = frame_id;
172 marker.header.stamp = rclcpp::Clock().now();
173 marker.ns = name_space;
174 marker.id = id;
175 marker.type = marker_shape_map().at(shape);
176 marker.action = action;
177
178 marker.pose.orientation.x = 0.0;
179 marker.pose.orientation.y = 0.0;
180 marker.pose.orientation.z = 0.0;
181 marker.pose.orientation.w = 1.0;
182
183 marker.pose.position.x = 0;
184 marker.pose.position.y = 0;
185 marker.pose.position.z = 0;
186
187 marker.scale.x = scale;
188 marker.scale.y = scale;
189 marker.scale.z = scale;
190
191 marker.color.r = color_array[0];
192 marker.color.g = color_array[1];
193 marker.color.b = color_array[2];
194 marker.color.a = color_array[3];
195
196 for (size_t i = 0; i < structure_array.size(); ++i) {
197 geometry_msgs::msg::Point point;
198 point.x = structure_array[i].position.x;
199 point.y = structure_array[i].position.y,
200
201 marker.points.push_back(point);
202 }
203
204 return marker;
205}
206
220visualization_msgs::msg::Marker marker_from_position(
221 const common_lib::structures::Position& position, const std::string& name_space, const int id,
222 const std::string& color = "red", float scale = 0.5, const std::string& frame_id = "map",
223 const std::string& shape = "sphere", int action = visualization_msgs::msg::Marker::ADD);
224
237visualization_msgs::msg::Marker lines_marker_from_triangulations(
238 const std::vector<std::pair<Point, Point>>& triangulations, const std::string& name_space,
239 const std::string& frame_id, int id, const std::string& color, float scale, int action);
240
252visualization_msgs::msg::MarkerArray velocity_hover_markers(
253 const std::vector<common_lib::structures::PathPoint>& path_array, const std::string& name_space,
254 const std::string& frame_id, float scale = 0.2f, int every_nth = 1);
255} // namespace common_lib::communication
K::Point_2 Point
Definition marker.hpp:18
CGAL::Exact_predicates_inexact_constructions_kernel K
Definition marker.hpp:17
const std::map< std::string, std::array< float, 4 >, std::less<> > & marker_color_map()
Definition marker.hpp:21
visualization_msgs::msg::Marker line_marker_from_structure_array(const std::vector< T > &structure_array, const std::string &name_space, const std::string &frame_id, const int id, const std::string &color="red", const std::string &shape="line", float scale=0.1f, int action=visualization_msgs::msg::Marker::MODIFY)
Converts a vector of cones to a marker array.
Definition marker.hpp:158
visualization_msgs::msg::MarkerArray marker_array_from_structure_array(const std::vector< T > &structure_array, const std::string &name_space, const std::string &frame_id, const std::string &color="red", const std::string &shape="cylinder", float scale=0.5, int action=visualization_msgs::msg::Marker::MODIFY)
Converts a vector of cones to a marker array.
Definition marker.hpp:79
visualization_msgs::msg::MarkerArray velocity_hover_markers(const std::vector< common_lib::structures::PathPoint > &path_array, const std::string &name_space, const std::string &frame_id, float scale=0.2f, int every_nth=1)
Creates sphere markers at each path point with velocity in the marker namespace When you hover over t...
Definition marker.cpp:79
const std::map< std::string, int, std::less<> > & marker_shape_map()
Definition marker.hpp:31
visualization_msgs::msg::Marker marker_from_position(const common_lib::structures::Position &position, const std::string &name_space, const int id, const std::string &color="red", float scale=0.5, const std::string &frame_id="map", const std::string &shape="sphere", int action=visualization_msgs::msg::Marker::ADD)
Converts a position to a marker.
Definition marker.cpp:5
visualization_msgs::msg::Marker lines_marker_from_triangulations(const std::vector< std::pair< Point, Point > > &triangulations, const std::string &name_space, const std::string &frame_id, int id, const std::string &color, float scale, int action)
Converts a vector of triangulation edges to a marker.
Definition marker.cpp:41
Hash function for cones.
Definition cone.hpp:36
A helper struct to check if a type T has a member named 'position'.
Definition marker.hpp:51