xref: /aosp_15_r20/external/eigen/test/geo_alignedbox.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008-2009 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li #include <Eigen/Geometry>
12*bf2c3715SXin Li 
13*bf2c3715SXin Li using namespace std;
14*bf2c3715SXin Li 
15*bf2c3715SXin Li // NOTE the following workaround was needed on some 32 bits builds to kill extra precision of x87 registers.
16*bf2c3715SXin Li // It seems that it is not needed anymore, but let's keep it here, just in case...
17*bf2c3715SXin Li 
18*bf2c3715SXin Li template<typename T> EIGEN_DONT_INLINE
kill_extra_precision(T &)19*bf2c3715SXin Li void kill_extra_precision(T& /* x */) {
20*bf2c3715SXin Li   // This one worked but triggered a warning:
21*bf2c3715SXin Li   /* eigen_assert((void*)(&x) != (void*)0); */
22*bf2c3715SXin Li   // An alternative could be:
23*bf2c3715SXin Li   /* volatile T tmp = x; */
24*bf2c3715SXin Li   /* x = tmp; */
25*bf2c3715SXin Li }
26*bf2c3715SXin Li 
27*bf2c3715SXin Li 
alignedbox(const BoxType & box)28*bf2c3715SXin Li template<typename BoxType> void alignedbox(const BoxType& box)
29*bf2c3715SXin Li {
30*bf2c3715SXin Li   /* this test covers the following files:
31*bf2c3715SXin Li      AlignedBox.h
32*bf2c3715SXin Li   */
33*bf2c3715SXin Li   typedef typename BoxType::Scalar Scalar;
34*bf2c3715SXin Li   typedef NumTraits<Scalar> ScalarTraits;
35*bf2c3715SXin Li   typedef typename ScalarTraits::Real RealScalar;
36*bf2c3715SXin Li   typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
37*bf2c3715SXin Li 
38*bf2c3715SXin Li   const Index dim = box.dim();
39*bf2c3715SXin Li 
40*bf2c3715SXin Li   VectorType p0 = VectorType::Random(dim);
41*bf2c3715SXin Li   VectorType p1 = VectorType::Random(dim);
42*bf2c3715SXin Li   while( p1 == p0 ){
43*bf2c3715SXin Li       p1 =  VectorType::Random(dim); }
44*bf2c3715SXin Li   RealScalar s1 = internal::random<RealScalar>(0,1);
45*bf2c3715SXin Li 
46*bf2c3715SXin Li   BoxType b0(dim);
47*bf2c3715SXin Li   BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
48*bf2c3715SXin Li   BoxType b2;
49*bf2c3715SXin Li 
50*bf2c3715SXin Li   kill_extra_precision(b1);
51*bf2c3715SXin Li   kill_extra_precision(p0);
52*bf2c3715SXin Li   kill_extra_precision(p1);
53*bf2c3715SXin Li 
54*bf2c3715SXin Li   b0.extend(p0);
55*bf2c3715SXin Li   b0.extend(p1);
56*bf2c3715SXin Li   VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
57*bf2c3715SXin Li   VERIFY(b0.contains(b0.center()));
58*bf2c3715SXin Li   VERIFY_IS_APPROX(b0.center(),(p0+p1)/Scalar(2));
59*bf2c3715SXin Li 
60*bf2c3715SXin Li   (b2 = b0).extend(b1);
61*bf2c3715SXin Li   VERIFY(b2.contains(b0));
62*bf2c3715SXin Li   VERIFY(b2.contains(b1));
63*bf2c3715SXin Li   VERIFY_IS_APPROX(b2.clamp(b0), b0);
64*bf2c3715SXin Li 
65*bf2c3715SXin Li   // intersection
66*bf2c3715SXin Li   BoxType box1(VectorType::Random(dim));
67*bf2c3715SXin Li   box1.extend(VectorType::Random(dim));
68*bf2c3715SXin Li   BoxType box2(VectorType::Random(dim));
69*bf2c3715SXin Li   box2.extend(VectorType::Random(dim));
70*bf2c3715SXin Li 
71*bf2c3715SXin Li   VERIFY(box1.intersects(box2) == !box1.intersection(box2).isEmpty());
72*bf2c3715SXin Li 
73*bf2c3715SXin Li   // alignment -- make sure there is no memory alignment assertion
74*bf2c3715SXin Li   BoxType *bp0 = new BoxType(dim);
75*bf2c3715SXin Li   BoxType *bp1 = new BoxType(dim);
76*bf2c3715SXin Li   bp0->extend(*bp1);
77*bf2c3715SXin Li   delete bp0;
78*bf2c3715SXin Li   delete bp1;
79*bf2c3715SXin Li 
80*bf2c3715SXin Li   // sampling
81*bf2c3715SXin Li   for( int i=0; i<10; ++i )
82*bf2c3715SXin Li   {
83*bf2c3715SXin Li       VectorType r = b0.sample();
84*bf2c3715SXin Li       VERIFY(b0.contains(r));
85*bf2c3715SXin Li   }
86*bf2c3715SXin Li 
87*bf2c3715SXin Li }
88*bf2c3715SXin Li 
alignedboxTranslatable(const BoxType & box)89*bf2c3715SXin Li template<typename BoxType> void alignedboxTranslatable(const BoxType& box)
90*bf2c3715SXin Li {
91*bf2c3715SXin Li   typedef typename BoxType::Scalar Scalar;
92*bf2c3715SXin Li   typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
93*bf2c3715SXin Li   typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Isometry> IsometryTransform;
94*bf2c3715SXin Li   typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Affine> AffineTransform;
95*bf2c3715SXin Li 
96*bf2c3715SXin Li   alignedbox(box);
97*bf2c3715SXin Li 
98*bf2c3715SXin Li   const VectorType Ones = VectorType::Ones();
99*bf2c3715SXin Li   const VectorType UnitX = VectorType::UnitX();
100*bf2c3715SXin Li   const Index dim = box.dim();
101*bf2c3715SXin Li 
102*bf2c3715SXin Li   // box((-1, -1, -1), (1, 1, 1))
103*bf2c3715SXin Li   BoxType a(-Ones, Ones);
104*bf2c3715SXin Li 
105*bf2c3715SXin Li   VERIFY_IS_APPROX(a.sizes(), Ones * Scalar(2));
106*bf2c3715SXin Li 
107*bf2c3715SXin Li   BoxType b = a;
108*bf2c3715SXin Li   VectorType translate = Ones;
109*bf2c3715SXin Li   translate[0] = Scalar(2);
110*bf2c3715SXin Li   b.translate(translate);
111*bf2c3715SXin Li   // translate by (2, 1, 1) -> box((1, 0, 0), (3, 2, 2))
112*bf2c3715SXin Li 
113*bf2c3715SXin Li   VERIFY_IS_APPROX(b.sizes(), Ones * Scalar(2));
114*bf2c3715SXin Li   VERIFY_IS_APPROX((b.min)(), UnitX);
115*bf2c3715SXin Li   VERIFY_IS_APPROX((b.max)(), Ones * Scalar(2) + UnitX);
116*bf2c3715SXin Li 
117*bf2c3715SXin Li   // Test transform
118*bf2c3715SXin Li 
119*bf2c3715SXin Li   IsometryTransform tf = IsometryTransform::Identity();
120*bf2c3715SXin Li   tf.translation() = -translate;
121*bf2c3715SXin Li 
122*bf2c3715SXin Li   BoxType c = b.transformed(tf);
123*bf2c3715SXin Li   // translate by (-2, -1, -1) -> box((-1, -1, -1), (1, 1, 1))
124*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), a.sizes());
125*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), (a.min)());
126*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), (a.max)());
127*bf2c3715SXin Li 
128*bf2c3715SXin Li   c.transform(tf);
129*bf2c3715SXin Li   // translate by (-2, -1, -1) -> box((-3, -2, -2), (-1, 0, 0))
130*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), a.sizes());
131*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), Ones * Scalar(-2) - UnitX);
132*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), -UnitX);
133*bf2c3715SXin Li 
134*bf2c3715SXin Li   // Scaling
135*bf2c3715SXin Li 
136*bf2c3715SXin Li   AffineTransform atf = AffineTransform::Identity();
137*bf2c3715SXin Li   atf.scale(Scalar(3));
138*bf2c3715SXin Li   c.transform(atf);
139*bf2c3715SXin Li   // scale by 3 -> box((-9, -6, -6), (-3, 0, 0))
140*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), Scalar(3) * a.sizes());
141*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), Ones * Scalar(-6) - UnitX * Scalar(3));
142*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), UnitX * Scalar(-3));
143*bf2c3715SXin Li 
144*bf2c3715SXin Li   atf = AffineTransform::Identity();
145*bf2c3715SXin Li   atf.scale(Scalar(-3));
146*bf2c3715SXin Li   c.transform(atf);
147*bf2c3715SXin Li   // scale by -3 -> box((27, 18, 18), (9, 0, 0))
148*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), Scalar(9) * a.sizes());
149*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), UnitX * Scalar(9));
150*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), Ones * Scalar(18) + UnitX * Scalar(9));
151*bf2c3715SXin Li 
152*bf2c3715SXin Li   // Check identity transform within numerical precision.
153*bf2c3715SXin Li   BoxType transformedC = c.transformed(IsometryTransform::Identity());
154*bf2c3715SXin Li   VERIFY_IS_APPROX(transformedC, c);
155*bf2c3715SXin Li 
156*bf2c3715SXin Li   for (size_t i = 0; i < 10; ++i)
157*bf2c3715SXin Li   {
158*bf2c3715SXin Li     VectorType minCorner;
159*bf2c3715SXin Li     VectorType maxCorner;
160*bf2c3715SXin Li     for (Index d = 0; d < dim; ++d)
161*bf2c3715SXin Li     {
162*bf2c3715SXin Li       minCorner[d] = internal::random<Scalar>(-10,10);
163*bf2c3715SXin Li       maxCorner[d] = minCorner[d] + internal::random<Scalar>(0, 10);
164*bf2c3715SXin Li     }
165*bf2c3715SXin Li 
166*bf2c3715SXin Li     c = BoxType(minCorner, maxCorner);
167*bf2c3715SXin Li 
168*bf2c3715SXin Li     translate = VectorType::Random();
169*bf2c3715SXin Li     c.translate(translate);
170*bf2c3715SXin Li 
171*bf2c3715SXin Li     VERIFY_IS_APPROX((c.min)(), minCorner + translate);
172*bf2c3715SXin Li     VERIFY_IS_APPROX((c.max)(), maxCorner + translate);
173*bf2c3715SXin Li   }
174*bf2c3715SXin Li }
175*bf2c3715SXin Li 
176*bf2c3715SXin Li template<typename Scalar, typename Rotation>
rotate2D(Scalar angle)177*bf2c3715SXin Li Rotation rotate2D(Scalar angle) {
178*bf2c3715SXin Li   return Rotation2D<Scalar>(angle);
179*bf2c3715SXin Li }
180*bf2c3715SXin Li 
181*bf2c3715SXin Li template<typename Scalar, typename Rotation>
rotate2DIntegral(typename NumTraits<Scalar>::NonInteger angle)182*bf2c3715SXin Li Rotation rotate2DIntegral(typename NumTraits<Scalar>::NonInteger angle) {
183*bf2c3715SXin Li   typedef typename NumTraits<Scalar>::NonInteger NonInteger;
184*bf2c3715SXin Li   return Rotation2D<NonInteger>(angle).toRotationMatrix().
185*bf2c3715SXin Li       template cast<Scalar>();
186*bf2c3715SXin Li }
187*bf2c3715SXin Li 
188*bf2c3715SXin Li template<typename Scalar, typename Rotation>
rotate3DZAxis(Scalar angle)189*bf2c3715SXin Li Rotation rotate3DZAxis(Scalar angle) {
190*bf2c3715SXin Li   return AngleAxis<Scalar>(angle, Matrix<Scalar, 3, 1>(0, 0, 1));
191*bf2c3715SXin Li }
192*bf2c3715SXin Li 
193*bf2c3715SXin Li template<typename Scalar, typename Rotation>
rotate3DZAxisIntegral(typename NumTraits<Scalar>::NonInteger angle)194*bf2c3715SXin Li Rotation rotate3DZAxisIntegral(typename NumTraits<Scalar>::NonInteger angle) {
195*bf2c3715SXin Li   typedef typename NumTraits<Scalar>::NonInteger NonInteger;
196*bf2c3715SXin Li   return AngleAxis<NonInteger>(angle, Matrix<NonInteger, 3, 1>(0, 0, 1)).
197*bf2c3715SXin Li       toRotationMatrix().template cast<Scalar>();
198*bf2c3715SXin Li }
199*bf2c3715SXin Li 
200*bf2c3715SXin Li template<typename Scalar, typename Rotation>
rotate4DZWAxis(Scalar angle)201*bf2c3715SXin Li Rotation rotate4DZWAxis(Scalar angle) {
202*bf2c3715SXin Li   Rotation result = Matrix<Scalar, 4, 4>::Identity();
203*bf2c3715SXin Li   result.block(0, 0, 3, 3) = rotate3DZAxis<Scalar, AngleAxisd>(angle).toRotationMatrix();
204*bf2c3715SXin Li   return result;
205*bf2c3715SXin Li }
206*bf2c3715SXin Li 
207*bf2c3715SXin Li template <typename MatrixType>
randomRotationMatrix()208*bf2c3715SXin Li MatrixType randomRotationMatrix()
209*bf2c3715SXin Li {
210*bf2c3715SXin Li   // algorithm from
211*bf2c3715SXin Li   // https://www.isprs-ann-photogramm-remote-sens-spatial-inf-sci.net/III-7/103/2016/isprs-annals-III-7-103-2016.pdf
212*bf2c3715SXin Li   const MatrixType rand = MatrixType::Random();
213*bf2c3715SXin Li   const MatrixType q = rand.householderQr().householderQ();
214*bf2c3715SXin Li   const JacobiSVD<MatrixType> svd = q.jacobiSvd(ComputeFullU | ComputeFullV);
215*bf2c3715SXin Li   const typename MatrixType::Scalar det = (svd.matrixU() * svd.matrixV().transpose()).determinant();
216*bf2c3715SXin Li   MatrixType diag = rand.Identity();
217*bf2c3715SXin Li   diag(MatrixType::RowsAtCompileTime - 1, MatrixType::ColsAtCompileTime - 1) = det;
218*bf2c3715SXin Li   const MatrixType rotation = svd.matrixU() * diag * svd.matrixV().transpose();
219*bf2c3715SXin Li   return rotation;
220*bf2c3715SXin Li }
221*bf2c3715SXin Li 
222*bf2c3715SXin Li template <typename Scalar, int Dim>
boxGetCorners(const Matrix<Scalar,Dim,1> & min_,const Matrix<Scalar,Dim,1> & max_)223*bf2c3715SXin Li Matrix<Scalar, Dim, (1<<Dim)> boxGetCorners(const Matrix<Scalar, Dim, 1>& min_, const Matrix<Scalar, Dim, 1>& max_)
224*bf2c3715SXin Li {
225*bf2c3715SXin Li   Matrix<Scalar, Dim, (1<<Dim) > result;
226*bf2c3715SXin Li   for(Index i=0; i<(1<<Dim); ++i)
227*bf2c3715SXin Li   {
228*bf2c3715SXin Li     for(Index j=0; j<Dim; ++j)
229*bf2c3715SXin Li       result(j,i) = (i & (1<<j)) ? min_(j) : max_(j);
230*bf2c3715SXin Li   }
231*bf2c3715SXin Li   return result;
232*bf2c3715SXin Li }
233*bf2c3715SXin Li 
alignedboxRotatable(const BoxType & box,Rotation (* rotate)(typename NumTraits<typename BoxType::Scalar>::NonInteger))234*bf2c3715SXin Li template<typename BoxType, typename Rotation> void alignedboxRotatable(
235*bf2c3715SXin Li     const BoxType& box,
236*bf2c3715SXin Li     Rotation (*rotate)(typename NumTraits<typename BoxType::Scalar>::NonInteger /*_angle*/))
237*bf2c3715SXin Li {
238*bf2c3715SXin Li   alignedboxTranslatable(box);
239*bf2c3715SXin Li 
240*bf2c3715SXin Li   typedef typename BoxType::Scalar Scalar;
241*bf2c3715SXin Li   typedef typename NumTraits<Scalar>::NonInteger NonInteger;
242*bf2c3715SXin Li   typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
243*bf2c3715SXin Li   typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Isometry> IsometryTransform;
244*bf2c3715SXin Li   typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Affine> AffineTransform;
245*bf2c3715SXin Li 
246*bf2c3715SXin Li   const VectorType Zero = VectorType::Zero();
247*bf2c3715SXin Li   const VectorType Ones = VectorType::Ones();
248*bf2c3715SXin Li   const VectorType UnitX = VectorType::UnitX();
249*bf2c3715SXin Li   const VectorType UnitY = VectorType::UnitY();
250*bf2c3715SXin Li   // this is vector (0, 0, -1, -1, -1, ...), i.e. with zeros at first and second dimensions
251*bf2c3715SXin Li   const VectorType UnitZ = Ones - UnitX - UnitY;
252*bf2c3715SXin Li 
253*bf2c3715SXin Li   // in this kind of comments the 3D case values will be illustrated
254*bf2c3715SXin Li   // box((-1, -1, -1), (1, 1, 1))
255*bf2c3715SXin Li   BoxType a(-Ones, Ones);
256*bf2c3715SXin Li 
257*bf2c3715SXin Li   // to allow templating this test for both 2D and 3D cases, we always set all
258*bf2c3715SXin Li   // but the first coordinate to the same value; so basically 3D case works as
259*bf2c3715SXin Li   // if you were looking at the scene from top
260*bf2c3715SXin Li 
261*bf2c3715SXin Li   VectorType minPoint = -2 * Ones;
262*bf2c3715SXin Li   minPoint[0] = -3;
263*bf2c3715SXin Li   VectorType maxPoint = Zero;
264*bf2c3715SXin Li   maxPoint[0] = -1;
265*bf2c3715SXin Li   BoxType c(minPoint, maxPoint);
266*bf2c3715SXin Li   // box((-3, -2, -2), (-1, 0, 0))
267*bf2c3715SXin Li 
268*bf2c3715SXin Li   IsometryTransform tf2 = IsometryTransform::Identity();
269*bf2c3715SXin Li   // for some weird reason the following statement has to be put separate from
270*bf2c3715SXin Li   // the following rotate call, otherwise precision problems arise...
271*bf2c3715SXin Li   Rotation rot = rotate(NonInteger(EIGEN_PI));
272*bf2c3715SXin Li   tf2.rotate(rot);
273*bf2c3715SXin Li 
274*bf2c3715SXin Li   c.transform(tf2);
275*bf2c3715SXin Li   // rotate by 180 deg around origin -> box((1, 0, -2), (3, 2, 0))
276*bf2c3715SXin Li 
277*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), a.sizes());
278*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), UnitX - UnitZ * Scalar(2));
279*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), UnitX * Scalar(3) + UnitY * Scalar(2));
280*bf2c3715SXin Li 
281*bf2c3715SXin Li   rot = rotate(NonInteger(EIGEN_PI / 2));
282*bf2c3715SXin Li   tf2.setIdentity();
283*bf2c3715SXin Li   tf2.rotate(rot);
284*bf2c3715SXin Li 
285*bf2c3715SXin Li   c.transform(tf2);
286*bf2c3715SXin Li   // rotate by 90 deg around origin ->  box((-2, 1, -2), (0, 3, 0))
287*bf2c3715SXin Li 
288*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), a.sizes());
289*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), Ones * Scalar(-2) + UnitY * Scalar(3));
290*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), UnitY * Scalar(3));
291*bf2c3715SXin Li 
292*bf2c3715SXin Li   // box((-1, -1, -1), (1, 1, 1))
293*bf2c3715SXin Li   AffineTransform atf = AffineTransform::Identity();
294*bf2c3715SXin Li   atf.linearExt()(0, 1) = Scalar(1);
295*bf2c3715SXin Li   c = BoxType(-Ones, Ones);
296*bf2c3715SXin Li   c.transform(atf);
297*bf2c3715SXin Li   // 45 deg shear in x direction -> box((-2, -1, -1), (2, 1, 1))
298*bf2c3715SXin Li 
299*bf2c3715SXin Li   VERIFY_IS_APPROX(c.sizes(), Ones * Scalar(2) + UnitX * Scalar(2));
300*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), -Ones - UnitX);
301*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), Ones + UnitX);
302*bf2c3715SXin Li }
303*bf2c3715SXin Li 
alignedboxNonIntegralRotatable(const BoxType & box,Rotation (* rotate)(typename NumTraits<typename BoxType::Scalar>::NonInteger))304*bf2c3715SXin Li template<typename BoxType, typename Rotation> void alignedboxNonIntegralRotatable(
305*bf2c3715SXin Li     const BoxType& box,
306*bf2c3715SXin Li     Rotation (*rotate)(typename NumTraits<typename BoxType::Scalar>::NonInteger /*_angle*/))
307*bf2c3715SXin Li {
308*bf2c3715SXin Li   alignedboxRotatable(box, rotate);
309*bf2c3715SXin Li 
310*bf2c3715SXin Li   typedef typename BoxType::Scalar Scalar;
311*bf2c3715SXin Li   typedef typename NumTraits<Scalar>::NonInteger NonInteger;
312*bf2c3715SXin Li   enum { Dim = BoxType::AmbientDimAtCompileTime };
313*bf2c3715SXin Li   typedef Matrix<Scalar, Dim, 1> VectorType;
314*bf2c3715SXin Li   typedef Matrix<Scalar, Dim, (1 << Dim)> CornersType;
315*bf2c3715SXin Li   typedef Transform<Scalar, Dim, Isometry> IsometryTransform;
316*bf2c3715SXin Li   typedef Transform<Scalar, Dim, Affine> AffineTransform;
317*bf2c3715SXin Li 
318*bf2c3715SXin Li   const Index dim = box.dim();
319*bf2c3715SXin Li   const VectorType Zero = VectorType::Zero();
320*bf2c3715SXin Li   const VectorType Ones = VectorType::Ones();
321*bf2c3715SXin Li 
322*bf2c3715SXin Li   VectorType minPoint = -2 * Ones;
323*bf2c3715SXin Li   minPoint[1] = 1;
324*bf2c3715SXin Li   VectorType maxPoint = Zero;
325*bf2c3715SXin Li   maxPoint[1] = 3;
326*bf2c3715SXin Li   BoxType c(minPoint, maxPoint);
327*bf2c3715SXin Li   // ((-2, 1, -2), (0, 3, 0))
328*bf2c3715SXin Li 
329*bf2c3715SXin Li   VectorType cornerBL = (c.min)();
330*bf2c3715SXin Li   VectorType cornerTR = (c.max)();
331*bf2c3715SXin Li   VectorType cornerBR = (c.min)(); cornerBR[0] = cornerTR[0];
332*bf2c3715SXin Li   VectorType cornerTL = (c.max)(); cornerTL[0] = cornerBL[0];
333*bf2c3715SXin Li 
334*bf2c3715SXin Li   NonInteger angle = NonInteger(EIGEN_PI/3);
335*bf2c3715SXin Li   Rotation rot = rotate(angle);
336*bf2c3715SXin Li   IsometryTransform tf2;
337*bf2c3715SXin Li   tf2.setIdentity();
338*bf2c3715SXin Li   tf2.rotate(rot);
339*bf2c3715SXin Li 
340*bf2c3715SXin Li   c.transform(tf2);
341*bf2c3715SXin Li   // rotate by 60 deg ->  box((-3.59, -1.23, -2), (-0.86, 1.5, 0))
342*bf2c3715SXin Li 
343*bf2c3715SXin Li   cornerBL = tf2 * cornerBL;
344*bf2c3715SXin Li   cornerBR = tf2 * cornerBR;
345*bf2c3715SXin Li   cornerTL = tf2 * cornerTL;
346*bf2c3715SXin Li   cornerTR = tf2 * cornerTR;
347*bf2c3715SXin Li 
348*bf2c3715SXin Li   VectorType minCorner = Ones * Scalar(-2);
349*bf2c3715SXin Li   VectorType maxCorner = Zero;
350*bf2c3715SXin Li   minCorner[0] = (min)((min)(cornerBL[0], cornerBR[0]), (min)(cornerTL[0], cornerTR[0]));
351*bf2c3715SXin Li   maxCorner[0] = (max)((max)(cornerBL[0], cornerBR[0]), (max)(cornerTL[0], cornerTR[0]));
352*bf2c3715SXin Li   minCorner[1] = (min)((min)(cornerBL[1], cornerBR[1]), (min)(cornerTL[1], cornerTR[1]));
353*bf2c3715SXin Li   maxCorner[1] = (max)((max)(cornerBL[1], cornerBR[1]), (max)(cornerTL[1], cornerTR[1]));
354*bf2c3715SXin Li 
355*bf2c3715SXin Li   for (Index d = 2; d < dim; ++d)
356*bf2c3715SXin Li     VERIFY_IS_APPROX(c.sizes()[d], Scalar(2));
357*bf2c3715SXin Li 
358*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), minCorner);
359*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), maxCorner);
360*bf2c3715SXin Li 
361*bf2c3715SXin Li   VectorType minCornerValue = Ones * Scalar(-2);
362*bf2c3715SXin Li   VectorType maxCornerValue = Zero;
363*bf2c3715SXin Li   minCornerValue[0] = Scalar(Scalar(-sqrt(2*2 + 3*3)) * Scalar(cos(Scalar(atan(2.0/3.0)) - angle/2)));
364*bf2c3715SXin Li   minCornerValue[1] = Scalar(Scalar(-sqrt(1*1 + 2*2)) * Scalar(sin(Scalar(atan(2.0/1.0)) - angle/2)));
365*bf2c3715SXin Li   maxCornerValue[0] = Scalar(-sin(angle));
366*bf2c3715SXin Li   maxCornerValue[1] = Scalar(3 * cos(angle));
367*bf2c3715SXin Li   VERIFY_IS_APPROX((c.min)(), minCornerValue);
368*bf2c3715SXin Li   VERIFY_IS_APPROX((c.max)(), maxCornerValue);
369*bf2c3715SXin Li 
370*bf2c3715SXin Li   // randomized test - translate and rotate the box and compare to a box made of transformed vertices
371*bf2c3715SXin Li   for (size_t i = 0; i < 10; ++i)
372*bf2c3715SXin Li   {
373*bf2c3715SXin Li     for (Index d = 0; d < dim; ++d)
374*bf2c3715SXin Li     {
375*bf2c3715SXin Li       minCorner[d] = internal::random<Scalar>(-10,10);
376*bf2c3715SXin Li       maxCorner[d] = minCorner[d] + internal::random<Scalar>(0, 10);
377*bf2c3715SXin Li     }
378*bf2c3715SXin Li 
379*bf2c3715SXin Li     c = BoxType(minCorner, maxCorner);
380*bf2c3715SXin Li 
381*bf2c3715SXin Li     CornersType corners = boxGetCorners(minCorner, maxCorner);
382*bf2c3715SXin Li 
383*bf2c3715SXin Li     typename AffineTransform::LinearMatrixType rotation =
384*bf2c3715SXin Li         randomRotationMatrix<typename AffineTransform::LinearMatrixType>();
385*bf2c3715SXin Li 
386*bf2c3715SXin Li     tf2.setIdentity();
387*bf2c3715SXin Li     tf2.rotate(rotation);
388*bf2c3715SXin Li     tf2.translate(VectorType::Random());
389*bf2c3715SXin Li 
390*bf2c3715SXin Li     c.transform(tf2);
391*bf2c3715SXin Li     corners = tf2 * corners;
392*bf2c3715SXin Li 
393*bf2c3715SXin Li     minCorner = corners.rowwise().minCoeff();
394*bf2c3715SXin Li     maxCorner = corners.rowwise().maxCoeff();
395*bf2c3715SXin Li 
396*bf2c3715SXin Li     VERIFY_IS_APPROX((c.min)(), minCorner);
397*bf2c3715SXin Li     VERIFY_IS_APPROX((c.max)(), maxCorner);
398*bf2c3715SXin Li   }
399*bf2c3715SXin Li 
400*bf2c3715SXin Li   // randomized test - transform the box with a random affine matrix and compare to a box made of transformed vertices
401*bf2c3715SXin Li   for (size_t i = 0; i < 10; ++i)
402*bf2c3715SXin Li   {
403*bf2c3715SXin Li     for (Index d = 0; d < dim; ++d)
404*bf2c3715SXin Li     {
405*bf2c3715SXin Li       minCorner[d] = internal::random<Scalar>(-10,10);
406*bf2c3715SXin Li       maxCorner[d] = minCorner[d] + internal::random<Scalar>(0, 10);
407*bf2c3715SXin Li     }
408*bf2c3715SXin Li 
409*bf2c3715SXin Li     c = BoxType(minCorner, maxCorner);
410*bf2c3715SXin Li 
411*bf2c3715SXin Li     CornersType corners = boxGetCorners(minCorner, maxCorner);
412*bf2c3715SXin Li 
413*bf2c3715SXin Li     AffineTransform atf = AffineTransform::Identity();
414*bf2c3715SXin Li     atf.linearExt() = AffineTransform::LinearPart::Random();
415*bf2c3715SXin Li     atf.translate(VectorType::Random());
416*bf2c3715SXin Li 
417*bf2c3715SXin Li     c.transform(atf);
418*bf2c3715SXin Li     corners = atf * corners;
419*bf2c3715SXin Li 
420*bf2c3715SXin Li     minCorner = corners.rowwise().minCoeff();
421*bf2c3715SXin Li     maxCorner = corners.rowwise().maxCoeff();
422*bf2c3715SXin Li 
423*bf2c3715SXin Li     VERIFY_IS_APPROX((c.min)(), minCorner);
424*bf2c3715SXin Li     VERIFY_IS_APPROX((c.max)(), maxCorner);
425*bf2c3715SXin Li   }
426*bf2c3715SXin Li }
427*bf2c3715SXin Li 
428*bf2c3715SXin Li template<typename BoxType>
alignedboxCastTests(const BoxType & box)429*bf2c3715SXin Li void alignedboxCastTests(const BoxType& box)
430*bf2c3715SXin Li {
431*bf2c3715SXin Li   // casting
432*bf2c3715SXin Li   typedef typename BoxType::Scalar Scalar;
433*bf2c3715SXin Li   typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
434*bf2c3715SXin Li 
435*bf2c3715SXin Li   const Index dim = box.dim();
436*bf2c3715SXin Li 
437*bf2c3715SXin Li   VectorType p0 = VectorType::Random(dim);
438*bf2c3715SXin Li   VectorType p1 = VectorType::Random(dim);
439*bf2c3715SXin Li 
440*bf2c3715SXin Li   BoxType b0(dim);
441*bf2c3715SXin Li 
442*bf2c3715SXin Li   b0.extend(p0);
443*bf2c3715SXin Li   b0.extend(p1);
444*bf2c3715SXin Li 
445*bf2c3715SXin Li   const int Dim = BoxType::AmbientDimAtCompileTime;
446*bf2c3715SXin Li   typedef typename GetDifferentType<Scalar>::type OtherScalar;
447*bf2c3715SXin Li   AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
448*bf2c3715SXin Li   VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
449*bf2c3715SXin Li   AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
450*bf2c3715SXin Li   VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
451*bf2c3715SXin Li }
452*bf2c3715SXin Li 
453*bf2c3715SXin Li 
specificTest1()454*bf2c3715SXin Li void specificTest1()
455*bf2c3715SXin Li {
456*bf2c3715SXin Li     Vector2f m; m << -1.0f, -2.0f;
457*bf2c3715SXin Li     Vector2f M; M <<  1.0f,  5.0f;
458*bf2c3715SXin Li 
459*bf2c3715SXin Li     typedef AlignedBox2f  BoxType;
460*bf2c3715SXin Li     BoxType box( m, M );
461*bf2c3715SXin Li 
462*bf2c3715SXin Li     Vector2f sides = M-m;
463*bf2c3715SXin Li     VERIFY_IS_APPROX(sides, box.sizes() );
464*bf2c3715SXin Li     VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
465*bf2c3715SXin Li     VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
466*bf2c3715SXin Li     VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
467*bf2c3715SXin Li 
468*bf2c3715SXin Li     VERIFY_IS_APPROX( 14.0f, box.volume() );
469*bf2c3715SXin Li     VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
470*bf2c3715SXin Li     VERIFY_IS_APPROX( std::sqrt( 53.0f ), box.diagonal().norm() );
471*bf2c3715SXin Li 
472*bf2c3715SXin Li     VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
473*bf2c3715SXin Li     VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
474*bf2c3715SXin Li     Vector2f bottomRight; bottomRight << M[0], m[1];
475*bf2c3715SXin Li     Vector2f topLeft; topLeft << m[0], M[1];
476*bf2c3715SXin Li     VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
477*bf2c3715SXin Li     VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
478*bf2c3715SXin Li }
479*bf2c3715SXin Li 
480*bf2c3715SXin Li 
specificTest2()481*bf2c3715SXin Li void specificTest2()
482*bf2c3715SXin Li {
483*bf2c3715SXin Li     Vector3i m; m << -1, -2, 0;
484*bf2c3715SXin Li     Vector3i M; M <<  1,  5, 3;
485*bf2c3715SXin Li 
486*bf2c3715SXin Li     typedef AlignedBox3i  BoxType;
487*bf2c3715SXin Li     BoxType box( m, M );
488*bf2c3715SXin Li 
489*bf2c3715SXin Li     Vector3i sides = M-m;
490*bf2c3715SXin Li     VERIFY_IS_APPROX(sides, box.sizes() );
491*bf2c3715SXin Li     VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
492*bf2c3715SXin Li     VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
493*bf2c3715SXin Li     VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
494*bf2c3715SXin Li 
495*bf2c3715SXin Li     VERIFY_IS_APPROX( 42, box.volume() );
496*bf2c3715SXin Li     VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
497*bf2c3715SXin Li 
498*bf2c3715SXin Li     VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
499*bf2c3715SXin Li     VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
500*bf2c3715SXin Li     Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
501*bf2c3715SXin Li     Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
502*bf2c3715SXin Li     VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
503*bf2c3715SXin Li     VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
504*bf2c3715SXin Li }
505*bf2c3715SXin Li 
506*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(geo_alignedbox)507*bf2c3715SXin Li EIGEN_DECLARE_TEST(geo_alignedbox)
508*bf2c3715SXin Li {
509*bf2c3715SXin Li   for(int i = 0; i < g_repeat; i++)
510*bf2c3715SXin Li   {
511*bf2c3715SXin Li     CALL_SUBTEST_1( (alignedboxNonIntegralRotatable<AlignedBox2f, Rotation2Df>(AlignedBox2f(), &rotate2D)) );
512*bf2c3715SXin Li     CALL_SUBTEST_2( alignedboxCastTests(AlignedBox2f()) );
513*bf2c3715SXin Li 
514*bf2c3715SXin Li     CALL_SUBTEST_3( (alignedboxNonIntegralRotatable<AlignedBox3f, AngleAxisf>(AlignedBox3f(), &rotate3DZAxis)) );
515*bf2c3715SXin Li     CALL_SUBTEST_4( alignedboxCastTests(AlignedBox3f()) );
516*bf2c3715SXin Li 
517*bf2c3715SXin Li     CALL_SUBTEST_5( (alignedboxNonIntegralRotatable<AlignedBox4d, Matrix4d>(AlignedBox4d(), &rotate4DZWAxis)) );
518*bf2c3715SXin Li     CALL_SUBTEST_6( alignedboxCastTests(AlignedBox4d()) );
519*bf2c3715SXin Li 
520*bf2c3715SXin Li     CALL_SUBTEST_7( alignedboxTranslatable(AlignedBox1d()) );
521*bf2c3715SXin Li     CALL_SUBTEST_8( alignedboxCastTests(AlignedBox1d()) );
522*bf2c3715SXin Li 
523*bf2c3715SXin Li     CALL_SUBTEST_9( alignedboxTranslatable(AlignedBox1i()) );
524*bf2c3715SXin Li     CALL_SUBTEST_10( (alignedboxRotatable<AlignedBox2i, Matrix2i>(AlignedBox2i(), &rotate2DIntegral<int, Matrix2i>)) );
525*bf2c3715SXin Li     CALL_SUBTEST_11( (alignedboxRotatable<AlignedBox3i, Matrix3i>(AlignedBox3i(), &rotate3DZAxisIntegral<int, Matrix3i>)) );
526*bf2c3715SXin Li 
527*bf2c3715SXin Li     CALL_SUBTEST_14( alignedbox(AlignedBox<double,Dynamic>(4)) );
528*bf2c3715SXin Li   }
529*bf2c3715SXin Li   CALL_SUBTEST_12( specificTest1() );
530*bf2c3715SXin Li   CALL_SUBTEST_13( specificTest2() );
531*bf2c3715SXin Li }
532