/src/Geometry_Eigen/Eigen/src/Core/Fuzzy.h

http://github.com/Akranar/daguerreo · C Header · 161 lines · 83 code · 14 blank · 64 comment · 3 complexity · 26f7a314741ac28ac604a3a5c28040c3 MD5 · raw file

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. //
  7. // Eigen is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU Lesser General Public
  9. // License as published by the Free Software Foundation; either
  10. // version 3 of the License, or (at your option) any later version.
  11. //
  12. // Alternatively, you can redistribute it and/or
  13. // modify it under the terms of the GNU General Public License as
  14. // published by the Free Software Foundation; either version 2 of
  15. // the License, or (at your option) any later version.
  16. //
  17. // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
  18. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public
  23. // License and a copy of the GNU General Public License along with
  24. // Eigen. If not, see <http://www.gnu.org/licenses/>.
  25. #ifndef EIGEN_FUZZY_H
  26. #define EIGEN_FUZZY_H
  27. namespace internal
  28. {
  29. template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
  30. struct isApprox_selector
  31. {
  32. static bool run(const Derived& x, const OtherDerived& y, typename Derived::RealScalar prec)
  33. {
  34. using std::min;
  35. const typename internal::nested<Derived,2>::type nested(x);
  36. const typename internal::nested<OtherDerived,2>::type otherNested(y);
  37. return (nested - otherNested).cwiseAbs2().sum() <= prec * prec * (min)(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum());
  38. }
  39. };
  40. template<typename Derived, typename OtherDerived>
  41. struct isApprox_selector<Derived, OtherDerived, true>
  42. {
  43. static bool run(const Derived& x, const OtherDerived& y, typename Derived::RealScalar)
  44. {
  45. return x.matrix() == y.matrix();
  46. }
  47. };
  48. template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
  49. struct isMuchSmallerThan_object_selector
  50. {
  51. static bool run(const Derived& x, const OtherDerived& y, typename Derived::RealScalar prec)
  52. {
  53. return x.cwiseAbs2().sum() <= abs2(prec) * y.cwiseAbs2().sum();
  54. }
  55. };
  56. template<typename Derived, typename OtherDerived>
  57. struct isMuchSmallerThan_object_selector<Derived, OtherDerived, true>
  58. {
  59. static bool run(const Derived& x, const OtherDerived&, typename Derived::RealScalar)
  60. {
  61. return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
  62. }
  63. };
  64. template<typename Derived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
  65. struct isMuchSmallerThan_scalar_selector
  66. {
  67. static bool run(const Derived& x, const typename Derived::RealScalar& y, typename Derived::RealScalar prec)
  68. {
  69. return x.cwiseAbs2().sum() <= abs2(prec * y);
  70. }
  71. };
  72. template<typename Derived>
  73. struct isMuchSmallerThan_scalar_selector<Derived, true>
  74. {
  75. static bool run(const Derived& x, const typename Derived::RealScalar&, typename Derived::RealScalar)
  76. {
  77. return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
  78. }
  79. };
  80. } // end namespace internal
  81. /** \returns \c true if \c *this is approximately equal to \a other, within the precision
  82. * determined by \a prec.
  83. *
  84. * \note The fuzzy compares are done multiplicatively. Two vectors \f$ v \f$ and \f$ w \f$
  85. * are considered to be approximately equal within precision \f$ p \f$ if
  86. * \f[ \Vert v - w \Vert \leqslant p\,\(min)(\Vert v\Vert, \Vert w\Vert). \f]
  87. * For matrices, the comparison is done using the Hilbert-Schmidt norm (aka Frobenius norm
  88. * L2 norm).
  89. *
  90. * \note Because of the multiplicativeness of this comparison, one can't use this function
  91. * to check whether \c *this is approximately equal to the zero matrix or vector.
  92. * Indeed, \c isApprox(zero) returns false unless \c *this itself is exactly the zero matrix
  93. * or vector. If you want to test whether \c *this is zero, use internal::isMuchSmallerThan(const
  94. * RealScalar&, RealScalar) instead.
  95. *
  96. * \sa internal::isMuchSmallerThan(const RealScalar&, RealScalar) const
  97. */
  98. template<typename Derived>
  99. template<typename OtherDerived>
  100. bool DenseBase<Derived>::isApprox(
  101. const DenseBase<OtherDerived>& other,
  102. RealScalar prec
  103. ) const
  104. {
  105. return internal::isApprox_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
  106. }
  107. /** \returns \c true if the norm of \c *this is much smaller than \a other,
  108. * within the precision determined by \a prec.
  109. *
  110. * \note The fuzzy compares are done multiplicatively. A vector \f$ v \f$ is
  111. * considered to be much smaller than \f$ x \f$ within precision \f$ p \f$ if
  112. * \f[ \Vert v \Vert \leqslant p\,\vert x\vert. \f]
  113. *
  114. * For matrices, the comparison is done using the Hilbert-Schmidt norm. For this reason,
  115. * the value of the reference scalar \a other should come from the Hilbert-Schmidt norm
  116. * of a reference matrix of same dimensions.
  117. *
  118. * \sa isApprox(), isMuchSmallerThan(const DenseBase<OtherDerived>&, RealScalar) const
  119. */
  120. template<typename Derived>
  121. bool DenseBase<Derived>::isMuchSmallerThan(
  122. const typename NumTraits<Scalar>::Real& other,
  123. RealScalar prec
  124. ) const
  125. {
  126. return internal::isMuchSmallerThan_scalar_selector<Derived>::run(derived(), other, prec);
  127. }
  128. /** \returns \c true if the norm of \c *this is much smaller than the norm of \a other,
  129. * within the precision determined by \a prec.
  130. *
  131. * \note The fuzzy compares are done multiplicatively. A vector \f$ v \f$ is
  132. * considered to be much smaller than a vector \f$ w \f$ within precision \f$ p \f$ if
  133. * \f[ \Vert v \Vert \leqslant p\,\Vert w\Vert. \f]
  134. * For matrices, the comparison is done using the Hilbert-Schmidt norm.
  135. *
  136. * \sa isApprox(), isMuchSmallerThan(const RealScalar&, RealScalar) const
  137. */
  138. template<typename Derived>
  139. template<typename OtherDerived>
  140. bool DenseBase<Derived>::isMuchSmallerThan(
  141. const DenseBase<OtherDerived>& other,
  142. RealScalar prec
  143. ) const
  144. {
  145. return internal::isMuchSmallerThan_object_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
  146. }
  147. #endif // EIGEN_FUZZY_H