2008-08-29 01:44:27 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2008-08-29 01:44:27 +08:00
|
|
|
//
|
2010-06-25 05:21:58 +08:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2008-11-24 21:40:43 +08:00
|
|
|
// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-08-29 01:44:27 +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/.
|
2008-08-29 01:44:27 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <Eigen/Geometry>
|
|
|
|
#include <Eigen/LU>
|
2008-08-31 21:32:29 +08:00
|
|
|
#include <Eigen/QR>
|
2008-08-29 01:44:27 +08:00
|
|
|
|
2008-08-31 12:25:30 +08:00
|
|
|
template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
|
2008-08-29 01:44:27 +08:00
|
|
|
{
|
|
|
|
/* this test covers the following files:
|
2008-08-29 21:30:37 +08:00
|
|
|
Hyperplane.h
|
2008-08-29 01:44:27 +08:00
|
|
|
*/
|
2014-01-12 03:04:36 +08:00
|
|
|
using std::abs;
|
2010-06-21 17:36:00 +08:00
|
|
|
const Index dim = _plane.dim();
|
2011-01-28 00:17:06 +08:00
|
|
|
enum { Options = HyperplaneType::Options };
|
2008-08-31 12:25:30 +08:00
|
|
|
typedef typename HyperplaneType::Scalar Scalar;
|
2014-01-12 03:04:36 +08:00
|
|
|
typedef typename HyperplaneType::RealScalar RealScalar;
|
2008-08-31 12:25:30 +08:00
|
|
|
typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
|
2008-08-31 21:32:29 +08:00
|
|
|
typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
|
|
|
|
HyperplaneType::AmbientDimAtCompileTime> MatrixType;
|
2008-08-29 01:44:27 +08:00
|
|
|
|
|
|
|
VectorType p0 = VectorType::Random(dim);
|
|
|
|
VectorType p1 = VectorType::Random(dim);
|
|
|
|
|
|
|
|
VectorType n0 = VectorType::Random(dim).normalized();
|
|
|
|
VectorType n1 = VectorType::Random(dim).normalized();
|
2008-10-26 07:10:21 +08:00
|
|
|
|
2008-08-31 12:25:30 +08:00
|
|
|
HyperplaneType pl0(n0, p0);
|
|
|
|
HyperplaneType pl1(n1, p1);
|
2008-08-31 21:32:29 +08:00
|
|
|
HyperplaneType pl2 = pl1;
|
2008-08-29 01:44:27 +08:00
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
Scalar s0 = internal::random<Scalar>();
|
|
|
|
Scalar s1 = internal::random<Scalar>();
|
2008-08-29 01:44:27 +08:00
|
|
|
|
|
|
|
VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
|
2008-10-26 07:10:21 +08:00
|
|
|
|
2008-08-31 12:25:30 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
|
2014-01-12 03:04:36 +08:00
|
|
|
if(numext::abs2(s0)>RealScalar(1e-6))
|
|
|
|
VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0);
|
|
|
|
else
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( abs(pl1.signedDistance(p1 + n1 * s0) - s0), Scalar(1) );
|
2008-08-31 12:25:30 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
|
2008-08-31 21:32:29 +08:00
|
|
|
|
|
|
|
// transform
|
|
|
|
if (!NumTraits<Scalar>::IsComplex)
|
|
|
|
{
|
2009-12-03 00:11:09 +08:00
|
|
|
MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
|
2009-01-29 00:26:06 +08:00
|
|
|
DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
|
2008-08-31 21:32:29 +08:00
|
|
|
Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
|
2014-01-12 03:04:36 +08:00
|
|
|
|
|
|
|
while(scaling.diagonal().cwiseAbs().minCoeff()<RealScalar(1e-4)) scaling.diagonal() = VectorType::Random();
|
2008-10-26 07:10:21 +08:00
|
|
|
|
2008-08-31 21:32:29 +08:00
|
|
|
pl2 = pl1;
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
|
|
|
|
pl2 = pl1;
|
2008-09-02 01:14:34 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
|
2008-08-31 21:32:29 +08:00
|
|
|
pl2 = pl1;
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
|
2016-12-20 16:35:00 +08:00
|
|
|
VERIFY_IS_APPROX( pl2.normal().norm(), RealScalar(1) );
|
2008-08-31 21:32:29 +08:00
|
|
|
pl2 = pl1;
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
|
2014-01-12 03:04:36 +08:00
|
|
|
.absDistance((rot*scaling*translation) * p1), Scalar(1) );
|
2016-12-20 16:35:00 +08:00
|
|
|
VERIFY_IS_APPROX( pl2.normal().norm(), RealScalar(1) );
|
2008-08-31 21:32:29 +08:00
|
|
|
pl2 = pl1;
|
2008-09-02 01:14:34 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
|
2008-08-31 21:32:29 +08:00
|
|
|
.absDistance((rot*translation) * p1), Scalar(1) );
|
2016-12-20 16:35:00 +08:00
|
|
|
VERIFY_IS_APPROX( pl2.normal().norm(), RealScalar(1) );
|
2008-08-31 21:32:29 +08:00
|
|
|
}
|
2008-10-26 07:10:21 +08:00
|
|
|
|
|
|
|
// casting
|
|
|
|
const int Dim = HyperplaneType::AmbientDimAtCompileTime;
|
|
|
|
typedef typename GetDifferentType<Scalar>::type OtherScalar;
|
2011-01-28 00:17:06 +08:00
|
|
|
Hyperplane<OtherScalar,Dim,Options> hp1f = pl1.template cast<OtherScalar>();
|
2008-10-26 07:10:21 +08:00
|
|
|
VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
|
2011-01-28 00:17:06 +08:00
|
|
|
Hyperplane<Scalar,Dim,Options> hp1d = pl1.template cast<Scalar>();
|
2008-10-26 07:10:21 +08:00
|
|
|
VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
|
2008-08-31 12:25:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Scalar> void lines()
|
|
|
|
{
|
2012-11-06 22:25:50 +08:00
|
|
|
using std::abs;
|
2008-08-31 12:25:30 +08:00
|
|
|
typedef Hyperplane<Scalar, 2> HLine;
|
|
|
|
typedef ParametrizedLine<Scalar, 2> PLine;
|
|
|
|
typedef Matrix<Scalar,2,1> Vector;
|
|
|
|
typedef Matrix<Scalar,3,1> CoeffsType;
|
|
|
|
|
|
|
|
for(int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
Vector center = Vector::Random();
|
|
|
|
Vector u = Vector::Random();
|
|
|
|
Vector v = Vector::Random();
|
2010-10-25 22:15:22 +08:00
|
|
|
Scalar a = internal::random<Scalar>();
|
2016-05-05 19:35:45 +08:00
|
|
|
while (abs(a-1) < Scalar(1e-4)) a = internal::random<Scalar>();
|
|
|
|
while (u.norm() < Scalar(1e-4)) u = Vector::Random();
|
|
|
|
while (v.norm() < Scalar(1e-4)) v = Vector::Random();
|
2008-08-31 12:25:30 +08:00
|
|
|
|
|
|
|
HLine line_u = HLine::Through(center + u, center + a*u);
|
|
|
|
HLine line_v = HLine::Through(center + v, center + a*v);
|
2008-08-29 01:44:27 +08:00
|
|
|
|
2008-08-31 12:25:30 +08:00
|
|
|
// the line equations should be normalized so that a^2+b^2=1
|
|
|
|
VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
|
|
|
|
VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
|
|
|
|
|
|
|
|
Vector result = line_u.intersection(line_v);
|
|
|
|
|
|
|
|
// the lines should intersect at the point we called "center"
|
2016-05-05 19:35:45 +08:00
|
|
|
if(abs(a-1) > Scalar(1e-2) && abs(v.normalized().dot(u.normalized()))<Scalar(0.9))
|
2014-01-12 03:04:36 +08:00
|
|
|
VERIFY_IS_APPROX(result, center);
|
2008-08-31 12:25:30 +08:00
|
|
|
|
|
|
|
// check conversions between two types of lines
|
2009-10-23 03:56:10 +08:00
|
|
|
PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable
|
2014-01-12 03:04:36 +08:00
|
|
|
HLine line_u2(pl);
|
|
|
|
CoeffsType converted_coeffs = line_u2.coeffs();
|
2016-05-05 19:35:45 +08:00
|
|
|
if(line_u2.normal().dot(line_u.normal())<Scalar(0))
|
2014-01-12 03:04:36 +08:00
|
|
|
converted_coeffs = -line_u2.coeffs();
|
2008-08-31 12:25:30 +08:00
|
|
|
VERIFY(line_u.coeffs().isApprox(converted_coeffs));
|
|
|
|
}
|
2008-08-29 01:44:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 22:01:53 +08:00
|
|
|
template<typename Scalar> void planes()
|
|
|
|
{
|
|
|
|
using std::abs;
|
|
|
|
typedef Hyperplane<Scalar, 3> Plane;
|
|
|
|
typedef Matrix<Scalar,3,1> Vector;
|
|
|
|
|
|
|
|
for(int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
Vector v0 = Vector::Random();
|
|
|
|
Vector v1(v0), v2(v0);
|
|
|
|
if(internal::random<double>(0,1)>0.25)
|
|
|
|
v1 += Vector::Random();
|
|
|
|
if(internal::random<double>(0,1)>0.25)
|
|
|
|
v2 += v1 * std::pow(internal::random<Scalar>(0,1),internal::random<int>(1,16));
|
|
|
|
if(internal::random<double>(0,1)>0.25)
|
|
|
|
v2 += Vector::Random() * std::pow(internal::random<Scalar>(0,1),internal::random<int>(1,16));
|
|
|
|
|
|
|
|
Plane p0 = Plane::Through(v0, v1, v2);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(p0.normal().norm(), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v0), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v1), Scalar(1));
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v2), Scalar(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 00:17:06 +08:00
|
|
|
template<typename Scalar> void hyperplane_alignment()
|
|
|
|
{
|
|
|
|
typedef Hyperplane<Scalar,3,AutoAlign> Plane3a;
|
|
|
|
typedef Hyperplane<Scalar,3,DontAlign> Plane3u;
|
|
|
|
|
2015-07-29 17:11:23 +08:00
|
|
|
EIGEN_ALIGN_MAX Scalar array1[4];
|
|
|
|
EIGEN_ALIGN_MAX Scalar array2[4];
|
|
|
|
EIGEN_ALIGN_MAX Scalar array3[4+1];
|
2011-01-28 00:17:06 +08:00
|
|
|
Scalar* array3u = array3+1;
|
|
|
|
|
|
|
|
Plane3a *p1 = ::new(reinterpret_cast<void*>(array1)) Plane3a;
|
|
|
|
Plane3u *p2 = ::new(reinterpret_cast<void*>(array2)) Plane3u;
|
|
|
|
Plane3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Plane3u;
|
|
|
|
|
|
|
|
p1->coeffs().setRandom();
|
|
|
|
*p2 = *p1;
|
|
|
|
*p3 = *p1;
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(p1->coeffs(), p2->coeffs());
|
|
|
|
VERIFY_IS_APPROX(p1->coeffs(), p3->coeffs());
|
|
|
|
|
2015-07-29 17:11:23 +08:00
|
|
|
#if defined(EIGEN_VECTORIZE) && EIGEN_MAX_STATIC_ALIGN_BYTES > 0
|
2014-03-27 22:29:56 +08:00
|
|
|
if(internal::packet_traits<Scalar>::Vectorizable && internal::packet_traits<Scalar>::size<=4)
|
2011-05-20 15:59:15 +08:00
|
|
|
VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Plane3a));
|
2011-01-28 00:17:06 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(geo_hyperplane)
|
2008-08-29 01:44:27 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_1( hyperplane(Hyperplane<float,2>()) );
|
|
|
|
CALL_SUBTEST_2( hyperplane(Hyperplane<float,3>()) );
|
2011-01-28 00:17:06 +08:00
|
|
|
CALL_SUBTEST_2( hyperplane(Hyperplane<float,3,DontAlign>()) );
|
|
|
|
CALL_SUBTEST_2( hyperplane_alignment<float>() );
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_3( hyperplane(Hyperplane<double,4>()) );
|
|
|
|
CALL_SUBTEST_4( hyperplane(Hyperplane<std::complex<double>,5>()) );
|
|
|
|
CALL_SUBTEST_1( lines<float>() );
|
2010-02-15 18:00:30 +08:00
|
|
|
CALL_SUBTEST_3( lines<double>() );
|
2014-11-26 22:01:53 +08:00
|
|
|
CALL_SUBTEST_2( planes<float>() );
|
|
|
|
CALL_SUBTEST_5( planes<double>() );
|
2008-08-29 01:44:27 +08:00
|
|
|
}
|
|
|
|
}
|