AlignedBox.h
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 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 // Function void Eigen::AlignedBox::transform(const Transform& transform)
11 // is provided under the following license agreement:
12 //
13 // Software License Agreement (BSD License)
14 //
15 // Copyright (c) 2011-2014, Willow Garage, Inc.
16 // Copyright (c) 2014-2015, Open Source Robotics Foundation
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions
21 // are met:
22 //
23 // * Redistributions of source code must retain the above copyright
24 // notice, this list of conditions and the following disclaimer.
25 // * Redistributions in binary form must reproduce the above
26 // copyright notice, this list of conditions and the following
27 // disclaimer in the documentation and/or other materials provided
28 // with the distribution.
29 // * Neither the name of Open Source Robotics Foundation nor the names of its
30 // contributors may be used to endorse or promote products derived
31 // from this software without specific prior written permission.
32 //
33 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
36 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
43 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 // POSSIBILITY OF SUCH DAMAGE.
45 
46 #ifndef EIGEN_ALIGNEDBOX_H
47 #define EIGEN_ALIGNEDBOX_H
48 
49 // IWYU pragma: private
50 #include "./InternalHeaderCheck.h"
51 
52 namespace Eigen {
53 
68 template <typename Scalar_, int AmbientDim_>
69 class AlignedBox {
70  public:
72  enum { AmbientDimAtCompileTime = AmbientDim_ };
73  typedef Scalar_ Scalar;
75  typedef Eigen::Index Index;
76  typedef typename ScalarTraits::Real RealScalar;
77  typedef typename ScalarTraits::NonInteger NonInteger;
80 
82  enum CornerType {
84  Min = 0,
85  Max = 1,
91  TopLeft = 2,
92  TopRight = 3,
103  TopRightCeil = 7
105  };
106 
110  }
111 
113  EIGEN_DEVICE_FUNC inline explicit AlignedBox(Index _dim) : m_min(_dim), m_max(_dim) { setEmpty(); }
114 
118  template <typename OtherVectorType1, typename OtherVectorType2>
119  EIGEN_DEVICE_FUNC inline AlignedBox(const OtherVectorType1& _min, const OtherVectorType2& _max)
120  : m_min(_min), m_max(_max) {}
121 
123  template <typename Derived>
125 
127 
129  EIGEN_DEVICE_FUNC inline Index dim() const {
131  }
132 
134  EIGEN_DEVICE_FUNC inline bool isNull() const { return isEmpty(); }
135 
137  EIGEN_DEVICE_FUNC inline void setNull() { setEmpty(); }
138 
141  EIGEN_DEVICE_FUNC inline bool isEmpty() const { return (m_min.array() > m_max.array()).any(); }
142 
145  EIGEN_DEVICE_FUNC inline void setEmpty() {
146  m_min.setConstant(ScalarTraits::highest());
147  m_max.setConstant(ScalarTraits::lowest());
148  }
149 
151  EIGEN_DEVICE_FUNC inline const VectorType&(min)() const { return m_min; }
153  EIGEN_DEVICE_FUNC inline VectorType&(min)() { return m_min; }
155  EIGEN_DEVICE_FUNC inline const VectorType&(max)() const { return m_max; }
157  EIGEN_DEVICE_FUNC inline VectorType&(max)() { return m_max; }
158 
161  center() const {
162  return (m_min + m_max) / RealScalar(2);
163  }
164 
170  const VectorType>
171  sizes() const {
172  return m_max - m_min;
173  }
174 
176  EIGEN_DEVICE_FUNC inline Scalar volume() const { return isEmpty() ? Scalar(0) : sizes().prod(); }
177 
183  const VectorType>
184  diagonal() const {
185  return sizes();
186  }
187 
198  EIGEN_STATIC_ASSERT(AmbientDim_ <= 3, THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE);
199 
200  VectorType res;
201 
202  Index mult = 1;
203  for (Index d = 0; d < dim(); ++d) {
204  if (mult & corner)
205  res[d] = m_max[d];
206  else
207  res[d] = m_min[d];
208  mult *= 2;
209  }
210  return res;
211  }
212 
216  VectorType r(dim());
217  for (Index d = 0; d < dim(); ++d) {
219  r[d] = m_min[d] + (m_max[d] - m_min[d]) * internal::random<Scalar>(Scalar(0), Scalar(1));
220  } else
221  r[d] = internal::random(m_min[d], m_max[d]);
222  }
223  return r;
224  }
225 
227  template <typename Derived>
228  EIGEN_DEVICE_FUNC inline bool contains(const MatrixBase<Derived>& p) const {
229  typename internal::nested_eval<Derived, 2>::type p_n(p.derived());
230  return (m_min.array() <= p_n.array()).all() && (p_n.array() <= m_max.array()).all();
231  }
232 
234  EIGEN_DEVICE_FUNC inline bool contains(const AlignedBox& b) const {
235  return (m_min.array() <= (b.min)().array()).all() && ((b.max)().array() <= m_max.array()).all();
236  }
237 
240  EIGEN_DEVICE_FUNC inline bool intersects(const AlignedBox& b) const {
241  return (m_min.array() <= (b.max)().array()).all() && ((b.min)().array() <= m_max.array()).all();
242  }
243 
246  template <typename Derived>
248  typename internal::nested_eval<Derived, 2>::type p_n(p.derived());
249  m_min = m_min.cwiseMin(p_n);
250  m_max = m_max.cwiseMax(p_n);
251  return *this;
252  }
253 
257  m_min = m_min.cwiseMin(b.m_min);
258  m_max = m_max.cwiseMax(b.m_max);
259  return *this;
260  }
261 
266  m_min = m_min.cwiseMax(b.m_min);
267  m_max = m_max.cwiseMin(b.m_max);
268  return *this;
269  }
270 
275  return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max));
276  }
277 
282  return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max));
283  }
284 
286  template <typename Derived>
288  const typename internal::nested_eval<Derived, 2>::type t(a_t.derived());
289  m_min += t;
290  m_max += t;
291  return *this;
292  }
293 
295  template <typename Derived>
297  AlignedBox result(m_min, m_max);
298  result.translate(a_t);
299  return result;
300  }
301 
306  template <typename Derived>
308 
314 
319  template <typename Derived>
322  }
323 
330  }
331 
335  template <int Mode, int Options>
338  this->translate(translation);
339  }
340 
347  template <int Mode, int Options>
349  // Only Affine and Isometry transforms are currently supported.
350  EIGEN_STATIC_ASSERT(Mode == Affine || Mode == AffineCompact || Mode == Isometry,
351  THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS);
352 
353  // Method adapted from FCL src/shape/geometric_shapes_utility.cpp#computeBV<AABB, Box>(...)
354  // https://github.com/flexible-collision-library/fcl/blob/fcl-0.4/src/shape/geometric_shapes_utility.cpp#L292
355  //
356  // Here's a nice explanation why it works: https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/
357 
358  // two times rotated extent
359  const VectorType rotated_extent_2 = transform.linear().cwiseAbs() * sizes();
360  // two times new center
361  const VectorType rotated_center_2 =
362  transform.linear() * (this->m_max + this->m_min) + Scalar(2) * transform.translation();
363 
364  this->m_max = (rotated_center_2 + rotated_extent_2) / Scalar(2);
365  this->m_min = (rotated_center_2 - rotated_extent_2) / Scalar(2);
366  }
367 
372  template <int Mode, int Options>
375  AlignedBox result(m_min, m_max);
376  result.transform(transform);
377  return result;
378  }
379 
385  template <typename NewScalarType>
386  EIGEN_DEVICE_FUNC inline
388  cast() const {
390  *this);
391  }
392 
394  template <typename OtherScalarType>
396  m_min = (other.min)().template cast<Scalar>();
397  m_max = (other.max)().template cast<Scalar>();
398  }
399 
405  const RealScalar& prec = ScalarTraits::dummy_precision()) const {
406  return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec);
407  }
408 
409  protected:
411 };
412 
413 template <typename Scalar, int AmbientDim>
414 template <typename Derived>
416  const MatrixBase<Derived>& a_p) const {
418  Scalar dist2(0);
419  Scalar aux;
420  for (Index k = 0; k < dim(); ++k) {
421  if (m_min[k] > p[k]) {
422  aux = m_min[k] - p[k];
423  dist2 += aux * aux;
424  } else if (p[k] > m_max[k]) {
425  aux = p[k] - m_max[k];
426  dist2 += aux * aux;
427  }
428  }
429  return dist2;
430 }
431 
432 template <typename Scalar, int AmbientDim>
434  Scalar dist2(0);
435  Scalar aux;
436  for (Index k = 0; k < dim(); ++k) {
437  if (m_min[k] > b.m_max[k]) {
438  aux = m_min[k] - b.m_max[k];
439  dist2 += aux * aux;
440  } else if (b.m_min[k] > m_max[k]) {
441  aux = b.m_min[k] - m_max[k];
442  dist2 += aux * aux;
443  }
444  }
445  return dist2;
446 }
447 
465 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
466  \
467  typedef AlignedBox<Type, Size> AlignedBox##SizeSuffix##TypeSuffix;
468 
469 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
470  EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 1, 1) \
471  EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
472  EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
473  EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
474  EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X)
475 
479 
480 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
481 #undef EIGEN_MAKE_TYPEDEFS
482 
483 } // end namespace Eigen
484 
485 #endif // EIGEN_ALIGNEDBOX_H
#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix)
Definition: AlignedBox.h:469
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_CONST_CONDITIONAL(cond)
Definition: Macros.h:1060
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1090
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
Definition: Memory.h:880
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
float * p
Definition: Tutorial_Map_using.cpp:9
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
An axis aligned box.
Definition: AlignedBox.h:69
EIGEN_DEVICE_FUNC NonInteger exteriorDistance(const AlignedBox &b) const
Definition: AlignedBox.h:328
Scalar_ Scalar
Definition: AlignedBox.h:73
EIGEN_DEVICE_FUNC const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(VectorTypeSum, RealScalar, quotient) center() const
Definition: AlignedBox.h:160
EIGEN_DEVICE_FUNC AlignedBox(const AlignedBox< OtherScalarType, AmbientDimAtCompileTime > &other)
Definition: AlignedBox.h:395
EIGEN_DEVICE_FUNC AlignedBox merged(const AlignedBox &b) const
Definition: AlignedBox.h:281
EIGEN_DEVICE_FUNC AlignedBox intersection(const AlignedBox &b) const
Definition: AlignedBox.h:274
EIGEN_DEVICE_FUNC ~AlignedBox()
Definition: AlignedBox.h:126
CwiseBinaryOp< internal::scalar_sum_op< Scalar >, const VectorType, const VectorType > VectorTypeSum
Definition: AlignedBox.h:79
EIGEN_DEVICE_FUNC bool isApprox(const AlignedBox &other, const RealScalar &prec=ScalarTraits::dummy_precision()) const
Definition: AlignedBox.h:404
EIGEN_DEVICE_FUNC void transform(const typename Transform< Scalar, AmbientDimAtCompileTime, Mode, Options >::TranslationType &translation)
Definition: AlignedBox.h:336
EIGEN_DEVICE_FUNC Index dim() const
Definition: AlignedBox.h:129
EIGEN_DEVICE_FUNC internal::cast_return_type< AlignedBox, AlignedBox< NewScalarType, AmbientDimAtCompileTime > >::type cast() const
Definition: AlignedBox.h:388
EIGEN_DEVICE_FUNC AlignedBox & extend(const AlignedBox &b)
Definition: AlignedBox.h:256
Eigen::Index Index
Definition: AlignedBox.h:75
EIGEN_DEVICE_FUNC AlignedBox()
Definition: AlignedBox.h:108
EIGEN_DEVICE_FUNC AlignedBox & extend(const MatrixBase< Derived > &p)
Definition: AlignedBox.h:247
EIGEN_DEVICE_FUNC const VectorType &() min() const
Definition: AlignedBox.h:151
EIGEN_DEVICE_FUNC VectorType sample() const
Definition: AlignedBox.h:215
Matrix< Scalar, AmbientDimAtCompileTime, 1 > VectorType
Definition: AlignedBox.h:78
ScalarTraits::NonInteger NonInteger
Definition: AlignedBox.h:77
EIGEN_DEVICE_FUNC const VectorType &() max() const
Definition: AlignedBox.h:155
EIGEN_DEVICE_FUNC void transform(const Transform< Scalar, AmbientDimAtCompileTime, Mode, Options > &transform)
Definition: AlignedBox.h:348
EIGEN_DEVICE_FUNC VectorType corner(CornerType corner) const
Definition: AlignedBox.h:197
ScalarTraits::Real RealScalar
Definition: AlignedBox.h:76
EIGEN_DEVICE_FUNC Scalar volume() const
Definition: AlignedBox.h:176
EIGEN_DEVICE_FUNC VectorType &() min()
Definition: AlignedBox.h:153
VectorType m_min
Definition: AlignedBox.h:410
EIGEN_DEVICE_FUNC bool contains(const MatrixBase< Derived > &p) const
Definition: AlignedBox.h:228
EIGEN_DEVICE_FUNC Scalar squaredExteriorDistance(const MatrixBase< Derived > &p) const
Definition: AlignedBox.h:415
@ AmbientDimAtCompileTime
Definition: AlignedBox.h:72
EIGEN_DEVICE_FUNC bool isEmpty() const
Definition: AlignedBox.h:141
NumTraits< Scalar > ScalarTraits
Definition: AlignedBox.h:74
EIGEN_DEVICE_FUNC const CwiseBinaryOp< internal::scalar_difference_op< Scalar, Scalar >, const VectorType, const VectorType > sizes() const
Definition: AlignedBox.h:171
EIGEN_DEVICE_FUNC AlignedBox transformed(const Transform< Scalar, AmbientDimAtCompileTime, Mode, Options > &transform) const
Definition: AlignedBox.h:374
EIGEN_DEVICE_FUNC void setEmpty()
Definition: AlignedBox.h:145
EIGEN_DEVICE_FUNC void setNull()
Definition: AlignedBox.h:137
EIGEN_DEVICE_FUNC AlignedBox(const OtherVectorType1 &_min, const OtherVectorType2 &_max)
Definition: AlignedBox.h:119
EIGEN_DEVICE_FUNC AlignedBox & translate(const MatrixBase< Derived > &a_t)
Definition: AlignedBox.h:287
EIGEN_DEVICE_FUNC AlignedBox(Index _dim)
Definition: AlignedBox.h:113
EIGEN_DEVICE_FUNC CwiseBinaryOp< internal::scalar_difference_op< Scalar, Scalar >, const VectorType, const VectorType > diagonal() const
Definition: AlignedBox.h:184
EIGEN_DEVICE_FUNC VectorType &() max()
Definition: AlignedBox.h:157
EIGEN_DEVICE_FUNC NonInteger exteriorDistance(const MatrixBase< Derived > &p) const
Definition: AlignedBox.h:320
EIGEN_DEVICE_FUNC AlignedBox & clamp(const AlignedBox &b)
Definition: AlignedBox.h:265
EIGEN_DEVICE_FUNC AlignedBox translated(const MatrixBase< Derived > &a_t) const
Definition: AlignedBox.h:296
EIGEN_DEVICE_FUNC bool intersects(const AlignedBox &b) const
Definition: AlignedBox.h:240
VectorType m_max
Definition: AlignedBox.h:410
EIGEN_DEVICE_FUNC bool contains(const AlignedBox &b) const
Definition: AlignedBox.h:234
CornerType
Definition: AlignedBox.h:82
@ Max
Definition: AlignedBox.h:85
@ TopLeft
Definition: AlignedBox.h:91
@ TopRight
Definition: AlignedBox.h:92
@ TopLeftFloor
Definition: AlignedBox.h:98
@ BottomRight
Definition: AlignedBox.h:90
@ Min
Definition: AlignedBox.h:84
@ TopLeftCeil
Definition: AlignedBox.h:102
@ BottomLeft
Definition: AlignedBox.h:89
@ BottomRightFloor
Definition: AlignedBox.h:97
@ TopRightFloor
Definition: AlignedBox.h:99
@ BottomLeftCeil
Definition: AlignedBox.h:100
@ BottomRightCeil
Definition: AlignedBox.h:101
@ TopRightCeil
Definition: AlignedBox.h:103
@ BottomLeftFloor
Definition: AlignedBox.h:96
EIGEN_DEVICE_FUNC bool isNull() const
Definition: AlignedBox.h:134
EIGEN_DEVICE_FUNC AlignedBox(const MatrixBase< Derived > &p)
Definition: AlignedBox.h:124
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:365
Represents an homogeneous transformation in a N dimensional space.
Definition: Transform.h:192
EIGEN_DEVICE_FUNC ConstTranslationPart translation() const
Definition: Transform.h:384
Represents a translation transformation.
Definition: Translation.h:33
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
Definition: cxx11_tensor_map.cpp:237
static constexpr Eigen::internal::all_t all
Definition: IndexedViewHelper.h:86
@ Affine
Definition: Constants.h:458
@ AffineCompact
Definition: Constants.h:460
@ Isometry
Definition: Constants.h:455
char char char int int * k
Definition: level2_impl.h:374
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
r
Definition: UniformPSDSelfTest.py:20
type
Definition: compute_granudrum_aor.py:141
t
Definition: plotPSD.py:36
@ IsInteger
Definition: NumTraits.h:174
T Real
Definition: NumTraits.h:183
Definition: XprHelper.h:583
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: fft_test_shared.h:66