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