KeypointManager.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package <PACKAGE_NAME>::<CATEGORY>::KeypointManager
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <cmath>
26 
27 #include <boost/multi_index/mem_fun.hpp>
28 #include <boost/multi_index/ordered_index.hpp>
29 #include <boost/multi_index_container.hpp>
30 
32 
33 #include <VisionX/interface/components/OpenPoseEstimationInterface.h>
34 
35 namespace armarx
36 {
37  static void
38  checkInput(std::initializer_list<float> f)
39  {
40  for (float fe : f)
41  {
42  ARMARX_CHECK_EXPRESSION(fe >= 0.0f)
43  << "Within KeypointManager it is asserted that every value is greater than or "
44  "equal to zero. That is not true for "
45  << fe;
46  }
47  }
48 
49  struct Point2D
50  {
52  {
53  _x = NAN;
54  _y = NAN;
55  }
56 
57  Point2D(float x, float y)
58  {
59  checkInput({x, y});
60  _x = x;
61  _y = y;
62  }
63 
64  float _x = NAN;
65  float _y = NAN;
66 
67  bool
68  isnan() const
69  {
70  return std::isnan(_x) || std::isnan(_y);
71  }
72  };
73 
74  class Keypoint
75  {
76  public:
77  Keypoint() = delete;
78  Keypoint(const Keypoint& p);
79  Keypoint(float x,
80  float y,
81  unsigned int id,
82  const std::string& name,
83  float confidence = 0.0f);
84  Keypoint(const Point2D& point,
85  unsigned int id,
86  const std::string& name,
87  float confidence = 0.0f);
88  Keypoint(const Point2D& left,
89  const Point2D& right,
90  unsigned int id,
91  const std::string& name,
92  float confidence = 0.0f);
93 
95  {
96  }
97 
98  bool is3DSet() const;
99  bool isStereo() const;
100 
101  void set2D(const Point2D& point);
102  void setStereo2D(const Point2D& left, const Point2D& right);
103  void set3D(FramedPositionPtr pose);
104  void setConfidence(float confidence);
105  void setDominantColor(const DrawColor24Bit& color);
106 
107  Point2D get2D() const;
108  std::pair<Point2D, Point2D> getStereo2D() const;
109  FramedPositionPtr get3D() const;
110  float getConfidence() const;
111  DrawColor24Bit getDominantColor() const;
112  unsigned int getId() const;
113  std::string getName() const;
114  std::string toString2D() const;
115 
116  private:
117  unsigned int _id;
118  std::string _name;
119  Point2D _pointL; // point in left image if stereo image is used; otherwise value of keypoint
120  Point2D _pointR; // point in right image if stereo image is used
121  float _confidence;
122  FramedPositionPtr _pos3D;
123  DrawColor24Bit _dominantColor;
124  };
125 
126  using KeypointPtr = std::shared_ptr<Keypoint>;
127 
128 
129  // defining boost multi index container
130  using boost::multi_index_container;
131  using namespace boost::multi_index;
132  using KeypointSet = multi_index_container<
133  KeypointPtr,
134  indexed_by<ordered_unique<const_mem_fun<Keypoint, unsigned int, &Keypoint::getId>>,
135  ordered_non_unique<const_mem_fun<Keypoint, std::string, &Keypoint::getName>>>>;
136  using KeypointSetById = typename KeypointSet::nth_index<0>::type;
137  using KeypointSetByName = typename KeypointSet::nth_index<1>::type;
138 
139 
140  using IdNameMap = const std::map<unsigned int, std::string>&;
141 
142  class KeypointObject;
143  using KeypointObjectPtr = std::shared_ptr<KeypointObject>;
144 
145  // An object that contains serveral labeled keypoints.
147  {
148 
149  public:
151  {
152  }
153 
155  {
156  }
157 
158  typename KeypointSetById::const_iterator begin() const;
159  typename KeypointSetById::const_iterator end() const;
160 
161  KeypointPtr getNode(const std::string& nodeName) const;
162  KeypointPtr getNode(unsigned int id) const;
163  void insertNode(const KeypointPtr point);
164  void removeNode(const std::string& nodeName);
165  void removeNode(unsigned int id);
166  size_t size() const;
167  float getAverageDepth() const;
168 
169  Keypoint2DMap toIce2D(IdNameMap idNameMap) const;
170  Keypoint3DMap toIce3D(IdNameMap idNameMap, const VirtualRobot::RobotPtr&) const;
171 
172  // updates stereo2D-information of the entries of this KeypointObject with the 2D-information of the other KeypointObject
173  // This object keeps all its entries but a stereo2D-information is only set if the other object has a corresponding valid node
174  void matchWith(const KeypointObjectPtr object);
175 
176  std::string toString2D() const;
177 
178  private:
179  KeypointSet _keypoints;
180  };
181 
182 
183  class KeypointManager;
184  using KeypointManagerPtr = std::shared_ptr<KeypointManager>;
185 
186  /**
187  * @class KeypointManager
188  * @brief A brief description
189  *
190  * Detailed Description
191  */
193  {
194  public:
195  /**
196  * KeypointManager Constructor
197  */
198  KeypointManager() = delete;
199  KeypointManager(IdNameMap idNameMap);
200 
201  /**
202  * KeypointManager Destructor
203  */
204  ~KeypointManager();
205 
206  std::vector<KeypointObjectPtr>&
208  {
209  return _objects;
210  }
211 
212  IdNameMap
213  getIdNameMap() const
214  {
215  return _idNameMap;
216  }
217 
218  Keypoint2DMapList toIce2D() const;
219  Keypoint2DMapList toIce2D_normalized(int width, int height) const;
220  Keypoint3DMapList toIce3D(const VirtualRobot::RobotPtr&) const;
221 
222  void matchWith(const KeypointManagerPtr other);
223  // Filters keypointObjects to the nearest n objects
224  void filterToNearestN(unsigned int n);
225 
226  private:
227  std::vector<KeypointObjectPtr> _objects;
228  IdNameMap _idNameMap;
229  };
230 
231 
232 } // namespace armarx
armarx::Keypoint::setConfidence
void setConfidence(float confidence)
Definition: KeypointManager.cpp:109
armarx::Point2D::Point2D
Point2D(float x, float y)
Definition: KeypointManager.h:57
armarx::Point2D::_y
float _y
Definition: KeypointManager.h:65
armarx::Keypoint::is3DSet
bool is3DSet() const
Definition: KeypointManager.cpp:76
armarx::KeypointManagerPtr
std::shared_ptr< KeypointManager > KeypointManagerPtr
Definition: KeypointManager.h:184
armarx::KeypointManager
A brief description.
Definition: KeypointManager.h:192
armarx::KeypointObjectPtr
std::shared_ptr< KeypointObject > KeypointObjectPtr
Definition: KeypointManager.h:143
armarx::Point2D::_x
float _x
Definition: KeypointManager.h:64
armarx::KeypointSetById
typename KeypointSet::nth_index< 0 >::type KeypointSetById
Definition: KeypointManager.h:136
armarx::KeypointObject::KeypointObject
KeypointObject()
Definition: KeypointManager.h:150
armarx::Keypoint::getStereo2D
std::pair< Point2D, Point2D > getStereo2D() const
Definition: KeypointManager.cpp:127
armarx::Keypoint::get3D
FramedPositionPtr get3D() const
Definition: KeypointManager.cpp:134
armarx::KeypointSet
multi_index_container< KeypointPtr, indexed_by< ordered_unique< const_mem_fun< Keypoint, unsigned int, &Keypoint::getId > >, ordered_non_unique< const_mem_fun< Keypoint, std::string, &Keypoint::getName > >> > KeypointSet
Definition: KeypointManager.h:135
armarx::Keypoint::Keypoint
Keypoint()=delete
IceInternal::Handle< FramedPosition >
armarx::KeypointManager::getIdNameMap
IdNameMap getIdNameMap() const
Definition: KeypointManager.h:213
armarx::Point2D::isnan
bool isnan() const
Definition: KeypointManager.h:68
FramedPose.h
armarx::Keypoint::get2D
Point2D get2D() const
Definition: KeypointManager.cpp:121
armarx::KeypointSetByName
typename KeypointSet::nth_index< 1 >::type KeypointSetByName
Definition: KeypointManager.h:137
armarx::KeypointPtr
std::shared_ptr< Keypoint > KeypointPtr
Definition: KeypointManager.h:126
armarx::Keypoint::setDominantColor
void setDominantColor(const DrawColor24Bit &color)
Definition: KeypointManager.cpp:115
armarx::Keypoint::~Keypoint
~Keypoint()
Definition: KeypointManager.h:94
armarx::Keypoint
Definition: KeypointManager.h:74
armarx::Keypoint::set3D
void set3D(FramedPositionPtr pose)
Definition: KeypointManager.cpp:103
armarx::Keypoint::getConfidence
float getConfidence() const
Definition: KeypointManager.cpp:140
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::Keypoint::set2D
void set2D(const Point2D &point)
Definition: KeypointManager.cpp:88
armarx::IdNameMap
const std::map< unsigned int, std::string > & IdNameMap
Definition: KeypointManager.h:140
armarx::Point2D::Point2D
Point2D()
Definition: KeypointManager.h:51
armarx::Keypoint::getId
unsigned int getId() const
Definition: KeypointManager.cpp:152
armarx::Keypoint::getName
std::string getName() const
Definition: KeypointManager.cpp:158
armarx::Keypoint::getDominantColor
DrawColor24Bit getDominantColor() const
Definition: KeypointManager.cpp:146
armarx::Point2D
Definition: KeypointManager.h:49
armarx::Keypoint::setStereo2D
void setStereo2D(const Point2D &left, const Point2D &right)
Definition: KeypointManager.cpp:95
armarx::KeypointObject::~KeypointObject
~KeypointObject()
Definition: KeypointManager.h:154
armarx::Keypoint::toString2D
std::string toString2D() const
Definition: KeypointManager.cpp:164
armarx::KeypointObject
Definition: KeypointManager.h:146
armarx::KeypointManager::getData
std::vector< KeypointObjectPtr > & getData()
Definition: KeypointManager.h:207
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::Keypoint::isStereo
bool isStereo() const
Definition: KeypointManager.cpp:82