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 0 : inline sal_Int32 fround( double fVal )
61 : {
62 0 : 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 0 : inline sal_Int64 fround64( double fVal )
70 : {
71 0 : 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 0 : template <class T> inline const T& clamp(const T& value, const T& minimum, const T& maximum)
106 : {
107 0 : if(value < minimum)
108 : {
109 0 : return minimum;
110 : }
111 0 : else if(value > maximum)
112 : {
113 0 : return maximum;
114 : }
115 : else
116 : {
117 0 : 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 : /** return fValue with the sign of fSignCarrier, thus evtl. changed
152 : */
153 : inline double copySign(double fValue, double fSignCarrier)
154 : {
155 : #ifdef WNT
156 : return _copysign(fValue, fSignCarrier);
157 : #else
158 : return copysign(fValue, fSignCarrier);
159 : #endif
160 : }
161 :
162 : class BASEGFX_DLLPUBLIC fTools
163 : {
164 : public:
165 : /// Get threshold value for equalZero and friends
166 0 : static double getSmallValue() { return 0.000000001f; }
167 :
168 : /// Compare against small value
169 0 : static bool equalZero(const double& rfVal)
170 : {
171 0 : return (fabs(rfVal) <= getSmallValue());
172 : }
173 :
174 : /// Compare against given small value
175 0 : static bool equalZero(const double& rfVal, const double& rfSmallValue)
176 : {
177 0 : return (fabs(rfVal) <= rfSmallValue);
178 : }
179 :
180 0 : static bool equal(const double& rfValA, const double& rfValB)
181 : {
182 : // changed to approxEqual usage for better numerical correctness
183 0 : return rtl::math::approxEqual(rfValA, rfValB);
184 : }
185 :
186 0 : static bool equal(const double& rfValA, const double& rfValB, const double& rfSmallValue)
187 : {
188 0 : return (fabs(rfValA - rfValB) <= rfSmallValue);
189 : }
190 :
191 0 : static bool less(const double& rfValA, const double& rfValB)
192 : {
193 0 : return (rfValA < rfValB && !equal(rfValA, rfValB));
194 : }
195 :
196 0 : static bool lessOrEqual(const double& rfValA, const double& rfValB)
197 : {
198 0 : return (rfValA < rfValB || equal(rfValA, rfValB));
199 : }
200 :
201 0 : static bool more(const double& rfValA, const double& rfValB)
202 : {
203 0 : return (rfValA > rfValB && !equal(rfValA, rfValB));
204 : }
205 :
206 0 : static bool moreOrEqual(const double& rfValA, const double& rfValB)
207 : {
208 0 : return (rfValA > rfValB || equal(rfValA, rfValB));
209 : }
210 : };
211 : } // end of namespace basegfx
212 :
213 : #endif // INCLUDED_BASEGFX_NUMERIC_FTOOLS_HXX
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|