Introduce EIGEN_PI, get rid of M_PI and acos(-1.0)

This commit is contained in:
Gael Guennebaud 2015-06-10 17:12:10 +02:00
parent 9756c7fb4d
commit d93ba137f2
6 changed files with 29 additions and 31 deletions

View File

@ -9,10 +9,6 @@
#include "LU"
#include <limits>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/** \defgroup Geometry_Module Geometry module
*
*

View File

@ -10,6 +10,9 @@
#ifndef EIGEN_MATHFUNCTIONS_H
#define EIGEN_MATHFUNCTIONS_H
// source: http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406
namespace Eigen {
// On WINCE, std::abs is defined for int only, so let's defined our own overloads:
@ -415,8 +418,7 @@ struct round_retval
EIGEN_DEVICE_FUNC
static inline RealScalar run(const Scalar& x)
{
const double pi = std::acos(-1.0);
return (x < 0.0) ? pi : 0.0; }
return (x < 0.0) ? EIGEN_PI : 0.0; }
};
template<typename Scalar>

View File

@ -55,7 +55,7 @@ MatrixBase<Derived>::eulerAngles(Index a0, Index a1, Index a2) const
res[0] = atan2(coeff(j,i), coeff(k,i));
if((odd && res[0]<Scalar(0)) || ((!odd) && res[0]>Scalar(0)))
{
res[0] = (res[0] > Scalar(0)) ? res[0] - Scalar(M_PI) : res[0] + Scalar(M_PI);
res[0] = (res[0] > Scalar(0)) ? res[0] - Scalar(EIGEN_PI) : res[0] + Scalar(EIGEN_PI);
Scalar s2 = Vector2(coeff(j,i), coeff(k,i)).norm();
res[1] = -atan2(s2, coeff(i,i));
}
@ -84,7 +84,7 @@ MatrixBase<Derived>::eulerAngles(Index a0, Index a1, Index a2) const
res[0] = atan2(coeff(j,k), coeff(k,k));
Scalar c2 = Vector2(coeff(i,i), coeff(i,j)).norm();
if((odd && res[0]<Scalar(0)) || ((!odd) && res[0]>Scalar(0))) {
res[0] = (res[0] > Scalar(0)) ? res[0] - Scalar(M_PI) : res[0] + Scalar(M_PI);
res[0] = (res[0] > Scalar(0)) ? res[0] - Scalar(EIGEN_PI) : res[0] + Scalar(EIGEN_PI);
res[1] = atan2(-coeff(i,k), -c2);
}
else

View File

@ -26,16 +26,16 @@ void verify_euler(const Matrix<Scalar,3,1>& ea, int i, int j, int k)
VERIFY_IS_APPROX(m, mbis);
/* If I==K, and ea[1]==0, then there no unique solution. */
/* The remark apply in the case where I!=K, and |ea[1]| is close to pi/2. */
if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(M_PI/2),test_precision<Scalar>())) )
if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision<Scalar>())) )
VERIFY((ea-eabis).norm() <= test_precision<Scalar>());
// approx_or_less_than does not work for 0
VERIFY(0 < eabis[0] || test_isMuchSmallerThan(eabis[0], Scalar(1)));
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[0], Scalar(M_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(M_PI), eabis[1]);
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[1], Scalar(M_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(M_PI), eabis[2]);
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[2], Scalar(M_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[0], Scalar(EIGEN_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[1]);
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[1], Scalar(EIGEN_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[2]);
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[2], Scalar(EIGEN_PI));
}
template<typename Scalar> void check_all_var(const Matrix<Scalar,3,1>& ea)
@ -64,7 +64,7 @@ template<typename Scalar> void eulerangles()
typedef Quaternion<Scalar> Quaternionx;
typedef AngleAxis<Scalar> AngleAxisx;
Scalar a = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
Quaternionx q1;
q1 = AngleAxisx(a, Vector3::Random().normalized());
Matrix3 m;
@ -84,13 +84,13 @@ template<typename Scalar> void eulerangles()
check_all_var(ea);
// Check with random angles in range [0:pi]x[-pi:pi]x[-pi:pi].
ea = (Array3::Random() + Array3(1,0,0))*Scalar(M_PI)*Array3(0.5,1,1);
ea = (Array3::Random() + Array3(1,0,0))*Scalar(EIGEN_PI)*Array3(0.5,1,1);
check_all_var(ea);
ea[2] = ea[0] = internal::random<Scalar>(0,Scalar(M_PI));
ea[2] = ea[0] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
check_all_var(ea);
ea[0] = ea[1] = internal::random<Scalar>(0,Scalar(M_PI));
ea[0] = ea[1] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
check_all_var(ea);
ea[1] = 0;

