eigen2_hyperplane.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
00005 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 #include <Eigen/Geometry>
00028 #include <Eigen/LU>
00029 #include <Eigen/QR>
00030 
00031 template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
00032 {
00033   /* this test covers the following files:
00034      Hyperplane.h
00035   */
00036 
00037   const int dim = _plane.dim();
00038   typedef typename HyperplaneType::Scalar Scalar;
00039   typedef typename NumTraits<Scalar>::Real RealScalar;
00040   typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
00041   typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
00042                          HyperplaneType::AmbientDimAtCompileTime> MatrixType;
00043 
00044   VectorType p0 = VectorType::Random(dim);
00045   VectorType p1 = VectorType::Random(dim);
00046 
00047   VectorType n0 = VectorType::Random(dim).normalized();
00048   VectorType n1 = VectorType::Random(dim).normalized();
00049 
00050   HyperplaneType pl0(n0, p0);
00051   HyperplaneType pl1(n1, p1);
00052   HyperplaneType pl2 = pl1;
00053 
00054   Scalar s0 = ei_random<Scalar>();
00055   Scalar s1 = ei_random<Scalar>();
00056 
00057   VERIFY_IS_APPROX( n1.eigen2_dot(n1), Scalar(1) );
00058 
00059   VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
00060   VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0 );
00061   VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
00062   VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 +  pl1.normal().unitOrthogonal() * s1), Scalar(1) );
00063 
00064   // transform
00065   if (!NumTraits<Scalar>::IsComplex)
00066   {
00067     MatrixType rot = MatrixType::Random(dim,dim).qr().matrixQ();
00068     Scaling<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
00069     Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
00070 
00071     pl2 = pl1;
00072     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
00073     pl2 = pl1;
00074     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
00075     pl2 = pl1;
00076     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
00077     pl2 = pl1;
00078     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
00079                                  .absDistance((rot*scaling*translation) * p1), Scalar(1) );
00080     pl2 = pl1;
00081     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
00082                                  .absDistance((rot*translation) * p1), Scalar(1) );
00083   }
00084 
00085   // casting
00086   const int Dim = HyperplaneType::AmbientDimAtCompileTime;
00087   typedef typename GetDifferentType<Scalar>::type OtherScalar;
00088   Hyperplane<OtherScalar,Dim> hp1f = pl1.template cast<OtherScalar>();
00089   VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
00090   Hyperplane<Scalar,Dim> hp1d = pl1.template cast<Scalar>();
00091   VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
00092 }
00093 
00094 template<typename Scalar> void lines()
00095 {
00096   typedef Hyperplane<Scalar, 2> HLine;
00097   typedef ParametrizedLine<Scalar, 2> PLine;
00098   typedef Matrix<Scalar,2,1> Vector;
00099   typedef Matrix<Scalar,3,1> CoeffsType;
00100 
00101   for(int i = 0; i < 10; i++)
00102   {
00103     Vector center = Vector::Random();
00104     Vector u = Vector::Random();
00105     Vector v = Vector::Random();
00106     Scalar a = ei_random<Scalar>();
00107     while (ei_abs(a-1) < 1e-4) a = ei_random<Scalar>();
00108     while (u.norm() < 1e-4) u = Vector::Random();
00109     while (v.norm() < 1e-4) v = Vector::Random();
00110 
00111     HLine line_u = HLine::Through(center + u, center + a*u);
00112     HLine line_v = HLine::Through(center + v, center + a*v);
00113 
00114     // the line equations should be normalized so that a^2+b^2=1
00115     VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
00116     VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
00117 
00118     Vector result = line_u.intersection(line_v);
00119 
00120     // the lines should intersect at the point we called "center"
00121     VERIFY_IS_APPROX(result, center);
00122 
00123     // check conversions between two types of lines
00124     PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable
00125     CoeffsType converted_coeffs(HLine(pl).coeffs());
00126     converted_coeffs *= line_u.coeffs()(0)/converted_coeffs(0);
00127     VERIFY(line_u.coeffs().isApprox(converted_coeffs));
00128   }
00129 }
00130 
00131 void test_eigen2_hyperplane()
00132 {
00133   for(int i = 0; i < g_repeat; i++) {
00134     CALL_SUBTEST_1( hyperplane(Hyperplane<float,2>()) );
00135     CALL_SUBTEST_2( hyperplane(Hyperplane<float,3>()) );
00136     CALL_SUBTEST_3( hyperplane(Hyperplane<double,4>()) );
00137     CALL_SUBTEST_4( hyperplane(Hyperplane<std::complex<double>,5>()) );
00138     CALL_SUBTEST_5( lines<float>() );
00139     CALL_SUBTEST_6( lines<double>() );
00140   }
00141 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:38