visualizer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Roboception GmbH
3  *
4  * Author: Elena Gambaro
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 "visualizer.h"
34 
36 
38 {
39 static std::string make_tf_prefix(const ros::NodeHandle& nh)
40 {
41  std::string ns = ros::this_node::getNamespace();
42  if (ns.empty())
43  {
44  return {};
45  }
46  if (ns[0] == '/')
47  {
48  ns = ns.substr(1);
49  }
50  if (ns.empty())
51  {
52  return {};
53  }
54  else
55  {
56  if (ns[0] == '/')
57  {
58  ns = ns.substr(1);
59  }
60  std::replace(ns.begin(), ns.end(), '/', '_');
61  return ns + '_';
62  }
63 }
64 
66 {
67  marker_pub_ = nh.advertise<visualization_msgs::MarkerArray>("silhouettematch_markers", 10);
68 }
69 
71 {
72  try
73  {
74  deleteMarkers();
75  }
76  catch (const std::exception& ex)
77  {
78  ROS_FATAL("Exception during destruction of Visualization: %s", ex.what());
79  }
80  catch (...)
81  {
82  ROS_FATAL("Exception during destruction of Visualization");
83  }
84 }
85 
87 {
88  for (auto& i : markers_.markers)
89  {
90  i.action = visualization_msgs::Marker::DELETE;
91  }
93  markers_.markers.clear();
94 }
95 
96 void Visualizer::visBasePlane(const EstimatedPlane& plane, const ros::Time timestamp)
97 {
98  deleteMarkers();
99  // visualize base plane as flat disk
100  visualization_msgs::Marker marker;
101  marker.header.stamp = timestamp;
102  marker.header.frame_id = tf_prefix_ + plane.pose_frame;
103  marker.ns = tf_prefix_ + "base_plane";
104  marker.id = 0;
105  marker.lifetime = ros::Duration();
106  marker.type = visualization_msgs::Marker::CYLINDER;
107  marker.action = visualization_msgs::Marker::ADD;
108  marker.scale.x = 1.0;
109  marker.scale.y = 1.0;
110  marker.scale.z = 0.001;
111  marker.pose.position.x = -plane.normal.x * plane.distance;
112  marker.pose.position.y = -plane.normal.y * plane.distance;
113  marker.pose.position.z = -plane.normal.z * plane.distance;
114  // quaternion yielding double the desired rotation (if normal is normalized):
115  // q.w = dot(zaxis, normal), q.xyz = cross(zaxis, normal)
116  // add quaternion with zero rotation (xyz=0, w=1) to get half the rotation from above
117  // and normalize again
118  tf2::Quaternion q(-plane.normal.y, plane.normal.x, 0, plane.normal.z + 1);
119  q.normalize();
120  tf2::convert(q, marker.pose.orientation);
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  markers_.markers.push_back(marker);
127 }
128 
129 inline geometry_msgs::Transform toRosTf(const geometry_msgs::Pose& pose)
130 {
131  geometry_msgs::Transform msg;
132  msg.translation.x = pose.position.x;
133  msg.translation.y = pose.position.y;
134  msg.translation.z = pose.position.z;
135  msg.rotation.x = pose.orientation.x;
136  msg.rotation.y = pose.orientation.y;
137  msg.rotation.z = pose.orientation.z;
138  msg.rotation.w = pose.orientation.w;
139  return msg;
140 }
141 
142 void Visualizer::visInstances(const std::vector<Instance>& instances)
143 {
144  for (const auto& instance : instances)
145  {
146  geometry_msgs::TransformStamped tf;
147  tf.transform = toRosTf(instance.pose);
148  tf.header.frame_id = tf_prefix_ + instance.pose_frame;
149  tf.child_frame_id = tf_prefix_ + instance.object_id + "_" + instance.id;
150  tf.header.stamp = instance.timestamp;
151  tfb_.sendTransform(tf);
152  }
153 }
154 
155 } // namespace rc_silhouettematch_client
#define ROS_FATAL(...)
void publish(const boost::shared_ptr< M > &message) const
void visInstances(const std::vector< Instance > &instances)
Definition: visualizer.cpp:142
visualization_msgs::MarkerArray markers_
Definition: visualizer.h:60
tf2_ros::TransformBroadcaster tfb_
Definition: visualizer.h:61
Quaternion & normalize()
ROSCPP_DECL const std::string & getNamespace()
static std::string make_tf_prefix(const ros::NodeHandle &nh)
Definition: visualizer.cpp:39
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void sendTransform(const geometry_msgs::TransformStamped &transform)
void convert(const A &a, B &b)
void visBasePlane(const EstimatedPlane &plane, const ros::Time timestamp)
Definition: visualizer.cpp:96
geometry_msgs::Transform toRosTf(const geometry_msgs::Pose &pose)
Definition: visualizer.cpp:129


rc_silhouettematch_client
Author(s): Elena Gambaro
autogenerated on Sat Feb 13 2021 03:42:03