Rect.h
Go to the documentation of this file.
1 /********************************************************************
2  *
3  * Project Artec 3D Scanning SDK
4  *
5  * Purpose: Definition of template rectangle class
6  *
7  * Copyright: Artec Group
8  *
9  ********************************************************************/
10 
11 #ifndef _ARTEC_RECT_H_
12 #define _ARTEC_RECT_H_
13 
14 #include <artec/sdk/base/Types.h>
15 #include <artec/sdk/base/Point.h>
16 
17 namespace artec { namespace sdk { namespace base
18 {
19 
20 /*
21 * Template class for rectangle
22 */
23 
24 template < typename T = int >
25 struct Rect
26 {
27  typedef T value_type;
28 
29  Rect()
30  : left(0), top(0), right(0), bottom(0)
31  { }
32  Rect(int left, int top, int right, int bottom)
33  : left(left), top(top), right(right), bottom(bottom)
34  { }
35  Rect(const Point2<T>& topLeft, const Point2<T>& bottomRight)
36  : left(topLeft.x), top(topLeft.y), right(bottomRight.x), bottom(bottomRight.y) {};
37 
38  bool isEmpty() const { return (width() <= 0) || (height() <= 0); }
39  /// normalize rect
40  void normalize()
41  {
42  if (left > right)
43  std::swap(left, right);
44  if (top > bottom)
45  std::swap(top, bottom);
46  };
47  /// rect the width
48  T width() const { return right - left; }
49  /// rect the height
50  T height() const { return bottom - top; }
51  /// returns the size
52  Size size() const { return Size(width(), height()); }
53  /// returns the low boundaries
54  Point2<T> low() const { return Point2<T>(left, top); }
55  /// returns the low boundaries
56  Point2<T> high() const { return Point2<T>(right, bottom); }
57  /// check is point in rect
58  bool contains(T x, T y) const
59  {
60  if(x >= left && x <= right && y >= top && y <= bottom)
61  return true;
62  return false;
63  }
64  /// check is point in rect
65  bool contains(const Point2<T>& p) const
66  {
67  return contains(p.x, p.y);
68  }
69 
70  /// offset rect
71  Rect & offset(const Point2<T> & v) { left += v.x; top += v.y; right += v.x; bottom += v.y; return *this; }
72  Rect & operator+=(const Point2<T> & v) { offset(v); return *this; }
73  Rect operator+(const Point2<T> & v) const { Rect ret = *this; return ret += v; }
74 
75  /// intersect rect
76  void intersect(const Rect<T>& rect) {
77  left = std::max(left, rect.left);
78  top = std::max(top, rect.top);
79  right = std::min(right, rect.right);
80  bottom = std::min(bottom, rect.bottom);
81  if ( right <= left || bottom <= top){
82  right = left;
83  bottom = top;
84  }
85  }
86 
87  /// return center of rectangle
88  Point2<T> center() const { return Point2<T>((left + right)/2, (top + bottom)/2); }
89 
90  T left;
91  T top;
92  T right;
93  T bottom;
94 };
95 
99 
100 } } } // namespace artec::sdk::base
101 
102 #endif //_ARTEC_RECT_H_
Rect< double > RectD
Definition: Rect.h:98
Rect & operator+=(const Point2< T > &v)
Definition: Rect.h:72
Rect(int left, int top, int right, int bottom)
Definition: Rect.h:32
Rect operator+(const Point2< T > &v) const
Definition: Rect.h:73
T width() const
rect the width
Definition: Rect.h:48
Rect & offset(const Point2< T > &v)
offset rect
Definition: Rect.h:71
void normalize()
normalize rect
Definition: Rect.h:40
bool contains(T x, T y) const
check is point in rect
Definition: Rect.h:58
bool isEmpty() const
Definition: Rect.h:38
Rect< int > RectI
Definition: Rect.h:96
void intersect(const Rect< T > &rect)
intersect rect
Definition: Rect.h:76
Point2< T > high() const
returns the low boundaries
Definition: Rect.h:56
Size size() const
returns the size
Definition: Rect.h:52
Rect(const Point2< T > &topLeft, const Point2< T > &bottomRight)
Definition: Rect.h:35
bool contains(const Point2< T > &p) const
check is point in rect
Definition: Rect.h:65
Point2< T > low() const
returns the low boundaries
Definition: Rect.h:54
Point2< T > center() const
return center of rectangle
Definition: Rect.h:88
Rect< float > RectF
Definition: Rect.h:97
T height() const
rect the height
Definition: Rect.h:50