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 
49 namespace planning_scene {
50 MOVEIT_CLASS_FORWARD(PlanningScene);
51 }
52 
53 namespace moveit {
54 
55 namespace core {
56 MOVEIT_CLASS_FORWARD(LinkModel);
57 MOVEIT_CLASS_FORWARD(JointModelGroup);
58 MOVEIT_CLASS_FORWARD(RobotState);
59 } // namespace core
60 
61 namespace task_constructor {
62 MOVEIT_CLASS_FORWARD(Property);
63 
64 namespace utils {
65 
67 template <typename Enum>
68 class Flags
69 {
70  static_assert((sizeof(Enum) <= sizeof(int)), "Flags uses an int as storage, this enum will overflow!");
71 
72 public:
73  using Int = typename std::conditional<std::is_unsigned<Enum>::value, unsigned int, int>::type;
74  using enum_type = Enum;
75  // compiler-generated copy/move ctor/assignment operators are fine!
76 
77  // zero flags
78  constexpr inline Flags() noexcept : i(Int(0)) {}
79  // initialization from single enum
80  constexpr inline Flags(Enum f) noexcept : i(Int(f)) {}
81  // initialization from initializer_list
82  constexpr inline Flags(std::initializer_list<Enum> flags) noexcept
83  : i(initializer_list_helper(flags.begin(), flags.end())) {}
84 
85  const inline Flags& operator&=(int mask) noexcept {
86  i &= mask;
87  return *this;
88  }
89  const inline Flags& operator&=(unsigned int mask) noexcept {
90  i &= mask;
91  return *this;
92  }
93  const inline Flags& operator&=(Enum mask) noexcept {
94  i &= Int(mask);
95  return *this;
96  }
97  const inline Flags& operator|=(Flags f) noexcept {
98  i |= f.i;
99  return *this;
100  }
101  const inline Flags& operator|=(Enum f) noexcept {
102  i |= Int(f);
103  return *this;
104  }
105  const inline Flags& operator^=(Flags f) noexcept {
106  i ^= f.i;
107  return *this;
108  }
109  const inline Flags& operator^=(Enum f) noexcept {
110  i ^= Int(f);
111  return *this;
112  }
113 
114  constexpr inline operator Int() const noexcept { return i; }
115 
116  constexpr inline Flags operator|(Flags f) const noexcept { return Flags(i | f.i); }
117  constexpr inline Flags operator|(Enum f) const noexcept { return Flags(i | Int(f)); }
118  constexpr inline Flags operator^(Flags f) const noexcept { return Flags(i ^ f.i); }
119  constexpr inline Flags operator^(Enum f) const noexcept { return Flags(i ^ Int(f)); }
120  constexpr inline Flags operator&(int mask) const noexcept { return Flags(i & mask); }
121  constexpr inline Flags operator&(unsigned int mask) const noexcept { return Flags(i & mask); }
122  constexpr inline Flags operator&(Enum f) const noexcept { return Flags(i & Int(f)); }
123  constexpr inline Flags operator~() const noexcept { return Flags(~i); }
124 
125  constexpr inline bool operator!() const noexcept { return !i; }
126 
127  constexpr inline bool testFlag(Enum f) const noexcept {
128  return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f));
129  }
130 
131 private:
132  constexpr inline Flags(Int i) : i(i) {}
133  constexpr static inline Int
134  initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
135  typename std::initializer_list<Enum>::const_iterator end) noexcept {
136  return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
137  }
138 
139  Int i;
140 };
141 
142 bool getRobotTipForFrame(const Property& property, const planning_scene::PlanningScene& scene,
143  const moveit::core::JointModelGroup* jmg, std::string& error_msg,
144  const moveit::core::LinkModel*& robot_link, Eigen::Isometry3d& tip_in_global_frame);
145 } // namespace utils
146 } // namespace task_constructor
147 } // namespace moveit
Definition: properties.h:70