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 _BGFX_NUMERIC_FTOOLS_HXX
21 : #define _BGFX_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 1243322 : inline sal_Int32 fround( double fVal )
61 : {
62 1243322 : 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 : inline double rad2deg( double v )
133 : {
134 : // divide first, to get exact values for v being a multiple of
135 : // pi/2
136 : return v / M_PI_2 * 90.0;
137 : }
138 :
139 :
140 : class BASEGFX_DLLPUBLIC fTools
141 : {
142 : /// Threshold value for equalZero()
143 : static double mfSmallValue;
144 :
145 : public:
146 : /// Get threshold value for equalZero and friends
147 8932 : static double getSmallValue() { return mfSmallValue; }
148 : /// Set threshold value for equalZero and friends
149 : static void setSmallValue(const double& rfNew) { mfSmallValue = rfNew; }
150 :
151 : /// Compare against small value
152 8932 : static bool equalZero(const double& rfVal)
153 : {
154 8932 : return (fabs(rfVal) <= getSmallValue());
155 : }
156 :
157 : /// Compare against given small value
158 0 : static bool equalZero(const double& rfVal, const double& rfSmallValue)
159 : {
160 0 : return (fabs(rfVal) <= rfSmallValue);
161 : }
162 :
163 7135 : static bool equal(const double& rfValA, const double& rfValB)
164 : {
165 : // changed to approxEqual usage for better numerical correctness
166 7135 : return rtl::math::approxEqual(rfValA, rfValB);
167 : }
168 :
169 0 : static bool equal(const double& rfValA, const double& rfValB, const double& rfSmallValue)
170 : {
171 0 : return (fabs(rfValA - rfValB) <= rfSmallValue);
172 : }
173 :
174 2111 : static bool less(const double& rfValA, const double& rfValB)
175 : {
176 2111 : return (rfValA < rfValB && !equal(rfValA, rfValB));
177 : }
178 :
179 12 : static bool lessOrEqual(const double& rfValA, const double& rfValB)
180 : {
181 12 : return (rfValA < rfValB || equal(rfValA, rfValB));
182 : }
183 :
184 169 : static bool more(const double& rfValA, const double& rfValB)
185 : {
186 169 : return (rfValA > rfValB && !equal(rfValA, rfValB));
187 : }
188 :
189 0 : static bool moreOrEqual(const double& rfValA, const double& rfValB)
190 : {
191 0 : return (rfValA > rfValB || equal(rfValA, rfValB));
192 : }
193 : };
194 : } // end of namespace basegfx
195 :
196 : #endif /* _BGFX_NUMERIC_FTOOLS_HXX */
197 :
198 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|