libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNPoint2D.h
Go to the documentation of this file.
1 /* Copyright 2008-2016 INSA Lyon, CoReNum, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNPoint2D.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNPOINT2D_HEADER
23 #define CRNPOINT2D_HEADER
24 
25 #include <CRNObject.h>
26 #include <CRNString.h>
27 #include <CRNMath/CRNMath.h>
28 #include <type_traits>
29 
33 namespace crn
34 {
35  /****************************************************************************/
45  template<typename T> class Point2D: public Object
46  {
47  public:
49  using value_type = T;
50 
52  Point2D() noexcept(noexcept(T{0})) : X(0),Y(0) {}
54  Point2D(T x, T y) noexcept(std::is_nothrow_copy_constructible<T>::value) : X(x),Y(y) {}
55  Point2D(const Point2D &) = default;
56  Point2D(Point2D &&) = default;
58  virtual ~Point2D() override = default;
59 
61  String ToString() const { return String(U'(') + X + String(U" × ") + Y + String(U')'); }
62 
66  template<typename U> Point2D<U> Convert() const noexcept(noexcept(U{X})) { return Point2D<U>(U(X), U(Y)); }
67 
68  Point2D& operator=(const Point2D &) = default;
69  Point2D& operator=(Point2D &&) = default;
70 
71  bool operator==(const Point2D &p) const noexcept { return (X == p.X) && (Y == p.Y); }
73  Point2D<T>& operator+=(const Point2D<T> &p) noexcept { X += p.X; Y += p.Y; return *this; }
75  Point2D<T>& operator-=(const Point2D<T> &p) noexcept { X -= p.X; Y -= p.Y; return *this; }
76 
78  struct Comparer
79  {
80  public:
84  Comparer(Direction dir): direction(dir) { }
86  bool operator()(const Point2D &p1, const Point2D &p2) const
87  {
88  if (direction == Direction::LEFT)
89  return p1.X < p2.X;
90  else if (direction == Direction::RIGHT)
91  return p1.X > p2.X;
92  else if (direction == Direction::TOP)
93  return p1.Y < p2.Y;
94  else
95  return p1.Y > p2.Y;
96  }
97  private:
98  Direction direction;
99  };
100  };
101 
102  template<typename T> double Distance(const Point2D<T> &p1, const Point2D<T> &p2, DistanceType dt = DistanceType::EUCLIDEAN)
103  {
104  switch (dt)
105  {
106  case DistanceType::D4:
107  return Abs(p1.X - p2.X) + Abs(p1.Y - p2.Y);
108  case DistanceType::D8:
109  return Max(Abs(p1.X - p2.X), Abs(p1.Y - p2.Y));
111  return sqrt(Sqr(p1.X - p2.X) + Sqr(p1.Y - p2.Y));
112  default:
113  throw ExceptionInvalidArgument("double Distance(const Point2D<T>&, const Point2D<T>&, DistanceType): invalid distance type.");
114  }
115  }
116 }
117 
118 template<typename T> crn::Point2D<T> operator+(crn::Point2D<T> p1, const crn::Point2D<T> &p2) noexcept { return p1 += p2; }
119 template<typename T> crn::Point2D<T> operator-(crn::Point2D<T> p1, const crn::Point2D<T> &p2) noexcept { return p1 -= p2; }
120 template<typename T> crn::Point2D<double> operator*(double d, const crn::Point2D<T> &p) noexcept { return {p.X * d, p.Y * d}; }
121 template<typename T> crn::Point2D<double> operator*(const crn::Point2D<T> &p, double d) noexcept { return {p.X * d, p.Y * d}; }
122 
123 #endif
Point2D(T x, T y) noexcept(std::is_nothrow_copy_constructible< T >::value)
Constructor from values.
Definition: CRNPoint2D.h:54
bool operator==(const Point2D &p) const noexcept
Definition: CRNPoint2D.h:71
Point2D< U > Convert() const noexcept(noexcept(U{X}))
Converts to another type of Point.
Definition: CRNPoint2D.h:66
Functor to compare points.
Definition: CRNPoint2D.h:78
virtual ~Point2D() override=default
Destructor.
const T & Max(const T &a, const T &b)
Returns the max of two values.
Definition: CRNMath.h:47
Direction
An enumeration of directions.
Definition: CRNMath.h:122
A UTF32 character string class.
Definition: CRNString.h:61
crn::Point2D< T > operator-(crn::Point2D< T > p1, const crn::Point2D< T > &p2) noexcept
Definition: CRNPoint2D.h:119
int value_type
The value type.
Definition: CRNPoint2D.h:49
A 2D point class.
Definition: CRNPoint2D.h:45
Comparer(Direction dir)
Constructor.
Definition: CRNPoint2D.h:84
Point2D & operator=(const Point2D &)=default
value_type X
Definition: CRNPoint2D.h:63
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
Definition: CRNMath.h:61
Point2D< T > & operator-=(const Point2D< T > &p) noexcept
Translation.
Definition: CRNPoint2D.h:75
double Distance(const Int &i1, const Int &i2) noexcept
Definition: CRNInt.h:78
void Abs(Image< T > &img, typename std::enable_if< std::is_arithmetic< T >::value >::type *dummy=nullptr) noexcept
Replaces each pixel by its absolute value.
Definition: CRNImageGray.h:47
Point2D< T > & operator+=(const Point2D< T > &p) noexcept
Translation.
Definition: CRNPoint2D.h:73
DistanceType
An enumeration of distances.
Definition: CRNMath.h:159
value_type Y
Definition: CRNPoint2D.h:63
String ToString() const
Converts object to string.
Definition: CRNPoint2D.h:61
crn::Point2D< double > operator*(double d, const crn::Point2D< T > &p) noexcept
Definition: CRNPoint2D.h:120
Point2D() noexcept(noexcept(T{0}))
Default constructor (sets coordinates to 0)
Definition: CRNPoint2D.h:52
bool operator()(const Point2D &p1, const Point2D &p2) const
functor
Definition: CRNPoint2D.h:86
crn::Point2D< T > operator+(crn::Point2D< T > p1, const crn::Point2D< T > &p2) noexcept
Definition: CRNPoint2D.h:118
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107