Eigen  5.0.1-dev+60122df6
 
Loading...
Searching...
No Matches
AlignedBox.h
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
52namespace Eigen {
53
68template <typename Scalar_, int AmbientDim_>
70 public:
71 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar_, AmbientDim_)
72 enum { AmbientDimAtCompileTime = AmbientDim_ };
73 typedef Scalar_ Scalar;
74 typedef NumTraits<Scalar> ScalarTraits;
76 typedef typename ScalarTraits::Real RealScalar;
77 typedef typename ScalarTraits::NonInteger NonInteger;
79 typedef CwiseBinaryOp<internal::scalar_sum_op<Scalar>, const VectorType, const VectorType> VectorTypeSum;
80
106
108 EIGEN_DEVICE_FUNC inline AlignedBox() {
109 if (EIGEN_CONST_CONDITIONAL(AmbientDimAtCompileTime != Dynamic)) setEmpty();
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>
124 EIGEN_DEVICE_FUNC inline explicit AlignedBox(const MatrixBase<Derived>& p) : m_min(p), m_max(m_min) {}
125
126 EIGEN_DEVICE_FUNC ~AlignedBox() {}
127
129 EIGEN_DEVICE_FUNC inline Index dim() const {
130 return AmbientDimAtCompileTime == Dynamic ? m_min.size() : Index(AmbientDimAtCompileTime);
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
160 EIGEN_DEVICE_FUNC inline const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(VectorTypeSum, RealScalar, quotient)
161 center() const {
162 return (m_min + m_max) / RealScalar(2);
163 }
164
169 EIGEN_DEVICE_FUNC inline const CwiseBinaryOp<internal::scalar_difference_op<Scalar, Scalar>, const VectorType,
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
182 EIGEN_DEVICE_FUNC inline CwiseBinaryOp<internal::scalar_difference_op<Scalar, Scalar>, const VectorType,
183 const VectorType>
184 diagonal() const {
185 return sizes();
186 }
187
197 EIGEN_DEVICE_FUNC inline VectorType corner(CornerType corner) const {
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
215 EIGEN_DEVICE_FUNC inline VectorType sample() const {
216 VectorType r(dim());
217 for (Index d = 0; d < dim(); ++d) {
218 if (!ScalarTraits::IsInteger) {
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>
247 EIGEN_DEVICE_FUNC inline AlignedBox& extend(const MatrixBase<Derived>& p) {
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
256 EIGEN_DEVICE_FUNC inline AlignedBox& extend(const AlignedBox& b) {
257 m_min = m_min.cwiseMin(b.m_min);
258 m_max = m_max.cwiseMax(b.m_max);
259 return *this;
260 }
261
265 EIGEN_DEVICE_FUNC inline AlignedBox& clamp(const AlignedBox& b) {
266 m_min = m_min.cwiseMax(b.m_min);
267 m_max = m_max.cwiseMin(b.m_max);
268 return *this;
269 }
270
274 EIGEN_DEVICE_FUNC inline AlignedBox intersection(const AlignedBox& b) const {
275 return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max));
276 }
277
281 EIGEN_DEVICE_FUNC inline AlignedBox merged(const AlignedBox& b) const {
282 return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max));
283 }
284
286 template <typename Derived>
287 EIGEN_DEVICE_FUNC inline AlignedBox& translate(const MatrixBase<Derived>& a_t) {
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>
296 EIGEN_DEVICE_FUNC inline AlignedBox translated(const MatrixBase<Derived>& a_t) const {
297 AlignedBox result(m_min, m_max);
298 result.translate(a_t);
299 return result;
300 }
301
306 template <typename Derived>
307 EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const MatrixBase<Derived>& p) const;
308
313 EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const AlignedBox& b) const;
314
319 template <typename Derived>
320 EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const MatrixBase<Derived>& p) const {
321 EIGEN_USING_STD(sqrt) return sqrt(NonInteger(squaredExteriorDistance(p)));
322 }
323
328 EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const AlignedBox& b) const {
329 EIGEN_USING_STD(sqrt) return sqrt(NonInteger(squaredExteriorDistance(b)));
330 }
331
335 template <int Mode, int Options>
336 EIGEN_DEVICE_FUNC inline void transform(
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>
373 EIGEN_DEVICE_FUNC AlignedBox
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
387 typename internal::cast_return_type<AlignedBox, AlignedBox<NewScalarType, AmbientDimAtCompileTime> >::type
388 cast() const {
389 return typename internal::cast_return_type<AlignedBox, AlignedBox<NewScalarType, AmbientDimAtCompileTime> >::type(
390 *this);
391 }
392
394 template <typename OtherScalarType>
395 EIGEN_DEVICE_FUNC inline explicit AlignedBox(const AlignedBox<OtherScalarType, AmbientDimAtCompileTime>& other) {
396 m_min = (other.min)().template cast<Scalar>();
397 m_max = (other.max)().template cast<Scalar>();
398 }
399
404 EIGEN_DEVICE_FUNC bool isApprox(const AlignedBox& other,
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:
410 VectorType m_min, m_max;
411};
412
413template <typename Scalar, int AmbientDim>
414template <typename Derived>
416 const MatrixBase<Derived>& a_p) const {
417 typename internal::nested_eval<Derived, 2 * AmbientDim>::type p(a_p.derived());
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
432template <typename Scalar, int AmbientDim>
433EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar, AmbientDim>::squaredExteriorDistance(const AlignedBox& b) const {
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
464
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
476EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
477EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
478EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
479
480#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
481#undef EIGEN_MAKE_TYPEDEFS
482
483} // end namespace Eigen
484
485#endif // EIGEN_ALIGNEDBOX_H
An axis aligned box.
Definition AlignedBox.h:69
bool isApprox(const AlignedBox &other, const RealScalar &prec=ScalarTraits::dummy_precision()) const
Definition AlignedBox.h:404
const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(VectorTypeSum, RealScalar, quotient) center() const
Definition AlignedBox.h:160
AlignedBox intersection(const AlignedBox &b) const
Definition AlignedBox.h:274
const VectorType & max() const
Definition AlignedBox.h:155
AlignedBox(const OtherVectorType1 &_min, const OtherVectorType2 &_max)
Definition AlignedBox.h:119
void transform(const Transform< Scalar, AmbientDimAtCompileTime, Mode, Options > &transform)
Definition AlignedBox.h:348
bool isNull() const
Definition AlignedBox.h:134
VectorType sample() const
Definition AlignedBox.h:215
AlignedBox(const MatrixBase< Derived > &p)
Definition AlignedBox.h:124
bool contains(const AlignedBox &b) const
Definition AlignedBox.h:234
AlignedBox transformed(const Transform< Scalar, AmbientDimAtCompileTime, Mode, Options > &transform) const
Definition AlignedBox.h:374
bool intersects(const AlignedBox &b) const
Definition AlignedBox.h:240
AlignedBox merged(const AlignedBox &b) const
Definition AlignedBox.h:281
VectorType corner(CornerType corner) const
Definition AlignedBox.h:197
Eigen::Index Index
Definition AlignedBox.h:75
AlignedBox(Index _dim)
Definition AlignedBox.h:113
internal::cast_return_type< AlignedBox, AlignedBox< NewScalarType, AmbientDimAtCompileTime > >::type cast() const
Definition AlignedBox.h:388
AlignedBox & extend(const MatrixBase< Derived > &p)
Definition AlignedBox.h:247
AlignedBox translated(const MatrixBase< Derived > &a_t) const
Definition AlignedBox.h:296
bool contains(const MatrixBase< Derived > &p) const
Definition AlignedBox.h:228
Scalar squaredExteriorDistance(const MatrixBase< Derived > &p) const
Definition AlignedBox.h:415
Index dim() const
Definition AlignedBox.h:129
CwiseBinaryOp< internal::scalar_difference_op< Scalar, Scalar >, const VectorType, const VectorType > diagonal() const
Definition AlignedBox.h:184
AlignedBox()
Definition AlignedBox.h:108
AlignedBox & clamp(const AlignedBox &b)
Definition AlignedBox.h:265
bool isEmpty() const
Definition AlignedBox.h:141
Scalar squaredExteriorDistance(const AlignedBox &b) const
Definition AlignedBox.h:433
NonInteger exteriorDistance(const AlignedBox &b) const
Definition AlignedBox.h:328
const CwiseBinaryOp< internal::scalar_difference_op< Scalar, Scalar >, const VectorType, const VectorType > sizes() const
Definition AlignedBox.h:171
void transform(const typename Transform< Scalar, AmbientDimAtCompileTime, Mode, Options >::TranslationType &translation)
Definition AlignedBox.h:336
NonInteger exteriorDistance(const MatrixBase< Derived > &p) const
Definition AlignedBox.h:320
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
AlignedBox(const AlignedBox< OtherScalarType, AmbientDimAtCompileTime > &other)
Definition AlignedBox.h:395
Scalar volume() const
Definition AlignedBox.h:176
void setEmpty()
Definition AlignedBox.h:145
void setNull()
Definition AlignedBox.h:137
AlignedBox & extend(const AlignedBox &b)
Definition AlignedBox.h:256
AlignedBox & translate(const MatrixBase< Derived > &a_t)
Definition AlignedBox.h:287
const VectorType & min() const
Definition AlignedBox.h:151
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
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
Represents an homogeneous transformation in a N dimensional space.
Definition Transform.h:192
Translation< Scalar, Dim > TranslationType
Definition Transform.h:232
@ Affine
Definition Constants.h:458
@ AffineCompact
Definition Constants.h:460
@ Isometry
Definition Constants.h:455
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
const int Dynamic
Definition Constants.h:25
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:232