Commit 51527818 authored by Davis King's avatar Davis King

Refined the definition of the drectangle a little. In particular,

now it behaves more like the rectangle in terms of how it measures
width and height.
parent cd64c518
...@@ -56,18 +56,18 @@ namespace dlib ...@@ -56,18 +56,18 @@ namespace dlib
drectangle ( drectangle (
const rectangle& rect const rectangle& rect
) : l(rect.left()-0.5), ) : l(rect.left()),
t(rect.top()-0.5), t(rect.top()),
r(rect.right()+0.5), r(rect.right()),
b(rect.bottom()+0.5) {} b(rect.bottom()) {}
operator rectangle ( operator rectangle (
) const ) const
{ {
return rectangle((long)std::ceil(l), return rectangle((long)std::floor(l+0.5),
(long)std::ceil(t), (long)std::floor(t+0.5),
(long)std::floor(r), (long)std::floor(r+0.5),
(long)std::floor(b)); (long)std::floor(b+0.5));
} }
double left() const { return l; } double left() const { return l; }
...@@ -95,21 +95,19 @@ namespace dlib ...@@ -95,21 +95,19 @@ namespace dlib
double width ( double width (
) const ) const
{ {
// if either the width or height would be 0. if (is_empty())
if (t >= b || l >= r)
return 0; return 0;
else else
return r - l; return r - l + 1;
} }
double height ( double height (
) const ) const
{ {
// if either the width or height would be 0. if (is_empty())
if (t >= b || l >= r)
return 0; return 0;
else else
return b - t; return b - t + 1;
} }
double area ( double area (
...@@ -189,6 +187,15 @@ namespace dlib ...@@ -189,6 +187,15 @@ namespace dlib
return *this; return *this;
} }
drectangle& operator += (
const dlib::vector<double,2>& p
)
{
*this = *this + drectangle(p);
return *this;
}
private: private:
double l; double l;
double t; double t;
...@@ -298,10 +305,17 @@ namespace dlib ...@@ -298,10 +305,17 @@ namespace dlib
const double& scale const double& scale
) )
{ {
const double width = rect.width()*scale; if (!rect.is_empty())
const double height = rect.height()*scale; {
const dlib::vector<double,2> p = center(rect); const double width = (rect.right()-rect.left())*scale;
return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2); const double height = (rect.bottom()-rect.top())*scale;
const dlib::vector<double,2> p = center(rect);
return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2);
}
else
{
return rect;
}
} }
inline drectangle operator* ( inline drectangle operator* (
...@@ -361,10 +375,13 @@ namespace dlib ...@@ -361,10 +375,13 @@ namespace dlib
inline drectangle centered_drect ( inline drectangle centered_drect (
const dlib::vector<double,2>& p, const dlib::vector<double,2>& p,
const double& width, double width,
const double& height double height
) )
{ {
width--;
height--;
return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2); return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2);
} }
......
...@@ -19,9 +19,9 @@ namespace dlib ...@@ -19,9 +19,9 @@ namespace dlib
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This object is just like dlib::rectangle except that it stores the This object is just like dlib::rectangle except that it stores the
coordinates of the rectangle using double rather than long variables. As coordinates of the rectangle using double rather than long variables. As
such, this object represents a rectangular region inside a Cartesian such, this object represents a rectangular region inside an image. The
coordinate system. The region is the rectangle with its top left corner at region is the rectangle with its top left corner at position (left(),top())
position (left(),top()) and its bottom right corner at (right(),bottom()). and its bottom right corner at (right(),bottom()).
Note that the origin of the coordinate system, i.e. (0,0), is located at Note that the origin of the coordinate system, i.e. (0,0), is located at
the upper left corner. That is, points such as (1,1) or (3,5) represent the upper left corner. That is, points such as (1,1) or (3,5) represent
...@@ -91,10 +91,10 @@ namespace dlib ...@@ -91,10 +91,10 @@ namespace dlib
); );
/*! /*!
ensures ensures
- left() == rect.left()-0.5 - left() == rect.left()
- top() == rect.top()-0.5 - top() == rect.top()
- right() == rect.right()+0.5 - right() == rect.right()
- bottom() == rect.bottom()+0.5 - bottom() == rect.bottom()
- dcenter(*this) == dcenter(rect) - dcenter(*this) == dcenter(rect)
- width() == rect.width() - width() == rect.width()
- height() == rect.height() - height() == rect.height()
...@@ -104,14 +104,8 @@ namespace dlib ...@@ -104,14 +104,8 @@ namespace dlib
) const; ) const;
/*! /*!
ensures ensures
- returns a rectangle R such that: - returns a rectangle where left(), top(), right(), and bottom() have been
- R.left() == ceil(left()) rounded to the nearest integer values.
- R.top() == ceil(top())
- R.right() == floor(right())
- R.bottom() == floor(bottom())
- This function is designed this way so that converting a rectangle to a
drectangle and then back to a rectangle again results in the original
rectangle.
!*/ !*/
double left ( double left (
...@@ -214,7 +208,7 @@ namespace dlib ...@@ -214,7 +208,7 @@ namespace dlib
- returns 0 - returns 0
- else - else
- returns the width of this rectangle. - returns the width of this rectangle.
(i.e. right() - left()) (i.e. right() - left() + 1)
!*/ !*/
double height ( double height (
...@@ -225,7 +219,7 @@ namespace dlib ...@@ -225,7 +219,7 @@ namespace dlib
- returns 0 - returns 0
- else - else
- returns the height of this rectangle. - returns the height of this rectangle.
(i.e. bottom() - top()) (i.e. bottom() - top() + 1)
!*/ !*/
double area ( double area (
...@@ -316,6 +310,15 @@ namespace dlib ...@@ -316,6 +310,15 @@ namespace dlib
- returns #*this - returns #*this
!*/ !*/
drectangle& operator += (
const dlib::vector<double,2>& p
);
/*!
ensures
- performs: *this = *this + drectangle(p)
- returns #*this
!*/
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -388,12 +391,12 @@ namespace dlib ...@@ -388,12 +391,12 @@ namespace dlib
); );
/*! /*!
ensures ensures
- This function returns a rectangle that has the same center as rect but with a - This function returns a rectangle that has the same center as rect but with
width and height that are scale times larger. That is, we return a new dimensions that are scale times larger. That is, we return a new rectangle R
rectangle R such that: such that:
- center(R) == center(rect) - center(R) == center(rect)
- R.width() == rect.width()*scale - R.right()-R.left() == (rect.right()-rect.left())*scale
- R.height() == rect.height()*scale - R.bottom()-R.top() == (rect.bottom()-rect.top())*scale
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -484,20 +487,20 @@ namespace dlib ...@@ -484,20 +487,20 @@ namespace dlib
drectangle centered_drect ( drectangle centered_drect (
const vector<double,2>& p, const vector<double,2>& p,
const double& width, double width,
const double& height double height
); );
/*! /*!
ensures ensures
- returns a rectangle R such that: - returns a rectangle R such that:
- center(R) == p - center(R) == p
- if (width == 0 || height == 0) - if (width < 1 || height < 1)
- R.width() == 0 - R.width() == 0
- R.height() == 0 - R.height() == 0
- R.is_empty() == true
- else - else
- R.width() == width - R.width() == width
- R.height() == height - R.height() == height
- R.tl_corner() == point(p.x()-width/2, p.y()-height/2)
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment