AlignedBox.h 14 KB

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