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 (at) inria.fr> 5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1 (at) 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 16 template<typename LineType> void parametrizedline(const LineType& _line) 17 { 18 /* this test covers the following files: 19 ParametrizedLine.h 20 */ 21 typedef typename LineType::Index Index; 22 const Index dim = _line.dim(); 23 typedef typename LineType::Scalar Scalar; 24 typedef typename NumTraits<Scalar>::Real RealScalar; 25 typedef Matrix<Scalar, LineType::AmbientDimAtCompileTime, 1> VectorType; 26 typedef Matrix<Scalar, LineType::AmbientDimAtCompileTime, 27 LineType::AmbientDimAtCompileTime> MatrixType; 28 typedef Hyperplane<Scalar,LineType::AmbientDimAtCompileTime> HyperplaneType; 29 30 VectorType p0 = VectorType::Random(dim); 31 VectorType p1 = VectorType::Random(dim); 32 33 VectorType d0 = VectorType::Random(dim).normalized(); 34 35 LineType l0(p0, d0); 36 37 Scalar s0 = internal::random<Scalar>(); 38 Scalar s1 = internal::abs(internal::random<Scalar>()); 39 40 VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0), RealScalar(1) ); 41 VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0+s0*d0), RealScalar(1) ); 42 VERIFY_IS_APPROX( (l0.projection(p1)-p1).norm(), l0.distance(p1) ); 43 VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(l0.projection(p1)), RealScalar(1) ); 44 VERIFY_IS_APPROX( Scalar(l0.distance((p0+s0*d0) + d0.unitOrthogonal() * s1)), s1 ); 45 46 // casting 47 const int Dim = LineType::AmbientDimAtCompileTime; 48 typedef typename GetDifferentType<Scalar>::type OtherScalar; 49 ParametrizedLine<OtherScalar,Dim> hp1f = l0.template cast<OtherScalar>(); 50 VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),l0); 51 ParametrizedLine<Scalar,Dim> hp1d = l0.template cast<Scalar>(); 52 VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),l0); 53 54 // intersections 55 VectorType p2 = VectorType::Random(dim); 56 VectorType n2 = VectorType::Random(dim).normalized(); 57 HyperplaneType hp(p2,n2); 58 Scalar t = l0.intersectionParameter(hp); 59 VectorType pi = l0.pointAt(t); 60 VERIFY_IS_MUCH_SMALLER_THAN(hp.signedDistance(pi), RealScalar(1)); 61 VERIFY_IS_MUCH_SMALLER_THAN(l0.distance(pi), RealScalar(1)); 62 VERIFY_IS_APPROX(l0.intersectionPoint(hp), pi); 63 } 64 65 template<typename Scalar> void parametrizedline_alignment() 66 { 67 typedef ParametrizedLine<Scalar,4,AutoAlign> Line4a; 68 typedef ParametrizedLine<Scalar,4,DontAlign> Line4u; 69 70 EIGEN_ALIGN16 Scalar array1[8]; 71 EIGEN_ALIGN16 Scalar array2[8]; 72 EIGEN_ALIGN16 Scalar array3[8+1]; 73 Scalar* array3u = array3+1; 74 75 Line4a *p1 = ::new(reinterpret_cast<void*>(array1)) Line4a; 76 Line4u *p2 = ::new(reinterpret_cast<void*>(array2)) Line4u; 77 Line4u *p3 = ::new(reinterpret_cast<void*>(array3u)) Line4u; 78 79 p1->origin().setRandom(); 80 p1->direction().setRandom(); 81 *p2 = *p1; 82 *p3 = *p1; 83 84 VERIFY_IS_APPROX(p1->origin(), p2->origin()); 85 VERIFY_IS_APPROX(p1->origin(), p3->origin()); 86 VERIFY_IS_APPROX(p1->direction(), p2->direction()); 87 VERIFY_IS_APPROX(p1->direction(), p3->direction()); 88 89 #if defined(EIGEN_VECTORIZE) && EIGEN_ALIGN_STATICALLY 90 if(internal::packet_traits<Scalar>::Vectorizable) 91 VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Line4a)); 92 #endif 93 } 94 95 void test_geo_parametrizedline() 96 { 97 for(int i = 0; i < g_repeat; i++) { 98 CALL_SUBTEST_1( parametrizedline(ParametrizedLine<float,2>()) ); 99 CALL_SUBTEST_2( parametrizedline(ParametrizedLine<float,3>()) ); 100 CALL_SUBTEST_2( parametrizedline_alignment<float>() ); 101 CALL_SUBTEST_3( parametrizedline(ParametrizedLine<double,4>()) ); 102 CALL_SUBTEST_3( parametrizedline_alignment<double>() ); 103 CALL_SUBTEST_4( parametrizedline(ParametrizedLine<std::complex<double>,5>()) ); 104 } 105 } 106