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::visualizeGrasps(const std::vector<rc_pick_client::SuctionGrasp>& ros_grasps)
187 {
189  if (!ros_grasps.empty())
190  {
191  int counter = 0;
192  visualization_msgs::Marker marker;
193  marker.action = visualization_msgs::Marker::ADD;
194  marker.type = marker.SPHERE;
195  rc_pick_client::Rectangle rect;
196  for (auto& single_grasp : ros_grasps)
197  {
198  rect.x = single_grasp.max_suction_surface_length;
199  rect.y = single_grasp.max_suction_surface_width;
200  setMarker(marker, single_grasp.pose.pose, rect, single_grasp.pose.header.frame_id, counter);
201  publishTf(single_grasp.pose.pose, single_grasp.pose.header.frame_id, "grasp_" + std::to_string(counter));
202  markers_grasps_.markers.push_back(marker);
203  counter++;
204  }
206  }
207 }
208 
209 void Visualization::visualizeDetectedBoxes(const std::vector<rc_pick_client::Item>& ros_boxitems)
210 {
212  if (!ros_boxitems.empty())
213  {
214  int counter = 0;
215  visualization_msgs::Marker marker;
216  marker.type = marker.CUBE;
217  for (auto& item : ros_boxitems)
218  {
219  setMarker(marker, item.pose.pose, item.rectangle, item.pose.header.frame_id, counter);
220  publishTf(item.pose.pose, item.pose.header.frame_id, "boxitem_" + std::to_string(counter));
221  markers_boxes_.markers.push_back(marker);
222  counter++;
223  }
225  }
226 }
227 
228 void Visualization::setMarker(visualization_msgs::Marker& marker, const geometry_msgs::Pose& item_pose,
229  const rc_pick_client::Rectangle& rectangle, std::string frame_id, int marker_id)
230 {
231  marker.scale.z = 0.001;
232  marker.color.r = 0.800f;
233  marker.color.g = 0.20f;
234  marker.color.b = 0.0f;
235  marker.color.a = 0.80;
236  marker.action = visualization_msgs::Marker::ADD;
237  marker.id = marker_id;
238  marker.header.frame_id = frame_id;
239  marker.pose = item_pose;
240  marker.scale.x = rectangle.x;
241  marker.scale.y = rectangle.y;
242 }
243 
244 void Visualization::publishTf(const geometry_msgs::Pose& ros_pose, std::string frame_id, std::string id)
245 {
246  tf::Transform transform;
247  transform.setOrigin(tf::Vector3(ros_pose.position.x, ros_pose.position.y, ros_pose.position.z));
248  transform.setRotation(
249  tf::Quaternion(ros_pose.orientation.x, ros_pose.orientation.y, ros_pose.orientation.z, ros_pose.orientation.w));
251  tf::StampedTransform(transform, ros::Time::now(), frame_id, std::string(nh_.getNamespace() + "/" + id)));
252 }
253 
254 } // namespace pick_visualization
pick_visualization::Visualization::setMarker
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)
Definition: visualization.cpp:228
pick_visualization::Visualization::markers_grasps_
visualization_msgs::MarkerArray markers_grasps_
Definition: visualization.h:97
pick_visualization::Visualization::markers_boxes_
visualization_msgs::MarkerArray markers_boxes_
Definition: visualization.h:98
tf::TransformBroadcaster::sendTransform
void sendTransform(const geometry_msgs::TransformStamped &transform)
pick_visualization::Visualization::Visualization
Visualization(const ros::NodeHandle &nh)
Definition: visualization.cpp:37
pick_visualization::Visualization::markers_lcs_
visualization_msgs::MarkerArray markers_lcs_
Definition: visualization.h:96
tf::StampedTransform
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
pick_visualization::Visualization::publishTf
void publishTf(const geometry_msgs::Pose &ros_pose, std::string frame_id, std::string id)
Definition: visualization.cpp:244
pick_visualization::Visualization::nh_
ros::NodeHandle nh_
Definition: visualization.h:90
pick_visualization::Visualization::box_marker_pub_
ros::Publisher box_marker_pub_
Definition: visualization.h:94
tf::Transform::setRotation
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
pick_visualization::Visualization::deleteBoxItemMarkers
void deleteBoxItemMarkers()
Definition: visualization.cpp:160
pick_visualization::Visualization::visualizeLoadCarriers
void visualizeLoadCarriers(const std::vector< rc_pick_client::LoadCarrier > &ros_lcs)
Definition: visualization.cpp:170
pick_visualization::Visualization::deleteLoadCarrierMarkers
void deleteLoadCarrierMarkers()
Definition: visualization.cpp:140
tf2::Transform
tf::Transform
tf::Transform::setOrigin
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)
pick_visualization::Visualization::deleteGraspMarkers
void deleteGraspMarkers()
Definition: visualization.cpp:150
pick_visualization::Visualization::visualizeGrasps
void visualizeGrasps(const std::vector< rc_pick_client::SuctionGrasp > &ros_grasps)
Definition: visualization.cpp:186
ROS_FATAL
#define ROS_FATAL(...)
pick_visualization
Definition: visualization.cpp:35
pick_visualization::Visualization::~Visualization
~Visualization()
Definition: visualization.cpp:44
pick_visualization::Visualization::br_
tf::TransformBroadcaster br_
Definition: visualization.h:95
tf2::Quaternion
pick_visualization::Visualization::lc_marker_pub_
ros::Publisher lc_marker_pub_
Definition: visualization.h:93
ros::NodeHandle::getNamespace
const std::string & getNamespace() const
pick_visualization::Visualization::constructLoadCarrier
static void constructLoadCarrier(visualization_msgs::MarkerArray &marker_array, const rc_pick_client::LoadCarrier &lc, const int &lc_no)
Definition: visualization.cpp:61
tf::Quaternion
visualization.h
ros::NodeHandle
ros::Time::now
static Time now()
pick_visualization::Visualization::visualizeDetectedBoxes
void visualizeDetectedBoxes(const std::vector< rc_pick_client::Item > &ros_boxitems)
Definition: visualization.cpp:209
pick_visualization::Visualization::grasp_marker_pub_
ros::Publisher grasp_marker_pub_
Definition: visualization.h:92


rc_pick_client
Author(s): Monika Florek-Jasinska
autogenerated on Sun May 15 2022 02:24:50