Eigne Matrix Class Library
Dependents: MPC_current_control HydraulicControlBoard_SW AHRS Test_ekf ... more
src/Geometry/AlignedBox.h@0:13a5d365ba16, 2016-10-13 (annotated)
- Committer:
- ykuroda
- Date:
- Thu Oct 13 04:07:23 2016 +0000
- Revision:
- 0:13a5d365ba16
First commint, Eigne Matrix Class Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 0:13a5d365ba16 | 1 | // This file is part of Eigen, a lightweight C++ template library |
ykuroda | 0:13a5d365ba16 | 2 | // for linear algebra. |
ykuroda | 0:13a5d365ba16 | 3 | // |
ykuroda | 0:13a5d365ba16 | 4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> |
ykuroda | 0:13a5d365ba16 | 5 | // |
ykuroda | 0:13a5d365ba16 | 6 | // This Source Code Form is subject to the terms of the Mozilla |
ykuroda | 0:13a5d365ba16 | 7 | // Public License v. 2.0. If a copy of the MPL was not distributed |
ykuroda | 0:13a5d365ba16 | 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
ykuroda | 0:13a5d365ba16 | 9 | |
ykuroda | 0:13a5d365ba16 | 10 | #ifndef EIGEN_ALIGNEDBOX_H |
ykuroda | 0:13a5d365ba16 | 11 | #define EIGEN_ALIGNEDBOX_H |
ykuroda | 0:13a5d365ba16 | 12 | |
ykuroda | 0:13a5d365ba16 | 13 | namespace Eigen { |
ykuroda | 0:13a5d365ba16 | 14 | |
ykuroda | 0:13a5d365ba16 | 15 | /** \geometry_module \ingroup Geometry_Module |
ykuroda | 0:13a5d365ba16 | 16 | * |
ykuroda | 0:13a5d365ba16 | 17 | * |
ykuroda | 0:13a5d365ba16 | 18 | * \class AlignedBox |
ykuroda | 0:13a5d365ba16 | 19 | * |
ykuroda | 0:13a5d365ba16 | 20 | * \brief An axis aligned box |
ykuroda | 0:13a5d365ba16 | 21 | * |
ykuroda | 0:13a5d365ba16 | 22 | * \tparam _Scalar the type of the scalar coefficients |
ykuroda | 0:13a5d365ba16 | 23 | * \tparam _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic. |
ykuroda | 0:13a5d365ba16 | 24 | * |
ykuroda | 0:13a5d365ba16 | 25 | * This class represents an axis aligned box as a pair of the minimal and maximal corners. |
ykuroda | 0:13a5d365ba16 | 26 | * \warning The result of most methods is undefined when applied to an empty box. You can check for empty boxes using isEmpty(). |
ykuroda | 0:13a5d365ba16 | 27 | * \sa alignedboxtypedefs |
ykuroda | 0:13a5d365ba16 | 28 | */ |
ykuroda | 0:13a5d365ba16 | 29 | template <typename _Scalar, int _AmbientDim> |
ykuroda | 0:13a5d365ba16 | 30 | class AlignedBox |
ykuroda | 0:13a5d365ba16 | 31 | { |
ykuroda | 0:13a5d365ba16 | 32 | public: |
ykuroda | 0:13a5d365ba16 | 33 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim) |
ykuroda | 0:13a5d365ba16 | 34 | enum { AmbientDimAtCompileTime = _AmbientDim }; |
ykuroda | 0:13a5d365ba16 | 35 | typedef _Scalar Scalar; |
ykuroda | 0:13a5d365ba16 | 36 | typedef NumTraits<Scalar> ScalarTraits; |
ykuroda | 0:13a5d365ba16 | 37 | typedef DenseIndex Index; |
ykuroda | 0:13a5d365ba16 | 38 | typedef typename ScalarTraits::Real RealScalar; |
ykuroda | 0:13a5d365ba16 | 39 | typedef typename ScalarTraits::NonInteger NonInteger; |
ykuroda | 0:13a5d365ba16 | 40 | typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType; |
ykuroda | 0:13a5d365ba16 | 41 | |
ykuroda | 0:13a5d365ba16 | 42 | /** Define constants to name the corners of a 1D, 2D or 3D axis aligned bounding box */ |
ykuroda | 0:13a5d365ba16 | 43 | enum CornerType |
ykuroda | 0:13a5d365ba16 | 44 | { |
ykuroda | 0:13a5d365ba16 | 45 | /** 1D names @{ */ |
ykuroda | 0:13a5d365ba16 | 46 | Min=0, Max=1, |
ykuroda | 0:13a5d365ba16 | 47 | /** @} */ |
ykuroda | 0:13a5d365ba16 | 48 | |
ykuroda | 0:13a5d365ba16 | 49 | /** Identifier for 2D corner @{ */ |
ykuroda | 0:13a5d365ba16 | 50 | BottomLeft=0, BottomRight=1, |
ykuroda | 0:13a5d365ba16 | 51 | TopLeft=2, TopRight=3, |
ykuroda | 0:13a5d365ba16 | 52 | /** @} */ |
ykuroda | 0:13a5d365ba16 | 53 | |
ykuroda | 0:13a5d365ba16 | 54 | /** Identifier for 3D corner @{ */ |
ykuroda | 0:13a5d365ba16 | 55 | BottomLeftFloor=0, BottomRightFloor=1, |
ykuroda | 0:13a5d365ba16 | 56 | TopLeftFloor=2, TopRightFloor=3, |
ykuroda | 0:13a5d365ba16 | 57 | BottomLeftCeil=4, BottomRightCeil=5, |
ykuroda | 0:13a5d365ba16 | 58 | TopLeftCeil=6, TopRightCeil=7 |
ykuroda | 0:13a5d365ba16 | 59 | /** @} */ |
ykuroda | 0:13a5d365ba16 | 60 | }; |
ykuroda | 0:13a5d365ba16 | 61 | |
ykuroda | 0:13a5d365ba16 | 62 | |
ykuroda | 0:13a5d365ba16 | 63 | /** Default constructor initializing a null box. */ |
ykuroda | 0:13a5d365ba16 | 64 | inline AlignedBox() |
ykuroda | 0:13a5d365ba16 | 65 | { if (AmbientDimAtCompileTime!=Dynamic) setEmpty(); } |
ykuroda | 0:13a5d365ba16 | 66 | |
ykuroda | 0:13a5d365ba16 | 67 | /** Constructs a null box with \a _dim the dimension of the ambient space. */ |
ykuroda | 0:13a5d365ba16 | 68 | inline explicit AlignedBox(Index _dim) : m_min(_dim), m_max(_dim) |
ykuroda | 0:13a5d365ba16 | 69 | { setEmpty(); } |
ykuroda | 0:13a5d365ba16 | 70 | |
ykuroda | 0:13a5d365ba16 | 71 | /** Constructs a box with extremities \a _min and \a _max. |
ykuroda | 0:13a5d365ba16 | 72 | * \warning If either component of \a _min is larger than the same component of \a _max, the constructed box is empty. */ |
ykuroda | 0:13a5d365ba16 | 73 | template<typename OtherVectorType1, typename OtherVectorType2> |
ykuroda | 0:13a5d365ba16 | 74 | inline AlignedBox(const OtherVectorType1& _min, const OtherVectorType2& _max) : m_min(_min), m_max(_max) {} |
ykuroda | 0:13a5d365ba16 | 75 | |
ykuroda | 0:13a5d365ba16 | 76 | /** Constructs a box containing a single point \a p. */ |
ykuroda | 0:13a5d365ba16 | 77 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 78 | inline explicit AlignedBox(const MatrixBase<Derived>& p) : m_min(p), m_max(m_min) |
ykuroda | 0:13a5d365ba16 | 79 | { } |
ykuroda | 0:13a5d365ba16 | 80 | |
ykuroda | 0:13a5d365ba16 | 81 | ~AlignedBox() {} |
ykuroda | 0:13a5d365ba16 | 82 | |
ykuroda | 0:13a5d365ba16 | 83 | /** \returns the dimension in which the box holds */ |
ykuroda | 0:13a5d365ba16 | 84 | inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size() : Index(AmbientDimAtCompileTime); } |
ykuroda | 0:13a5d365ba16 | 85 | |
ykuroda | 0:13a5d365ba16 | 86 | /** \deprecated use isEmpty() */ |
ykuroda | 0:13a5d365ba16 | 87 | inline bool isNull() const { return isEmpty(); } |
ykuroda | 0:13a5d365ba16 | 88 | |
ykuroda | 0:13a5d365ba16 | 89 | /** \deprecated use setEmpty() */ |
ykuroda | 0:13a5d365ba16 | 90 | inline void setNull() { setEmpty(); } |
ykuroda | 0:13a5d365ba16 | 91 | |
ykuroda | 0:13a5d365ba16 | 92 | /** \returns true if the box is empty. |
ykuroda | 0:13a5d365ba16 | 93 | * \sa setEmpty */ |
ykuroda | 0:13a5d365ba16 | 94 | inline bool isEmpty() const { return (m_min.array() > m_max.array()).any(); } |
ykuroda | 0:13a5d365ba16 | 95 | |
ykuroda | 0:13a5d365ba16 | 96 | /** Makes \c *this an empty box. |
ykuroda | 0:13a5d365ba16 | 97 | * \sa isEmpty */ |
ykuroda | 0:13a5d365ba16 | 98 | inline void setEmpty() |
ykuroda | 0:13a5d365ba16 | 99 | { |
ykuroda | 0:13a5d365ba16 | 100 | m_min.setConstant( ScalarTraits::highest() ); |
ykuroda | 0:13a5d365ba16 | 101 | m_max.setConstant( ScalarTraits::lowest() ); |
ykuroda | 0:13a5d365ba16 | 102 | } |
ykuroda | 0:13a5d365ba16 | 103 | |
ykuroda | 0:13a5d365ba16 | 104 | /** \returns the minimal corner */ |
ykuroda | 0:13a5d365ba16 | 105 | inline const VectorType& (min)() const { return m_min; } |
ykuroda | 0:13a5d365ba16 | 106 | /** \returns a non const reference to the minimal corner */ |
ykuroda | 0:13a5d365ba16 | 107 | inline VectorType& (min)() { return m_min; } |
ykuroda | 0:13a5d365ba16 | 108 | /** \returns the maximal corner */ |
ykuroda | 0:13a5d365ba16 | 109 | inline const VectorType& (max)() const { return m_max; } |
ykuroda | 0:13a5d365ba16 | 110 | /** \returns a non const reference to the maximal corner */ |
ykuroda | 0:13a5d365ba16 | 111 | inline VectorType& (max)() { return m_max; } |
ykuroda | 0:13a5d365ba16 | 112 | |
ykuroda | 0:13a5d365ba16 | 113 | /** \returns the center of the box */ |
ykuroda | 0:13a5d365ba16 | 114 | inline const CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, |
ykuroda | 0:13a5d365ba16 | 115 | const CwiseBinaryOp<internal::scalar_sum_op<Scalar>, const VectorType, const VectorType> > |
ykuroda | 0:13a5d365ba16 | 116 | center() const |
ykuroda | 0:13a5d365ba16 | 117 | { return (m_min+m_max)/2; } |
ykuroda | 0:13a5d365ba16 | 118 | |
ykuroda | 0:13a5d365ba16 | 119 | /** \returns the lengths of the sides of the bounding box. |
ykuroda | 0:13a5d365ba16 | 120 | * Note that this function does not get the same |
ykuroda | 0:13a5d365ba16 | 121 | * result for integral or floating scalar types: see |
ykuroda | 0:13a5d365ba16 | 122 | */ |
ykuroda | 0:13a5d365ba16 | 123 | inline const CwiseBinaryOp< internal::scalar_difference_op<Scalar>, const VectorType, const VectorType> sizes() const |
ykuroda | 0:13a5d365ba16 | 124 | { return m_max - m_min; } |
ykuroda | 0:13a5d365ba16 | 125 | |
ykuroda | 0:13a5d365ba16 | 126 | /** \returns the volume of the bounding box */ |
ykuroda | 0:13a5d365ba16 | 127 | inline Scalar volume() const |
ykuroda | 0:13a5d365ba16 | 128 | { return sizes().prod(); } |
ykuroda | 0:13a5d365ba16 | 129 | |
ykuroda | 0:13a5d365ba16 | 130 | /** \returns an expression for the bounding box diagonal vector |
ykuroda | 0:13a5d365ba16 | 131 | * if the length of the diagonal is needed: diagonal().norm() |
ykuroda | 0:13a5d365ba16 | 132 | * will provide it. |
ykuroda | 0:13a5d365ba16 | 133 | */ |
ykuroda | 0:13a5d365ba16 | 134 | inline CwiseBinaryOp< internal::scalar_difference_op<Scalar>, const VectorType, const VectorType> diagonal() const |
ykuroda | 0:13a5d365ba16 | 135 | { return sizes(); } |
ykuroda | 0:13a5d365ba16 | 136 | |
ykuroda | 0:13a5d365ba16 | 137 | /** \returns the vertex of the bounding box at the corner defined by |
ykuroda | 0:13a5d365ba16 | 138 | * the corner-id corner. It works only for a 1D, 2D or 3D bounding box. |
ykuroda | 0:13a5d365ba16 | 139 | * For 1D bounding boxes corners are named by 2 enum constants: |
ykuroda | 0:13a5d365ba16 | 140 | * BottomLeft and BottomRight. |
ykuroda | 0:13a5d365ba16 | 141 | * For 2D bounding boxes, corners are named by 4 enum constants: |
ykuroda | 0:13a5d365ba16 | 142 | * BottomLeft, BottomRight, TopLeft, TopRight. |
ykuroda | 0:13a5d365ba16 | 143 | * For 3D bounding boxes, the following names are added: |
ykuroda | 0:13a5d365ba16 | 144 | * BottomLeftCeil, BottomRightCeil, TopLeftCeil, TopRightCeil. |
ykuroda | 0:13a5d365ba16 | 145 | */ |
ykuroda | 0:13a5d365ba16 | 146 | inline VectorType corner(CornerType corner) const |
ykuroda | 0:13a5d365ba16 | 147 | { |
ykuroda | 0:13a5d365ba16 | 148 | EIGEN_STATIC_ASSERT(_AmbientDim <= 3, THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE); |
ykuroda | 0:13a5d365ba16 | 149 | |
ykuroda | 0:13a5d365ba16 | 150 | VectorType res; |
ykuroda | 0:13a5d365ba16 | 151 | |
ykuroda | 0:13a5d365ba16 | 152 | Index mult = 1; |
ykuroda | 0:13a5d365ba16 | 153 | for(Index d=0; d<dim(); ++d) |
ykuroda | 0:13a5d365ba16 | 154 | { |
ykuroda | 0:13a5d365ba16 | 155 | if( mult & corner ) res[d] = m_max[d]; |
ykuroda | 0:13a5d365ba16 | 156 | else res[d] = m_min[d]; |
ykuroda | 0:13a5d365ba16 | 157 | mult *= 2; |
ykuroda | 0:13a5d365ba16 | 158 | } |
ykuroda | 0:13a5d365ba16 | 159 | return res; |
ykuroda | 0:13a5d365ba16 | 160 | } |
ykuroda | 0:13a5d365ba16 | 161 | |
ykuroda | 0:13a5d365ba16 | 162 | /** \returns a random point inside the bounding box sampled with |
ykuroda | 0:13a5d365ba16 | 163 | * a uniform distribution */ |
ykuroda | 0:13a5d365ba16 | 164 | inline VectorType sample() const |
ykuroda | 0:13a5d365ba16 | 165 | { |
ykuroda | 0:13a5d365ba16 | 166 | VectorType r(dim()); |
ykuroda | 0:13a5d365ba16 | 167 | for(Index d=0; d<dim(); ++d) |
ykuroda | 0:13a5d365ba16 | 168 | { |
ykuroda | 0:13a5d365ba16 | 169 | if(!ScalarTraits::IsInteger) |
ykuroda | 0:13a5d365ba16 | 170 | { |
ykuroda | 0:13a5d365ba16 | 171 | r[d] = m_min[d] + (m_max[d]-m_min[d]) |
ykuroda | 0:13a5d365ba16 | 172 | * internal::random<Scalar>(Scalar(0), Scalar(1)); |
ykuroda | 0:13a5d365ba16 | 173 | } |
ykuroda | 0:13a5d365ba16 | 174 | else |
ykuroda | 0:13a5d365ba16 | 175 | r[d] = internal::random(m_min[d], m_max[d]); |
ykuroda | 0:13a5d365ba16 | 176 | } |
ykuroda | 0:13a5d365ba16 | 177 | return r; |
ykuroda | 0:13a5d365ba16 | 178 | } |
ykuroda | 0:13a5d365ba16 | 179 | |
ykuroda | 0:13a5d365ba16 | 180 | /** \returns true if the point \a p is inside the box \c *this. */ |
ykuroda | 0:13a5d365ba16 | 181 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 182 | inline bool contains(const MatrixBase<Derived>& p) const |
ykuroda | 0:13a5d365ba16 | 183 | { |
ykuroda | 0:13a5d365ba16 | 184 | typename internal::nested<Derived,2>::type p_n(p.derived()); |
ykuroda | 0:13a5d365ba16 | 185 | return (m_min.array()<=p_n.array()).all() && (p_n.array()<=m_max.array()).all(); |
ykuroda | 0:13a5d365ba16 | 186 | } |
ykuroda | 0:13a5d365ba16 | 187 | |
ykuroda | 0:13a5d365ba16 | 188 | /** \returns true if the box \a b is entirely inside the box \c *this. */ |
ykuroda | 0:13a5d365ba16 | 189 | inline bool contains(const AlignedBox& b) const |
ykuroda | 0:13a5d365ba16 | 190 | { return (m_min.array()<=(b.min)().array()).all() && ((b.max)().array()<=m_max.array()).all(); } |
ykuroda | 0:13a5d365ba16 | 191 | |
ykuroda | 0:13a5d365ba16 | 192 | /** \returns true if the box \a b is intersecting the box \c *this. |
ykuroda | 0:13a5d365ba16 | 193 | * \sa intersection, clamp */ |
ykuroda | 0:13a5d365ba16 | 194 | inline bool intersects(const AlignedBox& b) const |
ykuroda | 0:13a5d365ba16 | 195 | { return (m_min.array()<=(b.max)().array()).all() && ((b.min)().array()<=m_max.array()).all(); } |
ykuroda | 0:13a5d365ba16 | 196 | |
ykuroda | 0:13a5d365ba16 | 197 | /** Extends \c *this such that it contains the point \a p and returns a reference to \c *this. |
ykuroda | 0:13a5d365ba16 | 198 | * \sa extend(const AlignedBox&) */ |
ykuroda | 0:13a5d365ba16 | 199 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 200 | inline AlignedBox& extend(const MatrixBase<Derived>& p) |
ykuroda | 0:13a5d365ba16 | 201 | { |
ykuroda | 0:13a5d365ba16 | 202 | typename internal::nested<Derived,2>::type p_n(p.derived()); |
ykuroda | 0:13a5d365ba16 | 203 | m_min = m_min.cwiseMin(p_n); |
ykuroda | 0:13a5d365ba16 | 204 | m_max = m_max.cwiseMax(p_n); |
ykuroda | 0:13a5d365ba16 | 205 | return *this; |
ykuroda | 0:13a5d365ba16 | 206 | } |
ykuroda | 0:13a5d365ba16 | 207 | |
ykuroda | 0:13a5d365ba16 | 208 | /** Extends \c *this such that it contains the box \a b and returns a reference to \c *this. |
ykuroda | 0:13a5d365ba16 | 209 | * \sa merged, extend(const MatrixBase&) */ |
ykuroda | 0:13a5d365ba16 | 210 | inline AlignedBox& extend(const AlignedBox& b) |
ykuroda | 0:13a5d365ba16 | 211 | { |
ykuroda | 0:13a5d365ba16 | 212 | m_min = m_min.cwiseMin(b.m_min); |
ykuroda | 0:13a5d365ba16 | 213 | m_max = m_max.cwiseMax(b.m_max); |
ykuroda | 0:13a5d365ba16 | 214 | return *this; |
ykuroda | 0:13a5d365ba16 | 215 | } |
ykuroda | 0:13a5d365ba16 | 216 | |
ykuroda | 0:13a5d365ba16 | 217 | /** Clamps \c *this by the box \a b and returns a reference to \c *this. |
ykuroda | 0:13a5d365ba16 | 218 | * \note If the boxes don't intersect, the resulting box is empty. |
ykuroda | 0:13a5d365ba16 | 219 | * \sa intersection(), intersects() */ |
ykuroda | 0:13a5d365ba16 | 220 | inline AlignedBox& clamp(const AlignedBox& b) |
ykuroda | 0:13a5d365ba16 | 221 | { |
ykuroda | 0:13a5d365ba16 | 222 | m_min = m_min.cwiseMax(b.m_min); |
ykuroda | 0:13a5d365ba16 | 223 | m_max = m_max.cwiseMin(b.m_max); |
ykuroda | 0:13a5d365ba16 | 224 | return *this; |
ykuroda | 0:13a5d365ba16 | 225 | } |
ykuroda | 0:13a5d365ba16 | 226 | |
ykuroda | 0:13a5d365ba16 | 227 | /** Returns an AlignedBox that is the intersection of \a b and \c *this |
ykuroda | 0:13a5d365ba16 | 228 | * \note If the boxes don't intersect, the resulting box is empty. |
ykuroda | 0:13a5d365ba16 | 229 | * \sa intersects(), clamp, contains() */ |
ykuroda | 0:13a5d365ba16 | 230 | inline AlignedBox intersection(const AlignedBox& b) const |
ykuroda | 0:13a5d365ba16 | 231 | {return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max)); } |
ykuroda | 0:13a5d365ba16 | 232 | |
ykuroda | 0:13a5d365ba16 | 233 | /** Returns an AlignedBox that is the union of \a b and \c *this. |
ykuroda | 0:13a5d365ba16 | 234 | * \note Merging with an empty box may result in a box bigger than \c *this. |
ykuroda | 0:13a5d365ba16 | 235 | * \sa extend(const AlignedBox&) */ |
ykuroda | 0:13a5d365ba16 | 236 | inline AlignedBox merged(const AlignedBox& b) const |
ykuroda | 0:13a5d365ba16 | 237 | { return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max)); } |
ykuroda | 0:13a5d365ba16 | 238 | |
ykuroda | 0:13a5d365ba16 | 239 | /** Translate \c *this by the vector \a t and returns a reference to \c *this. */ |
ykuroda | 0:13a5d365ba16 | 240 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 241 | inline AlignedBox& translate(const MatrixBase<Derived>& a_t) |
ykuroda | 0:13a5d365ba16 | 242 | { |
ykuroda | 0:13a5d365ba16 | 243 | const typename internal::nested<Derived,2>::type t(a_t.derived()); |
ykuroda | 0:13a5d365ba16 | 244 | m_min += t; |
ykuroda | 0:13a5d365ba16 | 245 | m_max += t; |
ykuroda | 0:13a5d365ba16 | 246 | return *this; |
ykuroda | 0:13a5d365ba16 | 247 | } |
ykuroda | 0:13a5d365ba16 | 248 | |
ykuroda | 0:13a5d365ba16 | 249 | /** \returns the squared distance between the point \a p and the box \c *this, |
ykuroda | 0:13a5d365ba16 | 250 | * and zero if \a p is inside the box. |
ykuroda | 0:13a5d365ba16 | 251 | * \sa exteriorDistance(const MatrixBase&), squaredExteriorDistance(const AlignedBox&) |
ykuroda | 0:13a5d365ba16 | 252 | */ |
ykuroda | 0:13a5d365ba16 | 253 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 254 | inline Scalar squaredExteriorDistance(const MatrixBase<Derived>& p) const; |
ykuroda | 0:13a5d365ba16 | 255 | |
ykuroda | 0:13a5d365ba16 | 256 | /** \returns the squared distance between the boxes \a b and \c *this, |
ykuroda | 0:13a5d365ba16 | 257 | * and zero if the boxes intersect. |
ykuroda | 0:13a5d365ba16 | 258 | * \sa exteriorDistance(const AlignedBox&), squaredExteriorDistance(const MatrixBase&) |
ykuroda | 0:13a5d365ba16 | 259 | */ |
ykuroda | 0:13a5d365ba16 | 260 | inline Scalar squaredExteriorDistance(const AlignedBox& b) const; |
ykuroda | 0:13a5d365ba16 | 261 | |
ykuroda | 0:13a5d365ba16 | 262 | /** \returns the distance between the point \a p and the box \c *this, |
ykuroda | 0:13a5d365ba16 | 263 | * and zero if \a p is inside the box. |
ykuroda | 0:13a5d365ba16 | 264 | * \sa squaredExteriorDistance(const MatrixBase&), exteriorDistance(const AlignedBox&) |
ykuroda | 0:13a5d365ba16 | 265 | */ |
ykuroda | 0:13a5d365ba16 | 266 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 267 | inline NonInteger exteriorDistance(const MatrixBase<Derived>& p) const |
ykuroda | 0:13a5d365ba16 | 268 | { using std::sqrt; return sqrt(NonInteger(squaredExteriorDistance(p))); } |
ykuroda | 0:13a5d365ba16 | 269 | |
ykuroda | 0:13a5d365ba16 | 270 | /** \returns the distance between the boxes \a b and \c *this, |
ykuroda | 0:13a5d365ba16 | 271 | * and zero if the boxes intersect. |
ykuroda | 0:13a5d365ba16 | 272 | * \sa squaredExteriorDistance(const AlignedBox&), exteriorDistance(const MatrixBase&) |
ykuroda | 0:13a5d365ba16 | 273 | */ |
ykuroda | 0:13a5d365ba16 | 274 | inline NonInteger exteriorDistance(const AlignedBox& b) const |
ykuroda | 0:13a5d365ba16 | 275 | { using std::sqrt; return sqrt(NonInteger(squaredExteriorDistance(b))); } |
ykuroda | 0:13a5d365ba16 | 276 | |
ykuroda | 0:13a5d365ba16 | 277 | /** \returns \c *this with scalar type casted to \a NewScalarType |
ykuroda | 0:13a5d365ba16 | 278 | * |
ykuroda | 0:13a5d365ba16 | 279 | * Note that if \a NewScalarType is equal to the current scalar type of \c *this |
ykuroda | 0:13a5d365ba16 | 280 | * then this function smartly returns a const reference to \c *this. |
ykuroda | 0:13a5d365ba16 | 281 | */ |
ykuroda | 0:13a5d365ba16 | 282 | template<typename NewScalarType> |
ykuroda | 0:13a5d365ba16 | 283 | inline typename internal::cast_return_type<AlignedBox, |
ykuroda | 0:13a5d365ba16 | 284 | AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const |
ykuroda | 0:13a5d365ba16 | 285 | { |
ykuroda | 0:13a5d365ba16 | 286 | return typename internal::cast_return_type<AlignedBox, |
ykuroda | 0:13a5d365ba16 | 287 | AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this); |
ykuroda | 0:13a5d365ba16 | 288 | } |
ykuroda | 0:13a5d365ba16 | 289 | |
ykuroda | 0:13a5d365ba16 | 290 | /** Copy constructor with scalar type conversion */ |
ykuroda | 0:13a5d365ba16 | 291 | template<typename OtherScalarType> |
ykuroda | 0:13a5d365ba16 | 292 | inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other) |
ykuroda | 0:13a5d365ba16 | 293 | { |
ykuroda | 0:13a5d365ba16 | 294 | m_min = (other.min)().template cast<Scalar>(); |
ykuroda | 0:13a5d365ba16 | 295 | m_max = (other.max)().template cast<Scalar>(); |
ykuroda | 0:13a5d365ba16 | 296 | } |
ykuroda | 0:13a5d365ba16 | 297 | |
ykuroda | 0:13a5d365ba16 | 298 | /** \returns \c true if \c *this is approximately equal to \a other, within the precision |
ykuroda | 0:13a5d365ba16 | 299 | * determined by \a prec. |
ykuroda | 0:13a5d365ba16 | 300 | * |
ykuroda | 0:13a5d365ba16 | 301 | * \sa MatrixBase::isApprox() */ |
ykuroda | 0:13a5d365ba16 | 302 | bool isApprox(const AlignedBox& other, const RealScalar& prec = ScalarTraits::dummy_precision()) const |
ykuroda | 0:13a5d365ba16 | 303 | { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); } |
ykuroda | 0:13a5d365ba16 | 304 | |
ykuroda | 0:13a5d365ba16 | 305 | protected: |
ykuroda | 0:13a5d365ba16 | 306 | |
ykuroda | 0:13a5d365ba16 | 307 | VectorType m_min, m_max; |
ykuroda | 0:13a5d365ba16 | 308 | }; |
ykuroda | 0:13a5d365ba16 | 309 | |
ykuroda | 0:13a5d365ba16 | 310 | |
ykuroda | 0:13a5d365ba16 | 311 | |
ykuroda | 0:13a5d365ba16 | 312 | template<typename Scalar,int AmbientDim> |
ykuroda | 0:13a5d365ba16 | 313 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 314 | inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const MatrixBase<Derived>& a_p) const |
ykuroda | 0:13a5d365ba16 | 315 | { |
ykuroda | 0:13a5d365ba16 | 316 | typename internal::nested<Derived,2*AmbientDim>::type p(a_p.derived()); |
ykuroda | 0:13a5d365ba16 | 317 | Scalar dist2(0); |
ykuroda | 0:13a5d365ba16 | 318 | Scalar aux; |
ykuroda | 0:13a5d365ba16 | 319 | for (Index k=0; k<dim(); ++k) |
ykuroda | 0:13a5d365ba16 | 320 | { |
ykuroda | 0:13a5d365ba16 | 321 | if( m_min[k] > p[k] ) |
ykuroda | 0:13a5d365ba16 | 322 | { |
ykuroda | 0:13a5d365ba16 | 323 | aux = m_min[k] - p[k]; |
ykuroda | 0:13a5d365ba16 | 324 | dist2 += aux*aux; |
ykuroda | 0:13a5d365ba16 | 325 | } |
ykuroda | 0:13a5d365ba16 | 326 | else if( p[k] > m_max[k] ) |
ykuroda | 0:13a5d365ba16 | 327 | { |
ykuroda | 0:13a5d365ba16 | 328 | aux = p[k] - m_max[k]; |
ykuroda | 0:13a5d365ba16 | 329 | dist2 += aux*aux; |
ykuroda | 0:13a5d365ba16 | 330 | } |
ykuroda | 0:13a5d365ba16 | 331 | } |
ykuroda | 0:13a5d365ba16 | 332 | return dist2; |
ykuroda | 0:13a5d365ba16 | 333 | } |
ykuroda | 0:13a5d365ba16 | 334 | |
ykuroda | 0:13a5d365ba16 | 335 | template<typename Scalar,int AmbientDim> |
ykuroda | 0:13a5d365ba16 | 336 | inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const AlignedBox& b) const |
ykuroda | 0:13a5d365ba16 | 337 | { |
ykuroda | 0:13a5d365ba16 | 338 | Scalar dist2(0); |
ykuroda | 0:13a5d365ba16 | 339 | Scalar aux; |
ykuroda | 0:13a5d365ba16 | 340 | for (Index k=0; k<dim(); ++k) |
ykuroda | 0:13a5d365ba16 | 341 | { |
ykuroda | 0:13a5d365ba16 | 342 | if( m_min[k] > b.m_max[k] ) |
ykuroda | 0:13a5d365ba16 | 343 | { |
ykuroda | 0:13a5d365ba16 | 344 | aux = m_min[k] - b.m_max[k]; |
ykuroda | 0:13a5d365ba16 | 345 | dist2 += aux*aux; |
ykuroda | 0:13a5d365ba16 | 346 | } |
ykuroda | 0:13a5d365ba16 | 347 | else if( b.m_min[k] > m_max[k] ) |
ykuroda | 0:13a5d365ba16 | 348 | { |
ykuroda | 0:13a5d365ba16 | 349 | aux = b.m_min[k] - m_max[k]; |
ykuroda | 0:13a5d365ba16 | 350 | dist2 += aux*aux; |
ykuroda | 0:13a5d365ba16 | 351 | } |
ykuroda | 0:13a5d365ba16 | 352 | } |
ykuroda | 0:13a5d365ba16 | 353 | return dist2; |
ykuroda | 0:13a5d365ba16 | 354 | } |
ykuroda | 0:13a5d365ba16 | 355 | |
ykuroda | 0:13a5d365ba16 | 356 | /** \defgroup alignedboxtypedefs Global aligned box typedefs |
ykuroda | 0:13a5d365ba16 | 357 | * |
ykuroda | 0:13a5d365ba16 | 358 | * \ingroup Geometry_Module |
ykuroda | 0:13a5d365ba16 | 359 | * |
ykuroda | 0:13a5d365ba16 | 360 | * Eigen defines several typedef shortcuts for most common aligned box types. |
ykuroda | 0:13a5d365ba16 | 361 | * |
ykuroda | 0:13a5d365ba16 | 362 | * The general patterns are the following: |
ykuroda | 0:13a5d365ba16 | 363 | * |
ykuroda | 0:13a5d365ba16 | 364 | * \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, |
ykuroda | 0:13a5d365ba16 | 365 | * and where \c Type can be \c i for integer, \c f for float, \c d for double. |
ykuroda | 0:13a5d365ba16 | 366 | * |
ykuroda | 0:13a5d365ba16 | 367 | * 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. |
ykuroda | 0:13a5d365ba16 | 368 | * |
ykuroda | 0:13a5d365ba16 | 369 | * \sa class AlignedBox |
ykuroda | 0:13a5d365ba16 | 370 | */ |
ykuroda | 0:13a5d365ba16 | 371 | |
ykuroda | 0:13a5d365ba16 | 372 | #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \ |
ykuroda | 0:13a5d365ba16 | 373 | /** \ingroup alignedboxtypedefs */ \ |
ykuroda | 0:13a5d365ba16 | 374 | typedef AlignedBox<Type, Size> AlignedBox##SizeSuffix##TypeSuffix; |
ykuroda | 0:13a5d365ba16 | 375 | |
ykuroda | 0:13a5d365ba16 | 376 | #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \ |
ykuroda | 0:13a5d365ba16 | 377 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 1, 1) \ |
ykuroda | 0:13a5d365ba16 | 378 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \ |
ykuroda | 0:13a5d365ba16 | 379 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \ |
ykuroda | 0:13a5d365ba16 | 380 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \ |
ykuroda | 0:13a5d365ba16 | 381 | EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) |
ykuroda | 0:13a5d365ba16 | 382 | |
ykuroda | 0:13a5d365ba16 | 383 | EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i) |
ykuroda | 0:13a5d365ba16 | 384 | EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f) |
ykuroda | 0:13a5d365ba16 | 385 | EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d) |
ykuroda | 0:13a5d365ba16 | 386 | |
ykuroda | 0:13a5d365ba16 | 387 | #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES |
ykuroda | 0:13a5d365ba16 | 388 | #undef EIGEN_MAKE_TYPEDEFS |
ykuroda | 0:13a5d365ba16 | 389 | |
ykuroda | 0:13a5d365ba16 | 390 | } // end namespace Eigen |
ykuroda | 0:13a5d365ba16 | 391 | |
ykuroda | 0:13a5d365ba16 | 392 | #endif // EIGEN_ALIGNEDBOX_H |