Descriptor.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_DESCRIPTOR_H
8 #define NUKLEI_DESCRIPTOR_H
9 
10 #include <memory>
11 #include <boost/utility.hpp>
12 #include <nuklei/LinearAlgebra.h>
13 #include <nuklei/Common.h>
14 #include <nuklei/Color.h>
15 #include <nuklei/Types.h>
16 
17 namespace nuklei {
18 
19  class Descriptor : boost::noncopyable
20  {
21  public:
22 
23  typedef NUKLEI_UNIQUE_PTR< Descriptor > ptr;
24 
25  virtual ~Descriptor() {}
26 
27  virtual void assertConsistency() const = 0;
28 
29  virtual NUKLEI_UNIQUE_PTR<Descriptor> clone() const = 0;
30  virtual NUKLEI_UNIQUE_PTR<Descriptor> create() const = 0;
31 
32  virtual double distanceTo(const Descriptor &d) const
33  {
34  NUKLEI_TRACE_BEGIN();
35  NUKLEI_THROW("Not implemented.");
36  NUKLEI_TRACE_END();
37  }
38 
39  private:
40  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
41  template<class Archive>
42  void serialize(Archive &ar, const unsigned int version)
43  {
44  }
45 
46  };
47 
48  inline Descriptor* new_clone(const Descriptor& d)
49  {
50  return d.clone().release();
51  }
52 
54  {
55  public:
56  typedef NUKLEI_UNIQUE_PTR< VisualDescriptor > ptr;
57 
58  virtual void setColor(const Color& c) = 0;
59 
60  private:
61  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
62  template<class Archive>
63  void serialize(Archive &ar, const unsigned int version)
64  {
65  }
66  };
67 
69  {
70  public:
71 
72  typedef NUKLEI_UNIQUE_PTR< ColorDescriptor > ptr;
73 
74  ColorDescriptor() : color_(new RGBColor) {}
75 
76  virtual void assertConsistency() const
77  {
78  color_->assertConsistency();
79  };
80 
81  NUKLEI_UNIQUE_PTR<Descriptor> clone() const
82  {
83  NUKLEI_UNIQUE_PTR<ColorDescriptor> c(new ColorDescriptor);
84  c->color_ = color_->clone();
85  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(c));
86  }
87 
88  NUKLEI_UNIQUE_PTR<Descriptor> create() const
89  {
90  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(new ColorDescriptor));
91  }
92 
93  Color& getColor() { return *color_; }
94  const Color& getColor() const { return *color_; }
95  void setColor(const Color& c) { color_ = c.clone(); }
96 
97  double distanceTo(const Descriptor &d) const;
98  protected:
99 
100  NUKLEI_UNIQUE_PTR<Color> color_;
101 
102  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
103  template<class Archive>
104  void serialize(Archive &ar, const unsigned int version)
105  {
106  ar & NUKLEI_SERIALIZATION_MAKE_NVP
107  ( "base",
108  NUKLEI_SERIALIZATION_BASE(Descriptor) );
109  ar & NUKLEI_SERIALIZATION_NVP(color_);
110  }
111 
112  };
113 
114 
116  {
117  public:
118 
119  typedef NUKLEI_UNIQUE_PTR< ColorPairDescriptor > ptr;
120 
121  ColorPairDescriptor() : leftColor_(new RGBColor), rightColor_(new RGBColor) {}
122 
123  virtual void assertConsistency() const
124  {
125  leftColor_->assertConsistency();
126  rightColor_->assertConsistency();
127  };
128 
129  NUKLEI_UNIQUE_PTR<Descriptor> clone() const
130  {
131  NUKLEI_UNIQUE_PTR<ColorPairDescriptor> pair(new ColorPairDescriptor);
132  pair->leftColor_ = leftColor_->clone();
133  pair->rightColor_ = rightColor_->clone();
134  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(pair));
135  }
136 
137  NUKLEI_UNIQUE_PTR<Descriptor> create() const
138  {
139  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(new ColorPairDescriptor));
140  }
141 
142  Color& getLeftColor() { return *leftColor_; }
143  const Color& getLeftColor() const { return *leftColor_; }
144  void setLeftColor(const Color& c) { leftColor_ = c.clone(); }
145 
146  Color& getRightColor() { return *rightColor_; }
147  const Color& getRightColor() const { return *rightColor_; }
148  void setRightColor(const Color& c) { rightColor_ = c.clone(); }
149 
150  void setColor(const Color& c) { rightColor_ = c.clone(); leftColor_ = c.clone(); }
151 
152  double distanceTo(const Descriptor &d) const;
153  private:
154 
155  NUKLEI_UNIQUE_PTR<Color> leftColor_;
156  NUKLEI_UNIQUE_PTR<Color> rightColor_;
157 
158  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
159  template<class Archive>
160  void serialize(Archive &ar, const unsigned int version)
161  {
162  ar & NUKLEI_SERIALIZATION_MAKE_NVP
163  ( "base",
164  NUKLEI_SERIALIZATION_BASE(Descriptor) );
165  ar & NUKLEI_SERIALIZATION_NVP(leftColor_);
166  ar & NUKLEI_SERIALIZATION_NVP(rightColor_);
167  }
168 
169  };
170 
171 
173  {
174  public:
175  typedef NUKLEI_UNIQUE_PTR< GraspDescriptor > ptr;
176  typedef enum { PHYSICAL_SUCCESS = 0,
177  PHYSICAL_FAILURE,
178  PHYSICAL_UNSTABLE,
179  VIRTUAL_SUCCESS,
180  VIRTUAL_FAILURE,
181  PE_ERROR,
182  KINEMATIC_ERROR,
183  UNKNOWN
184  } TrialOutcome;
185  typedef TrialOutcome Type;
186  static const Type defaultType = UNKNOWN;
187  static const std::string TypeNames[];
188 
189  GraspDescriptor() : trialOutcome_(UNKNOWN) {}
190 
191  TrialOutcome getTrialOutcome() const { return trialOutcome_; }
192  void setTrialOutcome(const TrialOutcome trialOutcome) { trialOutcome_ = trialOutcome; }
193 
194  std::vector<weight_t> getQuality() const { return graspQuality_; }
195  void setQuality(const std::vector<weight_t> q) { graspQuality_ = q; }
196 
197  private:
198 
199  TrialOutcome trialOutcome_;
200  std::vector<weight_t> graspQuality_;
201  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
202  template<class Archive>
203  void serialize(Archive &ar, const unsigned int version)
204  {
205  if (version >= 1)
206  {
207  ar & NUKLEI_SERIALIZATION_MAKE_NVP
208  ( "base",
209  NUKLEI_SERIALIZATION_BASE(Descriptor) );
210  }
211  if (version == 1)
212  {
213  NUKLEI_THROW("Unsupported obsolete format.");
214  }
215  else if (version >= 2)
216  {
217  std::string trialOutcome = nameFromType<GraspDescriptor>(trialOutcome_);
218  ar & NUKLEI_SERIALIZATION_MAKE_NVP("trialOutcome",
219  trialOutcome);
220  trialOutcome_ = typeFromName<GraspDescriptor>(trialOutcome);
221  }
222  if (version >= 3)
223  {
224  ar & NUKLEI_SERIALIZATION_NVP(graspQuality_);
225  }
226  }
227  };
228 
230  {
231  public:
232 
233  typedef NUKLEI_UNIQUE_PTR< TwoFingerDescriptor > ptr;
234 
235  TwoFingerDescriptor() : gap_(0), closeToGrasp_(true), covisEuler_(Vector3::ZERO) {}
236 
237  virtual void assertConsistency() const
238  {
239  NUKLEI_TRACE_BEGIN();
240  NUKLEI_ASSERT(gap_ >= 0);
241  NUKLEI_TRACE_END();
242  };
243 
244  NUKLEI_UNIQUE_PTR<Descriptor> clone() const
245  {
246  NUKLEI_UNIQUE_PTR<TwoFingerDescriptor> g(new TwoFingerDescriptor);
247  g->gap_ = gap_;
248  g->closeToGrasp_ = closeToGrasp_;
249  g->setTrialOutcome(getTrialOutcome());
250  g->setQuality(getQuality());
251  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(g));
252  }
253 
254  NUKLEI_UNIQUE_PTR<Descriptor> create() const
255  {
256  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(new TwoFingerDescriptor));
257  }
258 
259  coord_t getGap() const { return gap_; }
260  void setGap(const coord_t gap) { gap_ = gap; }
261 
262  bool getCloseToGrasp() const { return closeToGrasp_; }
263  void setCloseToGrasp(const bool closeToGrasp) { closeToGrasp_ = closeToGrasp; }
264 
265  // This should move to a sub-class
266  Vector3 getCoViSEuler() const { return covisEuler_; }
267  void setCoViSEuler(const Vector3& covisEuler) { covisEuler_ = covisEuler; }
268  private:
269 
270  coord_t gap_;
271  bool closeToGrasp_;
272  Vector3 covisEuler_;
273 
274  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
275  template<class Archive>
276  void serialize(Archive &ar, const unsigned int version)
277  {
278  if (version == 0)
279  ar & NUKLEI_SERIALIZATION_MAKE_NVP
280  ( "base",
281  NUKLEI_SERIALIZATION_BASE(Descriptor) );
282  else
283  ar & NUKLEI_SERIALIZATION_MAKE_NVP
284  ( "base",
285  NUKLEI_SERIALIZATION_BASE(GraspDescriptor) );
286  ar & NUKLEI_SERIALIZATION_NVP(gap_);
287  ar & NUKLEI_SERIALIZATION_NVP(closeToGrasp_);
288  ar & NUKLEI_SERIALIZATION_NVP(covisEuler_);
289  }
290 
291  };
292 
293 
295  {
296  public:
297  typedef NUKLEI_UNIQUE_PTR< GeometricDescriptor > ptr;
298 
299  private:
300  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
301  template<class Archive>
302  void serialize(Archive &ar, const unsigned int version)
303  {
304  }
305  };
306 
308  {
309  public:
310  typedef NUKLEI_UNIQUE_PTR< PlaneDescriptor > ptr;
311 
312  PlaneDescriptor() {}
313 
314  virtual void assertConsistency() const
315  {
316  NUKLEI_TRACE_BEGIN();
317  NUKLEI_TRACE_END();
318  };
319 
320  NUKLEI_UNIQUE_PTR<Descriptor> clone() const
321  {
322  NUKLEI_UNIQUE_PTR<PlaneDescriptor> g(new PlaneDescriptor);
323  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(g));
324  }
325 
326  NUKLEI_UNIQUE_PTR<Descriptor> create() const
327  {
328  return NUKLEI_UNIQUE_PTR<Descriptor>(NUKLEI_MOVE(new PlaneDescriptor));
329  }
330  private:
331  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
332  template<class Archive>
333  void serialize(Archive &ar, const unsigned int version)
334  {
335  ar & NUKLEI_SERIALIZATION_MAKE_NVP
336  ( "base",
337  NUKLEI_SERIALIZATION_BASE(Descriptor) );
338  }
339 
340  };
341 
342 
343 }
344 
345 NUKLEI_SERIALIZATION_CLASS_VERSION(nuklei::GraspDescriptor, 3);
346 NUKLEI_SERIALIZATION_CLASS_VERSION(nuklei::TwoFingerDescriptor, 1);
347 
348 #if BOOST_VERSION < 104100
349 
350 #else
351 
352 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::ColorPairDescriptor, "mdfh_Descriptor_ColorPair")
353 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::ColorDescriptor, "mdfh_Descriptor_Color")
354 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::GraspDescriptor, "mdfh_Descriptor_Grasp")
355 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::TwoFingerDescriptor, "mdfh_Descriptor_TwoFinger")
356 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::PlaneDescriptor, "mdfh_Descriptor_Plane")
357 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::RGBColor, "mdfh_Color_RGB")
358 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::HSVColor, "mdfh_Color_HSV")
359 NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(nuklei::HSVConeColor, "mdfh_Color_HSVCone")
360 
361 #endif // BOOST_VERSION
362 
363 #endif
Definition: Color.h:56
Public namespace.
Definition: Color.cpp:9
Definition: Descriptor.h:53
Definition: Descriptor.h:294
Definition: Descriptor.h:115
Definition: Descriptor.h:68
Definition: Descriptor.h:307
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
Definition: Descriptor.h:172
Definition: Color.h:18
Definition: Descriptor.h:229
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
Definition: Descriptor.h:19
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.