From 3265277468de1b140e656005fc0a9271d3e91329 Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 27 Feb 2012 20:04:57 -0500 Subject: [PATCH] Added dcenter(), point_transform_affine, and rotation_matrix() --- dlib/geometry/rectangle.h | 12 ++++++++ dlib/geometry/rectangle_abstract.h | 10 +++++++ dlib/geometry/vector.h | 39 ++++++++++++++++++++++++ dlib/geometry/vector_abstract.h | 48 ++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) diff --git a/dlib/geometry/rectangle.h b/dlib/geometry/rectangle.h index 0b617a1a2..61f42dbe7 100644 --- a/dlib/geometry/rectangle.h +++ b/dlib/geometry/rectangle.h @@ -379,6 +379,18 @@ namespace dlib return temp/2; } +// ---------------------------------------------------------------------------------------- + + inline dlib::vector dcenter ( + const dlib::rectangle& rect + ) + { + dlib::vector temp(rect.left() + rect.right(), + rect.top() + rect.bottom()); + + return temp/2.0; + } + // ---------------------------------------------------------------------------------------- inline long distance_to_rect_edge ( diff --git a/dlib/geometry/rectangle_abstract.h b/dlib/geometry/rectangle_abstract.h index 14e185560..aab8a37ae 100644 --- a/dlib/geometry/rectangle_abstract.h +++ b/dlib/geometry/rectangle_abstract.h @@ -412,6 +412,16 @@ namespace dlib - returns the center of the given rectangle !*/ +// ---------------------------------------------------------------------------------------- + + dlib::vector dcenter ( + const dlib::rectangle& rect + ); + /*! + ensures + - returns the center of the given rectangle using a real valued vector. + !*/ + // ---------------------------------------------------------------------------------------- inline const rectangle centered_rect ( diff --git a/dlib/geometry/vector.h b/dlib/geometry/vector.h index 4133670b0..6ff4b7781 100644 --- a/dlib/geometry/vector.h +++ b/dlib/geometry/vector.h @@ -1319,6 +1319,30 @@ namespace dlib dlib::vector translate; }; +// ---------------------------------------------------------------------------------------- + + class point_transform_affine + { + public: + point_transform_affine ( + const matrix& m_, + const dlib::vector& b_ + ) :m(m_), b(b_) + { + } + + const dlib::vector operator() ( + const dlib::vector& p + ) const + { + return m*p + b; + } + + private: + matrix m; + dlib::vector b; + }; + // ---------------------------------------------------------------------------------------- template @@ -1332,6 +1356,21 @@ namespace dlib return rot(p-center)+center; } +// ---------------------------------------------------------------------------------------- + + inline matrix rotation_matrix ( + double angle + ) + { + const double ca = cos(angle); + const double sa = sin(angle); + + matrix m; + m = ca, -sa, + sa, ca; + return m; + } + // ---------------------------------------------------------------------------------------- } diff --git a/dlib/geometry/vector_abstract.h b/dlib/geometry/vector_abstract.h index 482d66b8b..9cf98041b 100644 --- a/dlib/geometry/vector_abstract.h +++ b/dlib/geometry/vector_abstract.h @@ -431,6 +431,37 @@ namespace dlib typedef vector 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& m, + const dlib::vector& b + ); + /*! + ensures + - When (*this)(p) is invoked it will return a point P such that: + - P == m*p + b + !*/ + + const dlib::vector operator() ( + const dlib::vector& 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 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. + !*/ + // ---------------------------------------------------------------------------------------- }