8 #include <unordered_map>
14 const static std::vector<std::pair<std::uint8_t, std::uint8_t>> JOINT_PAIRS {
15 { Joint::TYPE_SHOULDER_SPINE, Joint::TYPE_HEAD },
16 { Joint::TYPE_SHOULDER_SPINE, Joint::TYPE_RIGHT_SHOULDER },
17 { Joint::TYPE_SHOULDER_SPINE, Joint::TYPE_LEFT_SHOULDER },
18 { Joint::TYPE_LEFT_SHOULDER, Joint::TYPE_LEFT_ELBOW },
19 { Joint::TYPE_LEFT_ELBOW, Joint::TYPE_LEFT_WRIST },
20 { Joint::TYPE_LEFT_WRIST, Joint::TYPE_LEFT_HAND },
21 { Joint::TYPE_RIGHT_SHOULDER, Joint::TYPE_RIGHT_ELBOW },
22 { Joint::TYPE_RIGHT_ELBOW, Joint::TYPE_RIGHT_WRIST },
23 { Joint::TYPE_RIGHT_WRIST, Joint::TYPE_RIGHT_HAND },
24 { Joint::TYPE_MID_SPINE, Joint::TYPE_SHOULDER_SPINE },
25 { Joint::TYPE_BASE_SPINE, Joint::TYPE_MID_SPINE },
26 { Joint::TYPE_LEFT_HIP, Joint::TYPE_BASE_SPINE },
27 { Joint::TYPE_LEFT_KNEE, Joint::TYPE_LEFT_HIP },
28 { Joint::TYPE_LEFT_FOOT, Joint::TYPE_LEFT_KNEE },
29 { Joint::TYPE_RIGHT_HIP, Joint::TYPE_BASE_SPINE },
30 { Joint::TYPE_RIGHT_KNEE, Joint::TYPE_RIGHT_HIP },
31 { Joint::TYPE_RIGHT_FOOT, Joint::TYPE_RIGHT_KNEE },
34 const static std::unordered_map<std::uint8_t, float> STATUS_OPACITIES {
35 { Joint::STATUS_NOT_TRACKED, 0.2 },
36 { Joint::STATUS_LOW_CONFIDENCE, 0.7 },
37 { Joint::STATUS_TRACKED, 1.0 },
50 using namespace visualization_msgs;
52 std::size_t id_iter = 0;
57 o <<
"astra_ros/" <<
static_cast<std::size_t
>(body.id);
58 const std::string ns = o.str();
61 const auto create_marker = [&](
const std::int32_t type) -> Marker {
63 ret.header = body.header;
67 ret.action = Marker::MODIFY;
68 ret.frame_locked =
false;
69 ret.lifetime = MARKER_LIFETIME;
73 std::unordered_map<std::uint8_t, const Joint *> lookup_map;
78 const auto lookup = [&](std::uint8_t type) ->
const Joint * {
79 const auto it = lookup_map.find(type);
80 if (it != lookup_map.cend())
return it->second;
82 for (
const auto &joint : body.joints)
84 if (joint.type != type)
continue;
86 lookup_map[type] = &joint;
96 Marker center_of_mass = create_marker(Marker::SPHERE);
97 center_of_mass.scale.x = center_of_mass.scale.y = center_of_mass.scale.z = 0.1;
98 center_of_mass.pose.position.x = body.center_of_mass.x;
99 center_of_mass.pose.position.y = body.center_of_mass.y;
100 center_of_mass.pose.position.z = body.center_of_mass.z;
101 center_of_mass.pose.orientation.w = 1.0;
102 center_of_mass.color = color;
103 ret.markers.push_back(center_of_mass);
106 for (
const auto &joint : body.joints)
108 Marker marker = create_marker(Marker::CUBE);
109 marker.scale.x = marker.scale.y = marker.scale.z = 0.05;
110 const auto it = STATUS_OPACITIES.find(joint.status);
111 marker.color = color.
toRgba(it == STATUS_OPACITIES.cend() ? 0.0 : it->second);
112 marker.pose = joint.pose;
113 ret.markers.push_back(marker);
118 for (
const auto &joint_pair : JOINT_PAIRS)
120 const Joint *
const left = lookup(joint_pair.first);
121 const Joint *
const right = lookup(joint_pair.second);
123 if (left ==
nullptr || right ==
nullptr)
continue;
125 Marker link = create_marker(Marker::CYLINDER);
127 const auto &left_position = left->pose.position;
128 const auto &right_position = right->pose.position;
130 link.scale.z = link.scale.y = 0.01;
133 link.scale.x =
distance(left_position, right_position);
136 link.pose.position = (left_position + right_position) / 2.0;
141 Eigen::Vector3d::UnitX(),
142 Eigen::Vector3d::UnitZ()
145 const auto left_it = STATUS_OPACITIES.find(left->status);
146 const auto right_it = STATUS_OPACITIES.find(right->status);
147 link.color = color.
toRgba(std::min(
148 left_it == STATUS_OPACITIES.cend() ? 0.0f : left_it->second,
149 right_it == STATUS_OPACITIES.cend() ? 0.0f : right_it->second
151 ret.markers.push_back(link);
159 visualization_msgs::MarkerArray ret;
161 for (
const auto &body : bodies)
164 ret.markers.insert(ret.markers.end(), body_markers.markers.cbegin(), body_markers.markers.cend());