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 
36 {
38 {
39  grasp_marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("grasp", 1);
40  lc_marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("lc", 1);
41  box_marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("boxitem", 1);
42 }
43 
45 {
46  try
47  {
50  }
51  catch (const std::exception& ex)
52  {
53  ROS_FATAL("Exception during destruction of Visualization: %s", ex.what());
54  }
55  catch (...)
56  {
57  ROS_FATAL("Exception during destruction of Visualization");
58  }
59 }
60 
61 void Visualization::constructLoadCarrier(visualization_msgs::MarkerArray& marker_array,
62  const rc_pick_client::LoadCarrier& lc, const int& lc_no)
63 {
64  visualization_msgs::Marker marker;
65  marker.header.frame_id = lc.pose.header.frame_id;
66  marker.type = marker.CUBE;
67  marker.color.r = 0.0f;
68  marker.color.g = 0.20f;
69  marker.color.b = 0.80f;
70  marker.color.a = 0.3;
71  tf2::Transform bin_transform(tf2::Quaternion(lc.pose.pose.orientation.x, lc.pose.pose.orientation.y,
72  lc.pose.pose.orientation.z, lc.pose.pose.orientation.w),
73  tf2::Vector3(lc.pose.pose.position.x, lc.pose.pose.position.y, lc.pose.pose.position.z));
74 
75  double rim_thickness_x = (lc.outer_dimensions.x - lc.inner_dimensions.x) / 2;
76  double dist_from_center_x = (lc.outer_dimensions.x - rim_thickness_x) / 2;
77  double rim_thickness_y = (lc.outer_dimensions.y - lc.inner_dimensions.y) / 2;
78  double dist_from_center_y = (lc.outer_dimensions.y - rim_thickness_y) / 2;
79  double bottom_thickness = lc.outer_dimensions.z - lc.inner_dimensions.z;
80  tf2::Vector3 pose(0, 0, -(lc.outer_dimensions.z - bottom_thickness) / 2);
81 
82  // calculate bottom
83  pose = bin_transform * pose;
84  marker.id = lc_no;
85  marker.pose = lc.pose.pose;
86  marker.pose.position.x = pose.getX();
87  marker.pose.position.y = pose.getY();
88  marker.pose.position.z = pose.getZ();
89  marker.scale.x = lc.outer_dimensions.x;
90  marker.scale.y = lc.outer_dimensions.y;
91  marker.scale.z = bottom_thickness;
92  marker_array.markers.push_back(marker);
93 
94  // calculate longer sides
95  pose = tf2::Vector3(dist_from_center_x, 0, 0);
96  pose = bin_transform * pose;
97 
98  marker.id = lc_no + 1;
99  marker.pose = lc.pose.pose;
100  marker.pose.position.x = pose.getX();
101  marker.pose.position.y = pose.getY();
102  marker.pose.position.z = pose.getZ();
103  marker.scale.x = rim_thickness_x;
104  marker.scale.y = lc.outer_dimensions.y;
105  marker.scale.z = lc.outer_dimensions.z;
106  marker_array.markers.push_back(marker);
107 
108  marker.id = lc_no + 2;
109  pose = tf2::Vector3(-dist_from_center_x, 0, 0);
110  pose = bin_transform * pose;
111  marker.pose.position.x = pose.getX();
112  marker.pose.position.y = pose.getY();
113  marker.pose.position.z = pose.getZ();
114  marker_array.markers.push_back(marker);
115 
116  // calculate shorter side
117  marker.id = lc_no + 3;
118  pose = tf2::Vector3(0, dist_from_center_y, 0);
119  pose = bin_transform * pose;
120  marker.pose = lc.pose.pose;
121  marker.pose.position.x = pose.getX();
122  marker.pose.position.y = pose.getY();
123  marker.pose.position.z = pose.getZ();
124  marker.scale.x = lc.outer_dimensions.x;
125  marker.scale.y = rim_thickness_y;
126  marker.scale.z = lc.outer_dimensions.z;
127  marker_array.markers.push_back(marker);
128 
129  marker.id = lc_no + 4;
130  pose = tf2::Vector3(0, -dist_from_center_y, 0);
131  pose = bin_transform * pose;
132  marker.pose = lc.pose.pose;
133  marker.pose.position.x = pose.getX();
134  marker.pose.position.y = pose.getY();
135  marker.pose.position.z = pose.getZ();
136  marker.scale.x = lc.outer_dimensions.x;
137  marker_array.markers.push_back(marker);
138 }
139 
141 {
142  for (auto& i : markers_lcs_.markers)
143  {
144  i.action = visualization_msgs::Marker::DELETE;
145  }
147  markers_lcs_.markers.clear();
148 }
149 
151 {
152  for (auto& i : markers_grasps_.markers)
153  {
154  i.action = visualization_msgs::Marker::DELETE;
155  }
157  markers_grasps_.markers.clear();
158 }
159 
161 {
162  for (auto& i : markers_boxes_.markers)
163  {
164  i.action = visualization_msgs::Marker::DELETE;
165  }
167  markers_boxes_.markers.clear();
168 }
169 
170 void Visualization::visualizeLoadCarriers(const std::vector<rc_pick_client::LoadCarrier>& ros_lcs)
171 {
173  if (!ros_lcs.empty())
174  {
175  int counter = 0;
176  for (auto& single_lc : ros_lcs)
177  {
178  constructLoadCarrier(markers_lcs_, single_lc, counter);
179  publishTf(single_lc.pose.pose, single_lc.pose.header.frame_id, "lc_" + std::to_string(counter));
180  counter++;
181  }
183  }
184 }
185 
186 void Visualization::visualizeLoadCarriers(const std::vector<rc_pick_client::LoadCarrierWithFillingLevel>& ros_lcs)
187 {
189  if (!ros_lcs.empty())
190  {
191  int counter = 0;
192  for (auto& lc_with_level : ros_lcs)
193  {
194  rc_pick_client::LoadCarrier lc;
195  lc.id = lc_with_level.id;
196  lc.outer_dimensions = lc_with_level.outer_dimensions;
197  lc.inner_dimensions = lc_with_level.inner_dimensions;
198  lc.rim_thickness = lc_with_level.rim_thickness;
199  lc.pose = lc_with_level.pose;
200  lc.overfilled = lc_with_level.overfilled;
201  constructLoadCarrier(markers_lcs_, lc, counter);
202  publishTf(lc.pose.pose, lc.pose.header.frame_id, "lc_" + std::to_string(counter));
203  // TODO: visualize filling level somehow?
204  counter++;
205  }
207  }
208 }
209 
210 void Visualization::visualizeGrasps(const std::vector<rc_pick_client::SuctionGrasp>& ros_grasps)
211 {
213  if (!ros_grasps.empty())
214  {
215  int counter = 0;
216  visualization_msgs::Marker marker;
217  marker.action = visualization_msgs::Marker::ADD;
218  marker.type = marker.SPHERE;
219  rc_pick_client::Rectangle rect;
220  for (auto& single_grasp : ros_grasps)
221  {
222  rect.x = single_grasp.max_suction_surface_length;
223  rect.y = single_grasp.max_suction_surface_width;
224  setMarker(marker, single_grasp.pose.pose, rect, single_grasp.pose.header.frame_id, counter);
225  publishTf(single_grasp.pose.pose, single_grasp.pose.header.frame_id, "grasp_" + std::to_string(counter));
226  markers_grasps_.markers.push_back(marker);
227  counter++;
228  }
230  }
231 }
232 
233 void Visualization::visualizeDetectedBoxes(const std::vector<rc_pick_client::Item>& ros_boxitems)
234 {
236  if (!ros_boxitems.empty())
237  {
238  int counter = 0;
239  visualization_msgs::Marker marker;
240  marker.type = marker.CUBE;
241  for (auto& item : ros_boxitems)
242  {
243  setMarker(marker, item.pose.pose, item.rectangle, item.pose.header.frame_id, counter);
244  publishTf(item.pose.pose, item.pose.header.frame_id, "boxitem_" + std::to_string(counter));
245  markers_boxes_.markers.push_back(marker);
246  counter++;
247  }
249  }
250 }
251 
252 void Visualization::setMarker(visualization_msgs::Marker& marker, const geometry_msgs::Pose& item_pose,
253  const rc_pick_client::Rectangle& rectangle, std::string frame_id, int marker_id)
254 {
255  marker.scale.z = 0.001;
256  marker.color.r = 0.800f;
257  marker.color.g = 0.20f;
258  marker.color.b = 0.0f;
259  marker.color.a = 0.80;
260  marker.action = visualization_msgs::Marker::ADD;
261  marker.id = marker_id;
262  marker.header.frame_id = frame_id;
263  marker.pose = item_pose;
264  marker.scale.x = rectangle.x;
265  marker.scale.y = rectangle.y;
266 }
267 
268 void Visualization::publishTf(const geometry_msgs::Pose& ros_pose, std::string frame_id, std::string id)
269 {
270  tf::Transform transform;
271  transform.setOrigin(tf::Vector3(ros_pose.position.x, ros_pose.position.y, ros_pose.position.z));
272  transform.setRotation(
273  tf::Quaternion(ros_pose.orientation.x, ros_pose.orientation.y, ros_pose.orientation.z, ros_pose.orientation.w));
275  tf::StampedTransform(transform, ros::Time::now(), frame_id, std::string(nh_.getNamespace() + "/" + id)));
276 }
277 
278 } // namespace pick_visualization
void visualizeGrasps(const std::vector< rc_pick_client::SuctionGrasp > &ros_grasps)
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
#define ROS_FATAL(...)
Visualization(const ros::NodeHandle &nh)
void publish(const boost::shared_ptr< M > &message) const
visualization_msgs::MarkerArray markers_lcs_
static void constructLoadCarrier(visualization_msgs::MarkerArray &marker_array, const rc_pick_client::LoadCarrier &lc, const int &lc_no)
void setMarker(visualization_msgs::Marker &marker, const geometry_msgs::Pose &item_pose, const rc_pick_client::Rectangle &rectangle, std::string frame_id, int marker_id)
void publishTf(const geometry_msgs::Pose &ros_pose, std::string frame_id, std::string id)
void visualizeDetectedBoxes(const std::vector< rc_pick_client::Item > &ros_boxitems)
void sendTransform(const StampedTransform &transform)
const std::string & getNamespace() const
visualization_msgs::MarkerArray markers_boxes_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
static Time now()
void visualizeLoadCarriers(const std::vector< rc_pick_client::LoadCarrier > &ros_lcs)
tf::TransformBroadcaster br_
visualization_msgs::MarkerArray markers_grasps_
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)


rc_pick_client
Author(s): Monika Florek-Jasinska
autogenerated on Sat Feb 13 2021 03:41:57