Commit 4dfe33e0 authored by Mike Kremer's avatar Mike Kremer
Browse files

Added geometry function to compute the rotation axis as wellas the rotation...

Added geometry function to compute the rotation axis as wellas the rotation angle of two given vectors.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@13029 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 0c89d3e3
......@@ -801,6 +801,46 @@ circumRadiusSquared( const VectorT<Scalar,3>& _v0,
//-----------------------------------------------------------------------------
template<typename Vec>
bool
rotationOfTwoVectors( const Vec& _v0,
const Vec& _v1,
Vec& _axis,
typename Vec::Scalar& _angle ) {
// Normalize axes
Vec v0 = _v0.normalize();
Vec v1 = _v1.normalize();
// Get rotation axis
_axis = (v0 % v1).normalize();
// Is nan?
if (_axis != _axis) {
return false;
}
// Get rotation angle (in radiant)
_angle = acos(v0 | v1);
// Convert to degree
_angle *= 180 / M_PI;
// Is nan?
if (_angle != _angle)
_angle = 0.0;
// Get orientation of right-handed coordinates system
// to determine whether we rotated clockwise
// or counter-clockwise (w.r.t. rotation axis)
if (determinant(v0, v1, _axis) >= 0)
_angle *= -1.0;
return true;
}
//-----------------------------------------------------------------------------
template<class VectorT>
int isObtuse(const VectorT& _p0,
const VectorT& _p1,
......
......@@ -96,6 +96,50 @@ circumRadius( const VectorT<Scalar,3>& _v0,
}
/** \brief Get rotation axis and signed angle of rotation between two vectors
*
* Get two vectors, _v0 and _v1, and compute rotation axis _v0 % _v1
* as well the angle between _v0 and _v1. Note that the angle is negative
* if the counter-clockwise rotation of _v1 with respect to the rotation axis is
* smaller than the same rotation in clockwise direction.
* The following illustration depicts this. The viewing direction
* is aligned to the negative rotation axis.
*
* v1 v0
* _ _
* |\ /|
* \-a->/ => a is negative
* \ /
* \/
*
* v0 v1
* _ _
* |\ /|
* \-a->/ => a is positive
* \ /
* \/
* @param _v0 The first vector
* @param _v1 The second vector
* @param _axis A reference to a vector in which the rotation axis is stored
* @param _angle A reference to a scalar type in which the signed angle is stored
*/
template<typename Vec>
bool
rotationOfTwoVectors( const Vec& _v0,
const Vec& _v1,
Vec& _axis,
typename Vec::Scalar& _angle );
/** \brief Get determinant of matrix [_v0, _v1, _v2]
*/
template<typename Scalar>
Scalar
determinant(const VectorT<Scalar,3>& _v1, const VectorT<Scalar,3>& _v2, const VectorT<Scalar,3>& _v3) {
return (_v1[0]*_v2[1]*_v3[2] + _v2[0]*_v3[1]*_v1[2] + _v3[0]*_v1[1]*_v2[2] -
_v3[0]*_v2[1]*_v1[2] - _v2[0]*_v1[1]*_v3[2] - _v1[0]*_v3[1]*_v2[2]);
}
/** \brief find a vector that's perpendicular to _v
*
* This function takes a vector and generates a new arbitrary
......
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