Line data Source code
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 : #ifndef INCLUDED_BASEGFX_NUMERIC_FTOOLS_HXX
21 : #define INCLUDED_BASEGFX_NUMERIC_FTOOLS_HXX
22 :
23 : #include <rtl/math.hxx>
24 : #include <basegfx/basegfxdllapi.h>
25 :
26 :
27 : // standard PI defines from solar.h, but we do not want to link against tools
28 :
29 : #ifndef F_PI
30 : #define F_PI M_PI
31 : #endif
32 : #ifndef F_PI2
33 : #define F_PI2 M_PI_2
34 : #endif
35 : #ifndef F_PI4
36 : #define F_PI4 M_PI_4
37 : #endif
38 : #ifndef F_PI180
39 : #define F_PI180 (M_PI/180.0)
40 : #endif
41 : #ifndef F_PI1800
42 : #define F_PI1800 (M_PI/1800.0)
43 : #endif
44 : #ifndef F_PI18000
45 : #define F_PI18000 (M_PI/18000.0)
46 : #endif
47 : #ifndef F_2PI
48 : #define F_2PI (2.0*M_PI)
49 : #endif
50 :
51 :
52 : // fTools defines
53 :
54 : namespace basegfx
55 : {
56 : /** Round double to nearest integer
57 :
58 : @return the nearest integer
59 : */
60 77322149 : inline sal_Int32 fround( double fVal )
61 : {
62 77322149 : return fVal > 0.0 ? static_cast<sal_Int32>( fVal + .5 ) : -static_cast<sal_Int32>( -fVal + .5 );
63 : }
64 :
65 : /** Round double to nearest integer
66 :
67 : @return the nearest 64 bit integer
68 : */
69 16 : inline sal_Int64 fround64( double fVal )
70 : {
71 16 : return fVal > 0.0 ? static_cast<sal_Int64>( fVal + .5 ) : -static_cast<sal_Int64>( -fVal + .5 );
72 : }
73 :
74 : /** Prune a small epsilon range around zero.
75 :
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.
79 :
80 : @param fVal
81 : An arbitrary, but finite and valid number
82 :
83 : @return either fVal, or a small value slightly above (when
84 : fVal>0) or below (when fVal<0) zero.
85 : */
86 0 : inline double pruneScaleValue( double fVal )
87 : {
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
91 : // solution some day.
92 :
93 : //return fVal < 0.0 ?
94 : // (::std::min(fVal,-0.00001)) :
95 : // (::std::max(fVal,0.00001));
96 :
97 0 : if(fVal < 0.0)
98 0 : return (fVal < -0.00001 ? fVal : -0.00001);
99 : else
100 0 : return (fVal > 0.00001 ? fVal : 0.00001);
101 : }
102 :
103 : /** clamp given value against given minimum and maximum values
104 : */
105 8721567 : template <class T> inline const T& clamp(const T& value, const T& minimum, const T& maximum)
106 : {
107 8721567 : if(value < minimum)
108 : {
109 25 : return minimum;
110 : }
111 8721542 : else if(value > maximum)
112 : {
113 5451 : return maximum;
114 : }
115 : else
116 : {
117 8716091 : return value;
118 : }
119 : }
120 :
121 : /** Convert value from degrees to radians
122 : */
123 0 : inline double deg2rad( double v )
124 : {
125 : // divide first, to get exact values for v being a multiple of
126 : // 90 degrees
127 0 : return v / 90.0 * M_PI_2;
128 : }
129 :
130 : /** Convert value radians to degrees
131 : */
132 0 : inline double rad2deg( double v )
133 : {
134 : // divide first, to get exact values for v being a multiple of
135 : // pi/2
136 0 : return v / M_PI_2 * 90.0;
137 : }
138 :
139 : /** Snap v to nearest multiple of fStep, from negative and
140 : positive side.
141 :
142 : Examples:
143 :
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
148 : */
149 : BASEGFX_DLLPUBLIC double snapToNearestMultiple(double v, const double fStep);
150 :
151 : class BASEGFX_DLLPUBLIC fTools
152 : {
153 : public:
154 : /// Get threshold value for equalZero and friends
155 21474419 : static double getSmallValue() { return 0.000000001f; }
156 :
157 : /// Compare against small value
158 21474419 : static bool equalZero(const double& rfVal)
159 : {
160 21474419 : return (fabs(rfVal) <= getSmallValue());
161 : }
162 :
163 : /// Compare against given small value
164 338 : static bool equalZero(const double& rfVal, const double& rfSmallValue)
165 : {
166 338 : return (fabs(rfVal) <= rfSmallValue);
167 : }
168 :
169 56283716 : static bool equal(const double& rfValA, const double& rfValB)
170 : {
171 : // changed to approxEqual usage for better numerical correctness
172 56283716 : return rtl::math::approxEqual(rfValA, rfValB);
173 : }
174 :
175 0 : static bool equal(const double& rfValA, const double& rfValB, const double& rfSmallValue)
176 : {
177 0 : return (fabs(rfValA - rfValB) <= rfSmallValue);
178 : }
179 :
180 6997077 : static bool less(const double& rfValA, const double& rfValB)
181 : {
182 6997077 : return (rfValA < rfValB && !equal(rfValA, rfValB));
183 : }
184 :
185 457647 : static bool lessOrEqual(const double& rfValA, const double& rfValB)
186 : {
187 457647 : return (rfValA < rfValB || equal(rfValA, rfValB));
188 : }
189 :
190 13056247 : static bool more(const double& rfValA, const double& rfValB)
191 : {
192 13056247 : return (rfValA > rfValB && !equal(rfValA, rfValB));
193 : }
194 :
195 1042896 : static bool moreOrEqual(const double& rfValA, const double& rfValB)
196 : {
197 1042896 : return (rfValA > rfValB || equal(rfValA, rfValB));
198 : }
199 : };
200 : } // end of namespace basegfx
201 :
202 : #endif // INCLUDED_BASEGFX_NUMERIC_FTOOLS_HXX
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|