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