BoundingBox.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. id_ = anID;
  117. }
  118. //! returns top left coordinate of the bounding box
  119. CoordT< int >
  120. BoundingBox::topLeft() const
  121. {
  122. return top_left_;
  123. }
  124. //! returns bottom right coordinate of the bounding box
  125. CoordT< int >
  126. BoundingBox::bottomRight() const
  127. {
  128. return bottom_right_;
  129. }
  130. //!
  131. int
  132. BoundingBox::width()
  133. {
  134. if (top_left_.x < bottom_right_.x)
  135. return bottom_right_.x - top_left_.x;
  136. else
  137. return -1;
  138. }
  139. //!
  140. int
  141. BoundingBox::height()
  142. {
  143. if (top_left_.y < bottom_right_.y)
  144. return bottom_right_.y - top_left_.y;
  145. else
  146. return -1;
  147. }
  148. //! returns a category ID of the bounding box
  149. int
  150. BoundingBox::id() const
  151. {
  152. return id_;
  153. }
  154. //! Checks whether bounding box is valid or not
  155. /*!
  156. * Member checks if top left and bottom right coordinates are really
  157. * so, if category id and coordinates are positive
  158. *
  159. * returns true on success.
  160. */
  161. bool
  162. BoundingBox::isValid() const
  163. {
  164. if (id_ < 0 ||
  165. top_left_.x < 0 ||
  166. top_left_.y < 0 ||
  167. bottom_right_.x < 0 ||
  168. bottom_right_.y < 0 ||
  169. bottom_right_.y <= top_left_.y ||
  170. top_left_.x >= bottom_right_.x)
  171. {
  172. return false;
  173. /* NOTREACHED */
  174. }
  175. return true;
  176. }
  177. //! swaps top left and bottom right coordinates if they are mixed
  178. void
  179. BoundingBox::validate()
  180. {
  181. if (bottom_right_.y < top_left_.y ||
  182. top_left_.x < bottom_right_.x)
  183. {
  184. CoordT< int > buffer = top_left_;
  185. top_left_ = bottom_right_;
  186. bottom_right_ = buffer;
  187. }
  188. }
  189. /*
  190. *
  191. */