| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef BASE_PRECISION_H |
| 27 |
#define BASE_PRECISION_H |
| 28 |
|
| 29 |
#include <cmath> |
| 30 |
|
| 31 |
namespace Base |
| 32 |
{ |
| 33 |
|
| 34 |
// The methods are copied from OCC's Precision class |
| 35 |
class Precision |
| 36 |
{ |
| 37 |
public: |
| 38 |
/*! |
| 39 |
* \brief Angular |
| 40 |
* Returns the recommended precision value when checking the equality of two angles (given in |
| 41 |
* radians). \return |
| 42 |
*/ |
| 43 |
static double Angular() |
| 44 |
{ |
| 45 |
return 1.e-12; |
| 46 |
} |
| 47 |
|
| 48 |
/*! |
| 49 |
* \brief Confusion |
| 50 |
* Returns the recommended precision value when checking coincidence of two points in real |
| 51 |
* space. \return |
| 52 |
*/ |
| 53 |
static double Confusion() |
| 54 |
{ |
| 55 |
return 1.e-7; |
| 56 |
} |
| 57 |
|
| 58 |
/*! |
| 59 |
* \brief SquareConfusion |
| 60 |
* Returns square of \ref Confusion. |
| 61 |
* \return |
| 62 |
*/ |
| 63 |
static double SquareConfusion() |
| 64 |
{ |
| 65 |
return Confusion() * Confusion(); |
| 66 |
} |
| 67 |
|
| 68 |
/*! |
| 69 |
* \brief Intersection |
| 70 |
* Returns the precision value in real space, frequently |
| 71 |
* used by intersection algorithms to decide that a solution is reached. |
| 72 |
* \return |
| 73 |
*/ |
| 74 |
static double Intersection() |
| 75 |
{ |
| 76 |
return Confusion() * 0.01; |
| 77 |
} |
| 78 |
|
| 79 |
/*! |
| 80 |
* \brief Approximation |
| 81 |
* Returns the precision value in real space, frequently used |
| 82 |
* by approximation algorithms. |
| 83 |
* \return |
| 84 |
*/ |
| 85 |
static double Approximation() |
| 86 |
{ |
| 87 |
return Confusion() * 10.0; |
| 88 |
} |
| 89 |
|
| 90 |
/*! |
| 91 |
* \brief Parametric |
| 92 |
* Convert a real space precision to a parametric space precision. |
| 93 |
* \param P |
| 94 |
* \param T |
| 95 |
* \return |
| 96 |
*/ |
| 97 |
static double Parametric(const double P, const double T) |
| 98 |
{ |
| 99 |
return P / T; |
| 100 |
} |
| 101 |
|
| 102 |
/*! |
| 103 |
* \brief PConfusion |
| 104 |
* Returns a precision value in parametric space. |
| 105 |
* \param T |
| 106 |
* \return |
| 107 |
*/ |
| 108 |
static double PConfusion(const double T) |
| 109 |
{ |
| 110 |
return Parametric(Confusion(), T); |
| 111 |
} |
| 112 |
|
| 113 |
/*! |
| 114 |
* \brief PConfusion |
| 115 |
* Used to test distances in parametric space on a default curve. |
| 116 |
* \return |
| 117 |
*/ |
| 118 |
static double PConfusion() |
| 119 |
{ |
| 120 |
return Parametric(Confusion()); |
| 121 |
} |
| 122 |
|
| 123 |
/*! |
| 124 |
* \brief SquarePConfusion |
| 125 |
* Returns square of \ref PConfusion. |
| 126 |
* \return |
| 127 |
*/ |
| 128 |
static double SquarePConfusion() |
| 129 |
{ |
| 130 |
return PConfusion() * PConfusion(); |
| 131 |
} |
| 132 |
|
| 133 |
/*! |
| 134 |
* \brief PIntersection |
| 135 |
* Returns a precision value in parametric space, which may be used by intersection algorithms, |
| 136 |
* to decide that a solution is reached. |
| 137 |
* \param T |
| 138 |
* \return |
| 139 |
*/ |
| 140 |
static double PIntersection(const double T) |
| 141 |
{ |
| 142 |
return Parametric(Intersection(), T); |
| 143 |
} |
| 144 |
|
| 145 |
/*! |
| 146 |
* \brief PApproximation |
| 147 |
* Returns a precision value in parametric space, which may be used by approximation |
| 148 |
* algorithms. \param T \return |
| 149 |
*/ |
| 150 |
static double PApproximation(const double T) |
| 151 |
{ |
| 152 |
return Parametric(Approximation(), T); |
| 153 |
} |
| 154 |
|
| 155 |
/*! |
| 156 |
* \brief Parametric |
| 157 |
* Convert a real space precision to a parametric space precision on a default curve. |
| 158 |
* \param P |
| 159 |
* \return |
| 160 |
*/ |
| 161 |
static double Parametric(const double P) |
| 162 |
{ |
| 163 |
return Parametric(P, 100.0); |
| 164 |
} |
| 165 |
|
| 166 |
/*! |
| 167 |
* \brief PIntersection |
| 168 |
* Used for Intersections in parametric space on a default curve. |
| 169 |
* \return |
| 170 |
*/ |
| 171 |
static double PIntersection() |
| 172 |
{ |
| 173 |
return Parametric(Intersection()); |
| 174 |
} |
| 175 |
|
| 176 |
/*! |
| 177 |
* \brief PApproximation |
| 178 |
* Used for Approximations in parametric space on a default curve. |
| 179 |
* \return |
| 180 |
*/ |
| 181 |
static double PApproximation() |
| 182 |
{ |
| 183 |
return Parametric(Approximation()); |
| 184 |
} |
| 185 |
|
| 186 |
/*! |
| 187 |
* \brief IsInfinite |
| 188 |
* Returns True if R may be considered as an infinite number. Currently Abs(R) > 1e100 |
| 189 |
* \param R |
| 190 |
* \return |
| 191 |
*/ |
| 192 |
static bool IsInfinite(const double R) |
| 193 |
{ |
| 194 |
return std::fabs(R) >= (0.5 * Precision::Infinite()); |
| 195 |
} |
| 196 |
|
| 197 |
/*! |
| 198 |
* \brief IsPositiveInfinite |
| 199 |
* Returns True if R may be considered as a positive infinite number. Currently R > 1e100 |
| 200 |
* \param R |
| 201 |
* \return |
| 202 |
*/ |
| 203 |
static bool IsPositiveInfinite(const double R) |
| 204 |
{ |
| 205 |
return R >= (0.5 * Precision::Infinite()); |
| 206 |
} |
| 207 |
|
| 208 |
/*! |
| 209 |
* \brief IsNegativeInfinite |
| 210 |
* Returns True if R may be considered as a negative infinite number. Currently R < -1e100 |
| 211 |
* \param R |
| 212 |
* \return |
| 213 |
*/ |
| 214 |
static bool IsNegativeInfinite(const double R) |
| 215 |
{ |
| 216 |
return R <= -(0.5 * Precision::Infinite()); |
| 217 |
} |
| 218 |
|
| 219 |
/*! |
| 220 |
* \brief Infinite |
| 221 |
* Returns a big number that can be considered as infinite. Use -Infinite() for a negative |
| 222 |
* big number. \return |
| 223 |
*/ |
| 224 |
static double Infinite() |
| 225 |
{ |
| 226 |
return 2.e+100; |
| 227 |
} |
| 228 |
}; |
| 229 |
|
| 230 |
} // namespace Base |
| 231 |
|
| 232 |
#endif // BASE_PRECISION_H |