geo_alignedbox.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 #include <Eigen/Geometry>
12 #include <Eigen/LU>
13 #include <Eigen/QR>
14 
15 #include<iostream>
16 using namespace std;
17 
18 template<typename T> EIGEN_DONT_INLINE
19 void kill_extra_precision(T& x) { eigen_assert((void*)(&x) != (void*)0); }
20 
21 
22 template<typename BoxType> void alignedbox(const BoxType& _box)
23 {
24  /* this test covers the following files:
25  AlignedBox.h
26  */
27  typedef typename BoxType::Scalar Scalar;
28  typedef typename NumTraits<Scalar>::Real RealScalar;
30 
31  const Index dim = _box.dim();
32 
33  VectorType p0 = VectorType::Random(dim);
34  VectorType p1 = VectorType::Random(dim);
35  while( p1 == p0 ){
36  p1 = VectorType::Random(dim); }
37  RealScalar s1 = internal::random<RealScalar>(0,1);
38 
39  BoxType b0(dim);
40  BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
41  BoxType b2;
42 
46 
47  b0.extend(p0);
48  b0.extend(p1);
49  VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
50  VERIFY(b0.contains(b0.center()));
51  VERIFY_IS_APPROX(b0.center(),(p0+p1)/Scalar(2));
52 
53  (b2 = b0).extend(b1);
54  VERIFY(b2.contains(b0));
55  VERIFY(b2.contains(b1));
56  VERIFY_IS_APPROX(b2.clamp(b0), b0);
57 
58  // intersection
59  BoxType box1(VectorType::Random(dim));
60  box1.extend(VectorType::Random(dim));
61  BoxType box2(VectorType::Random(dim));
62  box2.extend(VectorType::Random(dim));
63 
64  VERIFY(box1.intersects(box2) == !box1.intersection(box2).isEmpty());
65 
66  // alignment -- make sure there is no memory alignment assertion
67  BoxType *bp0 = new BoxType(dim);
68  BoxType *bp1 = new BoxType(dim);
69  bp0->extend(*bp1);
70  delete bp0;
71  delete bp1;
72 
73  // sampling
74  for( int i=0; i<10; ++i )
75  {
76  VectorType r = b0.sample();
77  VERIFY(b0.contains(r));
78  }
79 
80 }
81 
82 
83 
84 template<typename BoxType>
85 void alignedboxCastTests(const BoxType& _box)
86 {
87  // casting
88  typedef typename BoxType::Scalar Scalar;
90 
91  const Index dim = _box.dim();
92 
93  VectorType p0 = VectorType::Random(dim);
94  VectorType p1 = VectorType::Random(dim);
95 
96  BoxType b0(dim);
97 
98  b0.extend(p0);
99  b0.extend(p1);
100 
101  const int Dim = BoxType::AmbientDimAtCompileTime;
102  typedef typename GetDifferentType<Scalar>::type OtherScalar;
103  AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
104  VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
105  AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
106  VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
107 }
108 
109 
111 {
112  Vector2f m; m << -1.0f, -2.0f;
113  Vector2f M; M << 1.0f, 5.0f;
114 
115  typedef AlignedBox2f BoxType;
116  BoxType box( m, M );
117 
118  Vector2f sides = M-m;
119  VERIFY_IS_APPROX(sides, box.sizes() );
120  VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
121  VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
122  VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
123 
124  VERIFY_IS_APPROX( 14.0f, box.volume() );
125  VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
126  VERIFY_IS_APPROX( std::sqrt( 53.0f ), box.diagonal().norm() );
127 
128  VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
129  VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
130  Vector2f bottomRight; bottomRight << M[0], m[1];
131  Vector2f topLeft; topLeft << m[0], M[1];
132  VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
133  VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
134 }
135 
136 
138 {
139  Vector3i m; m << -1, -2, 0;
140  Vector3i M; M << 1, 5, 3;
141 
142  typedef AlignedBox3i BoxType;
143  BoxType box( m, M );
144 
145  Vector3i sides = M-m;
146  VERIFY_IS_APPROX(sides, box.sizes() );
147  VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
148  VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
149  VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
150 
151  VERIFY_IS_APPROX( 42, box.volume() );
152  VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
153 
154  VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
155  VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
156  Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
157  Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
158  VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
159  VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
160 }
161 
162 
164 {
165  for(int i = 0; i < g_repeat; i++)
166  {
167  CALL_SUBTEST_1( alignedbox(AlignedBox2f()) );
168  CALL_SUBTEST_2( alignedboxCastTests(AlignedBox2f()) );
169 
170  CALL_SUBTEST_3( alignedbox(AlignedBox3f()) );
171  CALL_SUBTEST_4( alignedboxCastTests(AlignedBox3f()) );
172 
173  CALL_SUBTEST_5( alignedbox(AlignedBox4d()) );
174  CALL_SUBTEST_6( alignedboxCastTests(AlignedBox4d()) );
175 
176  CALL_SUBTEST_7( alignedbox(AlignedBox1d()) );
177  CALL_SUBTEST_8( alignedboxCastTests(AlignedBox1d()) );
178 
179  CALL_SUBTEST_9( alignedbox(AlignedBox1i()) );
180  CALL_SUBTEST_10( alignedbox(AlignedBox2i()) );
181  CALL_SUBTEST_11( alignedbox(AlignedBox3i()) );
182 
183  CALL_SUBTEST_14( alignedbox(AlignedBox<double,Dynamic>(4)) );
184  }
185  CALL_SUBTEST_12( specificTest1() );
186  CALL_SUBTEST_13( specificTest2() );
187 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
void alignedboxCastTests(const BoxType &_box)
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:38
Vector3f p1
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Definition: Half.h:150
Vector3f p0
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
An axis aligned box.
#define EIGEN_DONT_INLINE
Definition: Macros.h:517
EIGEN_DONT_INLINE void kill_extra_precision(T &x)
void test_geo_alignedbox()
#define VERIFY_IS_APPROX(a, b)
void alignedbox(const BoxType &_box)
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Vector2 b2(4,-5)
#define eigen_assert(x)
Definition: Macros.h:579
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
GTSAM_EXPORT Matrix3 topLeft(const SO4 &Q, OptionalJacobian< 9, 6 > H)
Definition: SO4.cpp:206
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:34
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
Definition: mpreal.h:2201
Vector2 b1(2,-1)
#define VERIFY(a)
Definition: main.h:325
void specificTest1()
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
friend const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t rnd_mode)
Definition: mpreal.h:2201
void specificTest2()


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:07