Ponca  cb266528645085a12930c827fbb8336cd937ecbb
Point Cloud Analysis library
Loading...
Searching...
No Matches
floatutils.h
1/*
2This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6#pragma once
7
8#include "defines.h"
9#include <Eigen/Core>
10#include <concepts>
11
12#ifndef PONCA_ABS_EPS
13# define PONCA_ABS_EPS Scalar(1)
14#endif
15
16#ifndef PONCA_REL_EPS
17# define PONCA_REL_EPS Eigen::NumTraits<Scalar>::dummy_precision()
18#endif
19
20namespace Ponca
21{
22 namespace internal
23 {
24 template <typename Derived, typename OtherDerived, typename Scalar = typename Derived::RealScalar>
25 PONCA_MULTIARCH inline bool isApprox(const Eigen::DenseBase<Derived>& x,
26 const Eigen::DenseBase<OtherDerived>& y, Scalar abs = PONCA_ABS_EPS,
27 Scalar rel = PONCA_REL_EPS)
28 {
29 // From eigen isApprox_selector (Core/Fuzzy.h:25 for current Eigen's version)
30 typename Eigen::internal::nested_eval<Derived, 3>::type X(x.derived());
31 typename Eigen::internal::nested_eval<OtherDerived, 3>::type Y(y.derived());
32
33 const Scalar eps2 = rel * rel;
34 const Scalar diff = (X - Y).cwiseAbs2().sum();
35
36 // Add an absolute test here to avoid instabilities near 0
37 if (diff <= rel * abs)
38 return true;
39
40 return diff <= eps2 * X.cwiseAbs2().sum() && diff <= eps2 * Y.cwiseAbs2().sum();
41 }
42
43 template <std::floating_point Scalar>
44 PONCA_MULTIARCH inline bool isApprox(Scalar x, Scalar y, Scalar abs = PONCA_ABS_EPS, Scalar rel = PONCA_REL_EPS)
45 {
46 const Scalar eps2 = rel * rel;
47 const Scalar diff = (x - y) * (x - y);
48
49 if (diff <= eps2 * abs * abs)
50 return true;
51
52 return diff <= eps2 * x * x && diff <= eps2 * y * y;
53 }
54
55 template <std::integral Scalar>
56 PONCA_MULTIARCH inline bool isApprox(Scalar x, Scalar y, Scalar abs = Scalar(0), Scalar rel = Scalar(0))
57 {
58 return x == y;
59 }
60 } // namespace internal
61} // namespace Ponca
Copyright (c) 2022 Jacques-Olivier Lachaud (jacques-olivier.lachaud@univ-savoie.fr) Laboratory of Mat...
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11