mirror of
https://github.com/davisking/dlib.git
synced 2024-11-01 10:14:53 +08:00
Moved the point/vector rotation/transformation code into its own file.
This commit is contained in:
parent
a30fa632fa
commit
0f9f0e7733
@ -6,6 +6,7 @@
|
||||
#include "geometry/rectangle.h"
|
||||
#include "geometry/vector.h"
|
||||
#include "geometry/border_enumerator.h"
|
||||
#include "geometry/point_transforms.h"
|
||||
|
||||
#endif // DLIB_GEOMETRy_HEADER
|
||||
|
||||
|
133
dlib/geometry/point_transforms.h
Normal file
133
dlib/geometry/point_transforms.h
Normal file
@ -0,0 +1,133 @@
|
||||
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
|
||||
// License: Boost Software License See LICENSE.txt for the full license.
|
||||
#ifndef DLIB_POINT_TrANSFORMS_H_
|
||||
#define DLIB_POINT_TrANSFORMS_H_
|
||||
|
||||
#include "point_transforms_abstract.h"
|
||||
#include "../algs.h"
|
||||
#include "../matrix.h"
|
||||
#include "vector.h"
|
||||
#include <vector>
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
class point_rotator
|
||||
{
|
||||
public:
|
||||
point_rotator (
|
||||
const double& angle
|
||||
)
|
||||
{
|
||||
sin_angle = std::sin(angle);
|
||||
cos_angle = std::cos(angle);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& 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<double,2>(x,y);
|
||||
}
|
||||
|
||||
private:
|
||||
double sin_angle;
|
||||
double cos_angle;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
class point_transform
|
||||
{
|
||||
public:
|
||||
point_transform (
|
||||
const double& angle,
|
||||
const dlib::vector<double,2>& translate_
|
||||
)
|
||||
{
|
||||
sin_angle = std::sin(angle);
|
||||
cos_angle = std::cos(angle);
|
||||
translate = translate_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& 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<double,2>(x,y) + translate;
|
||||
}
|
||||
|
||||
private:
|
||||
double sin_angle;
|
||||
double cos_angle;
|
||||
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>
|
||||
const dlib::vector<T,2> rotate_point (
|
||||
const dlib::vector<T,2>& center,
|
||||
const dlib::vector<T,2>& p,
|
||||
double angle
|
||||
)
|
||||
{
|
||||
point_rotator rot(angle);
|
||||
return rot(p-center)+center;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
inline matrix<double,2,2> rotation_matrix (
|
||||
double angle
|
||||
)
|
||||
{
|
||||
const double ca = std::cos(angle);
|
||||
const double sa = std::sin(angle);
|
||||
|
||||
matrix<double,2,2> m;
|
||||
m = ca, -sa,
|
||||
sa, ca;
|
||||
return m;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
#endif // DLIB_POINT_TrANSFORMS_H_
|
||||
|
152
dlib/geometry/point_transforms_abstract.h
Normal file
152
dlib/geometry/point_transforms_abstract.h
Normal file
@ -0,0 +1,152 @@
|
||||
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
|
||||
// License: Boost Software License See LICENSE.txt for the full license.
|
||||
#undef DLIB_POINT_TrANSFORMS_ABSTRACT_H__
|
||||
#ifdef DLIB_POINT_TrANSFORMS_ABSTRACT_H__
|
||||
|
||||
#include "../matrix/matrix_abstract.h"
|
||||
#include "../vector_abstract.h"
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
{
|
||||
/*!
|
||||
WHAT THIS OBJECT REPRESENTS
|
||||
This is an object that takes 2D points or vectors and
|
||||
rotates them around the origin by a given angle and then
|
||||
translates them.
|
||||
!*/
|
||||
public:
|
||||
point_transform (
|
||||
const double& angle,
|
||||
const dlib::vector<double,2>& translate
|
||||
)
|
||||
/*!
|
||||
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 and then shifted by having translate added to it.
|
||||
(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 <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& p
|
||||
) const;
|
||||
/*!
|
||||
ensures
|
||||
- rotates p, then translates it and returns the result
|
||||
!*/
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
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 <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& p
|
||||
) const;
|
||||
/*!
|
||||
ensures
|
||||
- rotates p and returns the result
|
||||
!*/
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
const dlib::vector<T,2> rotate_point (
|
||||
const dlib::vector<T,2> center,
|
||||
const dlib::vector<T,2> p,
|
||||
double angle
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- returns a point P such that:
|
||||
- P is the point p rotated counter-clockwise around the given
|
||||
center point 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)
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
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.
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
#endif // DLIB_POINT_TrANSFORMS_ABSTRACT_H__
|
||||
|
||||
|
@ -1258,119 +1258,6 @@ namespace dlib
|
||||
|
||||
typedef vector<long,2> point;
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
class point_rotator
|
||||
{
|
||||
public:
|
||||
point_rotator (
|
||||
const double& angle
|
||||
)
|
||||
{
|
||||
sin_angle = std::sin(angle);
|
||||
cos_angle = std::cos(angle);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& 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<double,2>(x,y);
|
||||
}
|
||||
|
||||
private:
|
||||
double sin_angle;
|
||||
double cos_angle;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
class point_transform
|
||||
{
|
||||
public:
|
||||
point_transform (
|
||||
const double& angle,
|
||||
const dlib::vector<double,2>& translate_
|
||||
)
|
||||
{
|
||||
sin_angle = std::sin(angle);
|
||||
cos_angle = std::cos(angle);
|
||||
translate = translate_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& 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<double,2>(x,y) + translate;
|
||||
}
|
||||
|
||||
private:
|
||||
double sin_angle;
|
||||
double cos_angle;
|
||||
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>
|
||||
const dlib::vector<T,2> rotate_point (
|
||||
const dlib::vector<T,2>& center,
|
||||
const dlib::vector<T,2>& p,
|
||||
double angle
|
||||
)
|
||||
{
|
||||
point_rotator rot(angle);
|
||||
return rot(p-center)+center;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
inline matrix<double,2,2> rotation_matrix (
|
||||
double angle
|
||||
)
|
||||
{
|
||||
const double ca = std::cos(angle);
|
||||
const double sa = std::sin(angle);
|
||||
|
||||
matrix<double,2,2> m;
|
||||
m = ca, -sa,
|
||||
sa, ca;
|
||||
return m;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
@ -431,140 +431,6 @@ 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
|
||||
{
|
||||
/*!
|
||||
WHAT THIS OBJECT REPRESENTS
|
||||
This is an object that takes 2D points or vectors and
|
||||
rotates them around the origin by a given angle and then
|
||||
translates them.
|
||||
!*/
|
||||
public:
|
||||
point_transform (
|
||||
const double& angle,
|
||||
const dlib::vector<double,2>& translate
|
||||
)
|
||||
/*!
|
||||
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 and then shifted by having translate added to it.
|
||||
(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 <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& p
|
||||
) const;
|
||||
/*!
|
||||
ensures
|
||||
- rotates p, then translates it and returns the result
|
||||
!*/
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
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 <typename T>
|
||||
const dlib::vector<T,2> operator() (
|
||||
const dlib::vector<T,2>& p
|
||||
) const;
|
||||
/*!
|
||||
ensures
|
||||
- rotates p and returns the result
|
||||
!*/
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
const dlib::vector<T,2> rotate_point (
|
||||
const dlib::vector<T,2> center,
|
||||
const dlib::vector<T,2> p,
|
||||
double angle
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- returns a point P such that:
|
||||
- P is the point p rotated counter-clockwise around the given
|
||||
center point 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)
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
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.
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user