geo_alignedbox.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 #include <Eigen/Geometry>
00027 #include <Eigen/LU>
00028 #include <Eigen/QR>
00029 
00030 #include<iostream>
00031 using namespace std;
00032 
00033 template<typename BoxType> void alignedbox(const BoxType& _box)
00034 {
00035   /* this test covers the following files:
00036      AlignedBox.h
00037   */
00038   typedef typename BoxType::Index Index;  
00039   typedef typename BoxType::Scalar Scalar;
00040   typedef typename NumTraits<Scalar>::Real RealScalar;
00041   typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
00042 
00043   const Index dim = _box.dim();
00044 
00045   VectorType p0 = VectorType::Random(dim);
00046   VectorType p1 = VectorType::Random(dim);
00047   while( p1 == p0 ){
00048       p1 =  VectorType::Random(dim); }
00049   RealScalar s1 = internal::random<RealScalar>(0,1);
00050 
00051   BoxType b0(dim);
00052   BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
00053   BoxType b2;
00054 
00055   b0.extend(p0);
00056   b0.extend(p1);
00057   VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
00058 
00059   (b2 = b0).extend(b1);
00060   VERIFY(b2.contains(b0));
00061   VERIFY(b2.contains(b1));
00062   VERIFY_IS_APPROX(b2.clamp(b0), b0);
00063 
00064 
00065   // alignment -- make sure there is no memory alignment assertion
00066   BoxType *bp0 = new BoxType(dim);
00067   BoxType *bp1 = new BoxType(dim);
00068   bp0->extend(*bp1);
00069   delete bp0;
00070   delete bp1;
00071 
00072   // sampling
00073   for( int i=0; i<10; ++i )
00074   {
00075       VectorType r = b0.sample();
00076       VERIFY(b0.contains(r));
00077   }
00078 
00079 }
00080 
00081 
00082 
00083 template<typename BoxType>
00084 void alignedboxCastTests(const BoxType& _box)
00085 {
00086   // casting  
00087   typedef typename BoxType::Index Index;
00088   typedef typename BoxType::Scalar Scalar;
00089   typedef typename NumTraits<Scalar>::Real RealScalar;
00090   typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
00091 
00092   const Index dim = _box.dim();
00093 
00094   VectorType p0 = VectorType::Random(dim);
00095   VectorType p1 = VectorType::Random(dim);
00096 
00097   BoxType b0(dim);
00098 
00099   b0.extend(p0);
00100   b0.extend(p1);
00101 
00102   const int Dim = BoxType::AmbientDimAtCompileTime;
00103   typedef typename GetDifferentType<Scalar>::type OtherScalar;
00104   AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
00105   VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
00106   AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
00107   VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
00108 }
00109 
00110 
00111 void specificTest1()
00112 {
00113     Vector2f m; m << -1.0f, -2.0f;
00114     Vector2f M; M <<  1.0f,  5.0f;
00115 
00116     typedef AlignedBox<float,2>  BoxType;
00117     BoxType box( m, M );
00118 
00119     Vector2f sides = M-m;
00120     VERIFY_IS_APPROX(sides, box.sizes() );
00121     VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
00122     VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
00123     VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
00124 
00125     VERIFY_IS_APPROX( 14.0f, box.volume() );
00126     VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
00127     VERIFY_IS_APPROX( internal::sqrt( 53.0f ), box.diagonal().norm() );
00128 
00129     VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
00130     VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
00131     Vector2f bottomRight; bottomRight << M[0], m[1];
00132     Vector2f topLeft; topLeft << m[0], M[1];
00133     VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
00134     VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
00135 }
00136 
00137 
00138 void specificTest2()
00139 {
00140     Vector3i m; m << -1, -2, 0;
00141     Vector3i M; M <<  1,  5, 3;
00142 
00143     typedef AlignedBox<int,3>  BoxType;
00144     BoxType box( m, M );
00145 
00146     Vector3i sides = M-m;
00147     VERIFY_IS_APPROX(sides, box.sizes() );
00148     VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
00149     VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
00150     VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
00151 
00152     VERIFY_IS_APPROX( 42, box.volume() );
00153     VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
00154 
00155     VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
00156     VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
00157     Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
00158     Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
00159     VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
00160     VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
00161 }
00162 
00163 
00164 void test_geo_alignedbox()
00165 {
00166   for(int i = 0; i < g_repeat; i++)
00167   {
00168     CALL_SUBTEST_1( alignedbox(AlignedBox<float,2>()) );
00169     CALL_SUBTEST_2( alignedboxCastTests(AlignedBox<float,2>()) );
00170 
00171     CALL_SUBTEST_3( alignedbox(AlignedBox<float,3>()) );
00172     CALL_SUBTEST_4( alignedboxCastTests(AlignedBox<float,3>()) );
00173 
00174     CALL_SUBTEST_5( alignedbox(AlignedBox<double,4>()) );
00175     CALL_SUBTEST_6( alignedboxCastTests(AlignedBox<double,4>()) );
00176 
00177     CALL_SUBTEST_7( alignedbox(AlignedBox<double,1>()) );
00178     CALL_SUBTEST_8( alignedboxCastTests(AlignedBox<double,1>()) );
00179 
00180     CALL_SUBTEST_9( alignedbox(AlignedBox<int,1>()) );
00181     CALL_SUBTEST_10( alignedbox(AlignedBox<int,2>()) );
00182     CALL_SUBTEST_11( alignedbox(AlignedBox<int,3>()) );
00183   }
00184   CALL_SUBTEST_12( specificTest1() );
00185   CALL_SUBTEST_13( specificTest2() );
00186 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:15