RectT.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef _IMAGE_RECT_H
  2. #define _IMAGE_RECT_H
  3. #include "core/image/ippwrapper.h"
  4. #include "core/image/CoordT.h"
  5. #include "core/vector/VectorT.h"
  6. #include <ostream>
  7. namespace NICE {
  8. /**
  9. * A simple discrete rectangle (oriented along coordinate axes).
  10. *
  11. * @author Ferid Bajramovic
  12. **/
  13. template<class T>
  14. class RectT {
  15. public:
  16. //! left border
  17. T left;
  18. //! top border
  19. T top;
  20. //! width
  21. T width;
  22. //! height
  23. T height;
  24. public:
  25. /**
  26. * Create an empty Rect.
  27. **/
  28. inline RectT()
  29. : left(0), top(0), width(0), height(0) {
  30. }
  31. /**
  32. * Create a Rect with given parameters.
  33. * @param _left left border
  34. * @param _top top border
  35. * @param _width width
  36. * @param _height height
  37. **/
  38. inline RectT(const T _left, const T _top, const T _width, const T _height)
  39. : left(_left), top(_top), width(_width), height(_height) {
  40. }
  41. /**
  42. * Create a Rect with given coordinates
  43. * @param c1 first coordinate
  44. * @param c2 second coordinate
  45. **/
  46. template <class Tp>
  47. inline RectT(const CoordT<Tp>& c1, const CoordT<Tp>& c2) {
  48. left = std::min(c1.x, c2.x);
  49. top = std::min(c1.y, c2.y);
  50. width = std::max(c1.x, c2.x) - left;
  51. height = std::max(c1.y, c2.y) - top;
  52. }
  53. /**
  54. * Copy-constructor.
  55. * @param other Original Rect.
  56. **/
  57. inline RectT(const RectT<T>& other) {
  58. left = other.left;
  59. top = other.top;
  60. width = other.width;
  61. height = other.height;
  62. }
  63. ~RectT() {
  64. }
  65. /**
  66. * Compute the right border (which is NOT part of the rect's area).
  67. * @return right border
  68. **/
  69. inline T right() const {
  70. return left + width;
  71. }
  72. /**
  73. * Compute the bottom border (which is NOT part of the rect's area).
  74. * @return bottom border
  75. **/
  76. inline T bottom() const {
  77. return top + height;
  78. }
  79. /**
  80. * Compute the area of \c this .
  81. * @return center
  82. **/
  83. inline T area() const {
  84. return width*height;
  85. }
  86. /**
  87. * Compute the center of \c this .
  88. * @return center
  89. **/
  90. inline CoordT<T> center() const {
  91. return CoordT<T>(left+width/2, top+height/2);
  92. }
  93. /**
  94. * Are \c this and \c rect equal ?
  95. **/
  96. inline bool operator==(const RectT<T> &rect) const {
  97. return (left==rect.left && top==rect.top &&
  98. width==rect.width && height==rect.height);
  99. }
  100. /**
  101. * Are \c this and \c rect not equal ?
  102. **/
  103. inline bool operator!=(const RectT<T> &rect) const {
  104. return !(operator==(rect));
  105. }
  106. /**
  107. * Assign \c rect to \c this
  108. **/
  109. inline void operator=(const RectT<T> &rect) {
  110. left = rect.left;
  111. top = rect.top;
  112. width = rect.width;
  113. height = rect.height;
  114. }
  115. /**
  116. * Is (\c x , \c y ) inside \c this ?
  117. */
  118. template <class Tp>
  119. inline bool isWithin(Tp x, Tp y) const {
  120. return x >= left && x < right() && y >= top && y < bottom();
  121. }
  122. /**
  123. * Is \c pt inside \c this ?
  124. */
  125. template <class Tp>
  126. inline bool isWithin(const CoordT<Tp>& pt) const {
  127. return isWithin(pt.x, pt.y);
  128. }
  129. /**
  130. * Calculate the intersection of \c this with \c rect
  131. * @param rect rect to calculate the intersection with
  132. * @param intersectRect resulting rect
  133. * @return true if there's an intersection of \c this with \c rect
  134. * @remark current implementation works only for T=int
  135. */
  136. bool intersect(const RectT<T>& rect, RectT<T>& intersectRect);
  137. };
  138. template <class T>
  139. inline std::ostream& operator<< (std::ostream& out, const RectT<T>& rect) {
  140. out << rect.left << ", " << rect.top
  141. << ", " << rect.width << ", " << rect.height;
  142. return out;
  143. }
  144. typedef RectT<int> Rect;
  145. } // namespace
  146. //#ifdef __GNUC__
  147. #include "core/image/RectT.tcc"
  148. //#endif
  149. #endif // _IMAGE_RECT_H