visualization.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  *
4  * Author: Monika Florek-Jasinska
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  * may be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "visualization.h"
34 
35 namespace rc_tagdetect_client
36 {
38 {
40  if (ns.empty())
41  {
42  return {};
43  }
44  if (ns[0] == '/')
45  {
46  ns = ns.substr(1);
47  }
48  if (ns.empty())
49  {
50  return {};
51  }
52  else
53  {
54  if (ns[0] == '/')
55  {
56  ns = ns.substr(1);
57  }
58  std::replace(ns.begin(), ns.end(), '/', '_');
59  return ns + '_';
60  }
61 }
62 
63 Visualization::Visualization(const ros::NodeHandle& nh) : nh_(nh), tf_prefix_(make_tf_prefix(nh_))
64 {
65  tag_marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("tag_markers", 10);
66 }
67 
69 {
70  try
71  {
72  deleteMarkers();
73  }
74  catch (const std::exception& ex)
75  {
76  ROS_FATAL("Exception during destruction of Visualization: %s", ex.what());
77  }
78  catch (...)
79  {
80  ROS_FATAL("Exception during destruction of Visualization");
81  }
82 }
83 
85 {
86  for (auto& i : markers_.markers)
87  {
88  i.action = visualization_msgs::Marker::DELETE;
89  }
91  markers_.markers.clear();
92 }
93 
94 void Visualization::publishTags(const std::vector<rc_tagdetect_client::DetectedTag>& tags)
95 {
96  deleteMarkers();
97  for (const auto& t : tags)
98  {
100  markers_.markers.push_back(createMarker(t));
101  }
103 }
104 
105 visualization_msgs::Marker Visualization::createMarker(const rc_tagdetect_client::DetectedTag& tag) const
106 {
107  visualization_msgs::Marker marker;
108  marker.header.stamp = tag.header.stamp;
109  marker.header.frame_id = tf_prefix_ + tag.tag.id + '_' + tag.instance_id;
110  marker.id = static_cast<int32_t>(std::stol(tag.instance_id) % std::numeric_limits<int32_t>::max());
111  marker.lifetime = ros::Duration();
112  marker.type = visualization_msgs::Marker::CUBE;
113  marker.action = visualization_msgs::Marker::ADD;
114  marker.scale.x = tag.tag.size;
115  marker.scale.y = tag.tag.size;
116  marker.scale.z = 0.001;
117  marker.pose.orientation.w = 1;
118  marker.pose.position.x = tag.tag.size / 2;
119  marker.pose.position.y = tag.tag.size / 2;
120  marker.pose.position.z = 0.001 / 2;
121  marker.color.a = 0.5;
122  marker.color.r = 0.0;
123  marker.color.g = 1.0;
124  marker.color.b = 0.0;
125  return marker;
126 }
127 
128 geometry_msgs::TransformStamped Visualization::createTf(const rc_tagdetect_client::DetectedTag& tag) const
129 {
130  geometry_msgs::TransformStamped tf;
131  tf.header.frame_id = tf_prefix_ + tag.header.frame_id;
132  tf.child_frame_id = tf_prefix_ + tag.tag.id + '_' + tag.instance_id;
133  tf.header.stamp = tag.header.stamp;
134  tf.transform.translation.x = tag.pose.pose.position.x;
135  tf.transform.translation.y = tag.pose.pose.position.y;
136  tf.transform.translation.z = tag.pose.pose.position.z;
137  tf.transform.rotation.x = tag.pose.pose.orientation.x;
138  tf.transform.rotation.y = tag.pose.pose.orientation.y;
139  tf.transform.rotation.z = tag.pose.pose.orientation.z;
140  tf.transform.rotation.w = tag.pose.pose.orientation.w;
141  return tf;
142 }
143 
144 } // namespace rc_tagdetect_client
void publishTags(const std::vector< rc_tagdetect_client::DetectedTag > &tags)
#define ROS_FATAL(...)
static std::string make_tf_prefix(const ros::NodeHandle &nh)
void publish(const boost::shared_ptr< M > &message) const
geometry_msgs::TransformStamped createTf(const rc_tagdetect_client::DetectedTag &tag) const
::std::string string
Definition: gtest-port.h:1129
#define ADD(args)
Definition: tool_setopt.c:180
geometry_msgs::TransformStamped t
unsigned int i
Definition: unit1303.c:79
visualization_msgs::MarkerArray markers_
Definition: visualization.h:68
ROSCPP_DECL const std::string & getNamespace()
Visualization(const ros::NodeHandle &nh)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void sendTransform(const geometry_msgs::TransformStamped &transform)
visualization_msgs::Marker createMarker(const rc_tagdetect_client::DetectedTag &tag) const
tf2_ros::TransformBroadcaster transform_broadcaster_
Definition: visualization.h:69


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:17