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_TUPLE_B2DTUPLE_HXX
30 : : #define _BGFX_TUPLE_B2DTUPLE_HXX
31 : :
32 : : #include <sal/types.h>
33 : : #include <basegfx/numeric/ftools.hxx>
34 : : #include <basegfx/basegfxdllapi.h>
35 : :
36 : : namespace basegfx
37 : : {
38 : : // predeclarations
39 : : class B2ITuple;
40 : :
41 : : /** Base class for all Points/Vectors with two double values
42 : :
43 : : This class provides all methods common to Point
44 : : avd Vector classes which are derived from here.
45 : :
46 : : @derive Use this class to implement Points or Vectors
47 : : which are based on two double values
48 : : */
49 : : class B2DTuple
50 : : {
51 : : protected:
52 : : double mfX;
53 : : double mfY;
54 : :
55 : : public:
56 : : /** Create a 2D Tuple
57 : :
58 : : The tuple is initialized to (0.0, 0.0)
59 : : */
60 : 695737 : B2DTuple()
61 : : : mfX(0.0),
62 : 695737 : mfY(0.0)
63 : 695737 : {}
64 : :
65 : : /** Create a 2D Tuple
66 : :
67 : : @param fX
68 : : This parameter is used to initialize the X-coordinate
69 : : of the 2D Tuple.
70 : :
71 : : @param fY
72 : : This parameter is used to initialize the Y-coordinate
73 : : of the 2D Tuple.
74 : : */
75 : 15749766 : B2DTuple(double fX, double fY)
76 : : : mfX( fX ),
77 : 15749766 : mfY( fY )
78 : 15749766 : {}
79 : :
80 : : /** Create a copy of a 2D Tuple
81 : :
82 : : @param rTup
83 : : The 2D Tuple which will be copied.
84 : : */
85 : 2359632 : B2DTuple(const B2DTuple& rTup)
86 : : : mfX( rTup.mfX ),
87 : 2359632 : mfY( rTup.mfY )
88 : 2359632 : {}
89 : :
90 : : /** Create a copy of a 2D integer Tuple
91 : :
92 : : @param rTup
93 : : The 2D Tuple which will be copied.
94 : : */
95 : : BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
96 : :
97 : 52118889 : ~B2DTuple()
98 : 52118889 : {}
99 : :
100 : : /// Get X-Coordinate of 2D Tuple
101 : 23451338 : double getX() const
102 : : {
103 : 23451338 : return mfX;
104 : : }
105 : :
106 : : /// Get Y-Coordinate of 2D Tuple
107 : 44633522 : double getY() const
108 : : {
109 : 44633522 : return mfY;
110 : : }
111 : :
112 : : /// Set X-Coordinate of 2D Tuple
113 : 303565 : void setX(double fX)
114 : : {
115 : 303565 : mfX = fX;
116 : 303565 : }
117 : :
118 : : /// Set Y-Coordinate of 2D Tuple
119 : 298272 : void setY(double fY)
120 : : {
121 : 298272 : mfY = fY;
122 : 298272 : }
123 : :
124 : : /// Array-access to 2D Tuple
125 : : const double& operator[] (int nPos) const
126 : : {
127 : : // Here, normally one if(...) should be used. In the assumption that
128 : : // both double members can be accessed as an array a shortcut is used here.
129 : : // if(0 == nPos) return mfX; return mfY;
130 : : return *((&mfX) + nPos);
131 : : }
132 : :
133 : : /// Array-access to 2D Tuple
134 : 0 : double& operator[] (int nPos)
135 : : {
136 : : // Here, normally one if(...) should be used. In the assumption that
137 : : // both double members can be accessed as an array a shortcut is used here.
138 : : // if(0 == nPos) return mfX; return mfY;
139 : 0 : return *((&mfX) + nPos);
140 : : }
141 : :
142 : : // comparators with tolerance
143 : : //////////////////////////////////////////////////////////////////////
144 : :
145 : 29983 : bool equalZero() const
146 : : {
147 : 29983 : return (this == &getEmptyTuple() ||
148 [ + + ][ + + ]: 29983 : (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
[ + - ]
149 : : }
150 : :
151 : : bool equalZero(const double& rfSmallValue) const
152 : : {
153 : : return (this == &getEmptyTuple() ||
154 : : (fTools::equalZero(mfX, rfSmallValue) && fTools::equalZero(mfY, rfSmallValue)));
155 : : }
156 : :
157 : 104012 : bool equal(const B2DTuple& rTup) const
158 : : {
159 : : return (
160 : : this == &rTup ||
161 : 104012 : (fTools::equal(mfX, rTup.mfX) &&
162 [ + - + + ]: 208024 : fTools::equal(mfY, rTup.mfY)));
[ + + ]
163 : : }
164 : :
165 : : bool equal(const B2DTuple& rTup, const double& rfSmallValue) const
166 : : {
167 : : return (
168 : : this == &rTup ||
169 : : (fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
170 : : fTools::equal(mfY, rTup.mfY, rfSmallValue)));
171 : : }
172 : :
173 : : // operators
174 : : //////////////////////////////////////////////////////////////////////
175 : :
176 : 391499 : B2DTuple& operator+=( const B2DTuple& rTup )
177 : : {
178 : 391499 : mfX += rTup.mfX;
179 : 391499 : mfY += rTup.mfY;
180 : 391499 : return *this;
181 : : }
182 : :
183 : 235959 : B2DTuple& operator-=( const B2DTuple& rTup )
184 : : {
185 : 235959 : mfX -= rTup.mfX;
186 : 235959 : mfY -= rTup.mfY;
187 : 235959 : return *this;
188 : : }
189 : :
190 : 0 : B2DTuple& operator/=( const B2DTuple& rTup )
191 : : {
192 : 0 : mfX /= rTup.mfX;
193 : 0 : mfY /= rTup.mfY;
194 : 0 : return *this;
195 : : }
196 : :
197 : 0 : B2DTuple& operator*=( const B2DTuple& rTup )
198 : : {
199 : 0 : mfX *= rTup.mfX;
200 : 0 : mfY *= rTup.mfY;
201 : 0 : return *this;
202 : : }
203 : :
204 : 444951 : B2DTuple& operator*=(double t)
205 : : {
206 : 444951 : mfX *= t;
207 : 444951 : mfY *= t;
208 : 444951 : return *this;
209 : : }
210 : :
211 : 40296 : B2DTuple& operator/=(double t)
212 : : {
213 : 40296 : const double fVal(1.0 / t);
214 : 40296 : mfX *= fVal;
215 : 40296 : mfY *= fVal;
216 : 40296 : return *this;
217 : : }
218 : :
219 : 0 : B2DTuple operator-(void) const
220 : : {
221 : 0 : return B2DTuple(-mfX, -mfY);
222 : : }
223 : :
224 : 12769 : bool operator==( const B2DTuple& rTup ) const
225 : : {
226 : 12769 : return equal(rTup);
227 : : }
228 : :
229 : 77883 : bool operator!=( const B2DTuple& rTup ) const
230 : : {
231 : 77883 : return !equal(rTup);
232 : : }
233 : :
234 : 463221 : B2DTuple& operator=( const B2DTuple& rTup )
235 : : {
236 : 463221 : mfX = rTup.mfX;
237 : 463221 : mfY = rTup.mfY;
238 : 463221 : return *this;
239 : : }
240 : :
241 : : BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
242 : : };
243 : :
244 : : // external operators
245 : : //////////////////////////////////////////////////////////////////////////
246 : :
247 : : inline B2DTuple minimum(const B2DTuple& rTupA, const B2DTuple& rTupB)
248 : : {
249 : : B2DTuple aMin(
250 : : (rTupB.getX() < rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
251 : : (rTupB.getY() < rTupA.getY()) ? rTupB.getY() : rTupA.getY());
252 : : return aMin;
253 : : }
254 : :
255 : : inline B2DTuple maximum(const B2DTuple& rTupA, const B2DTuple& rTupB)
256 : : {
257 : : B2DTuple aMax(
258 : : (rTupB.getX() > rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
259 : : (rTupB.getY() > rTupA.getY()) ? rTupB.getY() : rTupA.getY());
260 : : return aMax;
261 : : }
262 : :
263 : 13467 : inline B2DTuple absolute(const B2DTuple& rTup)
264 : : {
265 : : B2DTuple aAbs(
266 : 13467 : (0.0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
267 [ - + ][ - + ]: 26934 : (0.0 > rTup.getY()) ? -rTup.getY() : rTup.getY());
268 : 13467 : return aAbs;
269 : : }
270 : :
271 : 0 : inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
272 : : {
273 : : B2DTuple aInt(
274 : 0 : ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
275 : 0 : ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
276 : 0 : return aInt;
277 : : }
278 : :
279 : 0 : inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
280 : : {
281 : : B2DTuple aAvg(
282 : 0 : (rOld1.getX() + rOld2.getX()) * 0.5,
283 : 0 : (rOld1.getY() + rOld2.getY()) * 0.5);
284 : 0 : return aAvg;
285 : : }
286 : :
287 : : inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2, const B2DTuple& rOld3)
288 : : {
289 : : B2DTuple aAvg(
290 : : (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
291 : : (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
292 : : return aAvg;
293 : : }
294 : :
295 : 251823 : inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
296 : : {
297 : 251823 : B2DTuple aSum(rTupA);
298 : 251823 : aSum += rTupB;
299 : 251823 : return aSum;
300 : : }
301 : :
302 : 235959 : inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
303 : : {
304 : 235959 : B2DTuple aSub(rTupA);
305 : 235959 : aSub -= rTupB;
306 : 235959 : return aSub;
307 : : }
308 : :
309 : : inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
310 : : {
311 : : B2DTuple aDiv(rTupA);
312 : : aDiv /= rTupB;
313 : : return aDiv;
314 : : }
315 : :
316 : 0 : inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
317 : : {
318 : 0 : B2DTuple aMul(rTupA);
319 : 0 : aMul *= rTupB;
320 : 0 : return aMul;
321 : : }
322 : :
323 : 379526 : inline B2DTuple operator*(const B2DTuple& rTup, double t)
324 : : {
325 : 379526 : B2DTuple aNew(rTup);
326 : 379526 : aNew *= t;
327 : 379526 : return aNew;
328 : : }
329 : :
330 : 65425 : inline B2DTuple operator*(double t, const B2DTuple& rTup)
331 : : {
332 : 65425 : B2DTuple aNew(rTup);
333 : 65425 : aNew *= t;
334 : 65425 : return aNew;
335 : : }
336 : :
337 : 40294 : inline B2DTuple operator/(const B2DTuple& rTup, double t)
338 : : {
339 : 40294 : B2DTuple aNew(rTup);
340 : 40294 : aNew /= t;
341 : 40294 : return aNew;
342 : : }
343 : :
344 : : inline B2DTuple operator/(double t, const B2DTuple& rTup)
345 : : {
346 : : B2DTuple aNew(t, t);
347 : : B2DTuple aTmp(rTup);
348 : : aNew /= aTmp;
349 : : return aNew;
350 : : }
351 : :
352 : : /** Round double to nearest integer for 2D tuple
353 : :
354 : : @return the nearest integer for this tuple
355 : : */
356 : : BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
357 : : } // end of namespace basegfx
358 : :
359 : : #endif /* _BGFX_TUPLE_B2DTUPLE_HXX */
360 : :
361 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|