Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _BGFX_NUMERIC_FTOOLS_HXX
30 : : #define _BGFX_NUMERIC_FTOOLS_HXX
31 : :
32 : : #include <rtl/math.hxx>
33 : : #include <basegfx/basegfxdllapi.h>
34 : :
35 : : //////////////////////////////////////////////////////////////////////////////
36 : : // standard PI defines from solar.h, but we do not want to link against tools
37 : :
38 : : #ifndef F_PI
39 : : #define F_PI M_PI
40 : : #endif
41 : : #ifndef F_PI2
42 : : #define F_PI2 M_PI_2
43 : : #endif
44 : : #ifndef F_PI4
45 : : #define F_PI4 M_PI_4
46 : : #endif
47 : : #ifndef F_PI180
48 : : #define F_PI180 (M_PI/180.0)
49 : : #endif
50 : : #ifndef F_PI1800
51 : : #define F_PI1800 (M_PI/1800.0)
52 : : #endif
53 : : #ifndef F_PI18000
54 : : #define F_PI18000 (M_PI/18000.0)
55 : : #endif
56 : : #ifndef F_2PI
57 : : #define F_2PI (2.0*M_PI)
58 : : #endif
59 : :
60 : : //////////////////////////////////////////////////////////////////////////////
61 : : // fTools defines
62 : :
63 : : namespace basegfx
64 : : {
65 : : /** Round double to nearest integer
66 : :
67 : : @return the nearest integer
68 : : */
69 : 61113727 : inline sal_Int32 fround( double fVal )
70 : : {
71 [ + + ]: 61113727 : return fVal > 0.0 ? static_cast<sal_Int32>( fVal + .5 ) : -static_cast<sal_Int32>( -fVal + .5 );
72 : : }
73 : :
74 : : /** Round double to nearest integer
75 : :
76 : : @return the nearest 64 bit integer
77 : : */
78 : 0 : inline sal_Int64 fround64( double fVal )
79 : : {
80 [ # # ]: 0 : return fVal > 0.0 ? static_cast<sal_Int64>( fVal + .5 ) : -static_cast<sal_Int64>( -fVal + .5 );
81 : : }
82 : :
83 : : /** Prune a small epsilon range around zero.
84 : :
85 : : Use this method e.g. for calculating scale values. There, it
86 : : is usually advisable not to set a scaling to 0.0, because that
87 : : yields singular transformation matrices.
88 : :
89 : : @param fVal
90 : : An arbitrary, but finite and valid number
91 : :
92 : : @return either fVal, or a small value slightly above (when
93 : : fVal>0) or below (when fVal<0) zero.
94 : : */
95 : 0 : inline double pruneScaleValue( double fVal )
96 : : {
97 : : // old version used ::std::min/max, but this collides if min is defined as preprocessor
98 : : // macro which is the case e.g with windows.h headers. The simplest way to avoid this is to
99 : : // just use the full comparison. I keep the original here, maybe there will be a better
100 : : // solution some day.
101 : : //
102 : : //return fVal < 0.0 ?
103 : : // (::std::min(fVal,-0.00001)) :
104 : : // (::std::max(fVal,0.00001));
105 : :
106 : 0 : if(fVal < 0.0)
107 : 0 : return (fVal < -0.00001 ? fVal : -0.00001);
108 : : else
109 : 0 : return (fVal > 0.00001 ? fVal : 0.00001);
110 : : }
111 : :
112 : : /** clamp given value against given minimum and maximum values
113 : : */
114 : 6688179 : template <class T> inline const T& clamp(const T& value, const T& minimum, const T& maximum)
115 : : {
116 [ + + ]: 6688179 : if(value < minimum)
117 : : {
118 : 20 : return minimum;
119 : : }
120 [ + + ]: 6688159 : else if(value > maximum)
121 : : {
122 : 227 : return maximum;
123 : : }
124 : : else
125 : : {
126 : 6688179 : return value;
127 : : }
128 : : }
129 : :
130 : : /** Convert value from degrees to radians
131 : : */
132 : 0 : inline double deg2rad( double v )
133 : : {
134 : : // divide first, to get exact values for v being a multiple of
135 : : // 90 degrees
136 : 0 : return v / 90.0 * M_PI_2;
137 : : }
138 : :
139 : : /** Convert value radians to degrees
140 : : */
141 : : inline double rad2deg( double v )
142 : : {
143 : : // divide first, to get exact values for v being a multiple of
144 : : // pi/2
145 : : return v / M_PI_2 * 90.0;
146 : : }
147 : :
148 : :
149 : : class BASEGFX_DLLPUBLIC fTools
150 : : {
151 : : /// Threshold value for equalZero()
152 : : static double mfSmallValue;
153 : :
154 : : public:
155 : : /// Get threshold value for equalZero and friends
156 : 583882 : static double getSmallValue() { return mfSmallValue; }
157 : : /// Set threshold value for equalZero and friends
158 : : static void setSmallValue(const double& rfNew) { mfSmallValue = rfNew; }
159 : :
160 : : /// Compare against small value
161 : 583882 : static bool equalZero(const double& rfVal)
162 : : {
163 : 583882 : return (fabs(rfVal) <= getSmallValue());
164 : : }
165 : :
166 : : /// Compare against given small value
167 : 0 : static bool equalZero(const double& rfVal, const double& rfSmallValue)
168 : : {
169 : 0 : return (fabs(rfVal) <= rfSmallValue);
170 : : }
171 : :
172 : 3306846 : static bool equal(const double& rfValA, const double& rfValB)
173 : : {
174 : : // changed to approxEqual usage for better numerical correctness
175 : 3306846 : return rtl::math::approxEqual(rfValA, rfValB);
176 : : }
177 : :
178 : : static bool equal(const double& rfValA, const double& rfValB, const double& rfSmallValue)
179 : : {
180 : : return (fabs(rfValA - rfValB) <= rfSmallValue);
181 : : }
182 : :
183 : 159001 : static bool less(const double& rfValA, const double& rfValB)
184 : : {
185 [ - + ][ # # ]: 159001 : return (rfValA < rfValB && !equal(rfValA, rfValB));
186 : : }
187 : :
188 : 116210 : static bool lessOrEqual(const double& rfValA, const double& rfValB)
189 : : {
190 [ + + ][ + + ]: 116210 : return (rfValA < rfValB || equal(rfValA, rfValB));
191 : : }
192 : :
193 : 2737308 : static bool more(const double& rfValA, const double& rfValB)
194 : : {
195 [ + + ][ + - ]: 2737308 : return (rfValA > rfValB && !equal(rfValA, rfValB));
196 : : }
197 : :
198 : 1576 : static bool moreOrEqual(const double& rfValA, const double& rfValB)
199 : : {
200 [ + + ][ - + ]: 1576 : return (rfValA > rfValB || equal(rfValA, rfValB));
201 : : }
202 : : };
203 : : } // end of namespace basegfx
204 : :
205 : : #endif /* _BGFX_NUMERIC_FTOOLS_HXX */
206 : :
207 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|