Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 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 | |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 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 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 46 | #ifndef EIGEN_ALIGNEDBOX_H |
| 47 | #define EIGEN_ALIGNEDBOX_H |
| 48 | |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 49 | namespace Eigen { |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 50 | |
| 51 | /** \geometry_module \ingroup Geometry_Module |
| 52 | * |
| 53 | * |
| 54 | * \class AlignedBox |
| 55 | * |
| 56 | * \brief An axis aligned box |
| 57 | * |
| 58 | * \tparam _Scalar the type of the scalar coefficients |
| 59 | * \tparam _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic. |
| 60 | * |
| 61 | * This class represents an axis aligned box as a pair of the minimal and maximal corners. |
| 62 | * \warning The result of most methods is undefined when applied to an empty box. You can check for empty boxes using isEmpty(). |
| 63 | * \sa alignedboxtypedefs |
| 64 | */ |
| 65 | template <typename _Scalar, int _AmbientDim> |
| 66 | class AlignedBox |
| 67 | { |
| 68 | public: |
| 69 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim) |
| 70 | enum { AmbientDimAtCompileTime = _AmbientDim }; |
| 71 | typedef _Scalar Scalar; |
| 72 | typedef NumTraits<Scalar> ScalarTraits; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 73 | typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3 |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 74 | typedef typename ScalarTraits::Real RealScalar; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 75 | typedef typename ScalarTraits::NonInteger NonInteger; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 76 | typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 77 | typedef CwiseBinaryOp<internal::scalar_sum_op<Scalar>, const VectorType, const VectorType> VectorTypeSum; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 78 | |
| 79 | /** Define constants to name the corners of a 1D, 2D or 3D axis aligned bounding box */ |
| 80 | enum CornerType |
| 81 | { |
| 82 | /** 1D names @{ */ |
| 83 | Min=0, Max=1, |
| 84 | /** @} */ |
| 85 | |
| 86 | /** Identifier for 2D corner @{ */ |
| 87 | BottomLeft=0, BottomRight=1, |
| 88 | TopLeft=2, TopRight=3, |
| 89 | /** @} */ |
| 90 | |
| 91 | /** Identifier for 3D corner @{ */ |
| 92 | BottomLeftFloor=0, BottomRightFloor=1, |
| 93 | TopLeftFloor=2, TopRightFloor=3, |
| 94 | BottomLeftCeil=4, BottomRightCeil=5, |
| 95 | TopLeftCeil=6, TopRightCeil=7 |
| 96 | /** @} */ |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | /** Default constructor initializing a null box. */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 101 | EIGEN_DEVICE_FUNC inline AlignedBox() |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 102 | { if (EIGEN_CONST_CONDITIONAL(AmbientDimAtCompileTime!=Dynamic)) setEmpty(); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 103 | |
| 104 | /** Constructs a null box with \a _dim the dimension of the ambient space. */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 105 | EIGEN_DEVICE_FUNC inline explicit AlignedBox(Index _dim) : m_min(_dim), m_max(_dim) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 106 | { setEmpty(); } |
| 107 | |
| 108 | /** Constructs a box with extremities \a _min and \a _max. |
| 109 | * \warning If either component of \a _min is larger than the same component of \a _max, the constructed box is empty. */ |
| 110 | template<typename OtherVectorType1, typename OtherVectorType2> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 111 | EIGEN_DEVICE_FUNC inline AlignedBox(const OtherVectorType1& _min, const OtherVectorType2& _max) : m_min(_min), m_max(_max) {} |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 112 | |
| 113 | /** Constructs a box containing a single point \a p. */ |
| 114 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 115 | EIGEN_DEVICE_FUNC inline explicit AlignedBox(const MatrixBase<Derived>& p) : m_min(p), m_max(m_min) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 116 | { } |
| 117 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 118 | EIGEN_DEVICE_FUNC ~AlignedBox() {} |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 119 | |
| 120 | /** \returns the dimension in which the box holds */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 121 | EIGEN_DEVICE_FUNC inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size() : Index(AmbientDimAtCompileTime); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 122 | |
| 123 | /** \deprecated use isEmpty() */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 124 | EIGEN_DEVICE_FUNC inline bool isNull() const { return isEmpty(); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 125 | |
| 126 | /** \deprecated use setEmpty() */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 127 | EIGEN_DEVICE_FUNC inline void setNull() { setEmpty(); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 128 | |
| 129 | /** \returns true if the box is empty. |
| 130 | * \sa setEmpty */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 131 | EIGEN_DEVICE_FUNC inline bool isEmpty() const { return (m_min.array() > m_max.array()).any(); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 132 | |
| 133 | /** Makes \c *this an empty box. |
| 134 | * \sa isEmpty */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 135 | EIGEN_DEVICE_FUNC inline void setEmpty() |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 136 | { |
| 137 | m_min.setConstant( ScalarTraits::highest() ); |
| 138 | m_max.setConstant( ScalarTraits::lowest() ); |
| 139 | } |
| 140 | |
| 141 | /** \returns the minimal corner */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 142 | EIGEN_DEVICE_FUNC inline const VectorType& (min)() const { return m_min; } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 143 | /** \returns a non const reference to the minimal corner */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 144 | EIGEN_DEVICE_FUNC inline VectorType& (min)() { return m_min; } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 145 | /** \returns the maximal corner */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 146 | EIGEN_DEVICE_FUNC inline const VectorType& (max)() const { return m_max; } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 147 | /** \returns a non const reference to the maximal corner */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 148 | EIGEN_DEVICE_FUNC inline VectorType& (max)() { return m_max; } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 149 | |
| 150 | /** \returns the center of the box */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 151 | EIGEN_DEVICE_FUNC inline const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(VectorTypeSum, RealScalar, quotient) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 152 | center() const |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 153 | { return (m_min+m_max)/RealScalar(2); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 154 | |
| 155 | /** \returns the lengths of the sides of the bounding box. |
| 156 | * Note that this function does not get the same |
| 157 | * result for integral or floating scalar types: see |
| 158 | */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 159 | EIGEN_DEVICE_FUNC inline const CwiseBinaryOp< internal::scalar_difference_op<Scalar,Scalar>, const VectorType, const VectorType> sizes() const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 160 | { return m_max - m_min; } |
| 161 | |
| 162 | /** \returns the volume of the bounding box */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 163 | EIGEN_DEVICE_FUNC inline Scalar volume() const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 164 | { return sizes().prod(); } |
| 165 | |
| 166 | /** \returns an expression for the bounding box diagonal vector |
| 167 | * if the length of the diagonal is needed: diagonal().norm() |
| 168 | * will provide it. |
| 169 | */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 170 | EIGEN_DEVICE_FUNC inline CwiseBinaryOp< internal::scalar_difference_op<Scalar,Scalar>, const VectorType, const VectorType> diagonal() const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 171 | { return sizes(); } |
| 172 | |
| 173 | /** \returns the vertex of the bounding box at the corner defined by |
| 174 | * the corner-id corner. It works only for a 1D, 2D or 3D bounding box. |
| 175 | * For 1D bounding boxes corners are named by 2 enum constants: |
| 176 | * BottomLeft and BottomRight. |
| 177 | * For 2D bounding boxes, corners are named by 4 enum constants: |
| 178 | * BottomLeft, BottomRight, TopLeft, TopRight. |
| 179 | * For 3D bounding boxes, the following names are added: |
| 180 | * BottomLeftCeil, BottomRightCeil, TopLeftCeil, TopRightCeil. |
| 181 | */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 182 | EIGEN_DEVICE_FUNC inline VectorType corner(CornerType corner) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 183 | { |
| 184 | EIGEN_STATIC_ASSERT(_AmbientDim <= 3, THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE); |
| 185 | |
| 186 | VectorType res; |
| 187 | |
| 188 | Index mult = 1; |
| 189 | for(Index d=0; d<dim(); ++d) |
| 190 | { |
| 191 | if( mult & corner ) res[d] = m_max[d]; |
| 192 | else res[d] = m_min[d]; |
| 193 | mult *= 2; |
| 194 | } |
| 195 | return res; |
| 196 | } |
| 197 | |
| 198 | /** \returns a random point inside the bounding box sampled with |
| 199 | * a uniform distribution */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 200 | EIGEN_DEVICE_FUNC inline VectorType sample() const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 201 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 202 | VectorType r(dim()); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 203 | for(Index d=0; d<dim(); ++d) |
| 204 | { |
| 205 | if(!ScalarTraits::IsInteger) |
| 206 | { |
| 207 | r[d] = m_min[d] + (m_max[d]-m_min[d]) |
| 208 | * internal::random<Scalar>(Scalar(0), Scalar(1)); |
| 209 | } |
| 210 | else |
| 211 | r[d] = internal::random(m_min[d], m_max[d]); |
| 212 | } |
| 213 | return r; |
| 214 | } |
| 215 | |
| 216 | /** \returns true if the point \a p is inside the box \c *this. */ |
| 217 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 218 | EIGEN_DEVICE_FUNC inline bool contains(const MatrixBase<Derived>& p) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 219 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 220 | typename internal::nested_eval<Derived,2>::type p_n(p.derived()); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 221 | return (m_min.array()<=p_n.array()).all() && (p_n.array()<=m_max.array()).all(); |
| 222 | } |
| 223 | |
| 224 | /** \returns true if the box \a b is entirely inside the box \c *this. */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 225 | EIGEN_DEVICE_FUNC inline bool contains(const AlignedBox& b) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 226 | { return (m_min.array()<=(b.min)().array()).all() && ((b.max)().array()<=m_max.array()).all(); } |
| 227 | |
| 228 | /** \returns true if the box \a b is intersecting the box \c *this. |
| 229 | * \sa intersection, clamp */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 230 | EIGEN_DEVICE_FUNC inline bool intersects(const AlignedBox& b) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 231 | { return (m_min.array()<=(b.max)().array()).all() && ((b.min)().array()<=m_max.array()).all(); } |
| 232 | |
| 233 | /** Extends \c *this such that it contains the point \a p and returns a reference to \c *this. |
| 234 | * \sa extend(const AlignedBox&) */ |
| 235 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 236 | EIGEN_DEVICE_FUNC inline AlignedBox& extend(const MatrixBase<Derived>& p) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 237 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 238 | typename internal::nested_eval<Derived,2>::type p_n(p.derived()); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 239 | m_min = m_min.cwiseMin(p_n); |
| 240 | m_max = m_max.cwiseMax(p_n); |
| 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | /** Extends \c *this such that it contains the box \a b and returns a reference to \c *this. |
| 245 | * \sa merged, extend(const MatrixBase&) */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 246 | EIGEN_DEVICE_FUNC inline AlignedBox& extend(const AlignedBox& b) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 247 | { |
| 248 | m_min = m_min.cwiseMin(b.m_min); |
| 249 | m_max = m_max.cwiseMax(b.m_max); |
| 250 | return *this; |
| 251 | } |
| 252 | |
| 253 | /** Clamps \c *this by the box \a b and returns a reference to \c *this. |
| 254 | * \note If the boxes don't intersect, the resulting box is empty. |
| 255 | * \sa intersection(), intersects() */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 256 | EIGEN_DEVICE_FUNC inline AlignedBox& clamp(const AlignedBox& b) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 257 | { |
| 258 | m_min = m_min.cwiseMax(b.m_min); |
| 259 | m_max = m_max.cwiseMin(b.m_max); |
| 260 | return *this; |
| 261 | } |
| 262 | |
| 263 | /** Returns an AlignedBox that is the intersection of \a b and \c *this |
| 264 | * \note If the boxes don't intersect, the resulting box is empty. |
| 265 | * \sa intersects(), clamp, contains() */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 266 | EIGEN_DEVICE_FUNC inline AlignedBox intersection(const AlignedBox& b) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 267 | {return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max)); } |
| 268 | |
| 269 | /** Returns an AlignedBox that is the union of \a b and \c *this. |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 270 | * \note Merging with an empty box may result in a box bigger than \c *this. |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 271 | * \sa extend(const AlignedBox&) */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 272 | EIGEN_DEVICE_FUNC inline AlignedBox merged(const AlignedBox& b) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 273 | { return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max)); } |
| 274 | |
| 275 | /** Translate \c *this by the vector \a t and returns a reference to \c *this. */ |
| 276 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 277 | EIGEN_DEVICE_FUNC inline AlignedBox& translate(const MatrixBase<Derived>& a_t) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 278 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 279 | const typename internal::nested_eval<Derived,2>::type t(a_t.derived()); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 280 | m_min += t; |
| 281 | m_max += t; |
| 282 | return *this; |
| 283 | } |
| 284 | |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 285 | /** \returns a copy of \c *this translated by the vector \a t. */ |
| 286 | template<typename Derived> |
| 287 | EIGEN_DEVICE_FUNC inline AlignedBox translated(const MatrixBase<Derived>& a_t) const |
| 288 | { |
| 289 | AlignedBox result(m_min, m_max); |
| 290 | result.translate(a_t); |
| 291 | return result; |
| 292 | } |
| 293 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 294 | /** \returns the squared distance between the point \a p and the box \c *this, |
| 295 | * and zero if \a p is inside the box. |
| 296 | * \sa exteriorDistance(const MatrixBase&), squaredExteriorDistance(const AlignedBox&) |
| 297 | */ |
| 298 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 299 | EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const MatrixBase<Derived>& p) const; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 300 | |
| 301 | /** \returns the squared distance between the boxes \a b and \c *this, |
| 302 | * and zero if the boxes intersect. |
| 303 | * \sa exteriorDistance(const AlignedBox&), squaredExteriorDistance(const MatrixBase&) |
| 304 | */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 305 | EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const AlignedBox& b) const; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 306 | |
| 307 | /** \returns the distance between the point \a p and the box \c *this, |
| 308 | * and zero if \a p is inside the box. |
| 309 | * \sa squaredExteriorDistance(const MatrixBase&), exteriorDistance(const AlignedBox&) |
| 310 | */ |
| 311 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 312 | EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const MatrixBase<Derived>& p) const |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 313 | { EIGEN_USING_STD(sqrt) return sqrt(NonInteger(squaredExteriorDistance(p))); } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 314 | |
| 315 | /** \returns the distance between the boxes \a b and \c *this, |
| 316 | * and zero if the boxes intersect. |
| 317 | * \sa squaredExteriorDistance(const AlignedBox&), exteriorDistance(const MatrixBase&) |
| 318 | */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 319 | EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const AlignedBox& b) const |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame^] | 320 | { EIGEN_USING_STD(sqrt) return sqrt(NonInteger(squaredExteriorDistance(b))); } |
| 321 | |
| 322 | /** |
| 323 | * Specialization of transform for pure translation. |
| 324 | */ |
| 325 | template<int Mode, int Options> |
| 326 | EIGEN_DEVICE_FUNC inline void transform( |
| 327 | const typename Transform<Scalar, AmbientDimAtCompileTime, Mode, Options>::TranslationType& translation) |
| 328 | { |
| 329 | this->translate(translation); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Transforms this box by \a transform and recomputes it to |
| 334 | * still be an axis-aligned box. |
| 335 | * |
| 336 | * \note This method is provided under BSD license (see the top of this file). |
| 337 | */ |
| 338 | template<int Mode, int Options> |
| 339 | EIGEN_DEVICE_FUNC inline void transform(const Transform<Scalar, AmbientDimAtCompileTime, Mode, Options>& transform) |
| 340 | { |
| 341 | // Only Affine and Isometry transforms are currently supported. |
| 342 | EIGEN_STATIC_ASSERT(Mode == Affine || Mode == AffineCompact || Mode == Isometry, THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS); |
| 343 | |
| 344 | // Method adapted from FCL src/shape/geometric_shapes_utility.cpp#computeBV<AABB, Box>(...) |
| 345 | // https://github.com/flexible-collision-library/fcl/blob/fcl-0.4/src/shape/geometric_shapes_utility.cpp#L292 |
| 346 | // |
| 347 | // Here's a nice explanation why it works: https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/ |
| 348 | |
| 349 | // two times rotated extent |
| 350 | const VectorType rotated_extent_2 = transform.linear().cwiseAbs() * sizes(); |
| 351 | // two times new center |
| 352 | const VectorType rotated_center_2 = transform.linear() * (this->m_max + this->m_min) + |
| 353 | Scalar(2) * transform.translation(); |
| 354 | |
| 355 | this->m_max = (rotated_center_2 + rotated_extent_2) / Scalar(2); |
| 356 | this->m_min = (rotated_center_2 - rotated_extent_2) / Scalar(2); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * \returns a copy of \c *this transformed by \a transform and recomputed to |
| 361 | * still be an axis-aligned box. |
| 362 | */ |
| 363 | template<int Mode, int Options> |
| 364 | EIGEN_DEVICE_FUNC AlignedBox transformed(const Transform<Scalar, AmbientDimAtCompileTime, Mode, Options>& transform) const |
| 365 | { |
| 366 | AlignedBox result(m_min, m_max); |
| 367 | result.transform(transform); |
| 368 | return result; |
| 369 | } |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 370 | |
| 371 | /** \returns \c *this with scalar type casted to \a NewScalarType |
| 372 | * |
| 373 | * Note that if \a NewScalarType is equal to the current scalar type of \c *this |
| 374 | * then this function smartly returns a const reference to \c *this. |
| 375 | */ |
| 376 | template<typename NewScalarType> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 377 | EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<AlignedBox, |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 378 | AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const |
| 379 | { |
| 380 | return typename internal::cast_return_type<AlignedBox, |
| 381 | AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this); |
| 382 | } |
| 383 | |
| 384 | /** Copy constructor with scalar type conversion */ |
| 385 | template<typename OtherScalarType> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 386 | EIGEN_DEVICE_FUNC inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other) |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 387 | { |
| 388 | m_min = (other.min)().template cast<Scalar>(); |
| 389 | m_max = (other.max)().template cast<Scalar>(); |
| 390 | } |
| 391 | |
| 392 | /** \returns \c true if \c *this is approximately equal to \a other, within the precision |
| 393 | * determined by \a prec. |
| 394 | * |
| 395 | * \sa MatrixBase::isApprox() */ |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 396 | EIGEN_DEVICE_FUNC bool isApprox(const AlignedBox& other, const RealScalar& prec = ScalarTraits::dummy_precision()) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 397 | { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); } |
| 398 | |
| 399 | protected: |
| 400 | |
| 401 | VectorType m_min, m_max; |
| 402 | }; |
| 403 | |
| 404 | |
| 405 | |
| 406 | template<typename Scalar,int AmbientDim> |
| 407 | template<typename Derived> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 408 | EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const MatrixBase<Derived>& a_p) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 409 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 410 | typename internal::nested_eval<Derived,2*AmbientDim>::type p(a_p.derived()); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 411 | Scalar dist2(0); |
| 412 | Scalar aux; |
| 413 | for (Index k=0; k<dim(); ++k) |
| 414 | { |
| 415 | if( m_min[k] > p[k] ) |
| 416 | { |
| 417 | aux = m_min[k] - p[k]; |
| 418 | dist2 += aux*aux; |
| 419 | } |
| 420 | else if( p[k] > m_max[k] ) |
| 421 | { |
| 422 | aux = p[k] - m_max[k]; |
| 423 | dist2 += aux*aux; |
| 424 | } |
| 425 | } |
| 426 | return dist2; |
| 427 | } |
| 428 | |
| 429 | template<typename Scalar,int AmbientDim> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame] | 430 | EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const AlignedBox& b) const |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 431 | { |
| 432 | Scalar dist2(0); |
| 433 | Scalar aux; |
| 434 | for (Index k=0; k<dim(); ++k) |
| 435 | { |
| 436 | if( m_min[k] > b.m_max[k] ) |
| 437 | { |
| 438 | aux = m_min[k] - b.m_max[k]; |
| 439 | dist2 += aux*aux; |
| 440 | } |
| 441 | else if( b.m_min[k] > m_max[k] ) |
| 442 | { |
| 443 | aux = b.m_min[k] - m_max[k]; |
| 444 | dist2 += aux*aux; |
| 445 | } |
| 446 | } |
| 447 | return dist2; |
| 448 | } |
| 449 | |
| 450 | /** \defgroup alignedboxtypedefs Global aligned box typedefs |
| 451 | * |
| 452 | * \ingroup Geometry_Module |
| 453 | * |
| 454 | * Eigen defines several typedef shortcuts for most common aligned box types. |
| 455 | * |
| 456 | * The general patterns are the following: |
| 457 | * |
| 458 | * \c AlignedBoxSizeType where \c Size can be \c 1, \c 2,\c 3,\c 4 for fixed size boxes or \c X for dynamic size, |
| 459 | * and where \c Type can be \c i for integer, \c f for float, \c d for double. |
| 460 | * |
| 461 | * For example, \c AlignedBox3d is a fixed-size 3x3 aligned box type of doubles, and \c AlignedBoxXf is a dynamic-size aligned box of floats. |
| 462 | * |
| 463 | * \sa class AlignedBox |
| 464 | */ |
| 465 | |
| 466 | #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \ |
| 467 | /** \ingroup alignedboxtypedefs */ \ |
| 468 | typedef AlignedBox<Type, Size> AlignedBox##SizeSuffix##TypeSuffix; |
| 469 | |
| 470 | #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \ |
| 471 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 1, 1) \ |
| 472 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \ |
| 473 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \ |
| 474 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \ |
| 475 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) |
| 476 | |
| 477 | EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i) |
| 478 | EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f) |
| 479 | EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d) |
| 480 | |
| 481 | #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES |
| 482 | #undef EIGEN_MAKE_TYPEDEFS |
| 483 | |
| 484 | } // end namespace Eigen |
| 485 | |
| 486 | #endif // EIGEN_ALIGNEDBOX_H |