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