2009-02-17 17:53:05 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2009-02-17 17:53:05 +08:00
|
|
|
//
|
2010-06-25 05:21:58 +08:00
|
|
|
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-02-17 17:53:05 +08:00
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-02-17 17:53:05 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <Eigen/Geometry>
|
|
|
|
#include <Eigen/LU>
|
|
|
|
#include <Eigen/SVD>
|
|
|
|
|
|
|
|
/* this test covers the following files:
|
|
|
|
Geometry/OrthoMethods.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename Scalar> void orthomethods_3()
|
|
|
|
{
|
2011-01-27 18:33:37 +08:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2009-02-17 17:53:05 +08:00
|
|
|
typedef Matrix<Scalar,3,3> Matrix3;
|
|
|
|
typedef Matrix<Scalar,3,1> Vector3;
|
|
|
|
|
2009-03-11 22:20:36 +08:00
|
|
|
typedef Matrix<Scalar,4,1> Vector4;
|
|
|
|
|
2009-02-17 17:53:05 +08:00
|
|
|
Vector3 v0 = Vector3::Random(),
|
|
|
|
v1 = Vector3::Random(),
|
|
|
|
v2 = Vector3::Random();
|
|
|
|
|
|
|
|
// cross product
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).dot(v1), Scalar(1));
|
2011-01-27 18:33:37 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v1.dot(v1.cross(v2)), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).dot(v2), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v2.dot(v1.cross(v2)), Scalar(1));
|
2014-08-01 20:47:33 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(Vector3::Random()).dot(v1), Scalar(1));
|
2009-02-17 17:53:05 +08:00
|
|
|
Matrix3 mat3;
|
|
|
|
mat3 << v0.normalized(),
|
|
|
|
(v0.cross(v1)).normalized(),
|
|
|
|
(v0.cross(v1).cross(v0)).normalized();
|
|
|
|
VERIFY(mat3.isUnitary());
|
2014-09-24 15:39:09 +08:00
|
|
|
|
|
|
|
mat3.setRandom();
|
|
|
|
VERIFY_IS_APPROX(v0.cross(mat3*v1), -(mat3*v1).cross(v0));
|
|
|
|
VERIFY_IS_APPROX(v0.cross(mat3.lazyProduct(v1)), -(mat3.lazyProduct(v1)).cross(v0));
|
2009-02-17 17:53:05 +08:00
|
|
|
|
|
|
|
// colwise/rowwise cross product
|
|
|
|
mat3.setRandom();
|
|
|
|
Vector3 vec3 = Vector3::Random();
|
|
|
|
Matrix3 mcross;
|
2010-10-25 22:15:22 +08:00
|
|
|
int i = internal::random<int>(0,2);
|
2009-02-17 17:53:05 +08:00
|
|
|
mcross = mat3.colwise().cross(vec3);
|
|
|
|
VERIFY_IS_APPROX(mcross.col(i), mat3.col(i).cross(vec3));
|
2014-08-01 20:47:33 +08:00
|
|
|
|
2014-08-01 22:26:23 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN((mat3.adjoint() * mat3.colwise().cross(vec3)).diagonal().cwiseAbs().sum(), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN((mat3.adjoint() * mat3.colwise().cross(Vector3::Random())).diagonal().cwiseAbs().sum(), Scalar(1));
|
2014-08-01 20:47:33 +08:00
|
|
|
|
2014-08-01 22:26:23 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN((vec3.adjoint() * mat3.colwise().cross(vec3)).cwiseAbs().sum(), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN((vec3.adjoint() * Matrix3::Random().colwise().cross(vec3)).cwiseAbs().sum(), Scalar(1));
|
2014-08-01 20:47:33 +08:00
|
|
|
|
2009-02-17 17:53:05 +08:00
|
|
|
mcross = mat3.rowwise().cross(vec3);
|
|
|
|
VERIFY_IS_APPROX(mcross.row(i), mat3.row(i).cross(vec3));
|
|
|
|
|
2009-03-11 22:20:36 +08:00
|
|
|
// cross3
|
|
|
|
Vector4 v40 = Vector4::Random(),
|
|
|
|
v41 = Vector4::Random(),
|
|
|
|
v42 = Vector4::Random();
|
|
|
|
v40.w() = v41.w() = v42.w() = 0;
|
2010-01-05 10:24:43 +08:00
|
|
|
v42.template head<3>() = v40.template head<3>().cross(v41.template head<3>());
|
2009-03-11 22:20:36 +08:00
|
|
|
VERIFY_IS_APPROX(v40.cross3(v41), v42);
|
2014-08-01 20:47:33 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v40.cross3(Vector4::Random()).dot(v40), Scalar(1));
|
2011-01-27 18:33:37 +08:00
|
|
|
|
|
|
|
// check mixed product
|
|
|
|
typedef Matrix<RealScalar, 3, 1> RealVector3;
|
|
|
|
RealVector3 rv1 = RealVector3::Random();
|
|
|
|
VERIFY_IS_APPROX(v1.cross(rv1.template cast<Scalar>()), v1.cross(rv1));
|
|
|
|
VERIFY_IS_APPROX(rv1.template cast<Scalar>().cross(v1), rv1.cross(v1));
|
2009-02-17 17:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Scalar, int Size> void orthomethods(int size=Size)
|
|
|
|
{
|
2009-02-26 18:09:23 +08:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2009-02-17 17:53:05 +08:00
|
|
|
typedef Matrix<Scalar,Size,1> VectorType;
|
|
|
|
typedef Matrix<Scalar,3,Size> Matrix3N;
|
|
|
|
typedef Matrix<Scalar,Size,3> MatrixN3;
|
|
|
|
typedef Matrix<Scalar,3,1> Vector3;
|
|
|
|
|
2011-11-06 06:30:49 +08:00
|
|
|
VectorType v0 = VectorType::Random(size);
|
2009-02-17 17:53:05 +08:00
|
|
|
|
|
|
|
// unitOrthogonal
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().dot(v0), Scalar(1));
|
2009-02-26 18:09:23 +08:00
|
|
|
VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), RealScalar(1));
|
2009-02-17 17:53:05 +08:00
|
|
|
|
2009-09-07 18:53:25 +08:00
|
|
|
if (size>=3)
|
2009-02-17 17:57:32 +08:00
|
|
|
{
|
2010-01-05 10:24:43 +08:00
|
|
|
v0.template head<2>().setZero();
|
|
|
|
v0.tail(size-2).setRandom();
|
2009-02-17 17:57:32 +08:00
|
|
|
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().dot(v0), Scalar(1));
|
2009-02-26 18:09:23 +08:00
|
|
|
VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), RealScalar(1));
|
2009-02-17 17:57:32 +08:00
|
|
|
}
|
|
|
|
|
2009-02-17 17:53:05 +08:00
|
|
|
// colwise/rowwise cross product
|
|
|
|
Vector3 vec3 = Vector3::Random();
|
2010-10-25 22:15:22 +08:00
|
|
|
int i = internal::random<int>(0,size-1);
|
2009-02-17 17:53:05 +08:00
|
|
|
|
|
|
|
Matrix3N mat3N(3,size), mcross3N(3,size);
|
|
|
|
mat3N.setRandom();
|
|
|
|
mcross3N = mat3N.colwise().cross(vec3);
|
|
|
|
VERIFY_IS_APPROX(mcross3N.col(i), mat3N.col(i).cross(vec3));
|
|
|
|
|
|
|
|
MatrixN3 matN3(size,3), mcrossN3(size,3);
|
|
|
|
matN3.setRandom();
|
|
|
|
mcrossN3 = matN3.rowwise().cross(vec3);
|
|
|
|
VERIFY_IS_APPROX(mcrossN3.row(i), matN3.row(i).cross(vec3));
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(geo_orthomethods)
|
2009-02-17 17:53:05 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_1( orthomethods_3<float>() );
|
|
|
|
CALL_SUBTEST_2( orthomethods_3<double>() );
|
2011-01-27 18:33:37 +08:00
|
|
|
CALL_SUBTEST_4( orthomethods_3<std::complex<double> >() );
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_1( (orthomethods<float,2>()) );
|
|
|
|
CALL_SUBTEST_2( (orthomethods<double,2>()) );
|
|
|
|
CALL_SUBTEST_1( (orthomethods<float,3>()) );
|
|
|
|
CALL_SUBTEST_2( (orthomethods<double,3>()) );
|
|
|
|
CALL_SUBTEST_3( (orthomethods<float,7>()) );
|
|
|
|
CALL_SUBTEST_4( (orthomethods<std::complex<double>,8>()) );
|
|
|
|
CALL_SUBTEST_5( (orthomethods<float,Dynamic>(36)) );
|
|
|
|
CALL_SUBTEST_6( (orthomethods<double,Dynamic>(35)) );
|
2009-02-17 17:53:05 +08:00
|
|
|
}
|
|
|
|
}
|