From 44861dcbfeee041223c4aac1ee075e92fa4daa01 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 18 Sep 2016 12:42:15 +0200 Subject: update --- eigen/test/eigen2/eigen2_hyperplane.cpp | 126 ++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 eigen/test/eigen2/eigen2_hyperplane.cpp (limited to 'eigen/test/eigen2/eigen2_hyperplane.cpp') diff --git a/eigen/test/eigen2/eigen2_hyperplane.cpp b/eigen/test/eigen2/eigen2_hyperplane.cpp new file mode 100644 index 0000000..f3f85e1 --- /dev/null +++ b/eigen/test/eigen2/eigen2_hyperplane.cpp @@ -0,0 +1,126 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud +// Copyright (C) 2008 Benoit Jacob +// +// 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/. + +#include "main.h" +#include +#include +#include + +template void hyperplane(const HyperplaneType& _plane) +{ + /* this test covers the following files: + Hyperplane.h + */ + + const int dim = _plane.dim(); + typedef typename HyperplaneType::Scalar Scalar; + typedef typename NumTraits::Real RealScalar; + typedef Matrix VectorType; + typedef Matrix MatrixType; + + VectorType p0 = VectorType::Random(dim); + VectorType p1 = VectorType::Random(dim); + + VectorType n0 = VectorType::Random(dim).normalized(); + VectorType n1 = VectorType::Random(dim).normalized(); + + HyperplaneType pl0(n0, p0); + HyperplaneType pl1(n1, p1); + HyperplaneType pl2 = pl1; + + Scalar s0 = ei_random(); + Scalar s1 = ei_random(); + + VERIFY_IS_APPROX( n1.eigen2_dot(n1), Scalar(1) ); + + VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) ); + VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0 ); + 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) ); + + // transform + if (!NumTraits::IsComplex) + { + MatrixType rot = MatrixType::Random(dim,dim).qr().matrixQ(); + Scaling scaling(VectorType::Random()); + Translation translation(VectorType::Random()); + + pl2 = pl1; + VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) ); + pl2 = pl1; + VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) ); + pl2 = pl1; + VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) ); + pl2 = pl1; + VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation) + .absDistance((rot*scaling*translation) * p1), Scalar(1) ); + pl2 = pl1; + VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry) + .absDistance((rot*translation) * p1), Scalar(1) ); + } + + // casting + const int Dim = HyperplaneType::AmbientDimAtCompileTime; + typedef typename GetDifferentType::type OtherScalar; + Hyperplane hp1f = pl1.template cast(); + VERIFY_IS_APPROX(hp1f.template cast(),pl1); + Hyperplane hp1d = pl1.template cast(); + VERIFY_IS_APPROX(hp1d.template cast(),pl1); +} + +template void lines() +{ + typedef Hyperplane HLine; + typedef ParametrizedLine PLine; + typedef Matrix Vector; + typedef Matrix CoeffsType; + + for(int i = 0; i < 10; i++) + { + Vector center = Vector::Random(); + Vector u = Vector::Random(); + Vector v = Vector::Random(); + Scalar a = ei_random(); + while (ei_abs(a-1) < 1e-4) a = ei_random(); + while (u.norm() < 1e-4) u = Vector::Random(); + while (v.norm() < 1e-4) v = Vector::Random(); + + HLine line_u = HLine::Through(center + u, center + a*u); + HLine line_v = HLine::Through(center + v, center + a*v); + + // 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" + VERIFY_IS_APPROX(result, center); + + // check conversions between two types of lines + PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable + CoeffsType converted_coeffs(HLine(pl).coeffs()); + converted_coeffs *= line_u.coeffs()(0)/converted_coeffs(0); + VERIFY(line_u.coeffs().isApprox(converted_coeffs)); + } +} + +void test_eigen2_hyperplane() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_1( hyperplane(Hyperplane()) ); + CALL_SUBTEST_2( hyperplane(Hyperplane()) ); + CALL_SUBTEST_3( hyperplane(Hyperplane()) ); + CALL_SUBTEST_4( hyperplane(Hyperplane,5>()) ); + CALL_SUBTEST_5( lines() ); + CALL_SUBTEST_6( lines() ); + } +} -- cgit v1.2.3