blob: f26fc1329993f39b84edc8d4b863d299c29135d4 [file] [log] [blame]
Narayan Kamathc981c482012-11-02 10:59:05 +00001// 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
16template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
17{
18 /* this test covers the following files:
19 Hyperplane.h
20 */
21 typedef typename HyperplaneType::Index Index;
22 const Index dim = _plane.dim();
23 enum { Options = HyperplaneType::Options };
24 typedef typename HyperplaneType::Scalar Scalar;
Narayan Kamathc981c482012-11-02 10:59:05 +000025 typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
26 typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
27 HyperplaneType::AmbientDimAtCompileTime> MatrixType;
28
29 VectorType p0 = VectorType::Random(dim);
30 VectorType p1 = VectorType::Random(dim);
31
32 VectorType n0 = VectorType::Random(dim).normalized();
33 VectorType n1 = VectorType::Random(dim).normalized();
34
35 HyperplaneType pl0(n0, p0);
36 HyperplaneType pl1(n1, p1);
37 HyperplaneType pl2 = pl1;
38
39 Scalar s0 = internal::random<Scalar>();
40 Scalar s1 = internal::random<Scalar>();
41
42 VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
43
44 VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
45 VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0 );
46 VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
47 VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
48
49 // transform
50 if (!NumTraits<Scalar>::IsComplex)
51 {
52 MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
53 DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
54 Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
55
56 pl2 = pl1;
57 VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
58 pl2 = pl1;
59 VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
60 pl2 = pl1;
61 VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
62 pl2 = pl1;
63 VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
64 .absDistance((rot*scaling*translation) * p1), Scalar(1) );
65 pl2 = pl1;
66 VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
67 .absDistance((rot*translation) * p1), Scalar(1) );
68 }
69
70 // casting
71 const int Dim = HyperplaneType::AmbientDimAtCompileTime;
72 typedef typename GetDifferentType<Scalar>::type OtherScalar;
73 Hyperplane<OtherScalar,Dim,Options> hp1f = pl1.template cast<OtherScalar>();
74 VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
75 Hyperplane<Scalar,Dim,Options> hp1d = pl1.template cast<Scalar>();
76 VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
77}
78
79template<typename Scalar> void lines()
80{
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070081 using std::abs;
Narayan Kamathc981c482012-11-02 10:59:05 +000082 typedef Hyperplane<Scalar, 2> HLine;
83 typedef ParametrizedLine<Scalar, 2> PLine;
84 typedef Matrix<Scalar,2,1> Vector;
85 typedef Matrix<Scalar,3,1> CoeffsType;
86
87 for(int i = 0; i < 10; i++)
88 {
89 Vector center = Vector::Random();
90 Vector u = Vector::Random();
91 Vector v = Vector::Random();
92 Scalar a = internal::random<Scalar>();
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070093 while (abs(a-1) < 1e-4) a = internal::random<Scalar>();
Narayan Kamathc981c482012-11-02 10:59:05 +000094 while (u.norm() < 1e-4) u = Vector::Random();
95 while (v.norm() < 1e-4) v = Vector::Random();
96
97 HLine line_u = HLine::Through(center + u, center + a*u);
98 HLine line_v = HLine::Through(center + v, center + a*v);
99
100 // the line equations should be normalized so that a^2+b^2=1
101 VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
102 VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
103
104 Vector result = line_u.intersection(line_v);
105
106 // the lines should intersect at the point we called "center"
107 VERIFY_IS_APPROX(result, center);
108
109 // check conversions between two types of lines
110 PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable
111 CoeffsType converted_coeffs = HLine(pl).coeffs();
112 converted_coeffs *= (line_u.coeffs()[0])/(converted_coeffs[0]);
113 VERIFY(line_u.coeffs().isApprox(converted_coeffs));
114 }
115}
116
117template<typename Scalar> void hyperplane_alignment()
118{
119 typedef Hyperplane<Scalar,3,AutoAlign> Plane3a;
120 typedef Hyperplane<Scalar,3,DontAlign> Plane3u;
121
122 EIGEN_ALIGN16 Scalar array1[4];
123 EIGEN_ALIGN16 Scalar array2[4];
124 EIGEN_ALIGN16 Scalar array3[4+1];
125 Scalar* array3u = array3+1;
126
127 Plane3a *p1 = ::new(reinterpret_cast<void*>(array1)) Plane3a;
128 Plane3u *p2 = ::new(reinterpret_cast<void*>(array2)) Plane3u;
129 Plane3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Plane3u;
130
131 p1->coeffs().setRandom();
132 *p2 = *p1;
133 *p3 = *p1;
134
135 VERIFY_IS_APPROX(p1->coeffs(), p2->coeffs());
136 VERIFY_IS_APPROX(p1->coeffs(), p3->coeffs());
137
138 #if defined(EIGEN_VECTORIZE) && EIGEN_ALIGN_STATICALLY
139 if(internal::packet_traits<Scalar>::Vectorizable)
140 VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Plane3a));
141 #endif
142}
143
144
145void test_geo_hyperplane()
146{
147 for(int i = 0; i < g_repeat; i++) {
148 CALL_SUBTEST_1( hyperplane(Hyperplane<float,2>()) );
149 CALL_SUBTEST_2( hyperplane(Hyperplane<float,3>()) );
150 CALL_SUBTEST_2( hyperplane(Hyperplane<float,3,DontAlign>()) );
151 CALL_SUBTEST_2( hyperplane_alignment<float>() );
152 CALL_SUBTEST_3( hyperplane(Hyperplane<double,4>()) );
153 CALL_SUBTEST_4( hyperplane(Hyperplane<std::complex<double>,5>()) );
154 CALL_SUBTEST_1( lines<float>() );
155 CALL_SUBTEST_3( lines<double>() );
156 }
157}