BoundingBox.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*!
  2. * \file BoundingBox.cpp
  3. * \brief
  4. * \author Gapchich Vladislav
  5. * \date 23/10/11
  6. */
  7. #include "vislearning/cbaselib/BoundingBox.h"
  8. using namespace OBJREC;
  9. using namespace std;
  10. using namespace NICE;
  11. //! A constructor
  12. BoundingBox::BoundingBox()
  13. {
  14. top_left_.x = -1;
  15. top_left_.y = -1;
  16. bottom_right_.x = -1;
  17. bottom_right_.y = -1;
  18. id_ = -1;
  19. }
  20. // A copy-constructor
  21. BoundingBox::BoundingBox(const BoundingBox &copy)
  22. {
  23. top_left_ = copy.topLeft();
  24. bottom_right_ = copy.bottomRight();
  25. id_ = copy.id();
  26. }
  27. //! A desctructor
  28. BoundingBox::BoundingBox::~BoundingBox()
  29. {
  30. }
  31. //! Sets top left coordinate of the bounding box
  32. /*!
  33. * /param[in] aTopLeft should not contain negative data
  34. */
  35. void
  36. BoundingBox::setTopLeft(const CoordT< int > &aTopLeft)
  37. {
  38. if (aTopLeft.x < 0 || aTopLeft.y < 0) {
  39. return;
  40. /* NOTREACHED */
  41. }
  42. top_left_ = aTopLeft;
  43. }
  44. //! overloaded
  45. /*!
  46. * \see setTopLeft(const CoordT< int > &aTopLeft)
  47. */
  48. void
  49. BoundingBox::setTopLeft(const int &x, const int &y)
  50. {
  51. if (x < 0 || y < 0) {
  52. return;
  53. /* NOTREACHED */
  54. }
  55. CoordT< int > topLeft(x, y);
  56. top_left_ = topLeft;
  57. }
  58. //! Sets bottom right coordinate of the bounding box
  59. /*!
  60. * /param[in] aBottomRight should not contain negative data
  61. */
  62. void
  63. BoundingBox::setBottomRight(const CoordT< int > &aBottomRight)
  64. {
  65. if (aBottomRight.x < 0 || aBottomRight.y < 0) {
  66. return;
  67. /* NOTREACHED */
  68. }
  69. bottom_right_ = aBottomRight;
  70. }
  71. //! overloaded
  72. /*!
  73. * \see setBottomRight(const CoordT< int > &aBottomRight)
  74. */
  75. void
  76. BoundingBox::setBottomRight(const int &x, const int &y)
  77. {
  78. if (x < 0 || y < 0) {
  79. return;
  80. /* NOTREACHED */
  81. }
  82. CoordT< int > bottomRight(x, y);
  83. bottom_right_ = bottomRight;
  84. }
  85. //!
  86. void
  87. BoundingBox::setWidth(const int &aWidth)
  88. {
  89. if (aWidth < 0) {
  90. return;
  91. /* NOTREACHED */
  92. }
  93. bottom_right_.x = top_left_.x + aWidth;
  94. }
  95. //!
  96. void
  97. BoundingBox::setHeight(const int &aHeight)
  98. {
  99. if (aHeight < 0) {
  100. return;
  101. /* NOTREACHED */
  102. }
  103. bottom_right_.y = top_left_.y + aHeight;
  104. }
  105. //! Sets a category ID(label ID) for the bounding box
  106. /*!
  107. * /param[in] anID should not be less than zero
  108. */
  109. void
  110. BoundingBox::setID(const int &anID)
  111. {
  112. if (anID < 0) {
  113. return;
  114. /* NOTREACHED */
  115. }
  116. }
  117. //! returns top left coordinate of the bounding box
  118. CoordT< int >
  119. BoundingBox::topLeft() const
  120. {
  121. return top_left_;
  122. }
  123. //! returns bottom right coordinate of the bounding box
  124. CoordT< int >
  125. BoundingBox::bottomRight() const
  126. {
  127. return bottom_right_;
  128. }
  129. //!
  130. int
  131. BoundingBox::width()
  132. {
  133. if (top_left_.x < bottom_right_.x)
  134. return bottom_right_.x - top_left_.x;
  135. else
  136. return -1;
  137. }
  138. //!
  139. int
  140. BoundingBox::height()
  141. {
  142. if (top_left_.y < bottom_right_.y)
  143. return bottom_right_.y - top_left_.y;
  144. else
  145. return -1;
  146. }
  147. //! returns a category ID of the bounding box
  148. int
  149. BoundingBox::id() const
  150. {
  151. return id_;
  152. }
  153. //! Checks whether bounding box is valid or not
  154. /*!
  155. * Member checks if top left and bottom right coordinates are really
  156. * so, if category id and coordinates are positive
  157. *
  158. * returns true on success.
  159. */
  160. bool
  161. BoundingBox::isValid() const
  162. {
  163. if (id_ < 0 ||
  164. top_left_.x < 0 ||
  165. top_left_.y < 0 ||
  166. bottom_right_.x < 0 ||
  167. bottom_right_.y < 0 ||
  168. bottom_right_.y <= top_left_.y ||
  169. top_left_.x <= bottom_right_.x)
  170. {
  171. return false;
  172. /* NOTREACHED */
  173. }
  174. return true;
  175. }
  176. //! swaps top left and bottom right coordinates if they are mixed
  177. void
  178. BoundingBox::validate()
  179. {
  180. if (bottom_right_.y < top_left_.y ||
  181. top_left_.x < bottom_right_.x)
  182. {
  183. CoordT< int > buffer = top_left_;
  184. top_left_ = bottom_right_;
  185. bottom_right_ = buffer;
  186. }
  187. }
  188. /*
  189. *
  190. */