display_solution.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Bielefeld University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Bielefeld University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Robert Haschke */
36 
41 #include <ros/console.h>
42 #include <fmt/core.h>
43 
44 namespace moveit_rviz_plugin {
45 
46 std::pair<size_t, size_t> DisplaySolution::indexPair(size_t index) const {
47  size_t part = 0;
48  for (const auto& d : data_) {
49  if (index < d.trajectory_->getWayPointCount())
50  break;
51  index -= d.trajectory_->getWayPointCount();
52  ++part;
53  }
54  assert(part < data_.size());
55  assert(index < data_[part].trajectory_->getWayPointCount());
56  return std::make_pair(part, index);
57 }
58 
59 DisplaySolution::DisplaySolution(const DisplaySolution& master, uint32_t sub)
60  : start_scene_(sub == 0 ? master.start_scene_ : master.data_[sub - 1].scene_), data_({ master.data_[sub] }) {
61  steps_ = data_.front().trajectory_->getWayPointCount();
62 }
63 
64 float DisplaySolution::getWayPointDurationFromPrevious(const IndexPair& idx_pair) const {
65  return data_[idx_pair.first].trajectory_->getWayPointDurationFromPrevious(idx_pair.second);
66 }
67 
68 const moveit::core::RobotStatePtr& DisplaySolution::getWayPointPtr(const IndexPair& idx_pair) const {
69  return data_[idx_pair.first].trajectory_->getWayPointPtr(idx_pair.second);
70 }
71 
72 const planning_scene::PlanningSceneConstPtr& DisplaySolution::scene(const IndexPair& idx_pair) const {
73  // start scene is parent of end scene
74  return data_[idx_pair.first].scene_->getParent();
75 }
76 
77 const std::string& DisplaySolution::comment(const IndexPair& idx_pair) const {
78  return data_[idx_pair.first].comment_;
79 }
80 
81 uint32_t DisplaySolution::creatorId(const DisplaySolution::IndexPair& idx_pair) const {
82  return data_[idx_pair.first].creator_id_;
83 }
84 
85 const MarkerVisualizationPtr DisplaySolution::markers(const DisplaySolution::IndexPair& idx_pair) const {
86  return data_[idx_pair.first].markers_;
87 }
88 
89 void DisplaySolution::setFromMessage(const planning_scene::PlanningScenePtr& start_scene,
90  const moveit_task_constructor_msgs::Solution& msg) {
91  if (msg.start_scene.robot_model_name != start_scene->getRobotModel()->getName())
92  throw std::invalid_argument(fmt::format("Solution for model '{}' but model '{}' was expected",
93  msg.start_scene.robot_model_name,
94  start_scene->getRobotModel()->getName()));
95 
96  // initialize parent scene from solution's start scene
97  start_scene->setPlanningSceneMsg(msg.start_scene);
98  start_scene_ = start_scene;
99  planning_scene::PlanningScenePtr ref_scene = start_scene_->diff();
100 
101  data_.resize(msg.sub_trajectory.size());
102 
103  steps_ = 0;
104  size_t i = 0;
105  for (const auto& sub : msg.sub_trajectory) {
106  data_[i].trajectory_.reset(new robot_trajectory::RobotTrajectory(ref_scene->getRobotModel(), nullptr));
107  data_[i].trajectory_->setRobotTrajectoryMsg(ref_scene->getCurrentState(), sub.trajectory);
108  data_[i].joints_ = sub.trajectory.joint_trajectory.joint_names;
109  data_[i].joints_.insert(data_[i].joints_.end(), sub.trajectory.multi_dof_joint_trajectory.joint_names.begin(),
110  sub.trajectory.multi_dof_joint_trajectory.joint_names.end());
111  data_[i].comment_ = sub.info.comment;
112  data_[i].creator_id_ = sub.info.stage_id;
113  steps_ += data_[i].trajectory_->getWayPointCount();
114 
115  ref_scene->setPlanningSceneDiffMsg(sub.scene_diff);
116  data_[i].scene_ = ref_scene;
117 
118  // create new reference scene for next iteration
119  ref_scene = ref_scene->diff();
120 
121  if (!sub.info.markers.empty())
122  data_[i].markers_.reset(new MarkerVisualization(sub.info.markers, *ref_scene));
123  else
124  data_[i].markers_.reset();
125  ++i;
126  }
127 }
128 
129 void DisplaySolution::fillMessage(moveit_task_constructor_msgs::Solution& msg) const {
130  start_scene_->getPlanningSceneMsg(msg.start_scene);
131  msg.sub_trajectory.resize(data_.size());
132  auto traj_it = msg.sub_trajectory.begin();
133  for (const auto& sub : data_) {
134  sub.scene_->getPlanningSceneDiffMsg(traj_it->scene_diff);
135  sub.trajectory_->getRobotTrajectoryMsg(traj_it->trajectory, sub.joints_);
136  ++traj_it;
137  }
138 }
139 
140 } // namespace moveit_rviz_plugin
moveit_rviz_plugin::DisplaySolution::indexPair
IndexPair indexPair(size_t index) const
Definition: display_solution.cpp:78
moveit_rviz_plugin::DisplaySolution::data_
std::vector< Data > data_
Definition: display_solution.h:88
marker_visualization.h
moveit_rviz_plugin::DisplaySolution::IndexPair
std::pair< size_t, size_t > IndexPair
pair of trajectory part and way point index within part
Definition: display_solution.h:101
moveit_rviz_plugin::MarkerVisualization
Definition: marker_visualization.h:39
uint32_t
uint32_t
robot_trajectory::RobotTrajectory
moveit_rviz_plugin::DisplaySolution::getWayPointCount
size_t getWayPointCount() const
Definition: display_solution.h:97
robot_trajectory.h
console.h
moveit_rviz_plugin::DisplaySolution::DisplaySolution
DisplaySolution()=default
d
d
moveit_rviz_plugin
planning_scene.h
index
unsigned int index
display_solution.h


visualization
Author(s): Robert Haschke
autogenerated on Thu Feb 27 2025 03:39:51