Warning, file /include/eigen3/Eigen/src/Geometry/AlignedBox.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 #ifndef EIGEN_ALIGNEDBOX_H
0047 #define EIGEN_ALIGNEDBOX_H
0048
0049 namespace Eigen {
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 template <typename _Scalar, int _AmbientDim>
0066 class AlignedBox
0067 {
0068 public:
0069 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim)
0070 enum { AmbientDimAtCompileTime = _AmbientDim };
0071 typedef _Scalar Scalar;
0072 typedef NumTraits<Scalar> ScalarTraits;
0073 typedef Eigen::Index Index;
0074 typedef typename ScalarTraits::Real RealScalar;
0075 typedef typename ScalarTraits::NonInteger NonInteger;
0076 typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
0077 typedef CwiseBinaryOp<internal::scalar_sum_op<Scalar>, const VectorType, const VectorType> VectorTypeSum;
0078
0079
0080 enum CornerType
0081 {
0082
0083 Min=0, Max=1,
0084
0085
0086
0087 BottomLeft=0, BottomRight=1,
0088 TopLeft=2, TopRight=3,
0089
0090
0091
0092 BottomLeftFloor=0, BottomRightFloor=1,
0093 TopLeftFloor=2, TopRightFloor=3,
0094 BottomLeftCeil=4, BottomRightCeil=5,
0095 TopLeftCeil=6, TopRightCeil=7
0096
0097 };
0098
0099
0100
0101 EIGEN_DEVICE_FUNC inline AlignedBox()
0102 { if (EIGEN_CONST_CONDITIONAL(AmbientDimAtCompileTime!=Dynamic)) setEmpty(); }
0103
0104
0105 EIGEN_DEVICE_FUNC inline explicit AlignedBox(Index _dim) : m_min(_dim), m_max(_dim)
0106 { setEmpty(); }
0107
0108
0109
0110 template<typename OtherVectorType1, typename OtherVectorType2>
0111 EIGEN_DEVICE_FUNC inline AlignedBox(const OtherVectorType1& _min, const OtherVectorType2& _max) : m_min(_min), m_max(_max) {}
0112
0113
0114 template<typename Derived>
0115 EIGEN_DEVICE_FUNC inline explicit AlignedBox(const MatrixBase<Derived>& p) : m_min(p), m_max(m_min)
0116 { }
0117
0118 EIGEN_DEVICE_FUNC ~AlignedBox() {}
0119
0120
0121 EIGEN_DEVICE_FUNC inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_min.size() : Index(AmbientDimAtCompileTime); }
0122
0123
0124 EIGEN_DEVICE_FUNC inline bool isNull() const { return isEmpty(); }
0125
0126
0127 EIGEN_DEVICE_FUNC inline void setNull() { setEmpty(); }
0128
0129
0130
0131 EIGEN_DEVICE_FUNC inline bool isEmpty() const { return (m_min.array() > m_max.array()).any(); }
0132
0133
0134
0135 EIGEN_DEVICE_FUNC inline void setEmpty()
0136 {
0137 m_min.setConstant( ScalarTraits::highest() );
0138 m_max.setConstant( ScalarTraits::lowest() );
0139 }
0140
0141
0142 EIGEN_DEVICE_FUNC inline const VectorType& (min)() const { return m_min; }
0143
0144 EIGEN_DEVICE_FUNC inline VectorType& (min)() { return m_min; }
0145
0146 EIGEN_DEVICE_FUNC inline const VectorType& (max)() const { return m_max; }
0147
0148 EIGEN_DEVICE_FUNC inline VectorType& (max)() { return m_max; }
0149
0150
0151 EIGEN_DEVICE_FUNC inline const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(VectorTypeSum, RealScalar, quotient)
0152 center() const
0153 { return (m_min+m_max)/RealScalar(2); }
0154
0155
0156
0157
0158
0159 EIGEN_DEVICE_FUNC inline const CwiseBinaryOp< internal::scalar_difference_op<Scalar,Scalar>, const VectorType, const VectorType> sizes() const
0160 { return m_max - m_min; }
0161
0162
0163 EIGEN_DEVICE_FUNC inline Scalar volume() const
0164 { return sizes().prod(); }
0165
0166
0167
0168
0169
0170 EIGEN_DEVICE_FUNC inline CwiseBinaryOp< internal::scalar_difference_op<Scalar,Scalar>, const VectorType, const VectorType> diagonal() const
0171 { return sizes(); }
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 EIGEN_DEVICE_FUNC inline VectorType corner(CornerType corner) const
0183 {
0184 EIGEN_STATIC_ASSERT(_AmbientDim <= 3, THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE);
0185
0186 VectorType res;
0187
0188 Index mult = 1;
0189 for(Index d=0; d<dim(); ++d)
0190 {
0191 if( mult & corner ) res[d] = m_max[d];
0192 else res[d] = m_min[d];
0193 mult *= 2;
0194 }
0195 return res;
0196 }
0197
0198
0199
0200 EIGEN_DEVICE_FUNC inline VectorType sample() const
0201 {
0202 VectorType r(dim());
0203 for(Index d=0; d<dim(); ++d)
0204 {
0205 if(!ScalarTraits::IsInteger)
0206 {
0207 r[d] = m_min[d] + (m_max[d]-m_min[d])
0208 * internal::random<Scalar>(Scalar(0), Scalar(1));
0209 }
0210 else
0211 r[d] = internal::random(m_min[d], m_max[d]);
0212 }
0213 return r;
0214 }
0215
0216
0217 template<typename Derived>
0218 EIGEN_DEVICE_FUNC inline bool contains(const MatrixBase<Derived>& p) const
0219 {
0220 typename internal::nested_eval<Derived,2>::type p_n(p.derived());
0221 return (m_min.array()<=p_n.array()).all() && (p_n.array()<=m_max.array()).all();
0222 }
0223
0224
0225 EIGEN_DEVICE_FUNC inline bool contains(const AlignedBox& b) const
0226 { return (m_min.array()<=(b.min)().array()).all() && ((b.max)().array()<=m_max.array()).all(); }
0227
0228
0229
0230 EIGEN_DEVICE_FUNC inline bool intersects(const AlignedBox& b) const
0231 { return (m_min.array()<=(b.max)().array()).all() && ((b.min)().array()<=m_max.array()).all(); }
0232
0233
0234
0235 template<typename Derived>
0236 EIGEN_DEVICE_FUNC inline AlignedBox& extend(const MatrixBase<Derived>& p)
0237 {
0238 typename internal::nested_eval<Derived,2>::type p_n(p.derived());
0239 m_min = m_min.cwiseMin(p_n);
0240 m_max = m_max.cwiseMax(p_n);
0241 return *this;
0242 }
0243
0244
0245
0246 EIGEN_DEVICE_FUNC inline AlignedBox& extend(const AlignedBox& b)
0247 {
0248 m_min = m_min.cwiseMin(b.m_min);
0249 m_max = m_max.cwiseMax(b.m_max);
0250 return *this;
0251 }
0252
0253
0254
0255
0256 EIGEN_DEVICE_FUNC inline AlignedBox& clamp(const AlignedBox& b)
0257 {
0258 m_min = m_min.cwiseMax(b.m_min);
0259 m_max = m_max.cwiseMin(b.m_max);
0260 return *this;
0261 }
0262
0263
0264
0265
0266 EIGEN_DEVICE_FUNC inline AlignedBox intersection(const AlignedBox& b) const
0267 {return AlignedBox(m_min.cwiseMax(b.m_min), m_max.cwiseMin(b.m_max)); }
0268
0269
0270
0271
0272 EIGEN_DEVICE_FUNC inline AlignedBox merged(const AlignedBox& b) const
0273 { return AlignedBox(m_min.cwiseMin(b.m_min), m_max.cwiseMax(b.m_max)); }
0274
0275
0276 template<typename Derived>
0277 EIGEN_DEVICE_FUNC inline AlignedBox& translate(const MatrixBase<Derived>& a_t)
0278 {
0279 const typename internal::nested_eval<Derived,2>::type t(a_t.derived());
0280 m_min += t;
0281 m_max += t;
0282 return *this;
0283 }
0284
0285
0286 template<typename Derived>
0287 EIGEN_DEVICE_FUNC inline AlignedBox translated(const MatrixBase<Derived>& a_t) const
0288 {
0289 AlignedBox result(m_min, m_max);
0290 result.translate(a_t);
0291 return result;
0292 }
0293
0294
0295
0296
0297
0298 template<typename Derived>
0299 EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const MatrixBase<Derived>& p) const;
0300
0301
0302
0303
0304
0305 EIGEN_DEVICE_FUNC inline Scalar squaredExteriorDistance(const AlignedBox& b) const;
0306
0307
0308
0309
0310
0311 template<typename Derived>
0312 EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const MatrixBase<Derived>& p) const
0313 { EIGEN_USING_STD(sqrt) return sqrt(NonInteger(squaredExteriorDistance(p))); }
0314
0315
0316
0317
0318
0319 EIGEN_DEVICE_FUNC inline NonInteger exteriorDistance(const AlignedBox& b) const
0320 { EIGEN_USING_STD(sqrt) return sqrt(NonInteger(squaredExteriorDistance(b))); }
0321
0322
0323
0324
0325 template<int Mode, int Options>
0326 EIGEN_DEVICE_FUNC inline void transform(
0327 const typename Transform<Scalar, AmbientDimAtCompileTime, Mode, Options>::TranslationType& translation)
0328 {
0329 this->translate(translation);
0330 }
0331
0332
0333
0334
0335
0336
0337
0338 template<int Mode, int Options>
0339 EIGEN_DEVICE_FUNC inline void transform(const Transform<Scalar, AmbientDimAtCompileTime, Mode, Options>& transform)
0340 {
0341
0342 EIGEN_STATIC_ASSERT(Mode == Affine || Mode == AffineCompact || Mode == Isometry, THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS);
0343
0344
0345
0346
0347
0348
0349
0350 const VectorType rotated_extent_2 = transform.linear().cwiseAbs() * sizes();
0351
0352 const VectorType rotated_center_2 = transform.linear() * (this->m_max + this->m_min) +
0353 Scalar(2) * transform.translation();
0354
0355 this->m_max = (rotated_center_2 + rotated_extent_2) / Scalar(2);
0356 this->m_min = (rotated_center_2 - rotated_extent_2) / Scalar(2);
0357 }
0358
0359
0360
0361
0362
0363 template<int Mode, int Options>
0364 EIGEN_DEVICE_FUNC AlignedBox transformed(const Transform<Scalar, AmbientDimAtCompileTime, Mode, Options>& transform) const
0365 {
0366 AlignedBox result(m_min, m_max);
0367 result.transform(transform);
0368 return result;
0369 }
0370
0371
0372
0373
0374
0375
0376 template<typename NewScalarType>
0377 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<AlignedBox,
0378 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
0379 {
0380 return typename internal::cast_return_type<AlignedBox,
0381 AlignedBox<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
0382 }
0383
0384
0385 template<typename OtherScalarType>
0386 EIGEN_DEVICE_FUNC inline explicit AlignedBox(const AlignedBox<OtherScalarType,AmbientDimAtCompileTime>& other)
0387 {
0388 m_min = (other.min)().template cast<Scalar>();
0389 m_max = (other.max)().template cast<Scalar>();
0390 }
0391
0392
0393
0394
0395
0396 EIGEN_DEVICE_FUNC bool isApprox(const AlignedBox& other, const RealScalar& prec = ScalarTraits::dummy_precision()) const
0397 { return m_min.isApprox(other.m_min, prec) && m_max.isApprox(other.m_max, prec); }
0398
0399 protected:
0400
0401 VectorType m_min, m_max;
0402 };
0403
0404
0405
0406 template<typename Scalar,int AmbientDim>
0407 template<typename Derived>
0408 EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const MatrixBase<Derived>& a_p) const
0409 {
0410 typename internal::nested_eval<Derived,2*AmbientDim>::type p(a_p.derived());
0411 Scalar dist2(0);
0412 Scalar aux;
0413 for (Index k=0; k<dim(); ++k)
0414 {
0415 if( m_min[k] > p[k] )
0416 {
0417 aux = m_min[k] - p[k];
0418 dist2 += aux*aux;
0419 }
0420 else if( p[k] > m_max[k] )
0421 {
0422 aux = p[k] - m_max[k];
0423 dist2 += aux*aux;
0424 }
0425 }
0426 return dist2;
0427 }
0428
0429 template<typename Scalar,int AmbientDim>
0430 EIGEN_DEVICE_FUNC inline Scalar AlignedBox<Scalar,AmbientDim>::squaredExteriorDistance(const AlignedBox& b) const
0431 {
0432 Scalar dist2(0);
0433 Scalar aux;
0434 for (Index k=0; k<dim(); ++k)
0435 {
0436 if( m_min[k] > b.m_max[k] )
0437 {
0438 aux = m_min[k] - b.m_max[k];
0439 dist2 += aux*aux;
0440 }
0441 else if( b.m_min[k] > m_max[k] )
0442 {
0443 aux = b.m_min[k] - m_max[k];
0444 dist2 += aux*aux;
0445 }
0446 }
0447 return dist2;
0448 }
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
0467 \
0468 typedef AlignedBox<Type, Size> AlignedBox##SizeSuffix##TypeSuffix;
0469
0470 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
0471 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 1, 1) \
0472 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
0473 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
0474 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
0475 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X)
0476
0477 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
0478 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
0479 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
0480
0481 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
0482 #undef EIGEN_MAKE_TYPEDEFS
0483
0484 }
0485
0486 #endif