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 0 : B2DTuple()
53 : : mfX(0.0),
54 0 : mfY(0.0)
55 0 : {}
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 0 : B2DTuple(double fX, double fY)
68 : : mfX( fX ),
69 0 : mfY( fY )
70 0 : {}
71 :
72 : /** Create a copy of a 2D Tuple
73 :
74 : @param rTup
75 : The 2D Tuple which will be copied.
76 : */
77 0 : B2DTuple(const B2DTuple& rTup)
78 : : mfX( rTup.mfX ),
79 0 : mfY( rTup.mfY )
80 0 : {}
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 0 : ~B2DTuple()
90 0 : {}
91 :
92 : /// Get X-Coordinate of 2D Tuple
93 0 : double getX() const
94 : {
95 0 : return mfX;
96 : }
97 :
98 : /// Get Y-Coordinate of 2D Tuple
99 0 : double getY() const
100 : {
101 0 : return mfY;
102 : }
103 :
104 : /// Set X-Coordinate of 2D Tuple
105 0 : void setX(double fX)
106 : {
107 0 : mfX = fX;
108 0 : }
109 :
110 : /// Set Y-Coordinate of 2D Tuple
111 0 : void setY(double fY)
112 : {
113 0 : mfY = fY;
114 0 : }
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 0 : bool equalZero() const
138 : {
139 0 : return (this == &getEmptyTuple() ||
140 0 : (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 0 : bool equal(const B2DTuple& rTup) const
150 : {
151 : return (
152 0 : this == &rTup ||
153 0 : (fTools::equal(mfX, rTup.mfX) &&
154 0 : fTools::equal(mfY, rTup.mfY)));
155 : }
156 :
157 0 : bool equal(const B2DTuple& rTup, const double& rfSmallValue) const
158 : {
159 : return (
160 0 : this == &rTup ||
161 0 : (fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
162 0 : fTools::equal(mfY, rTup.mfY, rfSmallValue)));
163 : }
164 :
165 : // operators
166 :
167 :
168 0 : B2DTuple& operator+=( const B2DTuple& rTup )
169 : {
170 0 : mfX += rTup.mfX;
171 0 : mfY += rTup.mfY;
172 0 : return *this;
173 : }
174 :
175 0 : B2DTuple& operator-=( const B2DTuple& rTup )
176 : {
177 0 : mfX -= rTup.mfX;
178 0 : mfY -= rTup.mfY;
179 0 : return *this;
180 : }
181 :
182 0 : B2DTuple& operator/=( const B2DTuple& rTup )
183 : {
184 0 : mfX /= rTup.mfX;
185 0 : mfY /= rTup.mfY;
186 0 : return *this;
187 : }
188 :
189 0 : B2DTuple& operator*=( const B2DTuple& rTup )
190 : {
191 0 : mfX *= rTup.mfX;
192 0 : mfY *= rTup.mfY;
193 0 : return *this;
194 : }
195 :
196 0 : B2DTuple& operator*=(double t)
197 : {
198 0 : mfX *= t;
199 0 : mfY *= t;
200 0 : return *this;
201 : }
202 :
203 0 : B2DTuple& operator/=(double t)
204 : {
205 0 : const double fVal(1.0 / t);
206 0 : mfX *= fVal;
207 0 : mfY *= fVal;
208 0 : return *this;
209 : }
210 :
211 0 : B2DTuple operator-(void) const
212 : {
213 0 : return B2DTuple(-mfX, -mfY);
214 : }
215 :
216 0 : bool operator==( const B2DTuple& rTup ) const
217 : {
218 0 : return mfX == rTup.mfX && mfY == rTup.mfY;
219 : }
220 :
221 0 : bool operator!=( const B2DTuple& rTup ) const
222 : {
223 0 : return mfX != rTup.mfX || mfY != rTup.mfY;
224 : }
225 :
226 0 : B2DTuple& operator=( const B2DTuple& rTup )
227 : {
228 0 : mfX = rTup.mfX;
229 0 : mfY = rTup.mfY;
230 0 : 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 0 : inline B2DTuple absolute(const B2DTuple& rTup)
254 : {
255 : B2DTuple aAbs(
256 0 : fabs(rTup.getX()),
257 0 : fabs(rTup.getY()));
258 0 : return aAbs;
259 : }
260 :
261 0 : inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
262 : {
263 0 : if(rOld1 == rOld2)
264 : {
265 0 : return rOld1;
266 : }
267 0 : else if(0.0 >= t)
268 : {
269 0 : return rOld1;
270 : }
271 0 : else if(1.0 <= t)
272 : {
273 0 : return rOld2;
274 : }
275 : else
276 : {
277 : return B2DTuple(
278 0 : ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
279 0 : ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
280 : }
281 : }
282 :
283 0 : inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
284 : {
285 : return B2DTuple(
286 0 : rOld1.getX() == rOld2.getX() ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
287 0 : 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 0 : inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
298 : {
299 0 : B2DTuple aSum(rTupA);
300 0 : aSum += rTupB;
301 0 : return aSum;
302 : }
303 :
304 0 : inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
305 : {
306 0 : B2DTuple aSub(rTupA);
307 0 : aSub -= rTupB;
308 0 : return aSub;
309 : }
310 :
311 0 : inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
312 : {
313 0 : B2DTuple aDiv(rTupA);
314 0 : aDiv /= rTupB;
315 0 : return aDiv;
316 : }
317 :
318 0 : inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
319 : {
320 0 : B2DTuple aMul(rTupA);
321 0 : aMul *= rTupB;
322 0 : return aMul;
323 : }
324 :
325 0 : inline B2DTuple operator*(const B2DTuple& rTup, double t)
326 : {
327 0 : B2DTuple aNew(rTup);
328 0 : aNew *= t;
329 0 : return aNew;
330 : }
331 :
332 0 : inline B2DTuple operator*(double t, const B2DTuple& rTup)
333 : {
334 0 : B2DTuple aNew(rTup);
335 0 : aNew *= t;
336 0 : return aNew;
337 : }
338 :
339 0 : inline B2DTuple operator/(const B2DTuple& rTup, double t)
340 : {
341 0 : B2DTuple aNew(rTup);
342 0 : aNew /= t;
343 0 : 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: */
|