Bump version to 21.06.18.1
[LibreOffice.git] / include / basegfx / numeric / ftools.hxx
blob78f4eb1e722fbbc9b6ef72099c6d3881bc9fef6b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <rtl/math.h>
23 #include <cmath>
24 #include <math.h>
25 #include <basegfx/basegfxdllapi.h>
26 #include <limits>
27 #include <algorithm>
29 // standard PI defines from solar.h, but we do not want to link against tools
31 #ifndef F_PI
32 #define F_PI M_PI
33 #endif
34 #ifndef F_PI2
35 #define F_PI2 M_PI_2
36 #endif
37 #ifndef F_PI4
38 #define F_PI4 M_PI_4
39 #endif
40 #ifndef F_PI180
41 #define F_PI180 (M_PI/180.0)
42 #endif
43 #ifndef F_PI1800
44 #define F_PI1800 (M_PI/1800.0)
45 #endif
46 #ifndef F_PI18000
47 #define F_PI18000 (M_PI/18000.0)
48 #endif
49 #ifndef F_2PI
50 #define F_2PI (2.0*M_PI)
51 #endif
54 // fTools defines
56 namespace basegfx
58 /** Round double to nearest integer
60 @return the nearest integer
62 inline sal_Int32 fround( double fVal )
64 if (fVal >= std::numeric_limits<sal_Int32>::max() - .5)
65 return std::numeric_limits<sal_Int32>::max();
66 else if (fVal <= std::numeric_limits<sal_Int32>::min() + .5)
67 return std::numeric_limits<sal_Int32>::min();
68 return fVal > 0.0 ? static_cast<sal_Int32>( fVal + .5 ) : static_cast<sal_Int32>( fVal - .5 );
71 /** Round double to nearest integer
73 @return the nearest 64 bit integer
75 inline sal_Int64 fround64( double fVal )
77 return fVal > 0.0 ? static_cast<sal_Int64>( fVal + .5 ) : -static_cast<sal_Int64>( -fVal + .5 );
80 /** Prune a small epsilon range around zero.
82 Use this method e.g. for calculating scale values. There, it
83 is usually advisable not to set a scaling to 0.0, because that
84 yields singular transformation matrices.
86 @param fVal
87 An arbitrary, but finite and valid number
89 @return either fVal, or a small value slightly above (when
90 fVal>0) or below (when fVal<0) zero.
92 inline double pruneScaleValue( double fVal )
94 // old version used ::std::min/max, but this collides if min is defined as preprocessor
95 // macro which is the case e.g with windows.h headers. The simplest way to avoid this is to
96 // just use the full comparison. I keep the original here, maybe there will be a better
97 // solution some day.
99 //return fVal < 0.0 ?
100 // (::std::min(fVal,-0.00001)) :
101 // (::std::max(fVal,0.00001));
103 if(fVal < 0.0)
104 return std::min(fVal, -0.00001);
105 else
106 return std::max(fVal, 0.00001);
109 /** Convert value from degrees to radians
111 constexpr double deg2rad( double v )
113 // divide first, to get exact values for v being a multiple of
114 // 90 degrees
115 return v / 90.0 * M_PI_2;
118 /** Convert value radians to degrees
120 constexpr double rad2deg( double v )
122 // divide first, to get exact values for v being a multiple of
123 // pi/2
124 return v / M_PI_2 * 90.0;
127 /** Snap v to nearest multiple of fStep, from negative and
128 positive side.
130 Examples:
132 snapToNearestMultiple(-0.1, 0.5) = 0.0
133 snapToNearestMultiple(0.1, 0.5) = 0.0
134 snapToNearestMultiple(0.25, 0.5) = 0.0
135 snapToNearestMultiple(0.26, 0.5) = 0.5
137 BASEGFX_DLLPUBLIC double snapToNearestMultiple(double v, const double fStep);
139 /** Snap v to the range [0.0 .. fWidth] using modulo
141 double snapToZeroRange(double v, double fWidth);
143 /** Snap v to the range [fLow .. fHigh] using modulo
145 double snapToRange(double v, double fLow, double fHigh);
147 /** return fValue with the sign of fSignCarrier, thus evtl. changed
149 inline double copySign(double fValue, double fSignCarrier)
151 #ifdef _WIN32
152 return _copysign(fValue, fSignCarrier);
153 #else
154 return copysign(fValue, fSignCarrier);
155 #endif
158 /** RotateFlyFrame3: Normalize to range defined by [0.0 ... fRange[, independent
159 if v is positive or negative.
161 Examples:
163 normalizeToRange(0.5, -1.0) = 0.0
164 normalizeToRange(0.5, 0.0) = 0.0
165 normalizeToRange(0.5, 1.0) = 0.5
166 normalizeToRange(-0.5, 1.0) = 0.5
167 normalizeToRange(-0.3, 1.0) = 0.7
168 normalizeToRange(-0.7, 1.0) = 0.3
169 normalizeToRange(3.5, 1.0) = 0.5
170 normalizeToRange(3.3, 1.0) = 0.3
171 normalizeToRange(3.7, 1.0) = 0.7
172 normalizeToRange(-3.5, 1.0) = 0.5
173 normalizeToRange(-3.3, 1.0) = 0.7
174 normalizeToRange(-3.7, 1.0) = 0.3
176 BASEGFX_DLLPUBLIC double normalizeToRange(double v, const double fRange);
178 class BASEGFX_DLLPUBLIC fTools
180 public:
181 /// Get threshold value for equalZero and friends
182 static double getSmallValue() { return 0.000000001f; }
184 /// Compare against small value
185 static bool equalZero(const double& rfVal)
187 return (fabs(rfVal) <= getSmallValue());
190 /// Compare against given small value
191 static bool equalZero(const double& rfVal, const double& rfSmallValue)
193 return (fabs(rfVal) <= rfSmallValue);
196 static bool equal(const double& rfValA, const double& rfValB)
198 // changed to approxEqual usage for better numerical correctness
199 return rtl_math_approxEqual(rfValA, rfValB);
202 static bool equal(const double& rfValA, const double& rfValB, const double& rfSmallValue)
204 return (fabs(rfValA - rfValB) <= rfSmallValue);
207 static bool less(const double& rfValA, const double& rfValB)
209 return (rfValA < rfValB && !equal(rfValA, rfValB));
212 static bool lessOrEqual(const double& rfValA, const double& rfValB)
214 return (rfValA < rfValB || equal(rfValA, rfValB));
217 static bool more(const double& rfValA, const double& rfValB)
219 return (rfValA > rfValB && !equal(rfValA, rfValB));
222 static bool moreOrEqual(const double& rfValA, const double& rfValB)
224 return (rfValA > rfValB || equal(rfValA, rfValB));
227 static bool betweenOrEqualEither(const double& rfValA, const double& rfValB, const double& rfValC)
229 return (rfValA > rfValB && rfValA < rfValC) || equal(rfValA, rfValB) || equal(rfValA, rfValC);
233 } // end of namespace basegfx
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */