Added dcenter(), point_transform_affine, and rotation_matrix()

This commit is contained in:
Davis King 2012-02-27 20:04:57 -05:00
parent 1940012f33
commit 3265277468
4 changed files with 109 additions and 0 deletions

View File

@ -379,6 +379,18 @@ namespace dlib
return temp/2;
}
// ----------------------------------------------------------------------------------------
inline dlib::vector<double,2> dcenter (
const dlib::rectangle& rect
)
{
dlib::vector<double,2> temp(rect.left() + rect.right(),
rect.top() + rect.bottom());
return temp/2.0;
}
// ----------------------------------------------------------------------------------------
inline long distance_to_rect_edge (

View File

@ -412,6 +412,16 @@ namespace dlib
- returns the center of the given rectangle
!*/
// ----------------------------------------------------------------------------------------
dlib::vector<double,2> dcenter (
const dlib::rectangle& rect
);
/*!
ensures
- returns the center of the given rectangle using a real valued vector.
!*/
// ----------------------------------------------------------------------------------------
inline const rectangle centered_rect (

View File

@ -1319,6 +1319,30 @@ namespace dlib
dlib::vector<double,2> translate;
};
// ----------------------------------------------------------------------------------------
class point_transform_affine
{
public:
point_transform_affine (
const matrix<double,2,2>& m_,
const dlib::vector<double,2>& b_
) :m(m_), b(b_)
{
}
const dlib::vector<double,2> operator() (
const dlib::vector<double,2>& p
) const
{
return m*p + b;
}
private:
matrix<double,2,2> m;
dlib::vector<double,2> b;
};
// ----------------------------------------------------------------------------------------
template <typename T>
@ -1332,6 +1356,21 @@ namespace dlib
return rot(p-center)+center;
}
// ----------------------------------------------------------------------------------------
inline matrix<double,2,2> rotation_matrix (
double angle
)
{
const double ca = cos(angle);
const double sa = sin(angle);
matrix<double,2,2> m;
m = ca, -sa,
sa, ca;
return m;
}
// ----------------------------------------------------------------------------------------
}

View File

@ -431,6 +431,37 @@ namespace dlib
typedef vector<long,2> point;
// ----------------------------------------------------------------------------------------
class point_transform_affine
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 2D points or vectors and
applies an affine transformation to them.
!*/
public:
point_transform_affine (
const matrix<double,2,2>& m,
const dlib::vector<double,2>& b
);
/*!
ensures
- When (*this)(p) is invoked it will return a point P such that:
- P == m*p + b
!*/
const dlib::vector<double,2> operator() (
const dlib::vector<double,2>& p
) const;
/*!
ensures
- applies the affine transformation defined by this object's constructor
to p and returns the result.
!*/
};
// ----------------------------------------------------------------------------------------
class point_transform
@ -517,6 +548,23 @@ namespace dlib
to the right)
!*/
// ----------------------------------------------------------------------------------------
matrix<double,2,2> rotation_matrix (
double angle
);
/*!
ensures
- returns a rotation matrix which rotates points around the origin in a
counter-clockwise direction by angle radians.
(Note that this is counter clockwise with respect to the normal
coordinate system with positive y going up and positive x going
to the right)
Or in other words, this function returns a matrix M such that, given a
point P, M*P gives a point which is P rotated by angle radians around
the origin in a counter-clockwise direction.
!*/
// ----------------------------------------------------------------------------------------
}