bump product version to 4.1.6.2
[LibreOffice.git] / include / basegfx / tuple / b3dtuple.hxx
blob1eb4dd38de098044b876bbec5e4589e553ff554d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _BGFX_TUPLE_B3DTUPLE_HXX
21 #define _BGFX_TUPLE_B3DTUPLE_HXX
23 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <basegfx/basegfxdllapi.h>
27 namespace basegfx
29 // predeclarations
30 class B3ITuple;
32 /** Base class for all Points/Vectors with three double values
34 This class provides all methods common to Point
35 avd Vector classes which are derived from here.
37 @derive Use this class to implement Points or Vectors
38 which are based on three double values
40 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED B3DTuple
42 protected:
43 double mfX;
44 double mfY;
45 double mfZ;
47 public:
48 /** Create a 3D Tuple
50 The tuple is initialized to (0.0, 0.0, 0.0)
52 B3DTuple()
53 : mfX(0.0),
54 mfY(0.0),
55 mfZ(0.0)
58 /** Create a 3D Tuple
60 @param fX
61 This parameter is used to initialize the X-coordinate
62 of the 3D Tuple.
64 @param fY
65 This parameter is used to initialize the Y-coordinate
66 of the 3D Tuple.
68 @param fZ
69 This parameter is used to initialize the Z-coordinate
70 of the 3D Tuple.
72 B3DTuple(double fX, double fY, double fZ)
73 : mfX(fX),
74 mfY(fY),
75 mfZ(fZ)
78 /** Create a copy of a 3D Tuple
80 @param rTup
81 The 3D Tuple which will be copied.
83 B3DTuple(const B3DTuple& rTup)
84 : mfX( rTup.mfX ),
85 mfY( rTup.mfY ),
86 mfZ( rTup.mfZ )
89 ~B3DTuple()
92 /// get X-Coordinate of 3D Tuple
93 double getX() const
95 return mfX;
98 /// get Y-Coordinate of 3D Tuple
99 double getY() const
101 return mfY;
104 /// get Z-Coordinate of 3D Tuple
105 double getZ() const
107 return mfZ;
110 /// set X-Coordinate of 3D Tuple
111 void setX(double fX)
113 mfX = fX;
116 /// set Y-Coordinate of 3D Tuple
117 void setY(double fY)
119 mfY = fY;
122 /// set Z-Coordinate of 3D Tuple
123 void setZ(double fZ)
125 mfZ = fZ;
128 /// Array-access to 3D Tuple
129 const double& operator[] (int nPos) const
131 // Here, normally two if(...)'s should be used. In the assumption that
132 // both double members can be accessed as an array a shortcut is used here.
133 // if(0 == nPos) return mfX; if(1 == nPos) return mfY; return mfZ;
134 return *((&mfX) + nPos);
137 /// Array-access to 3D Tuple
138 double& operator[] (int nPos)
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);
146 // comparators with tolerance
147 //////////////////////////////////////////////////////////////////////
149 bool equalZero() const
151 return (this == &getEmptyTuple() ||
152 (::basegfx::fTools::equalZero(mfX)
153 && ::basegfx::fTools::equalZero(mfY)
154 && ::basegfx::fTools::equalZero(mfZ)));
157 bool equalZero(const double& rfSmallValue) const
159 return (this == &getEmptyTuple() ||
160 (::basegfx::fTools::equalZero(mfX, rfSmallValue)
161 && ::basegfx::fTools::equalZero(mfY, rfSmallValue)
162 && ::basegfx::fTools::equalZero(mfZ, rfSmallValue)));
165 bool equal(const B3DTuple& rTup) const
167 return (
168 this == &rTup ||
169 (::basegfx::fTools::equal(mfX, rTup.mfX) &&
170 ::basegfx::fTools::equal(mfY, rTup.mfY) &&
171 ::basegfx::fTools::equal(mfZ, rTup.mfZ)));
174 bool equal(const B3DTuple& rTup, const double& rfSmallValue) const
176 return (
177 this == &rTup ||
178 (::basegfx::fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
179 ::basegfx::fTools::equal(mfY, rTup.mfY, rfSmallValue) &&
180 ::basegfx::fTools::equal(mfZ, rTup.mfZ, rfSmallValue)));
183 // operators
184 //////////////////////////////////////////////////////////////////////
186 B3DTuple& operator+=( const B3DTuple& rTup )
188 mfX += rTup.mfX;
189 mfY += rTup.mfY;
190 mfZ += rTup.mfZ;
191 return *this;
194 B3DTuple& operator-=( const B3DTuple& rTup )
196 mfX -= rTup.mfX;
197 mfY -= rTup.mfY;
198 mfZ -= rTup.mfZ;
199 return *this;
202 B3DTuple& operator/=( const B3DTuple& rTup )
204 mfX /= rTup.mfX;
205 mfY /= rTup.mfY;
206 mfZ /= rTup.mfZ;
207 return *this;
210 B3DTuple& operator*=( const B3DTuple& rTup )
212 mfX *= rTup.mfX;
213 mfY *= rTup.mfY;
214 mfZ *= rTup.mfZ;
215 return *this;
218 B3DTuple& operator*=(double t)
220 mfX *= t;
221 mfY *= t;
222 mfZ *= t;
223 return *this;
226 B3DTuple& operator/=(double t)
228 const double fVal(1.0 / t);
229 mfX *= fVal;
230 mfY *= fVal;
231 mfZ *= fVal;
232 return *this;
235 B3DTuple operator-(void) const
237 return B3DTuple(-mfX, -mfY, -mfZ);
240 bool operator==( const B3DTuple& rTup ) const
242 return equal(rTup);
245 bool operator!=( const B3DTuple& rTup ) const
247 return !equal(rTup);
250 B3DTuple& operator=( const B3DTuple& rTup )
252 mfX = rTup.mfX;
253 mfY = rTup.mfY;
254 mfZ = rTup.mfZ;
255 return *this;
258 void correctValues(const double fCompareValue = 0.0)
260 if(0.0 == fCompareValue)
262 if(::basegfx::fTools::equalZero(mfX))
264 mfX = 0.0;
267 if(::basegfx::fTools::equalZero(mfY))
269 mfY = 0.0;
272 if(::basegfx::fTools::equalZero(mfZ))
274 mfZ = 0.0;
277 else
279 if(::basegfx::fTools::equal(mfX, fCompareValue))
281 mfX = fCompareValue;
284 if(::basegfx::fTools::equal(mfY, fCompareValue))
286 mfY = fCompareValue;
289 if(::basegfx::fTools::equal(mfZ, fCompareValue))
291 mfZ = fCompareValue;
296 static const B3DTuple& getEmptyTuple();
299 // external operators
300 //////////////////////////////////////////////////////////////////////////
302 inline B3DTuple minimum(const B3DTuple& rTupA, const B3DTuple& rTupB)
304 B3DTuple aMin(
305 (rTupB.getX() < rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
306 (rTupB.getY() < rTupA.getY()) ? rTupB.getY() : rTupA.getY(),
307 (rTupB.getZ() < rTupA.getZ()) ? rTupB.getZ() : rTupA.getZ());
308 return aMin;
311 inline B3DTuple maximum(const B3DTuple& rTupA, const B3DTuple& rTupB)
313 B3DTuple aMax(
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 aMax;
320 inline B3DTuple absolute(const B3DTuple& rTup)
322 B3DTuple aAbs(
323 (0.0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
324 (0.0 > rTup.getY()) ? -rTup.getY() : rTup.getY(),
325 (0.0 > rTup.getZ()) ? -rTup.getZ() : rTup.getZ());
326 return aAbs;
329 inline B3DTuple interpolate(const B3DTuple& rOld1, const B3DTuple& rOld2, double t)
331 B3DTuple aInt(
332 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
333 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY(),
334 ((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ());
335 return aInt;
338 inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2)
340 B3DTuple aAvg(
341 (rOld1.getX() + rOld2.getX()) * 0.5,
342 (rOld1.getY() + rOld2.getY()) * 0.5,
343 (rOld1.getZ() + rOld2.getZ()) * 0.5);
344 return aAvg;
347 inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2, const B3DTuple& rOld3)
349 B3DTuple aAvg(
350 (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
351 (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0),
352 (rOld1.getZ() + rOld2.getZ() + rOld3.getZ()) * (1.0 / 3.0));
353 return aAvg;
356 inline B3DTuple operator+(const B3DTuple& rTupA, const B3DTuple& rTupB)
358 B3DTuple aSum(rTupA);
359 aSum += rTupB;
360 return aSum;
363 inline B3DTuple operator-(const B3DTuple& rTupA, const B3DTuple& rTupB)
365 B3DTuple aSub(rTupA);
366 aSub -= rTupB;
367 return aSub;
370 inline B3DTuple operator/(const B3DTuple& rTupA, const B3DTuple& rTupB)
372 B3DTuple aDiv(rTupA);
373 aDiv /= rTupB;
374 return aDiv;
377 inline B3DTuple operator*(const B3DTuple& rTupA, const B3DTuple& rTupB)
379 B3DTuple aMul(rTupA);
380 aMul *= rTupB;
381 return aMul;
384 inline B3DTuple operator*(const B3DTuple& rTup, double t)
386 B3DTuple aNew(rTup);
387 aNew *= t;
388 return aNew;
391 inline B3DTuple operator*(double t, const B3DTuple& rTup)
393 B3DTuple aNew(rTup);
394 aNew *= t;
395 return aNew;
398 inline B3DTuple operator/(const B3DTuple& rTup, double t)
400 B3DTuple aNew(rTup);
401 aNew /= t;
402 return aNew;
405 inline B3DTuple operator/(double t, const B3DTuple& rTup)
407 B3DTuple aNew(rTup);
408 aNew /= t;
409 return aNew;
412 /** Round double to nearest integer for 3D tuple
414 @return the nearest integer for this tuple
416 BASEGFX_DLLPUBLIC B3ITuple fround(const B3DTuple& rTup);
417 } // end of namespace basegfx
419 #endif /* _BGFX_TUPLE_B3DTUPLE_HXX */
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */