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
)
42 if (fVal
>= std::numeric_limits
<sal_Int32
>::max() - .5)
43 return std::numeric_limits
<sal_Int32
>::max();
44 return static_cast<sal_Int32
>(fVal
+ .5);
46 if (fVal
<= std::numeric_limits
<sal_Int32
>::min() + .5)
47 return std::numeric_limits
<sal_Int32
>::min();
48 return static_cast<sal_Int32
>(fVal
- .5);
51 /** Round double to nearest integer
53 @return the nearest 64 bit integer
55 inline sal_Int64
fround64( double fVal
)
57 return fVal
> 0.0 ? static_cast<sal_Int64
>( fVal
+ .5 ) : -static_cast<sal_Int64
>( -fVal
+ .5 );
60 /** Prune a small epsilon range around zero.
62 Use this method e.g. for calculating scale values. There, it
63 is usually advisable not to set a scaling to 0.0, because that
64 yields singular transformation matrices.
67 An arbitrary, but finite and valid number
69 @return either fVal, or a small value slightly above (when
70 fVal>0) or below (when fVal<0) zero.
72 inline double pruneScaleValue( double fVal
)
75 return std::min(fVal
, -0.00001);
77 return std::max(fVal
, 0.00001);
80 /** Convert value from degrees to radians
82 template <int DegMultiple
= 1> constexpr double deg2rad( double v
)
84 // divide first, to get exact values for v being a multiple of
86 return v
/ (90.0 * DegMultiple
) * M_PI_2
;
89 /** Convert value radians to degrees
91 template <int DegMultiple
= 1> constexpr double rad2deg( double v
)
93 // divide first, to get exact values for v being a multiple of
95 return v
/ M_PI_2
* (90.0 * DegMultiple
);
98 /** Snap v to nearest multiple of fStep, from negative and
103 snapToNearestMultiple(-0.1, 0.5) = 0.0
104 snapToNearestMultiple(0.1, 0.5) = 0.0
105 snapToNearestMultiple(0.25, 0.5) = 0.0
106 snapToNearestMultiple(0.26, 0.5) = 0.5
108 BASEGFX_DLLPUBLIC
double snapToNearestMultiple(double v
, const double fStep
);
110 /** Snap v to the range [0.0 .. fWidth] using modulo
112 BASEGFX_DLLPUBLIC
double snapToZeroRange(double v
, double fWidth
);
114 /** Snap v to the range [fLow .. fHigh] using modulo
116 double snapToRange(double v
, double fLow
, double fHigh
);
118 /** return fValue with the sign of fSignCarrier, thus evtl. changed
120 inline double copySign(double fValue
, double fSignCarrier
)
123 return _copysign(fValue
, fSignCarrier
);
125 return copysign(fValue
, fSignCarrier
);
129 /** RotateFlyFrame3: Normalize to range defined by [0.0 ... fRange[, independent
130 if v is positive or negative.
134 normalizeToRange(0.5, -1.0) = 0.0
135 normalizeToRange(0.5, 0.0) = 0.0
136 normalizeToRange(0.5, 1.0) = 0.5
137 normalizeToRange(-0.5, 1.0) = 0.5
138 normalizeToRange(-0.3, 1.0) = 0.7
139 normalizeToRange(-0.7, 1.0) = 0.3
140 normalizeToRange(3.5, 1.0) = 0.5
141 normalizeToRange(3.3, 1.0) = 0.3
142 normalizeToRange(3.7, 1.0) = 0.7
143 normalizeToRange(-3.5, 1.0) = 0.5
144 normalizeToRange(-3.3, 1.0) = 0.7
145 normalizeToRange(-3.7, 1.0) = 0.3
147 BASEGFX_DLLPUBLIC
double normalizeToRange(double v
, const double fRange
);
151 /// Get threshold value for equalZero and friends
152 inline double getSmallValue() { return 0.000000001f
; }
154 /// Compare against small value
155 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
156 inline bool equalZero(const T
& rfVal
)
158 return (fabs(rfVal
) <= getSmallValue());
161 /// Compare against given small value
162 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
163 inline bool equalZero(const T
& rfVal
, const T
& rfSmallValue
)
165 return (fabs(rfVal
) <= rfSmallValue
);
168 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
169 inline bool equal(T
const& rfValA
, T
const& rfValB
)
171 // changed to approxEqual usage for better numerical correctness
172 return rtl_math_approxEqual(rfValA
, rfValB
);
175 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
176 inline bool equal(const T
& rfValA
, const T
& rfValB
, const T
& rfSmallValue
)
178 return (fabs(rfValA
- rfValB
) <= rfSmallValue
);
181 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
182 inline bool less(const T
& rfValA
, const T
& rfValB
)
184 return (rfValA
< rfValB
&& !equal(rfValA
, rfValB
));
187 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
188 inline bool lessOrEqual(const T
& rfValA
, const T
& rfValB
)
190 return (rfValA
< rfValB
|| equal(rfValA
, rfValB
));
193 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
194 inline bool more(const T
& rfValA
, const T
& rfValB
)
196 return (rfValA
> rfValB
&& !equal(rfValA
, rfValB
));
199 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
200 inline bool moreOrEqual(const T
& rfValA
, const T
& rfValB
)
202 return (rfValA
> rfValB
|| equal(rfValA
, rfValB
));
205 template <typename T
, std::enable_if_t
<std::is_floating_point_v
<T
>, int> = 0>
206 inline bool betweenOrEqualEither(const T
& rfValA
, const T
& rfValB
, const T
& rfValC
)
208 return (rfValA
> rfValB
&& rfValA
< rfValC
) || equal(rfValA
, rfValB
) || equal(rfValA
, rfValC
);
211 } // end of namespace basegfx
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */