From a73c1619c7e8217ad8b12f32d1ffb2c9a02a64cd Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 4 Apr 2009 15:41:21 +0000 Subject: [PATCH] Added the point_rotator object. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402981 --- dlib/geometry/vector.h | 41 +++++++++++++++++++++++++-------- dlib/geometry/vector_abstract.h | 33 ++++++++++++++++++++++++++ dlib/test/geometry.cpp | 4 ++++ 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/dlib/geometry/vector.h b/dlib/geometry/vector.h index 6ea30fa9a..578b4066c 100644 --- a/dlib/geometry/vector.h +++ b/dlib/geometry/vector.h @@ -1244,23 +1244,46 @@ namespace dlib typedef vector point; +// ---------------------------------------------------------------------------------------- + + class point_rotator + { + public: + point_rotator ( + const double& angle + ) + { + sin_angle = std::sin(angle); + cos_angle = std::cos(angle); + } + + template + const dlib::vector operator() ( + const dlib::vector& p + ) const + { + double x = cos_angle*p.x() - sin_angle*p.y(); + double y = sin_angle*p.x() + cos_angle*p.y(); + + return dlib::vector(x,y); + } + + private: + double sin_angle; + double cos_angle; + }; + // ---------------------------------------------------------------------------------------- template const dlib::vector rotate_point ( const dlib::vector& center, - dlib::vector p, + const dlib::vector& p, double angle ) { - p -= center; - dlib::vector temp; - const double cos_angle = cos(angle); - const double sin_angle = sin(angle); - temp.x() = cos_angle*p.x() - sin_angle*p.y(); - temp.y() = sin_angle*p.x() + cos_angle*p.y(); - - return temp + center; + point_rotator rot(angle); + return rot(p-center)+center; } // ---------------------------------------------------------------------------------------- diff --git a/dlib/geometry/vector_abstract.h b/dlib/geometry/vector_abstract.h index fdc8989e5..64d437e09 100644 --- a/dlib/geometry/vector_abstract.h +++ b/dlib/geometry/vector_abstract.h @@ -431,6 +431,39 @@ namespace dlib typedef vector point; +// ---------------------------------------------------------------------------------------- + + class point_rotator + { + /*! + WHAT THIS OBJECT REPRESENTS + This is an object that takes 2D points or vectors and + rotates them around the origin by a given angle. + !*/ + public: + point_rotator ( + const double& angle + ); + /*! + ensures + - When (*this)(p) is invoked it will return a point P such that: + - P is the point p rotated counter-clockwise around the origin + 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) + !*/ + + template + const dlib::vector operator() ( + const dlib::vector& p + ) const; + /*! + ensures + - rotates p and returns the result + !*/ + }; + // ---------------------------------------------------------------------------------------- template diff --git a/dlib/test/geometry.cpp b/dlib/test/geometry.cpp index 37c0a5981..cf1e2e9c5 100644 --- a/dlib/test/geometry.cpp +++ b/dlib/test/geometry.cpp @@ -288,6 +288,10 @@ namespace DLIB_TEST(rotate_point(center, p1, -pi/4 + 10*pi) == point(8,-2)); DLIB_TEST(rotate_point(center, p1, pi/4 - 10*pi) == point(8,8)); DLIB_TEST(rotate_point(center, p1, -pi/4 - 10*pi) == point(8,-2)); + + point_rotator rot(pi/2); + DLIB_TEST(rot(point(1,0)) == point(0,1)); + DLIB_TEST(rot(point(0,1)) == point(-1,0)); } }