1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef _BGFX_NUMERIC_FTOOLS_HXX
21 #define _BGFX_NUMERIC_FTOOLS_HXX
23 #include <rtl/math.hxx>
24 #include <basegfx/basegfxdllapi.h>
26 //////////////////////////////////////////////////////////////////////////////
27 // standard PI defines from solar.h, but we do not want to link against tools
39 #define F_PI180 (M_PI/180.0)
42 #define F_PI1800 (M_PI/1800.0)
45 #define F_PI18000 (M_PI/18000.0)
48 #define F_2PI (2.0*M_PI)
51 //////////////////////////////////////////////////////////////////////////////
56 /** Round double to nearest integer
58 @return the nearest integer
60 inline sal_Int32
fround( double fVal
)
62 return fVal
> 0.0 ? static_cast<sal_Int32
>( fVal
+ .5 ) : -static_cast<sal_Int32
>( -fVal
+ .5 );
65 /** Round double to nearest integer
67 @return the nearest 64 bit integer
69 inline sal_Int64
fround64( double fVal
)
71 return fVal
> 0.0 ? static_cast<sal_Int64
>( fVal
+ .5 ) : -static_cast<sal_Int64
>( -fVal
+ .5 );
74 /** Prune a small epsilon range around zero.
76 Use this method e.g. for calculating scale values. There, it
77 is usually advisable not to set a scaling to 0.0, because that
78 yields singular transformation matrices.
81 An arbitrary, but finite and valid number
83 @return either fVal, or a small value slightly above (when
84 fVal>0) or below (when fVal<0) zero.
86 inline double pruneScaleValue( double fVal
)
88 // old version used ::std::min/max, but this collides if min is defined as preprocessor
89 // macro which is the case e.g with windows.h headers. The simplest way to avoid this is to
90 // just use the full comparison. I keep the original here, maybe there will be a better
94 // (::std::min(fVal,-0.00001)) :
95 // (::std::max(fVal,0.00001));
98 return (fVal
< -0.00001 ? fVal
: -0.00001);
100 return (fVal
> 0.00001 ? fVal
: 0.00001);
103 /** clamp given value against given minimum and maximum values
105 template <class T
> inline const T
& clamp(const T
& value
, const T
& minimum
, const T
& maximum
)
111 else if(value
> maximum
)
121 /** Convert value from degrees to radians
123 inline double deg2rad( double v
)
125 // divide first, to get exact values for v being a multiple of
127 return v
/ 90.0 * M_PI_2
;
130 /** Convert value radians to degrees
132 inline double rad2deg( double v
)
134 // divide first, to get exact values for v being a multiple of
136 return v
/ M_PI_2
* 90.0;
139 /** Snap v to nearest multiple of fStep, from negative and
144 snapToNearestMultiple(-0.1, 0.5) = 0.0
145 snapToNearestMultiple(0.1, 0.5) = 0.0
146 snapToNearestMultiple(0.25, 0.5) = 0.0
147 snapToNearestMultiple(0.26, 0.5) = 0.5
149 double snapToNearestMultiple(double v
, const double fStep
);
151 /** Snap v to the range [0.0 .. fWidth] using modulo
153 double snapToZeroRange(double v
, double fWidth
);
155 /** Snap v to the range [fLow .. fHigh] using modulo
157 double snapToRange(double v
, double fLow
, double fHigh
);
159 /** return fValue with the sign of fSignCarrier, thus evtl. changed
161 inline double copySign(double fValue
, double fSignCarrier
)
164 return _copysign(fValue
, fSignCarrier
);
166 return copysign(fValue
, fSignCarrier
);
170 class BASEGFX_DLLPUBLIC fTools
172 /// Threshold value for equalZero()
173 static double mfSmallValue
;
176 /// Get threshold value for equalZero and friends
177 static double getSmallValue() { return mfSmallValue
; }
178 /// Set threshold value for equalZero and friends
179 static void setSmallValue(const double& rfNew
) { mfSmallValue
= rfNew
; }
181 /// Compare against small value
182 static bool equalZero(const double& rfVal
)
184 return (fabs(rfVal
) <= getSmallValue());
187 /// Compare against given small value
188 static bool equalZero(const double& rfVal
, const double& rfSmallValue
)
190 return (fabs(rfVal
) <= rfSmallValue
);
193 static bool equal(const double& rfValA
, const double& rfValB
)
195 // changed to approxEqual usage for better numerical correctness
196 return rtl::math::approxEqual(rfValA
, rfValB
);
199 static bool equal(const double& rfValA
, const double& rfValB
, const double& rfSmallValue
)
201 return (fabs(rfValA
- rfValB
) <= rfSmallValue
);
204 static bool less(const double& rfValA
, const double& rfValB
)
206 return (rfValA
< rfValB
&& !equal(rfValA
, rfValB
));
209 static bool lessOrEqual(const double& rfValA
, const double& rfValB
)
211 return (rfValA
< rfValB
|| equal(rfValA
, rfValB
));
214 static bool more(const double& rfValA
, const double& rfValB
)
216 return (rfValA
> rfValB
&& !equal(rfValA
, rfValB
));
219 static bool moreOrEqual(const double& rfValA
, const double& rfValB
)
221 return (rfValA
> rfValB
|| equal(rfValA
, rfValB
));
224 } // end of namespace basegfx
226 #endif /* _BGFX_NUMERIC_FTOOLS_HXX */
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */