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 #include <boost/multi_index/mem_fun.hpp>
27 #include <boost/multi_index/ordered_index.hpp>
28 #include <boost/multi_index_container.hpp>
29 
31 #include <VisionX/interface/components/OpenPoseEstimationInterface.h>
32 
33 namespace armarx
34 {
35  static void checkInput(std::initializer_list<float> f)
36  {
37  for (float fe : f)
38  {
39  ARMARX_CHECK_EXPRESSION(fe >= 0.0f) << "Within KeypointManager it is asserted that every value is greater than or equal to zero. That is not true for " << fe;
40  }
41  }
42 
43  struct Point2D
44  {
46  {
47  _x = NAN;
48  _y = NAN;
49  }
50 
51  Point2D(float x, float y)
52  {
53  checkInput({x, y});
54  _x = x;
55  _y = y;
56  }
57 
58  float _x = NAN;
59  float _y = NAN;
60 
61  bool isnan() const
62  {
63  return std::isnan(_x) || std::isnan(_y);
64  }
65  };
66 
67  class Keypoint
68  {
69  public:
70  Keypoint() = delete;
71  Keypoint(const Keypoint& p);
72  Keypoint(float x, float y, unsigned int id, const std::string& name, float confidence = 0.0f);
73  Keypoint(const Point2D& point, unsigned int id, const std::string& name, float confidence = 0.0f);
74  Keypoint(const Point2D& left, const Point2D& right, unsigned int id, const std::string& name, float confidence = 0.0f);
75  ~Keypoint() {}
76 
77  bool is3DSet() const;
78  bool isStereo() const;
79 
80  void set2D(const Point2D& point);
81  void setStereo2D(const Point2D& left, const Point2D& right);
82  void set3D(FramedPositionPtr pose);
83  void setConfidence(float confidence);
84  void setDominantColor(const DrawColor24Bit& color);
85 
86  Point2D get2D() const;
87  std::pair<Point2D, Point2D> getStereo2D() const;
88  FramedPositionPtr get3D() const;
89  float getConfidence() const;
90  DrawColor24Bit getDominantColor() const;
91  unsigned int getId() const;
92  std::string getName() const;
93  std::string toString2D() const;
94 
95  private:
96  unsigned int _id;
97  std::string _name;
98  Point2D _pointL; // point in left image if stereo image is used; otherwise value of keypoint
99  Point2D _pointR; // point in right image if stereo image is used
100  float _confidence;
101  FramedPositionPtr _pos3D;
102  DrawColor24Bit _dominantColor;
103  };
104  using KeypointPtr = std::shared_ptr<Keypoint>;
105 
106 
107  // defining boost multi index container
108  using boost::multi_index_container;
109  using namespace boost::multi_index;
110  using KeypointSet = multi_index_container <
111  KeypointPtr,
112  indexed_by <
113  ordered_unique < const_mem_fun<Keypoint, unsigned int, &Keypoint::getId> >,
114  ordered_non_unique < const_mem_fun<Keypoint, std::string, &Keypoint::getName> >
115  >
116  >;
117  using KeypointSetById = typename KeypointSet::nth_index<0>::type;
118  using KeypointSetByName = typename KeypointSet::nth_index<1>::type;
119 
120 
121  using IdNameMap = const std::map<unsigned int, std::string>& ;
122 
123  class KeypointObject;
124  using KeypointObjectPtr = std::shared_ptr<KeypointObject>;
125  // An object that contains serveral labeled keypoints.
127  {
128 
129  public:
132 
133  typename KeypointSetById::const_iterator begin() const;
134  typename KeypointSetById::const_iterator end() const;
135 
136  KeypointPtr getNode(const std::string& nodeName) const;
137  KeypointPtr getNode(unsigned int id) const;
138  void insertNode(const KeypointPtr point);
139  void removeNode(const std::string& nodeName);
140  void removeNode(unsigned int id);
141  size_t size() const;
142  float getAverageDepth() const;
143 
144  Keypoint2DMap toIce2D(IdNameMap idNameMap) const;
145  Keypoint3DMap toIce3D(IdNameMap idNameMap, const VirtualRobot::RobotPtr&) const;
146 
147  // updates stereo2D-information of the entries of this KeypointObject with the 2D-information of the other KeypointObject
148  // This object keeps all its entries but a stereo2D-information is only set if the other object has a corresponding valid node
149  void matchWith(const KeypointObjectPtr object);
150 
151  std::string toString2D() const;
152 
153  private:
154  KeypointSet _keypoints;
155  };
156 
157 
158  class KeypointManager;
159  using KeypointManagerPtr = std::shared_ptr<KeypointManager>;
160  /**
161  * @class KeypointManager
162  * @brief A brief description
163  *
164  * Detailed Description
165  */
167  {
168  public:
169  /**
170  * KeypointManager Constructor
171  */
172  KeypointManager() = delete;
173  KeypointManager(IdNameMap idNameMap);
174 
175  /**
176  * KeypointManager Destructor
177  */
178  ~KeypointManager();
179 
180  std::vector<KeypointObjectPtr>& getData()
181  {
182  return _objects;
183  }
184 
186  {
187  return _idNameMap;
188  }
189 
190  Keypoint2DMapList toIce2D() const;
191  Keypoint2DMapList toIce2D_normalized(int width, int height) const;
192  Keypoint3DMapList toIce3D(const VirtualRobot::RobotPtr&) const;
193 
194  void matchWith(const KeypointManagerPtr other);
195  // Filters keypointObjects to the nearest n objects
196  void filterToNearestN(unsigned int n);
197 
198  private:
199  std::vector<KeypointObjectPtr> _objects;
200  IdNameMap _idNameMap;
201  };
202 
203 
204 }
armarx::Keypoint::setConfidence
void setConfidence(float confidence)
Definition: KeypointManager.cpp:94
armarx::Point2D::Point2D
Point2D(float x, float y)
Definition: KeypointManager.h:51
armarx::Point2D::_y
float _y
Definition: KeypointManager.h:59
armarx::Keypoint::is3DSet
bool is3DSet() const
Definition: KeypointManager.cpp:66
armarx::KeypointManagerPtr
std::shared_ptr< KeypointManager > KeypointManagerPtr
Definition: KeypointManager.h:159
armarx::KeypointManager
A brief description.
Definition: KeypointManager.h:166
armarx::KeypointObjectPtr
std::shared_ptr< KeypointObject > KeypointObjectPtr
Definition: KeypointManager.h:124
armarx::Point2D::_x
float _x
Definition: KeypointManager.h:58
armarx::KeypointSetById
typename KeypointSet::nth_index< 0 >::type KeypointSetById
Definition: KeypointManager.h:117
armarx::KeypointObject::KeypointObject
KeypointObject()
Definition: KeypointManager.h:130
armarx::Keypoint::getStereo2D
std::pair< Point2D, Point2D > getStereo2D() const
Definition: KeypointManager.cpp:109
armarx::Keypoint::get3D
FramedPositionPtr get3D() const
Definition: KeypointManager.cpp:115
armarx::Keypoint::Keypoint
Keypoint()=delete
IceInternal::Handle< FramedPosition >
armarx::KeypointManager::getIdNameMap
IdNameMap getIdNameMap() const
Definition: KeypointManager.h:185
armarx::Point2D::isnan
bool isnan() const
Definition: KeypointManager.h:61
FramedPose.h
armarx::Keypoint::get2D
Point2D get2D() const
Definition: KeypointManager.cpp:104
armarx::KeypointSetByName
typename KeypointSet::nth_index< 1 >::type KeypointSetByName
Definition: KeypointManager.h:118
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:116
armarx::KeypointPtr
std::shared_ptr< Keypoint > KeypointPtr
Definition: KeypointManager.h:104
armarx::Keypoint::setDominantColor
void setDominantColor(const DrawColor24Bit &color)
Definition: KeypointManager.cpp:99
armarx::Keypoint::~Keypoint
~Keypoint()
Definition: KeypointManager.h:75
armarx::Keypoint
Definition: KeypointManager.h:67
armarx::Keypoint::set3D
void set3D(FramedPositionPtr pose)
Definition: KeypointManager.cpp:89
armarx::Keypoint::getConfidence
float getConfidence() const
Definition: KeypointManager.cpp:120
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:76
armarx::IdNameMap
const std::map< unsigned int, std::string > & IdNameMap
Definition: KeypointManager.h:121
armarx::Point2D::Point2D
Point2D()
Definition: KeypointManager.h:45
armarx::Keypoint::getId
unsigned int getId() const
Definition: KeypointManager.cpp:130
armarx::Keypoint::getName
std::string getName() const
Definition: KeypointManager.cpp:135
armarx::Keypoint::getDominantColor
DrawColor24Bit getDominantColor() const
Definition: KeypointManager.cpp:125
armarx::Point2D
Definition: KeypointManager.h:43
armarx::Keypoint::setStereo2D
void setStereo2D(const Point2D &left, const Point2D &right)
Definition: KeypointManager.cpp:82
armarx::KeypointObject::~KeypointObject
~KeypointObject()
Definition: KeypointManager.h:131
armarx::Keypoint::toString2D
std::string toString2D() const
Definition: KeypointManager.cpp:140
armarx::KeypointObject
Definition: KeypointManager.h:126
armarx::KeypointManager::getData
std::vector< KeypointObjectPtr > & getData()
Definition: KeypointManager.h:180
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::Keypoint::isStereo
bool isStereo() const
Definition: KeypointManager.cpp:71