View File

@ -30,8 +30,8 @@ template<typename QuatType> void check_slerp(const QuatType& q0, const QuatType&
Scalar largeEps = test_precision<Scalar>();
Scalar theta_tot = AA(q1*q0.inverse()).angle();
if(theta_tot>M_PI)
theta_tot = Scalar(2.*M_PI)-theta_tot;
if(theta_tot>EIGEN_PI)
theta_tot = Scalar(2.*EIGEN_PI)-theta_tot;
for(Scalar t=0; t<=Scalar(1.001); t+=Scalar(0.1))
{
QuatType q = q0.slerp(t,q1);
@ -64,8 +64,8 @@ template<typename Scalar, int Options> void quaternion(void)
v2 = Vector3::Random(),
v3 = Vector3::Random();
Scalar a = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI)),
b = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI)),
b = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
// Quaternion: Identity(), setIdentity();
Quaternionx q1, q2;
@ -82,8 +82,8 @@ template<typename Scalar, int Options> void quaternion(void)
// angular distance
Scalar refangle = abs(AngleAxisx(q1.inverse()*q2).angle());
if (refangle>Scalar(M_PI))
refangle = Scalar(2)*Scalar(M_PI) - refangle;
if (refangle>Scalar(EIGEN_PI))
refangle = Scalar(2)*Scalar(EIGEN_PI) - refangle;
if((q1.coeffs()-q2.coeffs()).norm() > 10*largeEps)
{
@ -156,7 +156,7 @@ template<typename Scalar, int Options> void quaternion(void)
check_slerp(q1,q2);
q1 = AngleAxisx(b, v1.normalized());
q2 = AngleAxisx(b+Scalar(M_PI), v1.normalized());
q2 = AngleAxisx(b+Scalar(EIGEN_PI), v1.normalized());
check_slerp(q1,q2);
q1 = AngleAxisx(b, v1.normalized());
@ -179,7 +179,7 @@ template<typename Scalar> void mapQuaternion(void){
Vector3 v0 = Vector3::Random(),
v1 = Vector3::Random();
Scalar a = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
EIGEN_ALIGN_DEFAULT Scalar array1[4];
EIGEN_ALIGN_DEFAULT Scalar array2[4];

View File

@ -29,7 +29,7 @@ template<typename Scalar, int Mode, int Options> void non_projective_only()
Transform3 t0, t1, t2;
Scalar a = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
Quaternionx q1, q2;
@ -97,14 +97,14 @@ template<typename Scalar, int Mode, int Options> void transformations()
v1 = Vector3::Random();
Matrix3 matrot1, m;
Scalar a = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
Scalar s0 = internal::random<Scalar>(), s1 = internal::random<Scalar>();
while(v0.norm() < test_precision<Scalar>()) v0 = Vector3::Random();
while(v1.norm() < test_precision<Scalar>()) v1 = Vector3::Random();
VERIFY_IS_APPROX(v0, AngleAxisx(a, v0.normalized()) * v0);
VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(M_PI), v0.unitOrthogonal()) * v0);
VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(EIGEN_PI), v0.unitOrthogonal()) * v0);
if(abs(cos(a)) > test_precision<Scalar>())
{
VERIFY_IS_APPROX(cos(a)*v0.squaredNorm(), v0.dot(AngleAxisx(a, v0.unitOrthogonal()) * v0));
@ -156,7 +156,7 @@ template<typename Scalar, int Mode, int Options> void transformations()
// TODO complete the tests !
a = 0;
while (abs(a)<Scalar(0.1))
a = internal::random<Scalar>(-Scalar(0.4)*Scalar(M_PI), Scalar(0.4)*Scalar(M_PI));
a = internal::random<Scalar>(-Scalar(0.4)*Scalar(EIGEN_PI), Scalar(0.4)*Scalar(EIGEN_PI));
q1 = AngleAxisx(a, v0.normalized());
Transform3 t0, t1, t2;
@ -202,7 +202,7 @@ template<typename Scalar, int Mode, int Options> void transformations()
tmat4.matrix()(3,3) = Scalar(1);
VERIFY_IS_APPROX(tmat3.matrix(), tmat4.matrix());
Scalar a3 = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
Scalar a3 = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
Vector3 v3 = Vector3::Random().normalized();
AngleAxisx aa3(a3, v3);
Transform3 t3(aa3);