1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
50 The tuple is initialized to (0.0, 0.0, 0.0)
61 This parameter is used to initialize the X-coordinate
65 This parameter is used to initialize the Y-coordinate
69 This parameter is used to initialize the Z-coordinate
72 B3DTuple(double fX
, double fY
, double fZ
)
78 /** Create a copy of a 3D Tuple
81 The 3D Tuple which will be copied.
83 B3DTuple(const B3DTuple
& rTup
)
92 /// get X-Coordinate of 3D Tuple
98 /// get Y-Coordinate of 3D Tuple
104 /// get Z-Coordinate of 3D Tuple
110 /// set X-Coordinate of 3D Tuple
116 /// set Y-Coordinate of 3D Tuple
122 /// set Z-Coordinate of 3D Tuple
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
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
178 (::basegfx::fTools::equal(mfX
, rTup
.mfX
, rfSmallValue
) &&
179 ::basegfx::fTools::equal(mfY
, rTup
.mfY
, rfSmallValue
) &&
180 ::basegfx::fTools::equal(mfZ
, rTup
.mfZ
, rfSmallValue
)));
184 //////////////////////////////////////////////////////////////////////
186 B3DTuple
& operator+=( const B3DTuple
& rTup
)
194 B3DTuple
& operator-=( const B3DTuple
& rTup
)
202 B3DTuple
& operator/=( const B3DTuple
& rTup
)
210 B3DTuple
& operator*=( const B3DTuple
& rTup
)
218 B3DTuple
& operator*=(double t
)
226 B3DTuple
& operator/=(double t
)
228 const double fVal(1.0 / t
);
235 B3DTuple
operator-(void) const
237 return B3DTuple(-mfX
, -mfY
, -mfZ
);
240 bool operator==( const B3DTuple
& rTup
) const
245 bool operator!=( const B3DTuple
& rTup
) const
250 B3DTuple
& operator=( const B3DTuple
& rTup
)
258 void correctValues(const double fCompareValue
= 0.0)
260 if(0.0 == fCompareValue
)
262 if(::basegfx::fTools::equalZero(mfX
))
267 if(::basegfx::fTools::equalZero(mfY
))
272 if(::basegfx::fTools::equalZero(mfZ
))
279 if(::basegfx::fTools::equal(mfX
, fCompareValue
))
284 if(::basegfx::fTools::equal(mfY
, fCompareValue
))
289 if(::basegfx::fTools::equal(mfZ
, fCompareValue
))
296 static const B3DTuple
& getEmptyTuple();
299 // external operators
300 //////////////////////////////////////////////////////////////////////////
302 inline B3DTuple
minimum(const B3DTuple
& rTupA
, const B3DTuple
& rTupB
)
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());
311 inline B3DTuple
maximum(const B3DTuple
& rTupA
, const B3DTuple
& rTupB
)
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());
320 inline B3DTuple
absolute(const B3DTuple
& rTup
)
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());
329 inline B3DTuple
interpolate(const B3DTuple
& rOld1
, const B3DTuple
& rOld2
, double t
)
332 ((rOld2
.getX() - rOld1
.getX()) * t
) + rOld1
.getX(),
333 ((rOld2
.getY() - rOld1
.getY()) * t
) + rOld1
.getY(),
334 ((rOld2
.getZ() - rOld1
.getZ()) * t
) + rOld1
.getZ());
338 inline B3DTuple
average(const B3DTuple
& rOld1
, const B3DTuple
& rOld2
)
341 (rOld1
.getX() + rOld2
.getX()) * 0.5,
342 (rOld1
.getY() + rOld2
.getY()) * 0.5,
343 (rOld1
.getZ() + rOld2
.getZ()) * 0.5);
347 inline B3DTuple
average(const B3DTuple
& rOld1
, const B3DTuple
& rOld2
, const B3DTuple
& rOld3
)
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));
356 inline B3DTuple
operator+(const B3DTuple
& rTupA
, const B3DTuple
& rTupB
)
358 B3DTuple
aSum(rTupA
);
363 inline B3DTuple
operator-(const B3DTuple
& rTupA
, const B3DTuple
& rTupB
)
365 B3DTuple
aSub(rTupA
);
370 inline B3DTuple
operator/(const B3DTuple
& rTupA
, const B3DTuple
& rTupB
)
372 B3DTuple
aDiv(rTupA
);
377 inline B3DTuple
operator*(const B3DTuple
& rTupA
, const B3DTuple
& rTupB
)
379 B3DTuple
aMul(rTupA
);
384 inline B3DTuple
operator*(const B3DTuple
& rTup
, double t
)
391 inline B3DTuple
operator*(double t
, const B3DTuple
& rTup
)
398 inline B3DTuple
operator/(const B3DTuple
& rTup
, double t
)
405 inline B3DTuple
operator/(double t
, const B3DTuple
& rTup
)
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: */