LibreOffice Module basegfx (master) 1
ftools.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include <rtl/math.h>
23#include <cmath>
24#include <math.h>
26#include <limits>
27#include <algorithm>
28
29
30// fTools defines
31
32namespace basegfx
33{
38 inline sal_Int32 fround( double fVal )
39 {
40 if (fVal >= 0.0)
41 {
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);
45 }
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);
49 }
50
55 inline sal_Int64 fround64( double fVal )
56 {
57 return fVal > 0.0 ? static_cast<sal_Int64>( fVal + .5 ) : -static_cast<sal_Int64>( -fVal + .5 );
58 }
59
72 inline double pruneScaleValue( double fVal )
73 {
74 if(fVal < 0.0)
75 return std::min(fVal, -0.00001);
76 else
77 return std::max(fVal, 0.00001);
78 }
79
82 template <int DegMultiple = 1> constexpr double deg2rad( double v )
83 {
84 // divide first, to get exact values for v being a multiple of
85 // 90 degrees
86 return v / (90.0 * DegMultiple) * M_PI_2;
87 }
88
91 template <int DegMultiple = 1> constexpr double rad2deg( double v )
92 {
93 // divide first, to get exact values for v being a multiple of
94 // pi/2
95 return v / M_PI_2 * (90.0 * DegMultiple);
96 }
97
108 BASEGFX_DLLPUBLIC double snapToNearestMultiple(double v, const double fStep);
109
112 BASEGFX_DLLPUBLIC double snapToZeroRange(double v, double fWidth);
113
116 double snapToRange(double v, double fLow, double fHigh);
117
120 inline double copySign(double fValue, double fSignCarrier)
121 {
122#ifdef _WIN32
123 return _copysign(fValue, fSignCarrier);
124#else
125 return copysign(fValue, fSignCarrier);
126#endif
127 }
128
147 BASEGFX_DLLPUBLIC double normalizeToRange(double v, const double fRange);
148
149 namespace fTools
150 {
152 inline double getSmallValue() { return 0.000000001f; }
153
155 template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
156 inline bool equalZero(const T& rfVal)
157 {
158 return (fabs(rfVal) <= getSmallValue());
159 }
160
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)
164 {
165 return (fabs(rfVal) <= rfSmallValue);
166 }
167
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)
170 {
171 // changed to approxEqual usage for better numerical correctness
172 return rtl_math_approxEqual(rfValA, rfValB);
173 }
174
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)
177 {
178 return (fabs(rfValA - rfValB) <= rfSmallValue);
179 }
180
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)
183 {
184 return (rfValA < rfValB && !equal(rfValA, rfValB));
185 }
186
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)
189 {
190 return (rfValA < rfValB || equal(rfValA, rfValB));
191 }
192
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)
195 {
196 return (rfValA > rfValB && !equal(rfValA, rfValB));
197 }
198
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)
201 {
202 return (rfValA > rfValB || equal(rfValA, rfValB));
203 }
204
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)
207 {
208 return (rfValA > rfValB && rfValA < rfValC) || equal(rfValA, rfValB) || equal(rfValA, rfValC);
209 }
210 };
211} // end of namespace basegfx
212
213/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define BASEGFX_DLLPUBLIC
Definition: basegfxdllapi.h:35
float v
bool lessOrEqual(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:188
bool more(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:194
bool equalZero(const T &rfVal)
Compare against small value.
Definition: ftools.hxx:156
bool betweenOrEqualEither(const T &rfValA, const T &rfValB, const T &rfValC)
Definition: ftools.hxx:206
bool equal(T const &rfValA, T const &rfValB)
Definition: ftools.hxx:169
bool less(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:182
double getSmallValue()
Get threshold value for equalZero and friends.
Definition: ftools.hxx:152
bool moreOrEqual(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:200
double snapToNearestMultiple(double v, const double fStep)
Snap v to nearest multiple of fStep, from negative and positive side.
Definition: ftools.cxx:24
double normalizeToRange(double v, const double fRange)
RotateFlyFrame3: Normalize to range defined by [0.0 ... fRange[, independent if v is positive or nega...
Definition: ftools.cxx:100
double pruneScaleValue(double fVal)
Prune a small epsilon range around zero.
Definition: ftools.hxx:72
constexpr double rad2deg(double v)
Convert value radians to degrees.
Definition: ftools.hxx:91
double copySign(double fValue, double fSignCarrier)
return fValue with the sign of fSignCarrier, thus evtl.
Definition: ftools.hxx:120
B2IRange fround(const B2DRange &rRange)
Round double to nearest integer for 2D range.
Definition: b2drange.cxx:64
double snapToRange(double v, double fLow, double fHigh)
Snap v to the range [fLow .
Definition: ftools.cxx:74
constexpr double deg2rad(double v)
Convert value from degrees to radians.
Definition: ftools.hxx:82
double snapToZeroRange(double v, double fWidth)
Snap v to the range [0.0 .
Definition: ftools.cxx:47
sal_Int64 fround64(double fVal)
Round double to nearest integer.
Definition: ftools.hxx:55