BoundingBox.cpp 3.5 KB

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