Artec 3D Scanning SDK  2.0
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 /// Structure that defines rectangle, i.e. two-dimensional range
26 struct Rect
27 {
28  typedef T value_type;
29 
30  Rect()
31  : left(0), top(0), right(0), bottom(0)
32  { }
33  Rect(T left, T top, T right, T bottom)
34  : left(left), top(top), right(right), bottom(bottom)
35  { }
36  Rect(const Point2<T>& topLeft, const Point2<T>& bottomRight)
37  : left(topLeft.x), top(topLeft.y), right(bottomRight.x), bottom(bottomRight.y) {};
38 
39  bool isEmpty() const { return (width() <= 0) || (height() <= 0); }
40  /// normalize rect
41  void normalize()
42  {
43  if (left > right)
44  std::swap(left, right);
45  if (top > bottom)
46  std::swap(top, bottom);
47  };
48  /// Rect the width
49  T width() const { return right - left; }
50  /// Rect the height
51  T height() const { return bottom - top; }
52  /// Returns the size
53  Size size() const { return Size(width(), height()); }
54  /// Returns the low boundaries
55  Point2<T> low() const { return Point2<T>(left, top); }
56  /// Returns the low boundaries
57  Point2<T> high() const { return Point2<T>(right, bottom); }
58  /// Check whether the point is in rect
59  bool contains(T x, T y) const
60  {
61  if(x >= left && x <= right && y >= top && y <= bottom)
62  return true;
63  return false;
64  }
65  /// Check whether the point is in rect
66  bool contains(const Point2<T>& p) const
67  {
68  return contains(p.x, p.y);
69  }
70 
71  /// Offset rect
72  Rect & offset(const Point2<T> & v) { left += v.x; top += v.y; right += v.x; bottom += v.y; return *this; }
73  Rect & operator+=(const Point2<T> & v) { offset(v); return *this; }
74  Rect operator+(const Point2<T> & v) const { Rect ret = *this; return ret += v; }
75 
76  /// Intersect rect
77  void intersect(const Rect<T>& rect) {
78  left = std::max(left, rect.left);
79  top = std::max(top, rect.top);
80  right = std::min(right, rect.right);
81  bottom = std::min(bottom, rect.bottom);
82  if ( right <= left || bottom <= top){
83  right = left;
84  bottom = top;
85  }
86  }
87 
88  /// Returns center of rectangle
89  Point2<T> center() const { return Point2<T>((left + right)/2, (top + bottom)/2); }
90 
91  T left;
92  T top;
93  T right;
94  T bottom;
95 };
96 
100 
101 } } } // namespace artec::sdk::base
102 
103 #endif //_ARTEC_RECT_H_
Rect< double > RectD
Definition: Rect.h:99
Rect & operator+=(const Point2< T > &v)
Definition: Rect.h:73
Rect operator+(const Point2< T > &v) const
Definition: Rect.h:74
T width() const
Rect the width.
Definition: Rect.h:49
Rect & offset(const Point2< T > &v)
Offset rect.
Definition: Rect.h:72
void normalize()
normalize rect
Definition: Rect.h:41
Structure that defines rectangle, i.e. two-dimensional range.
Definition: Rect.h:26
bool contains(T x, T y) const
Check whether the point is in rect.
Definition: Rect.h:59
bool isEmpty() const
Definition: Rect.h:39
Rect< int > RectI
Definition: Rect.h:97
2-dimensional point class.
Definition: Point.h:40
void intersect(const Rect< T > &rect)
Intersect rect.
Definition: Rect.h:77
Point2< T > high() const
Returns the low boundaries.
Definition: Rect.h:57
Size size() const
Returns the size.
Definition: Rect.h:53
Rect(const Point2< T > &topLeft, const Point2< T > &bottomRight)
Definition: Rect.h:36
bool contains(const Point2< T > &p) const
Check whether the point is in rect.
Definition: Rect.h:66
Rect(T left, T top, T right, T bottom)
Definition: Rect.h:33
Point2< T > low() const
Returns the low boundaries.
Definition: Rect.h:55
Image size defined by width and height.
Definition: Types.h:24
Point2< T > center() const
Returns center of rectangle.
Definition: Rect.h:89
Rect< float > RectF
Definition: Rect.h:98
T height() const
Rect the height.
Definition: Rect.h:51