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 .
25 #include <basegfx/basegfxdllapi.h>
34 /** Round double to nearest integer
36 @return the nearest integer
38 inline sal_Int32
fround( double fVal
)
40 if (fVal
>= std::numeric_limits
<sal_Int32
>::max() - .5)
41 return std::numeric_limits
<sal_Int32
>::max();
42 else if (fVal
<= std::numeric_limits
<sal_Int32
>::min() + .5)
43 return std::numeric_limits
<sal_Int32
>::min();
44 return fVal
> 0.0 ? static_cast<sal_Int32
>( fVal
+ .5 ) : static_cast<sal_Int32
>( fVal
- .5 );
47 /** Round double to nearest integer
49 @return the nearest 64 bit integer
51 inline sal_Int64
fround64( double fVal
)
53 return fVal
> 0.0 ? static_cast<sal_Int64
>( fVal
+ .5 ) : -static_cast<sal_Int64
>( -fVal
+ .5 );
56 /** Prune a small epsilon range around zero.
58 Use this method e.g. for calculating scale values. There, it
59 is usually advisable not to set a scaling to 0.0, because that
60 yields singular transformation matrices.
63 An arbitrary, but finite and valid number
65 @return either fVal, or a small value slightly above (when
66 fVal>0) or below (when fVal<0) zero.
68 inline double pruneScaleValue( double fVal
)
71 return std::min(fVal
, -0.00001);
73 return std::max(fVal
, 0.00001);
76 /** Convert value from degrees to radians
78 template <int DegMultiple
= 1> constexpr double deg2rad( double v
)
80 // divide first, to get exact values for v being a multiple of
82 return v
/ (90.0 * DegMultiple
) * M_PI_2
;
85 /** Convert value radians to degrees
87 template <int DegMultiple
= 1> constexpr double rad2deg( double v
)
89 // divide first, to get exact values for v being a multiple of
91 return v
/ M_PI_2
* (90.0 * DegMultiple
);
94 /** Snap v to nearest multiple of fStep, from negative and
99 snapToNearestMultiple(-0.1, 0.5) = 0.0
100 snapToNearestMultiple(0.1, 0.5) = 0.0
101 snapToNearestMultiple(0.25, 0.5) = 0.0
102 snapToNearestMultiple(0.26, 0.5) = 0.5
104 BASEGFX_DLLPUBLIC
double snapToNearestMultiple(double v
, const double fStep
);
106 /** Snap v to the range [0.0 .. fWidth] using modulo
108 double snapToZeroRange(double v
, double fWidth
);
110 /** Snap v to the range [fLow .. fHigh] using modulo
112 double snapToRange(double v
, double fLow
, double fHigh
);
114 /** return fValue with the sign of fSignCarrier, thus evtl. changed
116 inline double copySign(double fValue
, double fSignCarrier
)
119 return _copysign(fValue
, fSignCarrier
);
121 return copysign(fValue
, fSignCarrier
);
125 /** RotateFlyFrame3: Normalize to range defined by [0.0 ... fRange[, independent
126 if v is positive or negative.
130 normalizeToRange(0.5, -1.0) = 0.0
131 normalizeToRange(0.5, 0.0) = 0.0
132 normalizeToRange(0.5, 1.0) = 0.5
133 normalizeToRange(-0.5, 1.0) = 0.5
134 normalizeToRange(-0.3, 1.0) = 0.7
135 normalizeToRange(-0.7, 1.0) = 0.3
136 normalizeToRange(3.5, 1.0) = 0.5
137 normalizeToRange(3.3, 1.0) = 0.3
138 normalizeToRange(3.7, 1.0) = 0.7
139 normalizeToRange(-3.5, 1.0) = 0.5
140 normalizeToRange(-3.3, 1.0) = 0.7
141 normalizeToRange(-3.7, 1.0) = 0.3
143 BASEGFX_DLLPUBLIC
double normalizeToRange(double v
, const double fRange
);
147 /// Get threshold value for equalZero and friends
148 inline double getSmallValue() { return 0.000000001f
; }
150 /// Compare against small value
151 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
152 inline bool equalZero(const T
& rfVal
)
154 return (fabs(rfVal
) <= getSmallValue());
157 /// Compare against given small value
158 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
159 inline bool equalZero(const T
& rfVal
, const T
& rfSmallValue
)
161 return (fabs(rfVal
) <= rfSmallValue
);
164 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
165 inline bool equal(T
const& rfValA
, T
const& rfValB
)
167 // changed to approxEqual usage for better numerical correctness
168 return rtl_math_approxEqual(rfValA
, rfValB
);
171 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
172 inline bool equal(const T
& rfValA
, const T
& rfValB
, const T
& rfSmallValue
)
174 return (fabs(rfValA
- rfValB
) <= rfSmallValue
);
177 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
178 inline bool less(const T
& rfValA
, const T
& rfValB
)
180 return (rfValA
< rfValB
&& !equal(rfValA
, rfValB
));
183 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
184 inline bool lessOrEqual(const T
& rfValA
, const T
& rfValB
)
186 return (rfValA
< rfValB
|| equal(rfValA
, rfValB
));
189 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
190 inline bool more(const T
& rfValA
, const T
& rfValB
)
192 return (rfValA
> rfValB
&& !equal(rfValA
, rfValB
));
195 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
196 inline bool moreOrEqual(const T
& rfValA
, const T
& rfValB
)
198 return (rfValA
> rfValB
|| equal(rfValA
, rfValB
));
201 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
202 inline bool betweenOrEqualEither(const T
& rfValA
, const T
& rfValB
, const T
& rfValC
)
204 return (rfValA
> rfValB
&& rfValA
< rfValC
) || equal(rfValA
, rfValB
) || equal(rfValA
, rfValC
);
207 } // end of namespace basegfx
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */