• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #include "main.h"
12 #include <Eigen/Geometry>
13 #include <Eigen/LU>
14 #include <Eigen/QR>
15 
hyperplane(const HyperplaneType & _plane)16 template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
17 {
18   /* this test covers the following files:
19      Hyperplane.h
20   */
21   using std::abs;
22   typedef typename HyperplaneType::Index Index;
23   const Index dim = _plane.dim();
24   enum { Options = HyperplaneType::Options };
25   typedef typename HyperplaneType::Scalar Scalar;
26   typedef typename HyperplaneType::RealScalar RealScalar;
27   typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
28   typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
29                          HyperplaneType::AmbientDimAtCompileTime> MatrixType;
30 
31   VectorType p0 = VectorType::Random(dim);
32   VectorType p1 = VectorType::Random(dim);
33 
34   VectorType n0 = VectorType::Random(dim).normalized();
35   VectorType n1 = VectorType::Random(dim).normalized();
36 
37   HyperplaneType pl0(n0, p0);
38   HyperplaneType pl1(n1, p1);
39   HyperplaneType pl2 = pl1;
40 
41   Scalar s0 = internal::random<Scalar>();
42   Scalar s1 = internal::random<Scalar>();
43 
44   VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
45 
46   VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
47   if(numext::abs2(s0)>RealScalar(1e-6))
48     VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0);
49   else
50     VERIFY_IS_MUCH_SMALLER_THAN( abs(pl1.signedDistance(p1 + n1 * s0) - s0), Scalar(1) );
51   VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
52   VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 +  pl1.normal().unitOrthogonal() * s1), Scalar(1) );
53 
54   // transform
55   if (!NumTraits<Scalar>::IsComplex)
56   {
57     MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
58     DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
59     Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
60 
61     while(scaling.diagonal().cwiseAbs().minCoeff()<RealScalar(1e-4)) scaling.diagonal() = VectorType::Random();
62 
63     pl2 = pl1;
64     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
65     pl2 = pl1;
66     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
67     pl2 = pl1;
68     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
69     VERIFY_IS_APPROX( pl2.normal().norm(), RealScalar(1) );
70     pl2 = pl1;
71     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
72                                   .absDistance((rot*scaling*translation) * p1), Scalar(1) );
73     VERIFY_IS_APPROX( pl2.normal().norm(), RealScalar(1) );
74     pl2 = pl1;
75     VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
76                                  .absDistance((rot*translation) * p1), Scalar(1) );
77     VERIFY_IS_APPROX( pl2.normal().norm(), RealScalar(1) );
78   }
79 
80   // casting
81   const int Dim = HyperplaneType::AmbientDimAtCompileTime;
82   typedef typename GetDifferentType<Scalar>::type OtherScalar;
83   Hyperplane<OtherScalar,Dim,Options> hp1f = pl1.template cast<OtherScalar>();
84   VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
85   Hyperplane<Scalar,Dim,Options> hp1d = pl1.template cast<Scalar>();
86   VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
87 }
88 
lines()89 template<typename Scalar> void lines()
90 {
91   using std::abs;
92   typedef Hyperplane<Scalar, 2> HLine;
93   typedef ParametrizedLine<Scalar, 2> PLine;
94   typedef Matrix<Scalar,2,1> Vector;
95   typedef Matrix<Scalar,3,1> CoeffsType;
96 
97   for(int i = 0; i < 10; i++)
98   {
99     Vector center = Vector::Random();
100     Vector u = Vector::Random();
101     Vector v = Vector::Random();
102     Scalar a = internal::random<Scalar>();
103     while (abs(a-1) < Scalar(1e-4)) a = internal::random<Scalar>();
104     while (u.norm() < Scalar(1e-4)) u = Vector::Random();
105     while (v.norm() < Scalar(1e-4)) v = Vector::Random();
106 
107     HLine line_u = HLine::Through(center + u, center + a*u);
108     HLine line_v = HLine::Through(center + v, center + a*v);
109 
110     // the line equations should be normalized so that a^2+b^2=1
111     VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
112     VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
113 
114     Vector result = line_u.intersection(line_v);
115 
116     // the lines should intersect at the point we called "center"
117     if(abs(a-1) > Scalar(1e-2) && abs(v.normalized().dot(u.normalized()))<Scalar(0.9))
118       VERIFY_IS_APPROX(result, center);
119 
120     // check conversions between two types of lines
121     PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable
122     HLine line_u2(pl);
123     CoeffsType converted_coeffs = line_u2.coeffs();
124     if(line_u2.normal().dot(line_u.normal())<Scalar(0))
125       converted_coeffs = -line_u2.coeffs();
126     VERIFY(line_u.coeffs().isApprox(converted_coeffs));
127   }
128 }
129 
planes()130 template<typename Scalar> void planes()
131 {
132   using std::abs;
133   typedef Hyperplane<Scalar, 3> Plane;
134   typedef Matrix<Scalar,3,1> Vector;
135 
136   for(int i = 0; i < 10; i++)
137   {
138     Vector v0 = Vector::Random();
139     Vector v1(v0), v2(v0);
140     if(internal::random<double>(0,1)>0.25)
141       v1 += Vector::Random();
142     if(internal::random<double>(0,1)>0.25)
143       v2 += v1 * std::pow(internal::random<Scalar>(0,1),internal::random<int>(1,16));
144     if(internal::random<double>(0,1)>0.25)
145       v2 += Vector::Random() * std::pow(internal::random<Scalar>(0,1),internal::random<int>(1,16));
146 
147     Plane p0 = Plane::Through(v0, v1, v2);
148 
149     VERIFY_IS_APPROX(p0.normal().norm(), Scalar(1));
150     VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v0), Scalar(1));
151     VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v1), Scalar(1));
152     VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v2), Scalar(1));
153   }
154 }
155 
hyperplane_alignment()156 template<typename Scalar> void hyperplane_alignment()
157 {
158   typedef Hyperplane<Scalar,3,AutoAlign> Plane3a;
159   typedef Hyperplane<Scalar,3,DontAlign> Plane3u;
160 
161   EIGEN_ALIGN_MAX Scalar array1[4];
162   EIGEN_ALIGN_MAX Scalar array2[4];
163   EIGEN_ALIGN_MAX Scalar array3[4+1];
164   Scalar* array3u = array3+1;
165 
166   Plane3a *p1 = ::new(reinterpret_cast<void*>(array1)) Plane3a;
167   Plane3u *p2 = ::new(reinterpret_cast<void*>(array2)) Plane3u;
168   Plane3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Plane3u;
169 
170   p1->coeffs().setRandom();
171   *p2 = *p1;
172   *p3 = *p1;
173 
174   VERIFY_IS_APPROX(p1->coeffs(), p2->coeffs());
175   VERIFY_IS_APPROX(p1->coeffs(), p3->coeffs());
176 
177   #if defined(EIGEN_VECTORIZE) && EIGEN_MAX_STATIC_ALIGN_BYTES > 0
178   if(internal::packet_traits<Scalar>::Vectorizable && internal::packet_traits<Scalar>::size<=4)
179     VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Plane3a));
180   #endif
181 }
182 
183 
test_geo_hyperplane()184 void test_geo_hyperplane()
185 {
186   for(int i = 0; i < g_repeat; i++) {
187     CALL_SUBTEST_1( hyperplane(Hyperplane<float,2>()) );
188     CALL_SUBTEST_2( hyperplane(Hyperplane<float,3>()) );
189     CALL_SUBTEST_2( hyperplane(Hyperplane<float,3,DontAlign>()) );
190     CALL_SUBTEST_2( hyperplane_alignment<float>() );
191     CALL_SUBTEST_3( hyperplane(Hyperplane<double,4>()) );
192     CALL_SUBTEST_4( hyperplane(Hyperplane<std::complex<double>,5>()) );
193     CALL_SUBTEST_1( lines<float>() );
194     CALL_SUBTEST_3( lines<double>() );
195     CALL_SUBTEST_2( planes<float>() );
196     CALL_SUBTEST_5( planes<double>() );
197   }
198 }
199