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
alignedbox(const BoxType & _box)18 template<typename BoxType> void alignedbox(const BoxType& _box)
19 {
20 /* this test covers the following files:
21 AlignedBox.h
22 */
23 typedef typename BoxType::Index Index;
24 typedef typename BoxType::Scalar Scalar;
25 typedef typename NumTraits<Scalar>::Real RealScalar;
26 typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
27
28 const Index dim = _box.dim();
29
30 VectorType p0 = VectorType::Random(dim);
31 VectorType p1 = VectorType::Random(dim);
32 while( p1 == p0 ){
33 p1 = VectorType::Random(dim); }
34 RealScalar s1 = internal::random<RealScalar>(0,1);
35
36 BoxType b0(dim);
37 BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
38 BoxType b2;
39
40 b0.extend(p0);
41 b0.extend(p1);
42 VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
43
44 (b2 = b0).extend(b1);
45 VERIFY(b2.contains(b0));
46 VERIFY(b2.contains(b1));
47 VERIFY_IS_APPROX(b2.clamp(b0), b0);
48
49
50 // alignment -- make sure there is no memory alignment assertion
51 BoxType *bp0 = new BoxType(dim);
52 BoxType *bp1 = new BoxType(dim);
53 bp0->extend(*bp1);
54 delete bp0;
55 delete bp1;
56
57 // sampling
58 for( int i=0; i<10; ++i )
59 {
60 VectorType r = b0.sample();
61 VERIFY(b0.contains(r));
62 }
63
64 }
65
66
67
68 template<typename BoxType>
alignedboxCastTests(const BoxType & _box)69 void alignedboxCastTests(const BoxType& _box)
70 {
71 // casting
72 typedef typename BoxType::Index Index;
73 typedef typename BoxType::Scalar Scalar;
74 typedef typename NumTraits<Scalar>::Real RealScalar;
75 typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
76
77 const Index dim = _box.dim();
78
79 VectorType p0 = VectorType::Random(dim);
80 VectorType p1 = VectorType::Random(dim);
81
82 BoxType b0(dim);
83
84 b0.extend(p0);
85 b0.extend(p1);
86
87 const int Dim = BoxType::AmbientDimAtCompileTime;
88 typedef typename GetDifferentType<Scalar>::type OtherScalar;
89 AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
90 VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
91 AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
92 VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
93 }
94
95
specificTest1()96 void specificTest1()
97 {
98 Vector2f m; m << -1.0f, -2.0f;
99 Vector2f M; M << 1.0f, 5.0f;
100
101 typedef AlignedBox2f BoxType;
102 BoxType box( m, M );
103
104 Vector2f sides = M-m;
105 VERIFY_IS_APPROX(sides, box.sizes() );
106 VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
107 VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
108 VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
109
110 VERIFY_IS_APPROX( 14.0f, box.volume() );
111 VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
112 VERIFY_IS_APPROX( internal::sqrt( 53.0f ), box.diagonal().norm() );
113
114 VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
115 VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
116 Vector2f bottomRight; bottomRight << M[0], m[1];
117 Vector2f topLeft; topLeft << m[0], M[1];
118 VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
119 VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
120 }
121
122
specificTest2()123 void specificTest2()
124 {
125 Vector3i m; m << -1, -2, 0;
126 Vector3i M; M << 1, 5, 3;
127
128 typedef AlignedBox3i BoxType;
129 BoxType box( m, M );
130
131 Vector3i sides = M-m;
132 VERIFY_IS_APPROX(sides, box.sizes() );
133 VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
134 VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
135 VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
136
137 VERIFY_IS_APPROX( 42, box.volume() );
138 VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
139
140 VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
141 VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
142 Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
143 Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
144 VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
145 VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
146 }
147
148
test_geo_alignedbox()149 void test_geo_alignedbox()
150 {
151 for(int i = 0; i < g_repeat; i++)
152 {
153 CALL_SUBTEST_1( alignedbox(AlignedBox2f()) );
154 CALL_SUBTEST_2( alignedboxCastTests(AlignedBox2f()) );
155
156 CALL_SUBTEST_3( alignedbox(AlignedBox3f()) );
157 CALL_SUBTEST_4( alignedboxCastTests(AlignedBox3f()) );
158
159 CALL_SUBTEST_5( alignedbox(AlignedBox4d()) );
160 CALL_SUBTEST_6( alignedboxCastTests(AlignedBox4d()) );
161
162 CALL_SUBTEST_7( alignedbox(AlignedBox1d()) );
163 CALL_SUBTEST_8( alignedboxCastTests(AlignedBox1d()) );
164
165 CALL_SUBTEST_9( alignedbox(AlignedBox1i()) );
166 CALL_SUBTEST_10( alignedbox(AlignedBox2i()) );
167 CALL_SUBTEST_11( alignedbox(AlignedBox3i()) );
168 }
169 CALL_SUBTEST_12( specificTest1() );
170 CALL_SUBTEST_13( specificTest2() );
171 }
172