bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / basegfx / tuple / b3dtuple.hxx
blob6767d5142476537a8b20c2326359be1fec5acf4e
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 INCLUDED_BASEGFX_TUPLE_B3DTUPLE_HXX
21 #define INCLUDED_BASEGFX_TUPLE_B3DTUPLE_HXX
23 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <basegfx/basegfxdllapi.h>
27 namespace basegfx
29 class B3ITuple;
31 /** Base class for all Points/Vectors with three double values
33 This class provides all methods common to Point
34 avd Vector classes which are derived from here.
36 @derive Use this class to implement Points or Vectors
37 which are based on three double values
39 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B3DTuple
41 protected:
42 double mfX;
43 double mfY;
44 double mfZ;
46 public:
47 /** Create a 3D Tuple
49 The tuple is initialized to (0.0, 0.0, 0.0)
51 B3DTuple()
52 : mfX(0.0),
53 mfY(0.0),
54 mfZ(0.0)
57 /** Create a 3D Tuple
59 @param fX
60 This parameter is used to initialize the X-coordinate
61 of the 3D Tuple.
63 @param fY
64 This parameter is used to initialize the Y-coordinate
65 of the 3D Tuple.
67 @param fZ
68 This parameter is used to initialize the Z-coordinate
69 of the 3D Tuple.
71 B3DTuple(double fX, double fY, double fZ)
72 : mfX(fX),
73 mfY(fY),
74 mfZ(fZ)
77 /// get X-Coordinate of 3D Tuple
78 double getX() const
80 return mfX;
83 /// get Y-Coordinate of 3D Tuple
84 double getY() const
86 return mfY;
89 /// get Z-Coordinate of 3D Tuple
90 double getZ() const
92 return mfZ;
95 /// set X-Coordinate of 3D Tuple
96 void setX(double fX)
98 mfX = fX;
101 /// set Y-Coordinate of 3D Tuple
102 void setY(double fY)
104 mfY = fY;
107 /// set Z-Coordinate of 3D Tuple
108 void setZ(double fZ)
110 mfZ = fZ;
113 /// Array-access to 3D Tuple
114 const double& operator[] (int nPos) const
116 // Here, normally two if(...)'s should be used. In the assumption that
117 // both double members can be accessed as an array a shortcut is used here.
118 // if(0 == nPos) return mfX; if(1 == nPos) return mfY; return mfZ;
119 return *((&mfX) + nPos);
122 /// Array-access to 3D Tuple
123 double& operator[] (int nPos)
125 // Here, normally two if(...)'s should be used. In the assumption that
126 // both double members can be accessed as an array a shortcut is used here.
127 // if(0 == nPos) return mfX; if(1 == nPos) return mfY; return mfZ;
128 return *((&mfX) + nPos);
131 // comparators with tolerance
134 bool equalZero() const
136 return (this == &getEmptyTuple() ||
137 (::basegfx::fTools::equalZero(mfX)
138 && ::basegfx::fTools::equalZero(mfY)
139 && ::basegfx::fTools::equalZero(mfZ)));
142 bool equal(const B3DTuple& rTup) const
144 return (
145 this == &rTup ||
146 (::basegfx::fTools::equal(mfX, rTup.mfX) &&
147 ::basegfx::fTools::equal(mfY, rTup.mfY) &&
148 ::basegfx::fTools::equal(mfZ, rTup.mfZ)));
151 // operators
154 B3DTuple& operator+=( const B3DTuple& rTup )
156 mfX += rTup.mfX;
157 mfY += rTup.mfY;
158 mfZ += rTup.mfZ;
159 return *this;
162 B3DTuple& operator-=( const B3DTuple& rTup )
164 mfX -= rTup.mfX;
165 mfY -= rTup.mfY;
166 mfZ -= rTup.mfZ;
167 return *this;
170 B3DTuple& operator/=( const B3DTuple& rTup )
172 mfX /= rTup.mfX;
173 mfY /= rTup.mfY;
174 mfZ /= rTup.mfZ;
175 return *this;
178 B3DTuple& operator*=( const B3DTuple& rTup )
180 mfX *= rTup.mfX;
181 mfY *= rTup.mfY;
182 mfZ *= rTup.mfZ;
183 return *this;
186 B3DTuple& operator*=(double t)
188 mfX *= t;
189 mfY *= t;
190 mfZ *= t;
191 return *this;
194 B3DTuple& operator/=(double t)
196 const double fVal(1.0 / t);
197 mfX *= fVal;
198 mfY *= fVal;
199 mfZ *= fVal;
200 return *this;
203 B3DTuple operator-(void) const
205 return B3DTuple(-mfX, -mfY, -mfZ);
208 bool operator==( const B3DTuple& rTup ) const
210 return mfX == rTup.mfX && mfY == rTup.mfY && mfZ == rTup.mfZ;
213 bool operator!=( const B3DTuple& rTup ) const
215 return mfX != rTup.mfX || mfY != rTup.mfY || mfZ != rTup.mfZ;
218 void correctValues(const double fCompareValue = 0.0)
220 if(0.0 == fCompareValue)
222 if(::basegfx::fTools::equalZero(mfX))
224 mfX = 0.0;
227 if(::basegfx::fTools::equalZero(mfY))
229 mfY = 0.0;
232 if(::basegfx::fTools::equalZero(mfZ))
234 mfZ = 0.0;
237 else
239 if(::basegfx::fTools::equal(mfX, fCompareValue))
241 mfX = fCompareValue;
244 if(::basegfx::fTools::equal(mfY, fCompareValue))
246 mfY = fCompareValue;
249 if(::basegfx::fTools::equal(mfZ, fCompareValue))
251 mfZ = fCompareValue;
256 static const B3DTuple& getEmptyTuple();
259 // external operators
262 inline B3DTuple interpolate(const B3DTuple& rOld1, const B3DTuple& rOld2, double t)
264 if(rOld1 == rOld2)
266 return rOld1;
268 else if(0.0 >= t)
270 return rOld1;
272 else if(1.0 <= t)
274 return rOld2;
276 else
278 return B3DTuple(
279 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
280 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY(),
281 ((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ());
285 inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2)
287 return B3DTuple(
288 rtl::math::approxEqual(rOld1.getX(), rOld2.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
289 rtl::math::approxEqual(rOld1.getY(), rOld2.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5,
290 rtl::math::approxEqual(rOld1.getZ(), rOld2.getZ()) ? rOld1.getZ() : (rOld1.getZ() + rOld2.getZ()) * 0.5);
293 inline B3DTuple operator+(const B3DTuple& rTupA, const B3DTuple& rTupB)
295 B3DTuple aSum(rTupA);
296 aSum += rTupB;
297 return aSum;
300 inline B3DTuple operator-(const B3DTuple& rTupA, const B3DTuple& rTupB)
302 B3DTuple aSub(rTupA);
303 aSub -= rTupB;
304 return aSub;
307 inline B3DTuple operator*(const B3DTuple& rTupA, const B3DTuple& rTupB)
309 B3DTuple aMul(rTupA);
310 aMul *= rTupB;
311 return aMul;
314 inline B3DTuple operator*(const B3DTuple& rTup, double t)
316 B3DTuple aNew(rTup);
317 aNew *= t;
318 return aNew;
321 inline B3DTuple operator/(const B3DTuple& rTup, double t)
323 B3DTuple aNew(rTup);
324 aNew /= t;
325 return aNew;
328 /** Round double to nearest integer for 3D tuple
330 @return the nearest integer for this tuple
332 BASEGFX_DLLPUBLIC B3ITuple fround(const B3DTuple& rTup);
333 } // end of namespace basegfx
335 #endif // INCLUDED_BASEGFX_TUPLE_B3DTUPLE_HXX
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */