MIRA
Point.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_POINT_H_
48 #define _MIRA_POINT_H_
49 
50 #if CV_MAJOR_VERSION >= 3
51 # include <opencv2/core/types.hpp>
52 #else
53 # include <opencv2/core/core.hpp>
54 #endif
55 
56 #ifndef Q_MOC_RUN
57 #include <boost/geometry/core/cs.hpp>
58 #include <boost/geometry/geometries/point.hpp>
59 #endif
60 
61 #include <utils/PParam.h>
62 #include <math/Eigen.h>
63 
64 #include <utils/IsCheapToCopy.h>
65 
66 namespace mira {
67 
69 
81 template <typename T, int D, typename Derived>
82 class PointBase : public Eigen::Matrix<T,D,1>
83 {
84  typedef Eigen::Matrix<T,D,1> Base;
85 
86 public:
87  typedef T Type;
88  enum { Dim = D};
89 
90 public:
92  PointBase() : Base(Eigen::Matrix<T,D,1>::Zero()) {}
93 
95  PointBase(const boost::geometry::model::point<T,D,boost::geometry::cs::cartesian>& other) {
96  operator=(other);
97  }
98 
100  template <typename DerivedMatrix>
102 
104  template <typename DerivedMatrix>
106  Base::operator=(other);
107  return *this;
108  }
109 
111  Derived& operator=(const boost::geometry::model::point<T,D,boost::geometry::cs::cartesian>& other)
112  {
113  for(int i=0; i < D; i++) // compiler will unroll loop
114  (*this)[i] = other(i);
115  return *this;
116  }
117 
119  operator boost::geometry::model::point<T,D,boost::geometry::cs::cartesian>() const
120  {
121  boost::geometry::model::point<T,D,boost::geometry::cs::cartesian> r;
122  for(int i=0; i < D; i++) // compiler will unroll loop
123  r(i) = (*this)[i];
124  return r;
125  }
126 };
127 
129 
139 template <typename T, int D>
140 class Point : public PointBase<T,D, Point<T,D> >
141 {
143 
144 public:
146  Point() {}
147 
149  template <typename DerivedMatrix>
150  Point(const Eigen::MatrixBase<DerivedMatrix>& other) : Base(other) {}
151 
153  Point(const boost::geometry::model::point<T,D,boost::geometry::cs::cartesian>& other) {
154  operator=(other);
155  }
156 };
157 
159 
161 template <typename T, int D, typename SerializerTag>
162 class IsTransparentSerializable<Point<T,D>,SerializerTag> : public std::true_type {};
164 
166 
173 template <typename T>
174 class Point<T,2> : public PointBase<T,2, Point<T,2> >
175 {
177 
178 public:
180  Point() {}
181 
183  Point(T x, T y)
184  {
185  (*this)[0] = x;
186  (*this)[1] = y;
187  }
188 
190  template <typename DerivedMatrix>
191  Point(const Eigen::MatrixBase<DerivedMatrix>& other) : Base(other) {}
192 
194  Point(const boost::geometry::model::point<T,2,boost::geometry::cs::cartesian>& other) {
195  operator=(other);
196  }
197 
199  explicit Point<T,2>(const cv::Point_<T> & other) {
200  operator=(other);
201  }
202 
204  Point& operator=(const cv::Point_<T>& other) {
205  (*this)[0] = other.x;
206  (*this)[1] = other.y;
207  return *this;
208  }
209 
211  operator cv::Point_<T>() const
212  {
213  return cv::Point_<T>( this->x(), this->y() );
214  }
215 
217  template<typename Reflector>
218  void reflect(Reflector& reflector)
219  {
220  reflector.property("X", this->x(), "The x-coordinate");
221  reflector.property("Y", this->y(), "The y-coordinate");
222  }
223 };
224 
226 
228 template <typename T, typename SerializerTag>
229 class IsTransparentSerializable<Point<T,2>,SerializerTag> : public std::false_type {};
231 
233 
237 
239 
240 template <>
241 class IsCheapToCopy<Point2i> : public std::true_type {};
242 
243 template <>
244 class IsCheapToCopy<Point2f> : public std::true_type {};
245 
246 template <>
247 class IsCheapToCopy<Point2d> : public std::true_type {};
248 
250 
256 template <typename T>
257 class Point<T,3> : public PointBase<T,3, Point<T,3> >
258 {
260 
261 public:
263  Point(){}
264 
266  Point(T x, T y, T z)
267  {
268  (*this)[0] = x;
269  (*this)[1] = y;
270  (*this)[2] = z;
271  }
272 
274  template <typename DerivedMatrix>
275  Point(const Eigen::MatrixBase<DerivedMatrix>& other) : Base(other) {}
276 
278  Point(const boost::geometry::model::point<T,3,boost::geometry::cs::cartesian>& other) {
279  operator=(other);
280  }
281 
283  explicit Point(const cv::Point3_<T> & other) {
284  operator=(other);
285  }
286 
288  Point& operator=(const cv::Point3_<T>& other) {
289  (*this)[0] = other.x;
290  (*this)[1] = other.y;
291  (*this)[2] = other.z;
292  return *this;
293  }
294 
296  operator cv::Point3_<T>() const
297  {
298  return cv::Point3_<T>(this->x(),this->y(),this->z());
299  }
300 
302  template<typename Reflector>
303  void reflect(Reflector& reflector)
304  {
305  reflector.property("X", this->x(), "The x-coordinate");
306  reflector.property("Y", this->y(), "The y-coordinate");
307  reflector.property("Z", this->z(), "The z-coordinate");
308  }
309 
310 };
311 
313 
315 template <typename T, typename SerializerTag>
316 class IsTransparentSerializable<Point<T,3>,SerializerTag> : public std::false_type {};
318 
320 
324 
326 
327 template <>
328 class IsCheapToCopy<Point3i> : public std::true_type {};
329 
330 template <>
331 class IsCheapToCopy<Point3f> : public std::true_type {};
332 
333 template <>
334 class IsCheapToCopy<Point3d> : public std::true_type {};
335 
337 
338 } // namespace MIRA
339 
341 // BOOST specialization on mira::point to accept them as geometry entity
342 namespace boost { namespace geometry { namespace traits {
343 
345 
346 template<typename T, int D>
347 struct tag<mira::Point<T, D> >
348 { typedef point_tag type; };
349 
350 template<typename T, int D>
351 struct dimension<mira::Point<T, D> > : boost::mpl::int_<D>
352 {};
353 
354 template<typename T, int D>
355 struct coordinate_type<mira::Point<T, D> >
356 { typedef T type; };
357 
358 template<typename T, int D>
359 struct coordinate_system<mira::Point<T, D> >
360 { typedef cs::cartesian type; };
361 
362 template<typename T, int D, std::size_t Dimension>
363 struct access<mira::Point<T, D>, Dimension>
364 {
365  static inline T get(mira::Point<T, D> const& p)
366  {
367  return p(Dimension, 0);
368  }
369  static inline void set(mira::Point<T, D>& p, T const& value)
370  {
371  p(Dimension, 0) = value;
372  }
373 };
374 
376 
377 }}} // namespace boost { namespace geometry { namespace traits {
378 
380 
382 
383 #endif
Point & operator=(const cv::Point3_< T > &other)
converts from OpenCV point
Definition: Point.h:288
void reflect(Reflector &reflector)
the reflect method for 2D point serialization
Definition: Point.h:218
T Type
Definition: Point.h:87
Point(const Eigen::MatrixBase< DerivedMatrix > &other)
Creates Point from Eigen matrix.
Definition: Point.h:275
Definition: SyncTimedRead.h:62
Include file for all eigen related things.
General point class template.
Definition: Point.h:140
Preprocessor workaround to handle single parameters that contain a comma.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Point()
Default constructor.
Definition: Point.h:146
Point< int, 2 > Point2i
a 2D integer point
Definition: Point.h:234
Definition: YawPitchRoll.h:684
void reflect(Reflector &reflector)
the reflect method for 3D point serialization
Definition: Point.h:303
Point< double, 2 > Point2d
a 2D 64 bit floating precision point
Definition: Point.h:236
Point(const Eigen::MatrixBase< DerivedMatrix > &other)
Creates Point from Eigen matrix.
Definition: Point.h:191
Specialization of Point for 2 dimensions with specialized constructors and converters.
Definition: Point.h:174
Point(const cv::Point3_< T > &other)
Creates Point from OpenCV point.
Definition: Point.h:283
Point & operator=(const cv::Point_< T > &other)
converts from OpenCV point
Definition: Point.h:204
PointBase(const boost::geometry::model::point< T, D, boost::geometry::cs::cartesian > &other)
Create from boost geometry point.
Definition: Point.h:95
Point< int, 3 > Point3i
a 3D integer point
Definition: Point.h:321
Derived & operator=(const boost::geometry::model::point< T, D, boost::geometry::cs::cartesian > &other)
converts from native boost::geometry::model::point
Definition: Point.h:111
Point(const boost::geometry::model::point< T, D, boost::geometry::cs::cartesian > &other)
Creates Point from boost::geometry::point.
Definition: Point.h:153
By default, IsCheapToCopy<T>::value evaluates to true for fundamental types T, false for all other ty...
Definition: IsCheapToCopy.h:63
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Type trait to define if a class is cheap to copy.
Point()
Default-constructor.
Definition: Point.h:180
Matrix & operator=(const RotationBase< OtherDerived, ColsAtCompileTime > &r)
Point(T x, T y, T z)
Creates point from its three coordinates.
Definition: Point.h:266
PointBase(const Eigen::MatrixBase< DerivedMatrix > &other)
Create from Eigen matrix expression.
Definition: Point.h:101
Definition: Point.h:88
The base template class of point, which covers the basic functionality of each point.
Definition: Point.h:82
Point(T x, T y)
Creates point from its two coordinates.
Definition: Point.h:183
Point< float, 2 > Point2f
a 2D 32 bit floating precision point
Definition: Point.h:235
Point< float, 3 > Point3f
a 3D 32 bit floating precision point
Definition: Point.h:322
Point()
Default-constructor.
Definition: Point.h:263
Derived & operator=(const Eigen::MatrixBase< DerivedMatrix > &other)
assignment of Eigen matrix expression
Definition: Point.h:105
Point(const boost::geometry::model::point< T, 3, boost::geometry::cs::cartesian > &other)
Creates Point from boost::geometry::point.
Definition: Point.h:278
Point(const Eigen::MatrixBase< DerivedMatrix > &other)
Creates Point from Eigen matrix.
Definition: Point.h:150
Point< double, 3 > Point3d
a 3D 64 bit floating precision point
Definition: Point.h:323
PointBase()
Default constructor.
Definition: Point.h:92
Point(const boost::geometry::model::point< T, 2, boost::geometry::cs::cartesian > &other)
Creates Point from boost::geometry::point.
Definition: Point.h:194