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 : 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 1428891 : B2DTuple()
52 : : mfX(0.0),
53 1428891 : mfY(0.0)
54 1428891 : {}
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 43036829 : B2DTuple(double fX, double fY)
67 : : mfX( fX ),
68 43036829 : mfY( fY )
69 43036829 : {}
70 :
71 : /** Create a copy of a 2D Tuple
72 :
73 : @param rTup
74 : The 2D Tuple which will be copied.
75 : */
76 251014227 : B2DTuple(const B2DTuple& rTup)
77 : : mfX( rTup.mfX ),
78 251014227 : mfY( rTup.mfY )
79 251014227 : {}
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 295458150 : ~B2DTuple()
89 295458150 : {}
90 :
91 : /// Get X-Coordinate of 2D Tuple
92 208280945 : double getX() const
93 : {
94 208280945 : return mfX;
95 : }
96 :
97 : /// Get Y-Coordinate of 2D Tuple
98 234959702 : double getY() const
99 : {
100 234959702 : return mfY;
101 : }
102 :
103 : /// Set X-Coordinate of 2D Tuple
104 1038939 : void setX(double fX)
105 : {
106 1038939 : mfX = fX;
107 1038939 : }
108 :
109 : /// Set Y-Coordinate of 2D Tuple
110 1030271 : void setY(double fY)
111 : {
112 1030271 : mfY = fY;
113 1030271 : }
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 636 : 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 636 : return *((&mfX) + nPos);
131 : }
132 :
133 : // comparators with tolerance
134 :
135 :
136 5030949 : bool equalZero() const
137 : {
138 11048829 : return (this == &getEmptyTuple() ||
139 11815253 : (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
140 : }
141 :
142 16937067 : bool equal(const B2DTuple& rTup) const
143 : {
144 : return (
145 34103822 : this == &rTup ||
146 17355097 : (fTools::equal(mfX, rTup.mfX) &&
147 17382522 : fTools::equal(mfY, rTup.mfY)));
148 : }
149 :
150 : // operators
151 :
152 :
153 3541718 : B2DTuple& operator+=( const B2DTuple& rTup )
154 : {
155 3541718 : mfX += rTup.mfX;
156 3541718 : mfY += rTup.mfY;
157 3541718 : return *this;
158 : }
159 :
160 11861117 : B2DTuple& operator-=( const B2DTuple& rTup )
161 : {
162 11861117 : mfX -= rTup.mfX;
163 11861117 : mfY -= rTup.mfY;
164 11861117 : return *this;
165 : }
166 :
167 289 : B2DTuple& operator/=( const B2DTuple& rTup )
168 : {
169 289 : mfX /= rTup.mfX;
170 289 : mfY /= rTup.mfY;
171 289 : return *this;
172 : }
173 :
174 86 : B2DTuple& operator*=( const B2DTuple& rTup )
175 : {
176 86 : mfX *= rTup.mfX;
177 86 : mfY *= rTup.mfY;
178 86 : return *this;
179 : }
180 :
181 415384 : B2DTuple& operator*=(double t)
182 : {
183 415384 : mfX *= t;
184 415384 : mfY *= t;
185 415384 : return *this;
186 : }
187 :
188 49031 : B2DTuple& operator/=(double t)
189 : {
190 49031 : const double fVal(1.0 / t);
191 49031 : mfX *= fVal;
192 49031 : mfY *= fVal;
193 49031 : return *this;
194 : }
195 :
196 123 : B2DTuple operator-(void) const
197 : {
198 123 : return B2DTuple(-mfX, -mfY);
199 : }
200 :
201 17343685 : bool operator==( const B2DTuple& rTup ) const
202 : {
203 17343685 : return mfX == rTup.mfX && mfY == rTup.mfY;
204 : }
205 :
206 14180968 : bool operator!=( const B2DTuple& rTup ) const
207 : {
208 14180968 : return mfX != rTup.mfX || mfY != rTup.mfY;
209 : }
210 :
211 43104994 : B2DTuple& operator=( const B2DTuple& rTup )
212 : {
213 43104994 : mfX = rTup.mfX;
214 43104994 : mfY = rTup.mfY;
215 43104994 : return *this;
216 : }
217 :
218 : BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
219 : };
220 :
221 : // external operators
222 :
223 :
224 : inline B2DTuple minimum(const B2DTuple& rTupA, const B2DTuple& rTupB)
225 : {
226 : return B2DTuple(
227 : std::min(rTupB.getX(), rTupA.getX()),
228 : std::min(rTupB.getY(), rTupA.getY()));
229 : }
230 :
231 : inline B2DTuple maximum(const B2DTuple& rTupA, const B2DTuple& rTupB)
232 : {
233 : return B2DTuple(
234 : std::max(rTupB.getX(), rTupA.getX()),
235 : std::max(rTupB.getY(), rTupA.getY()));
236 : }
237 :
238 16845 : inline B2DTuple absolute(const B2DTuple& rTup)
239 : {
240 : B2DTuple aAbs(
241 16845 : fabs(rTup.getX()),
242 33690 : fabs(rTup.getY()));
243 16845 : return aAbs;
244 : }
245 :
246 16741418 : inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
247 : {
248 16741418 : if(rOld1 == rOld2)
249 : {
250 262021 : return rOld1;
251 : }
252 16479397 : else if(0.0 >= t)
253 : {
254 4944 : return rOld1;
255 : }
256 16474453 : else if(1.0 <= t)
257 : {
258 1844 : return rOld2;
259 : }
260 : else
261 : {
262 : return B2DTuple(
263 16472609 : ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
264 32945218 : ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
265 : }
266 : }
267 :
268 423474 : inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
269 : {
270 : return B2DTuple(
271 838187 : rOld1.getX() == rOld2.getX() ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
272 1261661 : rOld1.getY() == rOld2.getY() ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
273 : }
274 :
275 : inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2, const B2DTuple& rOld3)
276 : {
277 : return B2DTuple(
278 : (rOld1.getX() == rOld2.getX() && rOld2.getX() == rOld3.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
279 : (rOld1.getY() == rOld2.getY() && rOld2.getY() == rOld3.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
280 : }
281 :
282 3392378 : inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
283 : {
284 3392378 : B2DTuple aSum(rTupA);
285 3392378 : aSum += rTupB;
286 3392378 : return aSum;
287 : }
288 :
289 11860526 : inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
290 : {
291 11860526 : B2DTuple aSub(rTupA);
292 11860526 : aSub -= rTupB;
293 11860526 : return aSub;
294 : }
295 :
296 289 : inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
297 : {
298 289 : B2DTuple aDiv(rTupA);
299 289 : aDiv /= rTupB;
300 289 : return aDiv;
301 : }
302 :
303 86 : inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
304 : {
305 86 : B2DTuple aMul(rTupA);
306 86 : aMul *= rTupB;
307 86 : return aMul;
308 : }
309 :
310 402588 : inline B2DTuple operator*(const B2DTuple& rTup, double t)
311 : {
312 402588 : B2DTuple aNew(rTup);
313 402588 : aNew *= t;
314 402588 : return aNew;
315 : }
316 :
317 12598 : inline B2DTuple operator*(double t, const B2DTuple& rTup)
318 : {
319 12598 : B2DTuple aNew(rTup);
320 12598 : aNew *= t;
321 12598 : return aNew;
322 : }
323 :
324 49030 : inline B2DTuple operator/(const B2DTuple& rTup, double t)
325 : {
326 49030 : B2DTuple aNew(rTup);
327 49030 : aNew /= t;
328 49030 : return aNew;
329 : }
330 :
331 : inline B2DTuple operator/(double t, const B2DTuple& rTup)
332 : {
333 : B2DTuple aNew(t, t);
334 : B2DTuple aTmp(rTup);
335 : aNew /= aTmp;
336 : return aNew;
337 : }
338 :
339 : /** Round double to nearest integer for 2D tuple
340 :
341 : @return the nearest integer for this tuple
342 : */
343 : BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
344 : } // end of namespace basegfx
345 :
346 : #endif // INCLUDED_BASEGFX_TUPLE_B2DTUPLE_HXX
347 :
348 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|