Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / include / basegfx / tuple / b3i64tuple.hxx
blob6875ae9458dd0ed74566c19ce5922ea848a1afd7
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_B3I64TUPLE_HXX
21 #define INCLUDED_BASEGFX_TUPLE_B3I64TUPLE_HXX
23 #include <sal/types.h>
24 #include <basegfx/tuple/b3dtuple.hxx>
25 #include <basegfx/basegfxdllapi.h>
28 namespace basegfx
30 /** Base class for all Points/Vectors with three sal_Int64 values
32 This class provides all methods common to Point
33 avd Vector classes which are derived from here.
35 @derive Use this class to implement Points or Vectors
36 which are based on three sal_Int64 values
38 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED B3I64Tuple
40 protected:
41 sal_Int64 mnX;
42 sal_Int64 mnY;
43 sal_Int64 mnZ;
45 public:
46 /** Create a 3D Tuple
48 The tuple is initialized to (0, 0, 0)
50 B3I64Tuple()
51 : mnX(0),
52 mnY(0),
53 mnZ(0)
56 /** Create a 3D Tuple
58 @param nX
59 This parameter is used to initialize the X-coordinate
60 of the 3D Tuple.
62 @param nY
63 This parameter is used to initialize the Y-coordinate
64 of the 3D Tuple.
66 @param nZ
67 This parameter is used to initialize the Z-coordinate
68 of the 3D Tuple.
70 B3I64Tuple(sal_Int64 nX, sal_Int64 nY, sal_Int64 nZ)
71 : mnX(nX),
72 mnY(nY),
73 mnZ(nZ)
76 /** Create a copy of a 3D Tuple
78 @param rTup
79 The 3D Tuple which will be copied.
81 B3I64Tuple(const B3I64Tuple& rTup)
82 : mnX( rTup.mnX ),
83 mnY( rTup.mnY ),
84 mnZ( rTup.mnZ )
87 ~B3I64Tuple()
90 /// get X-Coordinate of 3D Tuple
91 sal_Int64 getX() const
93 return mnX;
96 /// get Y-Coordinate of 3D Tuple
97 sal_Int64 getY() const
99 return mnY;
102 /// get Z-Coordinate of 3D Tuple
103 sal_Int64 getZ() const
105 return mnZ;
108 /// set X-Coordinate of 3D Tuple
109 void setX(sal_Int64 nX)
111 mnX = nX;
114 /// set Y-Coordinate of 3D Tuple
115 void setY(sal_Int64 nY)
117 mnY = nY;
120 /// set Z-Coordinate of 3D Tuple
121 void setZ(sal_Int64 nZ)
123 mnZ = nZ;
126 /// Array-access to 3D Tuple
127 const sal_Int64& operator[] (int nPos) const
129 // Here, normally two if(...)'s should be used. In the assumption that
130 // both sal_Int64 members can be accessed as an array a shortcut is used here.
131 // if(0 == nPos) return mnX; if(1 == nPos) return mnY; return mnZ;
132 return *((&mnX) + nPos);
135 /// Array-access to 3D Tuple
136 sal_Int64& operator[] (int nPos)
138 // Here, normally two if(...)'s should be used. In the assumption that
139 // both sal_Int64 members can be accessed as an array a shortcut is used here.
140 // if(0 == nPos) return mnX; if(1 == nPos) return mnY; return mnZ;
141 return *((&mnX) + nPos);
144 // operators
147 B3I64Tuple& operator+=( const B3I64Tuple& rTup )
149 mnX += rTup.mnX;
150 mnY += rTup.mnY;
151 mnZ += rTup.mnZ;
152 return *this;
155 B3I64Tuple& operator-=( const B3I64Tuple& rTup )
157 mnX -= rTup.mnX;
158 mnY -= rTup.mnY;
159 mnZ -= rTup.mnZ;
160 return *this;
163 B3I64Tuple& operator/=( const B3I64Tuple& rTup )
165 mnX /= rTup.mnX;
166 mnY /= rTup.mnY;
167 mnZ /= rTup.mnZ;
168 return *this;
171 B3I64Tuple& operator*=( const B3I64Tuple& rTup )
173 mnX *= rTup.mnX;
174 mnY *= rTup.mnY;
175 mnZ *= rTup.mnZ;
176 return *this;
179 B3I64Tuple& operator*=(sal_Int64 t)
181 mnX *= t;
182 mnY *= t;
183 mnZ *= t;
184 return *this;
187 B3I64Tuple& operator/=(sal_Int64 t)
189 mnX /= t;
190 mnY /= t;
191 mnZ /= t;
192 return *this;
195 B3I64Tuple operator-(void) const
197 return B3I64Tuple(-mnX, -mnY, -mnZ);
200 bool operator==( const B3I64Tuple& rTup ) const
202 return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY && rTup.mnZ == mnZ);
205 bool operator!=( const B3I64Tuple& rTup ) const
207 return !(*this == rTup);
210 B3I64Tuple& operator=( const B3I64Tuple& rTup )
212 mnX = rTup.mnX;
213 mnY = rTup.mnY;
214 mnZ = rTup.mnZ;
215 return *this;
219 // external operators
222 inline B3I64Tuple minimum(const B3I64Tuple& rTupA, const B3I64Tuple& rTupB)
224 return B3I64Tuple(
225 std::min(rTupB.getX(), rTupA.getX()),
226 std::min(rTupB.getY(), rTupA.getY()),
227 std::min(rTupB.getZ(), rTupA.getZ()));
230 inline B3I64Tuple maximum(const B3I64Tuple& rTupA, const B3I64Tuple& rTupB)
232 return B3I64Tuple(
233 std::max(rTupB.getX(), rTupA.getX()),
234 std::max(rTupB.getY(), rTupA.getY()),
235 std::max(rTupB.getZ(), rTupA.getZ()));
238 inline B3I64Tuple absolute(const B3I64Tuple& rTup)
240 B3I64Tuple aAbs(
241 (0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
242 (0 > rTup.getY()) ? -rTup.getY() : rTup.getY(),
243 (0 > rTup.getZ()) ? -rTup.getZ() : rTup.getZ());
244 return aAbs;
247 inline B3I64Tuple interpolate(const B3I64Tuple& rOld1, const B3I64Tuple& rOld2, double t)
249 if(rOld1 == rOld2)
251 return rOld1;
253 else if(0.0 >= t)
255 return rOld1;
257 else if(1.0 <= t)
259 return rOld2;
261 else
263 return B3I64Tuple(
264 basegfx::fround64(((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX()),
265 basegfx::fround64(((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY()),
266 basegfx::fround64(((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ()));
270 inline B3I64Tuple average(const B3I64Tuple& rOld1, const B3I64Tuple& rOld2)
272 return B3I64Tuple(
273 rOld1.getX() == rOld2.getX() ? rOld1.getX() : basegfx::fround64((rOld1.getX() + rOld2.getX()) * 0.5),
274 rOld1.getY() == rOld2.getY() ? rOld1.getY() : basegfx::fround64((rOld1.getY() + rOld2.getY()) * 0.5),
275 rOld1.getZ() == rOld2.getZ() ? rOld1.getZ() : basegfx::fround64((rOld1.getZ() + rOld2.getZ()) * 0.5));
278 inline B3I64Tuple average(const B3I64Tuple& rOld1, const B3I64Tuple& rOld2, const B3I64Tuple& rOld3)
280 return B3I64Tuple(
281 (rOld1.getX() == rOld2.getX() && rOld2.getX() == rOld3.getX()) ? rOld1.getX() : basegfx::fround64((rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0)),
282 (rOld1.getY() == rOld2.getY() && rOld2.getY() == rOld3.getY()) ? rOld1.getY() : basegfx::fround64((rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0)),
283 (rOld1.getZ() == rOld2.getZ() && rOld2.getZ() == rOld3.getZ()) ? rOld1.getZ() : basegfx::fround64((rOld1.getZ() + rOld2.getZ() + rOld3.getZ()) * (1.0 / 3.0)));
286 inline B3I64Tuple operator+(const B3I64Tuple& rTupA, const B3I64Tuple& rTupB)
288 B3I64Tuple aSum(rTupA);
289 aSum += rTupB;
290 return aSum;
293 inline B3I64Tuple operator-(const B3I64Tuple& rTupA, const B3I64Tuple& rTupB)
295 B3I64Tuple aSub(rTupA);
296 aSub -= rTupB;
297 return aSub;
300 inline B3I64Tuple operator/(const B3I64Tuple& rTupA, const B3I64Tuple& rTupB)
302 B3I64Tuple aDiv(rTupA);
303 aDiv /= rTupB;
304 return aDiv;
307 inline B3I64Tuple operator*(const B3I64Tuple& rTupA, const B3I64Tuple& rTupB)
309 B3I64Tuple aMul(rTupA);
310 aMul *= rTupB;
311 return aMul;
314 inline B3I64Tuple operator*(const B3I64Tuple& rTup, sal_Int64 t)
316 B3I64Tuple aNew(rTup);
317 aNew *= t;
318 return aNew;
321 inline B3I64Tuple operator*(sal_Int64 t, const B3I64Tuple& rTup)
323 B3I64Tuple aNew(rTup);
324 aNew *= t;
325 return aNew;
328 inline B3I64Tuple operator/(const B3I64Tuple& rTup, sal_Int64 t)
330 B3I64Tuple aNew(rTup);
331 aNew /= t;
332 return aNew;
335 inline B3I64Tuple operator/(sal_Int64 t, const B3I64Tuple& rTup)
337 B3I64Tuple aNew(t, t, t);
338 B3I64Tuple aTmp(rTup);
339 aNew /= aTmp;
340 return aNew;
342 } // end of namespace basegfx
344 #endif // INCLUDED_BASEGFX_TUPLE_B3I64TUPLE_HXX
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */