MTC
utils.h
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 /* Authors: Robert Haschke, Michael 'v4hn' Goerner
36  Desc: Miscellaneous utilities
37 */
38 
39 #pragma once
40 
41 #include <string>
42 #include <type_traits>
43 #include <initializer_list>
44 
45 #include <Eigen/Geometry>
46 
47 #include <moveit/macros/class_forward.h>
48 #include <moveit/robot_trajectory/robot_trajectory.h>
49 #include <moveit/collision_detection/collision_common.h>
50 #include <moveit/task_constructor/solvers/planner_interface.h>
51 
52 namespace planning_scene {
53 MOVEIT_CLASS_FORWARD(PlanningScene);
54 }
55 
56 namespace moveit {
57 
58 namespace core {
59 MOVEIT_CLASS_FORWARD(LinkModel);
60 MOVEIT_CLASS_FORWARD(JointModelGroup);
61 MOVEIT_CLASS_FORWARD(RobotState);
62 } // namespace core
63 
64 namespace task_constructor {
65 MOVEIT_CLASS_FORWARD(Property);
66 
67 namespace utils {
68 
70 template <typename Enum>
71 class Flags
72 {
73  static_assert((sizeof(Enum) <= sizeof(int)), "Flags uses an int as storage, this enum will overflow!");
74 
75 public:
76  using Int = typename std::conditional<std::is_unsigned<Enum>::value, unsigned int, int>::type;
77  using enum_type = Enum;
78  // compiler-generated copy/move ctor/assignment operators are fine!
79 
80  // zero flags
81  constexpr inline Flags() noexcept : i(Int(0)) {}
82  // initialization from single enum
83  constexpr inline Flags(Enum f) noexcept : i(Int(f)) {}
84  // initialization from initializer_list
85  constexpr inline Flags(std::initializer_list<Enum> flags) noexcept
86  : i(initializer_list_helper(flags.begin(), flags.end())) {}
87 
88  const inline Flags& operator&=(int mask) noexcept {
89  i &= mask;
90  return *this;
91  }
92  const inline Flags& operator&=(unsigned int mask) noexcept {
93  i &= mask;
94  return *this;
95  }
96  const inline Flags& operator&=(Enum mask) noexcept {
97  i &= Int(mask);
98  return *this;
99  }
100  const inline Flags& operator|=(Flags f) noexcept {
101  i |= f.i;
102  return *this;
103  }
104  const inline Flags& operator|=(Enum f) noexcept {
105  i |= Int(f);
106  return *this;
107  }
108  const inline Flags& operator^=(Flags f) noexcept {
109  i ^= f.i;
110  return *this;
111  }
112  const inline Flags& operator^=(Enum f) noexcept {
113  i ^= Int(f);
114  return *this;
115  }
116 
117  constexpr inline operator Int() const noexcept { return i; }
118 
119  constexpr inline Flags operator|(Flags f) const noexcept { return Flags(i | f.i); }
120  constexpr inline Flags operator|(Enum f) const noexcept { return Flags(i | Int(f)); }
121  constexpr inline Flags operator^(Flags f) const noexcept { return Flags(i ^ f.i); }
122  constexpr inline Flags operator^(Enum f) const noexcept { return Flags(i ^ Int(f)); }
123  constexpr inline Flags operator&(int mask) const noexcept { return Flags(i & mask); }
124  constexpr inline Flags operator&(unsigned int mask) const noexcept { return Flags(i & mask); }
125  constexpr inline Flags operator&(Enum f) const noexcept { return Flags(i & Int(f)); }
126  constexpr inline Flags operator~() const noexcept { return Flags(~i); }
127 
128  constexpr inline bool operator!() const noexcept { return !i; }
129 
130  constexpr inline bool testFlag(Enum f) const noexcept {
131  return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f));
132  }
133 
134 private:
135  constexpr inline Flags(Int i) : i(i) {}
136  constexpr static inline Int
137  initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
138  typename std::initializer_list<Enum>::const_iterator end) noexcept {
139  return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
140  }
141 
142  Int i;
143 };
144 
146 bool getRobotTipForFrame(const Property& tip_pose, const planning_scene::PlanningScene& scene,
147  const moveit::core::JointModelGroup* jmg, std::string& error_msg,
148  const moveit::core::LinkModel*& robot_link, Eigen::Isometry3d& tip_in_global_frame);
149 
151 void addCollisionMarkers(std::vector<visualization_msgs::Marker>& markers, const std::string& frame_id,
152  const collision_detection::CollisionResult::ContactMap& contacts, double radius = 0.035);
153 
155 void addCollisionMarkers(std::vector<visualization_msgs::Marker>& markers,
156  const robot_trajectory::RobotTrajectory& trajectory,
157  const planning_scene::PlanningSceneConstPtr& planning_scene);
158 
160 bool hints_at_collisions(const solvers::PlannerInterface::Result& result);
161 
162 } // namespace utils
163 } // namespace task_constructor
164 } // namespace moveit
Definition: properties.h:70