SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector2d.hpp
Go to the documentation of this file.
1 /*
2  * Copyright(c):
3  * Signal Image and Communications (SIC) Department
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * - University of Poitiers, France http://www.univ-poitiers.fr
6  * - XLIM Institute UMR CNRS 7252 http://www.xlim.fr/
7  *
8  * and
9  *
10  * D2 Fluid, Thermic and Combustion
11  * - University of Poitiers, France http://www.univ-poitiers.fr
12  * - PPRIME Institute - UPR CNRS 3346 http://www.pprime.fr
13  * - ISAE-ENSMA http://www.ensma.fr
14  *
15  * Contributor(s):
16  * The SLIP team,
17  * Benoit Tremblais <tremblais_AT_sic.univ-poitiers.fr>,
18  * Laurent David <laurent.david_AT_lea.univ-poitiers.fr>,
19  * Ludovic Chatellier <ludovic.chatellier_AT_univ-poitiers.fr>,
20  * Lionel Thomas <lionel.thomas_AT_univ-poitiers.fr>,
21  * Denis Arrivault <arrivault_AT_sic.univ-poitiers.fr>,
22  * Julien Dombre <julien.dombre_AT_univ-poitiers.fr>.
23  *
24  * Description:
25  * The Simple Library of Image Processing (SLIP) is a new image processing
26  * library. It is written in the C++ language following as much as possible
27  * the ISO/ANSI C++ standard. It is consequently compatible with any system
28  * satisfying the ANSI C++ complience. It works on different Unix , Linux ,
29  * Mircrosoft Windows and Mac OS X plateforms. SLIP is a research library that
30  * was created by the Signal, Image and Communications (SIC) departement of
31  * the XLIM, UMR 7252 CNRS Institute in collaboration with the Fluids, Thermic
32  * and Combustion departement of the P', UPR 3346 CNRS Institute of the
33  * University of Poitiers.
34  *
35  * The SLIP Library source code has been registered to the APP (French Agency
36  * for the Protection of Programs) by the University of Poitiers and CNRS,
37  * under registration number IDDN.FR.001.300034.000.S.P.2010.000.21000.
38 
39  * http://www.sic.sp2mi.univ-poitiers.fr/slip/
40  *
41  * This software is governed by the CeCILL-C license under French law and
42  * abiding by the rules of distribution of free software. You can use,
43  * modify and/ or redistribute the software under the terms of the CeCILL-C
44  * license as circulated by CEA, CNRS and INRIA at the following URL
45  * http://www.cecill.info.
46  * As a counterpart to the access to the source code and rights to copy,
47  * modify and redistribute granted by the license, users are provided only
48  * with a limited warranty and the software's author, the holder of the
49  * economic rights, and the successive licensors have only limited
50  * liability.
51  *
52  * In this respect, the user's attention is drawn to the risks associated
53  * with loading, using, modifying and/or developing or reproducing the
54  * software by the user in light of its specific status of free software,
55  * that may mean that it is complicated to manipulate, and that also
56  * therefore means that it is reserved for developers and experienced
57  * professionals having in-depth computer knowledge. Users are therefore
58  * encouraged to load and test the software's suitability as regards their
59  * requirements in conditions enabling the security of their systems and/or
60  * data to be ensured and, more generally, to use and operate it in the
61  * same conditions as regards security.
62  *
63  * The fact that you are presently reading this means that you have had
64  * knowledge of the CeCILL-C license and that you accept its terms.
65  */
66 
67 
68 
76 #ifndef SLIP_VECTOR2D_HPP
77 #define SLIP_VECTOR2D_HPP
78 
79 #include <iostream>
80 #include <iterator>
81 #include <cassert>
82 #include <cmath>
83 #include <string>
84 #include <cstddef>
85 #include "KVector.hpp"
86 #include "norms.hpp"
87 #include "macros.hpp"
88 #include "compare.hpp"
89 
90 #include <boost/serialization/access.hpp>
91 #include <boost/serialization/split_member.hpp>
92 #include <boost/serialization/version.hpp>
93 #include <boost/serialization/base_object.hpp>
94 
95 namespace slip
96 {
97 
98 template <typename T>
99 struct Vector2d;
100 
101 template <typename T>
102 bool operator<(const Vector2d<T>& x, const Vector2d<T>& y);
103 
104 template <typename T>
105 bool operator>(const Vector2d<T>& x, const Vector2d<T>& y);
106 
107 template <typename T>
108 bool operator<=(const Vector2d<T>& x, const Vector2d<T>& y);
109 
110 template <typename T>
111 bool operator>=(const Vector2d<T>& x, const Vector2d<T>& y);
112 
124 template <typename T>
125 struct Vector2d : public kvector<T,2>
126 {
127 
128  typedef Vector2d<T> self;
129  typedef T value_type;
130  typedef value_type* pointer;
131  typedef const value_type* const_pointer;
133  typedef const value_type& const_reference;
134 
135  typedef ptrdiff_t difference_type;
136  typedef std::size_t size_type;
137 
138  typedef pointer iterator;
140 
141 
146 
151  Vector2d();
152 
158  Vector2d(const T& val);
159 
166  Vector2d(const T& x1,
167  const T& x2);
168 
174  Vector2d(const T* val);
175 
176 
182  Vector2d(const Vector2d<T>& rhs);
183 
184 
185 
189 // Vector2d<T>& operator=(const Vector2d<T>& rhs)
190 // {
191 // (*this)[0] = rhs[0];
192 // (*this)[1] = rhs[1];
193 // return *this;
194 // }
195 
196 
204  self operator-() const;
205 
217  T angle() const;
218 
224  T angle_degree() const;
225 
232 
233 
241  friend bool operator< <>(const Vector2d<T>& x, const Vector2d<T>& y);
242 
250  friend bool operator> <>(const Vector2d<T>& x, const Vector2d<T>& y);
251 
259  friend bool operator<= <>(const Vector2d<T>& x, const Vector2d<T>& y);
260 
268  friend bool operator> <>(const Vector2d<T>& x, const Vector2d<T>& y);
269 
270 
281  const T& get_x1() const;
286  const T& get_x() const;
291  const T& u() const;
292 
297  const T& get_x2() const;
302  const T& get_y() const;
307  const T& v() const;
308 
309 
310 
311 
316  void set_x1(const T& x1);
321  void set_x(const T& r);
326  void u(const T& u_);
327 
328 
333  void set_x2(const T& x2);
338  void set_y(const T& g);
343  void v(const T& v_);
344 
345 
351  void set(const T& x1,
352  const T& x2);
353 
354 
362  std::string name() const;
363 
364 
365  private:
367  template<class Archive>
368  void save(Archive & ar, const unsigned int version) const
369  {
370  ar & boost::serialization::base_object<slip::kvector<T,2> >(*this);
371  }
372  template<class Archive>
373  void load(Archive & ar, const unsigned int version)
374  {
375  ar & boost::serialization::base_object<slip::kvector<T,2> >(*this);
376  }
377  BOOST_SERIALIZATION_SPLIT_MEMBER()
378 };
379 
400 }//slip::
401 
402 namespace slip
403 {
404 
405  template<typename T>
406  inline
408  {
409  this->fill(T(0));
410  }
411 
412  template<typename T>
413  inline
414  Vector2d<T>::Vector2d(const T& val)
415  {
416  this->fill(val);
417  }
418 
419  template<typename T>
420  inline
422  const T& x2)
423  {
424  (*this)[0] = x1;
425  (*this)[1] = x2;
426  }
427 
428  template<typename T>
429  inline
430  Vector2d<T>::Vector2d(const T* val)
431  {
432  assert(val != 0);
433  this->fill(val);
434  }
435 
436 
437  template<typename T>
438  inline
440  {
441  *this = rhs;
442  }
443 
444  template<typename T>
445  inline
447  {
448  self tmp;
449  tmp[0] = -(*this)[0];
450  tmp[1] = -(*this)[1];
451  return tmp;
452  }
453 
454  template<typename T>
455  inline
457  {
458  return std::atan2((*this)[1],(*this)[0]);
459  }
460 
461  template<typename T>
462  inline
464  {
465  return (static_cast<T>(180.0)/slip::constants<T>::pi()) * this->angle();
466  }
467 
469  /* @{ */
470  template<typename T>
471  inline
472  bool operator<(const Vector2d<T>& x,
473  const Vector2d<T>& y)
474  {
475  return (slip::Euclidean_norm<T>(x.begin(),x.end())
476  <
477  slip::Euclidean_norm<T>(y.begin(),y.end())
478  );
479  }
480 
481 
482 
483  template<typename T>
484  inline
485  bool operator>(const Vector2d<T>& x,
486  const Vector2d<T>& y)
487  {
488  return y < x;
489  }
490 
491  template<typename T>
492  inline
493  bool operator<=(const Vector2d<T>& x,
494  const Vector2d<T>& y)
495  {
496  return !(y < x);
497  }
498 
499  template<typename T>
500  inline
501  bool operator>=(const Vector2d<T>& x,
502  const Vector2d<T>& y)
503  {
504  return !(x < y);
505  }
506  /* @} */
507 
508  template<typename T>
509  inline
510  const T& Vector2d<T>::get_x1() const {return (*this)[0];}
511 
512  template<typename T>
513  inline
514  const T& Vector2d<T>::get_x() const {return (*this)[0];}
515 
516  template<typename T>
517  inline
518  const T& Vector2d<T>::u() const {return (*this)[0];}
519 
520 
521  template<typename T>
522  inline
523  const T& Vector2d<T>::get_x2() const {return (*this)[1];}
524 
525  template<typename T>
526  inline
527  const T& Vector2d<T>::get_y() const {return (*this)[1];}
528 
529  template<typename T>
530  inline
531  const T& Vector2d<T>::v() const {return (*this)[1];}
532 
533 
534  template<typename T>
535  inline
536  void Vector2d<T>::set_x1(const T& x1){(*this)[0] = x1;}
537 
538  template<typename T>
539  inline
540  void Vector2d<T>::set_x(const T& x){(*this)[0] = x;}
541 
542  template<typename T>
543  inline
544  void Vector2d<T>::u(const T& x){(*this)[0] = x;}
545 
546 
547  template<typename T>
548  inline
549  void Vector2d<T>::set_x2(const T& x2){(*this)[1] = x2;}
550 
551  template<typename T>
552  inline
553  void Vector2d<T>::set_y(const T& y){(*this)[1] = y;}
554 
555  template<typename T>
556  inline
557  void Vector2d<T>::v(const T& y){(*this)[1] = y;}
558 
559  template<typename T>
560  inline
561  void Vector2d<T>::set(const T& x1,
562  const T& x2)
563  {
564  (*this)[0] = x1;
565  (*this)[1] = x2;
566  }
567 }//slip::
568 
569 
570 namespace slip
571 {
572 template<typename T>
573 inline
575  const Vector2d<T>& V2)
576 {
577  Vector2d<T> tmp;
578  tmp[0] = V1[0] + V2[0];
579  tmp[1] = V1[1] + V2[1];
580  return tmp;
581 }
582 
583 template<typename T>
584 inline
586  const T& val)
587 {
588  Vector2d<T> tmp = V1;
589  tmp+=val;
590  return tmp;
591 }
592 
593 template<typename T>
594 inline
595 Vector2d<T> operator+(const T& val,
596  const Vector2d<T>& V1)
597 {
598  return V1 + val;
599 }
600 
601 template<typename T>
602 inline
604  const Vector2d<T>& V2)
605 {
606  Vector2d<T> tmp;
607  tmp[0] = V1[0] - V2[0];
608  tmp[1] = V1[1] - V2[1];
609  return tmp;
610 }
611 
612 template<typename T>
613 inline
615  const T& val)
616 {
617  Vector2d<T> tmp = V1;
618  tmp-=val;
619  return tmp;
620 }
621 
622 template<typename T>
623 inline
624 Vector2d<T> operator-(const T& val,
625  const Vector2d<T>& V1)
626 {
627  Vector2d<T> tmp;
628  tmp[0] = val - V1[0];
629  tmp[1] = val - V1[1];
630  return tmp;
631 }
632 
633 
634 template<typename T>
635 inline
637  const Vector2d<T>& V2)
638 {
639  Vector2d<T> tmp;
640  tmp[0] = V1[0] * V2[0];
641  tmp[1] = V1[1] * V2[1];
642  return tmp;
643 }
644 
645 template<typename T>
646 inline
648  const T& val)
649 {
650  Vector2d<T> tmp = V1;
651  tmp*=val;
652  return tmp;
653 }
654 
655 template<typename T>
656 inline
657 Vector2d<T> operator*(const T& val,
658  const Vector2d<T>& V1)
659 {
660  return V1 * val;
661 }
662 
663 template<typename T>
664 inline
666  const Vector2d<T>& V2)
667 {
668  Vector2d<T> tmp;
669  tmp[0] = V1[0] / V2[0];
670  tmp[1] = V1[1] / V2[1];
671  return tmp;
672 }
673 
674 template<typename T>
675 inline
677  const T& val)
678 {
679  Vector2d<T> tmp = V1;
680  tmp/=val;
681  return tmp;
682 }
683 
684 
685 
686 
687 }//slip::
688 
689 namespace slip
690 {
706  template <class _Tp>
707  struct AE_rad : public std::binary_function<_Tp, slip::Vector2d<_Tp>,slip::Vector2d<_Tp> >
708  {
709  _Tp
710  operator()(const slip::Vector2d<_Tp>& __x, const slip::Vector2d<_Tp>& __y) const
711  {
712  _Tp x_norm = std::sqrt(_Tp(1) + __x[0]*__x[0] + __x[1]*__x[1]);
713  _Tp y_norm = std::sqrt(_Tp(1) + __y[0]*__y[0] + __y[1]*__y[1]);
714 
715  return std::acos((_Tp(1) +__x[0]*__y[0] + __x[1]*__y[1]) / (x_norm * y_norm));
716  }
717  };
718 
734  template <class _Tp>
735  struct AE_deg : public std::binary_function<_Tp, slip::Vector2d<_Tp>, slip::Vector2d<_Tp> >
736  {
737  _Tp
738  operator()(const slip::Vector2d<_Tp>& __x, const slip::Vector2d<_Tp>& __y) const
739  {
740  _Tp x_norm = std::sqrt(_Tp(1) + __x[0]*__x[0] + __x[1]*__x[1]);
741  _Tp y_norm = std::sqrt(_Tp(1) + __y[0]*__y[0] + __y[1]*__y[1]);
742 
743  return (_Tp(180)/ slip::constants<_Tp>::pi())* std::acos((_Tp(1) +__x[0]*__y[0] + __x[1]*__y[1]) / (x_norm * y_norm));
744  }
745  };
746 
747  template<typename _Tp>
748  inline
749  std::string
750  Vector2d<_Tp>::name() const {return "Vector2d";}
751 
752 
753 
754 }//slip::
755 
756 #endif //SLIP_VECTOR2D_HPP
const T & v() const
Accessor to the second dimension value of the Vector2d.
Definition: Vector2d.hpp:531
void set_x(const T &r)
Mutator of the first dimension value of the Vector2d.
Definition: Vector2d.hpp:540
void set_x2(const T &x2)
Mutator of the second dimension value of the Vector2d.
Definition: Vector2d.hpp:549
This is a linear (one-dimensional) static vector. This container statisfies the RandomAccessContainer...
Definition: KVector.hpp:100
std::size_t size_type
Definition: Vector2d.hpp:136
T angle_degree() const
Computes the angle with the cartesian axis. The result is between the range [-360,360].
Definition: Vector2d.hpp:463
value_type & reference
Definition: Vector2d.hpp:132
const T & get_x() const
Accessor to the first dimension value of the Vector2d.
Definition: Vector2d.hpp:514
slip::Vector2d< short > Vector2d_s
short alias
Definition: Vector2d.hpp:389
A structure for numeric constants.
Definition: macros.hpp:399
Color< T > operator+(const Color< T > &V1, const Color< T > &V2)
Definition: Color.hpp:602
void set(const T &x1, const T &x2)
Mutator of the three dimension values of the Vector2d.
Definition: Vector2d.hpp:561
self operator-() const
Computes the oposite of a Vector2d.
Definition: Vector2d.hpp:446
value_type * pointer
Definition: Vector2d.hpp:130
const T & get_x1() const
Accessor to the first dimension value of the Vector2d.
Definition: Vector2d.hpp:510
Provides some algorithms to compare ranges.
bool operator>(const Array< T > &x, const Array< T > &y)
Definition: Array.hpp:1469
void set_y(const T &g)
Mutator of the second dimension value of the Vector2d.
Definition: Vector2d.hpp:553
slip::Vector2d< int > Vector2d_i
int alias
Definition: Vector2d.hpp:393
_Tp operator()(const slip::Vector2d< _Tp > &__x, const slip::Vector2d< _Tp > &__y) const
Definition: Vector2d.hpp:710
Provides a class to manipulate static and generic vectors.
T angle() const
Computes the angle with the cartesian axis. The result is between the range [-PI,PI].
Definition: Vector2d.hpp:456
slip::Vector2d< unsigned int > Vector2d_ui
unsigned int alias
Definition: Vector2d.hpp:395
Vector2d()
Default constructor init all the components to 0.
Definition: Vector2d.hpp:407
slip::Vector2d< double > Vector2d_d
double alias
Definition: Vector2d.hpp:381
friend class boost::serialization::access
Definition: Vector2d.hpp:366
pointer iterator
Definition: Vector2d.hpp:138
const_pointer const_iterator
Definition: Vector2d.hpp:139
Provides some mathematical functors and constants.
HyperVolume< T > sqrt(const HyperVolume< T > &M)
ptrdiff_t difference_type
Definition: Vector2d.hpp:135
slip::Vector2d< float > Vector2d_f
float alias
Definition: Vector2d.hpp:383
const T & u() const
Accessor to the first dimension value of the Vector2d.
Definition: Vector2d.hpp:518
std::string name() const
Returns the name of the class.
Definition: Vector2d.hpp:750
_Tp operator()(const slip::Vector2d< _Tp > &__x, const slip::Vector2d< _Tp > &__y) const
Definition: Vector2d.hpp:738
This is a Vector2d struct. It is a specialization of kvector. It implements some peculiar 2d operatio...
Definition: Vector2d.hpp:99
slip::Vector2d< char > Vector2d_c
char alias
Definition: Vector2d.hpp:397
HyperVolume< T > acos(const HyperVolume< T > &M)
const T & get_x2() const
Accessor to the second dimension value of the Vector2d.
Definition: Vector2d.hpp:523
slip::Vector2d< unsigned char > Vector2d_uc
unsigned char alias
Definition: Vector2d.hpp:399
Computes the angular error usually used in optical flow estimation evaluations: where : ...
Definition: Vector2d.hpp:707
slip::Vector2d< long > Vector2d_l
long alias
Definition: Vector2d.hpp:385
const value_type & const_reference
Definition: Vector2d.hpp:133
const T & get_y() const
Accessor to the second dimension value of the Vector2d.
Definition: Vector2d.hpp:527
Color< T > operator*(const Color< T > &V1, const Color< T > &V2)
Definition: Color.hpp:658
slip::Vector2d< unsigned long > Vector2d_ul
unsigned long alias
Definition: Vector2d.hpp:387
Computes the angular error usually used in optical flow estimation evaluations: where: ...
Definition: Vector2d.hpp:735
Color< T > operator/(const Color< T > &V1, const Color< T > &V2)
Definition: Color.hpp:686
bool operator>=(const Array< T > &x, const Array< T > &y)
Definition: Array.hpp:1485
Color< T > operator-(const Color< T > &V1, const Color< T > &V2)
Definition: Color.hpp:630
void set_x1(const T &x1)
Mutator of the first dimension value of the Vector2d.
Definition: Vector2d.hpp:536
Provides some algorithms to compute norms.
const value_type * const_pointer
Definition: Vector2d.hpp:131
slip::Vector2d< unsigned short > Vector2d_us
unsigned long alias
Definition: Vector2d.hpp:391