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_B3DTUPLE_HXX
30 : : #define _BGFX_TUPLE_B3DTUPLE_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 B3ITuple;
40 : :
41 : : /** Base class for all Points/Vectors with three 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 three double values
48 : : */
49 : : class BASEGFX_DLLPUBLIC B3DTuple
50 : : {
51 : : protected:
52 : : double mfX;
53 : : double mfY;
54 : : double mfZ;
55 : :
56 : : public:
57 : : /** Create a 3D Tuple
58 : :
59 : : The tuple is initialized to (0.0, 0.0, 0.0)
60 : : */
61 : 230917 : B3DTuple()
62 : : : mfX(0.0),
63 : : mfY(0.0),
64 : 230917 : mfZ(0.0)
65 : 230917 : {}
66 : :
67 : : /** Create a 3D Tuple
68 : :
69 : : @param fX
70 : : This parameter is used to initialize the X-coordinate
71 : : of the 3D Tuple.
72 : :
73 : : @param fY
74 : : This parameter is used to initialize the Y-coordinate
75 : : of the 3D Tuple.
76 : :
77 : : @param fZ
78 : : This parameter is used to initialize the Z-coordinate
79 : : of the 3D Tuple.
80 : : */
81 : 582290 : B3DTuple(double fX, double fY, double fZ)
82 : : : mfX(fX),
83 : : mfY(fY),
84 : 582290 : mfZ(fZ)
85 : 582290 : {}
86 : :
87 : : /** Create a copy of a 3D Tuple
88 : :
89 : : @param rTup
90 : : The 3D Tuple which will be copied.
91 : : */
92 : 2257261 : B3DTuple(const B3DTuple& rTup)
93 : : : mfX( rTup.mfX ),
94 : : mfY( rTup.mfY ),
95 : 2257261 : mfZ( rTup.mfZ )
96 : 2257261 : {}
97 : :
98 : 3139571 : ~B3DTuple()
99 : 3139571 : {}
100 : :
101 : : /// get X-Coordinate of 3D Tuple
102 : 716822 : double getX() const
103 : : {
104 : 716822 : return mfX;
105 : : }
106 : :
107 : : /// get Y-Coordinate of 3D Tuple
108 : 724960 : double getY() const
109 : : {
110 : 724960 : return mfY;
111 : : }
112 : :
113 : : /// get Z-Coordinate of 3D Tuple
114 : 725564 : double getZ() const
115 : : {
116 : 725564 : return mfZ;
117 : : }
118 : :
119 : : /// set X-Coordinate of 3D Tuple
120 : 4561 : void setX(double fX)
121 : : {
122 : 4561 : mfX = fX;
123 : 4561 : }
124 : :
125 : : /// set Y-Coordinate of 3D Tuple
126 : 6783 : void setY(double fY)
127 : : {
128 : 6783 : mfY = fY;
129 : 6783 : }
130 : :
131 : : /// set Z-Coordinate of 3D Tuple
132 : 1662 : void setZ(double fZ)
133 : : {
134 : 1662 : mfZ = fZ;
135 : 1662 : }
136 : :
137 : : /// Array-access to 3D Tuple
138 : : const double& operator[] (int nPos) const
139 : : {
140 : : // Here, normally two if(...)'s should be used. In the assumption that
141 : : // both double members can be accessed as an array a shortcut is used here.
142 : : // if(0 == nPos) return mfX; if(1 == nPos) return mfY; return mfZ;
143 : : return *((&mfX) + nPos);
144 : : }
145 : :
146 : : /// Array-access to 3D Tuple
147 : 882 : double& operator[] (int nPos)
148 : : {
149 : : // Here, normally two if(...)'s should be used. In the assumption that
150 : : // both double members can be accessed as an array a shortcut is used here.
151 : : // if(0 == nPos) return mfX; if(1 == nPos) return mfY; return mfZ;
152 : 882 : return *((&mfX) + nPos);
153 : : }
154 : :
155 : : // comparators with tolerance
156 : : //////////////////////////////////////////////////////////////////////
157 : :
158 : 845 : bool equalZero() const
159 : : {
160 : 845 : return (this == &getEmptyTuple() ||
161 : 845 : (::basegfx::fTools::equalZero(mfX)
162 : 109 : && ::basegfx::fTools::equalZero(mfY)
163 [ - + ][ + - : 1799 : && ::basegfx::fTools::equalZero(mfZ)));
+ + + + ]
164 : : }
165 : :
166 : : bool equalZero(const double& rfSmallValue) const
167 : : {
168 : : return (this == &getEmptyTuple() ||
169 : : (::basegfx::fTools::equalZero(mfX, rfSmallValue)
170 : : && ::basegfx::fTools::equalZero(mfY, rfSmallValue)
171 : : && ::basegfx::fTools::equalZero(mfZ, rfSmallValue)));
172 : : }
173 : :
174 : 117474 : bool equal(const B3DTuple& rTup) const
175 : : {
176 : : return (
177 : : this == &rTup ||
178 : 113619 : (::basegfx::fTools::equal(mfX, rTup.mfX) &&
179 : 112446 : ::basegfx::fTools::equal(mfY, rTup.mfY) &&
180 [ + + + + : 343539 : ::basegfx::fTools::equal(mfZ, rTup.mfZ)));
+ + ][ + + ]
181 : : }
182 : :
183 : : bool equal(const B3DTuple& rTup, const double& rfSmallValue) const
184 : : {
185 : : return (
186 : : this == &rTup ||
187 : : (::basegfx::fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
188 : : ::basegfx::fTools::equal(mfY, rTup.mfY, rfSmallValue) &&
189 : : ::basegfx::fTools::equal(mfZ, rTup.mfZ, rfSmallValue)));
190 : : }
191 : :
192 : : // operators
193 : : //////////////////////////////////////////////////////////////////////
194 : :
195 : 138948 : B3DTuple& operator+=( const B3DTuple& rTup )
196 : : {
197 : 138948 : mfX += rTup.mfX;
198 : 138948 : mfY += rTup.mfY;
199 : 138948 : mfZ += rTup.mfZ;
200 : 138948 : return *this;
201 : : }
202 : :
203 : 7037 : B3DTuple& operator-=( const B3DTuple& rTup )
204 : : {
205 : 7037 : mfX -= rTup.mfX;
206 : 7037 : mfY -= rTup.mfY;
207 : 7037 : mfZ -= rTup.mfZ;
208 : 7037 : return *this;
209 : : }
210 : :
211 : : B3DTuple& operator/=( const B3DTuple& rTup )
212 : : {
213 : : mfX /= rTup.mfX;
214 : : mfY /= rTup.mfY;
215 : : mfZ /= rTup.mfZ;
216 : : return *this;
217 : : }
218 : :
219 : 1363 : B3DTuple& operator*=( const B3DTuple& rTup )
220 : : {
221 : 1363 : mfX *= rTup.mfX;
222 : 1363 : mfY *= rTup.mfY;
223 : 1363 : mfZ *= rTup.mfZ;
224 : 1363 : return *this;
225 : : }
226 : :
227 : 274911 : B3DTuple& operator*=(double t)
228 : : {
229 : 274911 : mfX *= t;
230 : 274911 : mfY *= t;
231 : 274911 : mfZ *= t;
232 : 274911 : return *this;
233 : : }
234 : :
235 : 0 : B3DTuple& operator/=(double t)
236 : : {
237 : 0 : const double fVal(1.0 / t);
238 : 0 : mfX *= fVal;
239 : 0 : mfY *= fVal;
240 : 0 : mfZ *= fVal;
241 : 0 : return *this;
242 : : }
243 : :
244 : 24 : B3DTuple operator-(void) const
245 : : {
246 : 24 : return B3DTuple(-mfX, -mfY, -mfZ);
247 : : }
248 : :
249 : 101551 : bool operator==( const B3DTuple& rTup ) const
250 : : {
251 : 101551 : return equal(rTup);
252 : : }
253 : :
254 : 15923 : bool operator!=( const B3DTuple& rTup ) const
255 : : {
256 : 15923 : return !equal(rTup);
257 : : }
258 : :
259 : 2321478 : B3DTuple& operator=( const B3DTuple& rTup )
260 : : {
261 : 2321478 : mfX = rTup.mfX;
262 : 2321478 : mfY = rTup.mfY;
263 : 2321478 : mfZ = rTup.mfZ;
264 : 2321478 : return *this;
265 : : }
266 : :
267 : : void correctValues(const double fCompareValue = 0.0)
268 : : {
269 : : if(0.0 == fCompareValue)
270 : : {
271 : : if(::basegfx::fTools::equalZero(mfX))
272 : : {
273 : : mfX = 0.0;
274 : : }
275 : :
276 : : if(::basegfx::fTools::equalZero(mfY))
277 : : {
278 : : mfY = 0.0;
279 : : }
280 : :
281 : : if(::basegfx::fTools::equalZero(mfZ))
282 : : {
283 : : mfZ = 0.0;
284 : : }
285 : : }
286 : : else
287 : : {
288 : : if(::basegfx::fTools::equal(mfX, fCompareValue))
289 : : {
290 : : mfX = fCompareValue;
291 : : }
292 : :
293 : : if(::basegfx::fTools::equal(mfY, fCompareValue))
294 : : {
295 : : mfY = fCompareValue;
296 : : }
297 : :
298 : : if(::basegfx::fTools::equal(mfZ, fCompareValue))
299 : : {
300 : : mfZ = fCompareValue;
301 : : }
302 : : }
303 : : }
304 : :
305 : : static const B3DTuple& getEmptyTuple();
306 : : };
307 : :
308 : : // external operators
309 : : //////////////////////////////////////////////////////////////////////////
310 : :
311 : : inline B3DTuple minimum(const B3DTuple& rTupA, const B3DTuple& rTupB)
312 : : {
313 : : B3DTuple aMin(
314 : : (rTupB.getX() < rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
315 : : (rTupB.getY() < rTupA.getY()) ? rTupB.getY() : rTupA.getY(),
316 : : (rTupB.getZ() < rTupA.getZ()) ? rTupB.getZ() : rTupA.getZ());
317 : : return aMin;
318 : : }
319 : :
320 : : inline B3DTuple maximum(const B3DTuple& rTupA, const B3DTuple& rTupB)
321 : : {
322 : : B3DTuple aMax(
323 : : (rTupB.getX() > rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
324 : : (rTupB.getY() > rTupA.getY()) ? rTupB.getY() : rTupA.getY(),
325 : : (rTupB.getZ() > rTupA.getZ()) ? rTupB.getZ() : rTupA.getZ());
326 : : return aMax;
327 : : }
328 : :
329 : : inline B3DTuple absolute(const B3DTuple& rTup)
330 : : {
331 : : B3DTuple aAbs(
332 : : (0.0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
333 : : (0.0 > rTup.getY()) ? -rTup.getY() : rTup.getY(),
334 : : (0.0 > rTup.getZ()) ? -rTup.getZ() : rTup.getZ());
335 : : return aAbs;
336 : : }
337 : :
338 : 181018 : inline B3DTuple interpolate(const B3DTuple& rOld1, const B3DTuple& rOld2, double t)
339 : : {
340 : : B3DTuple aInt(
341 : 181018 : ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
342 : 181018 : ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY(),
343 : 362036 : ((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ());
344 : 181018 : return aInt;
345 : : }
346 : :
347 : 0 : inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2)
348 : : {
349 : : B3DTuple aAvg(
350 : 0 : (rOld1.getX() + rOld2.getX()) * 0.5,
351 : 0 : (rOld1.getY() + rOld2.getY()) * 0.5,
352 : 0 : (rOld1.getZ() + rOld2.getZ()) * 0.5);
353 : 0 : return aAvg;
354 : : }
355 : :
356 : : inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2, const B3DTuple& rOld3)
357 : : {
358 : : B3DTuple aAvg(
359 : : (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
360 : : (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0),
361 : : (rOld1.getZ() + rOld2.getZ() + rOld3.getZ()) * (1.0 / 3.0));
362 : : return aAvg;
363 : : }
364 : :
365 : 137585 : inline B3DTuple operator+(const B3DTuple& rTupA, const B3DTuple& rTupB)
366 : : {
367 : 137585 : B3DTuple aSum(rTupA);
368 : 137585 : aSum += rTupB;
369 : 137585 : return aSum;
370 : : }
371 : :
372 : 7037 : inline B3DTuple operator-(const B3DTuple& rTupA, const B3DTuple& rTupB)
373 : : {
374 : 7037 : B3DTuple aSub(rTupA);
375 : 7037 : aSub -= rTupB;
376 : 7037 : return aSub;
377 : : }
378 : :
379 : : inline B3DTuple operator/(const B3DTuple& rTupA, const B3DTuple& rTupB)
380 : : {
381 : : B3DTuple aDiv(rTupA);
382 : : aDiv /= rTupB;
383 : : return aDiv;
384 : : }
385 : :
386 : 1363 : inline B3DTuple operator*(const B3DTuple& rTupA, const B3DTuple& rTupB)
387 : : {
388 : 1363 : B3DTuple aMul(rTupA);
389 : 1363 : aMul *= rTupB;
390 : 1363 : return aMul;
391 : : }
392 : :
393 : 274911 : inline B3DTuple operator*(const B3DTuple& rTup, double t)
394 : : {
395 : 274911 : B3DTuple aNew(rTup);
396 : 274911 : aNew *= t;
397 : 274911 : return aNew;
398 : : }
399 : :
400 : : inline B3DTuple operator*(double t, const B3DTuple& rTup)
401 : : {
402 : : B3DTuple aNew(rTup);
403 : : aNew *= t;
404 : : return aNew;
405 : : }
406 : :
407 : 0 : inline B3DTuple operator/(const B3DTuple& rTup, double t)
408 : : {
409 : 0 : B3DTuple aNew(rTup);
410 : 0 : aNew /= t;
411 : 0 : return aNew;
412 : : }
413 : :
414 : : inline B3DTuple operator/(double t, const B3DTuple& rTup)
415 : : {
416 : : B3DTuple aNew(rTup);
417 : : aNew /= t;
418 : : return aNew;
419 : : }
420 : :
421 : : /** Round double to nearest integer for 3D tuple
422 : :
423 : : @return the nearest integer for this tuple
424 : : */
425 : : BASEGFX_DLLPUBLIC B3ITuple fround(const B3DTuple& rTup);
426 : : } // end of namespace basegfx
427 : :
428 : : #endif /* _BGFX_TUPLE_B3DTUPLE_HXX */
429 : :
430 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|