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
35namespace 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
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<
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 */
205
206 std::vector<KeypointObjectPtr>&
208 {
209 return _objects;
210 }
211
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
Keypoint3DMapList toIce3D(const VirtualRobot::RobotPtr &) const
std::vector< KeypointObjectPtr > & getData()
void filterToNearestN(unsigned int n)
IdNameMap getIdNameMap() const
~KeypointManager()
KeypointManager Destructor.
Keypoint2DMapList toIce2D_normalized(int width, int height) const
KeypointManager()=delete
KeypointManager Constructor.
void matchWith(const KeypointManagerPtr other)
Keypoint2DMapList toIce2D() const
std::string toString2D() const
KeypointSetById::const_iterator begin() const
KeypointSetById::const_iterator end() const
void matchWith(const KeypointObjectPtr object)
KeypointPtr getNode(const std::string &nodeName) const
Keypoint2DMap toIce2D(IdNameMap idNameMap) const
void removeNode(const std::string &nodeName)
Keypoint3DMap toIce3D(IdNameMap idNameMap, const VirtualRobot::RobotPtr &) const
void insertNode(const KeypointPtr point)
std::string toString2D() const
void set2D(const Point2D &point)
void setDominantColor(const DrawColor24Bit &color)
Point2D get2D() const
void set3D(FramedPositionPtr pose)
void setConfidence(float confidence)
std::pair< Point2D, Point2D > getStereo2D() const
void setStereo2D(const Point2D &left, const Point2D &right)
std::string getName() const
DrawColor24Bit getDominantColor() const
FramedPositionPtr get3D() const
float getConfidence() const
unsigned int getId() const
bool isStereo() const
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
typename KeypointSet::nth_index< 1 >::type KeypointSetByName
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
std::shared_ptr< KeypointManager > KeypointManagerPtr
std::shared_ptr< Keypoint > KeypointPtr
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
std::shared_ptr< KeypointObject > KeypointObjectPtr
const std::map< unsigned int, std::string > & IdNameMap
typename KeypointSet::nth_index< 0 >::type KeypointSetById
bool isnan() const
Point2D(float x, float y)