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
drectangle (
const rectangle& rect
) : l(rect.left()-0.5),
t(rect.top()-0.5),
r(rect.right()+0.5),
b(rect.bottom()+0.5) {}
) : l(rect.left()),
t(rect.top()),
r(rect.right()),
b(rect.bottom()) {}
operator rectangle (
) const
{
return rectangle((long)std::ceil(l),
(long)std::ceil(t),
(long)std::floor(r),
(long)std::floor(b));
return rectangle((long)std::floor(l+0.5),
(long)std::floor(t+0.5),
(long)std::floor(r+0.5),
(long)std::floor(b+0.5));
}
double left() const { return l; }
......@@ -95,21 +95,19 @@ namespace dlib
double width (
) const
{
// if either the width or height would be 0.
if (t >= b || l >= r)
if (is_empty())
return 0;
else
return r - l;
return r - l + 1;
}
double height (
) const
{
// if either the width or height would be 0.
if (t >= b || l >= r)
if (is_empty())
return 0;
else
return b - t;
return b - t + 1;
}
double area (
......@@ -189,6 +187,15 @@ namespace dlib
return *this;
}
drectangle& operator += (
const dlib::vector<double,2>& p
)
{
*this = *this + drectangle(p);
return *this;
}
private:
double l;
double t;
......@@ -298,10 +305,17 @@ namespace dlib
const double& scale
)
{
const double width = rect.width()*scale;
const double height = rect.height()*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);
if (!rect.is_empty())
{
const double width = (rect.right()-rect.left())*scale;
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* (
......@@ -361,10 +375,13 @@ namespace dlib
inline drectangle centered_drect (
const dlib::vector<double,2>& p,
const double& width,
const double& height
double width,
double height
)
{
width--;
height--;
return drectangle(p.x()-width/2, p.y()-height/2, p.x()+width/2, p.y()+height/2);
}
......
......@@ -19,9 +19,9 @@ namespace dlib
WHAT THIS OBJECT REPRESENTS
This object is just like dlib::rectangle except that it stores the
coordinates of the rectangle using double rather than long variables. As
such, this object represents a rectangular region inside a Cartesian
coordinate system. The region is the rectangle with its top left corner at
position (left(),top()) and its bottom right corner at (right(),bottom()).
such, this object represents a rectangular region inside an image. The
region is the rectangle with its top left corner at position (left(),top())
and its bottom right corner at (right(),bottom()).
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
......@@ -91,10 +91,10 @@ namespace dlib
);
/*!
ensures
- left() == rect.left()-0.5
- top() == rect.top()-0.5
- right() == rect.right()+0.5
- bottom() == rect.bottom()+0.5
- left() == rect.left()
- top() == rect.top()
- right() == rect.right()
- bottom() == rect.bottom()
- dcenter(*this) == dcenter(rect)
- width() == rect.width()
- height() == rect.height()
......@@ -104,14 +104,8 @@ namespace dlib
) const;
/*!
ensures
- returns a rectangle R such that:
- R.left() == ceil(left())
- 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.
- returns a rectangle where left(), top(), right(), and bottom() have been
rounded to the nearest integer values.
!*/
double left (
......@@ -214,7 +208,7 @@ namespace dlib
- returns 0
- else
- returns the width of this rectangle.
(i.e. right() - left())
(i.e. right() - left() + 1)
!*/
double height (
......@@ -225,7 +219,7 @@ namespace dlib
- returns 0
- else
- returns the height of this rectangle.
(i.e. bottom() - top())
(i.e. bottom() - top() + 1)
!*/
double area (
......@@ -316,6 +310,15 @@ namespace dlib
- returns #*this
!*/
drectangle& operator += (
const dlib::vector<double,2>& p
);
/*!
ensures
- performs: *this = *this + drectangle(p)
- returns #*this
!*/
};
// ----------------------------------------------------------------------------------------
......@@ -388,12 +391,12 @@ namespace dlib
);
/*!
ensures
- This function returns a rectangle that has the same center as rect but with a
width and height that are scale times larger. That is, we return a new
rectangle R such that:
- This function returns a rectangle that has the same center as rect but with
dimensions that are scale times larger. That is, we return a new rectangle R
such that:
- center(R) == center(rect)
- R.width() == rect.width()*scale
- R.height() == rect.height()*scale
- R.right()-R.left() == (rect.right()-rect.left())*scale
- R.bottom()-R.top() == (rect.bottom()-rect.top())*scale
!*/
// ----------------------------------------------------------------------------------------
......@@ -484,20 +487,20 @@ namespace dlib
drectangle centered_drect (
const vector<double,2>& p,
const double& width,
const double& height
double width,
double height
);
/*!
ensures
- returns a rectangle R such that:
- center(R) == p
- if (width == 0 || height == 0)
- if (width < 1 || height < 1)
- R.width() == 0
- R.height() == 0
- R.is_empty() == true
- else
- R.width() == width
- 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