LCOV - code coverage report
Current view: top level - libreoffice/basegfx/inc/basegfx/tuple - b3dtuple.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 45 75 60.0 %
Date: 2012-12-17 Functions: 13 20 65.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #ifndef _BGFX_TUPLE_B3DTUPLE_HXX
      21             : #define _BGFX_TUPLE_B3DTUPLE_HXX
      22             : 
      23             : #include <sal/types.h>
      24             : #include <basegfx/numeric/ftools.hxx>
      25             : #include <basegfx/basegfxdllapi.h>
      26             : 
      27             : namespace basegfx
      28             : {
      29             :     // predeclarations
      30             :     class B3ITuple;
      31             : 
      32             :     /** Base class for all Points/Vectors with three double values
      33             : 
      34             :         This class provides all methods common to Point
      35             :         avd Vector classes which are derived from here.
      36             : 
      37             :         @derive Use this class to implement Points or Vectors
      38             :         which are based on three double values
      39             :     */
      40             :     class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED B3DTuple
      41             :     {
      42             :     protected:
      43             :         double                                      mfX;
      44             :         double                                      mfY;
      45             :         double                                      mfZ;
      46             : 
      47             :     public:
      48             :         /** Create a 3D Tuple
      49             : 
      50             :             The tuple is initialized to (0.0, 0.0, 0.0)
      51             :         */
      52          82 :         B3DTuple()
      53             :         :   mfX(0.0),
      54             :             mfY(0.0),
      55          82 :             mfZ(0.0)
      56          82 :         {}
      57             : 
      58             :         /** Create a 3D Tuple
      59             : 
      60             :             @param fX
      61             :             This parameter is used to initialize the X-coordinate
      62             :             of the 3D Tuple.
      63             : 
      64             :             @param fY
      65             :             This parameter is used to initialize the Y-coordinate
      66             :             of the 3D Tuple.
      67             : 
      68             :             @param fZ
      69             :             This parameter is used to initialize the Z-coordinate
      70             :             of the 3D Tuple.
      71             :         */
      72         854 :         B3DTuple(double fX, double fY, double fZ)
      73             :         :   mfX(fX),
      74             :             mfY(fY),
      75         854 :             mfZ(fZ)
      76         854 :         {}
      77             : 
      78             :         /** Create a copy of a 3D Tuple
      79             : 
      80             :             @param rTup
      81             :             The 3D Tuple which will be copied.
      82             :         */
      83          82 :         B3DTuple(const B3DTuple& rTup)
      84             :         :   mfX( rTup.mfX ),
      85             :             mfY( rTup.mfY ),
      86          82 :             mfZ( rTup.mfZ )
      87          82 :         {}
      88             : 
      89         820 :         ~B3DTuple()
      90         820 :         {}
      91             : 
      92             :         /// get X-Coordinate of 3D Tuple
      93         820 :         double getX() const
      94             :         {
      95         820 :             return mfX;
      96             :         }
      97             : 
      98             :         /// get Y-Coordinate of 3D Tuple
      99         820 :         double getY() const
     100             :         {
     101         820 :             return mfY;
     102             :         }
     103             : 
     104             :         /// get Z-Coordinate of 3D Tuple
     105        1312 :         double getZ() const
     106             :         {
     107        1312 :             return mfZ;
     108             :         }
     109             : 
     110             :         /// set X-Coordinate of 3D Tuple
     111         410 :         void setX(double fX)
     112             :         {
     113         410 :             mfX = fX;
     114         410 :         }
     115             : 
     116             :         /// set Y-Coordinate of 3D Tuple
     117         410 :         void setY(double fY)
     118             :         {
     119         410 :             mfY = fY;
     120         410 :         }
     121             : 
     122             :         /// set Z-Coordinate of 3D Tuple
     123         410 :         void setZ(double fZ)
     124             :         {
     125         410 :             mfZ = fZ;
     126         410 :         }
     127             : 
     128             :         /// Array-access to 3D Tuple
     129             :         const double& operator[] (int nPos) const
     130             :         {
     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);
     135             :         }
     136             : 
     137             :         /// Array-access to 3D Tuple
     138             :         double& operator[] (int nPos)
     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             :         // comparators with tolerance
     147             :         //////////////////////////////////////////////////////////////////////
     148             : 
     149           0 :         bool equalZero() const
     150             :         {
     151           0 :             return (this == &getEmptyTuple() ||
     152           0 :                 (::basegfx::fTools::equalZero(mfX)
     153           0 :                 && ::basegfx::fTools::equalZero(mfY)
     154           0 :                 && ::basegfx::fTools::equalZero(mfZ)));
     155             :         }
     156             : 
     157             :         bool equalZero(const double& rfSmallValue) const
     158             :         {
     159             :             return (this == &getEmptyTuple() ||
     160             :                 (::basegfx::fTools::equalZero(mfX, rfSmallValue)
     161             :                 && ::basegfx::fTools::equalZero(mfY, rfSmallValue)
     162             :                 && ::basegfx::fTools::equalZero(mfZ, rfSmallValue)));
     163             :         }
     164             : 
     165           0 :         bool equal(const B3DTuple& rTup) const
     166             :         {
     167             :             return (
     168             :                 this == &rTup ||
     169           0 :                 (::basegfx::fTools::equal(mfX, rTup.mfX) &&
     170           0 :                 ::basegfx::fTools::equal(mfY, rTup.mfY) &&
     171           0 :                 ::basegfx::fTools::equal(mfZ, rTup.mfZ)));
     172             :         }
     173             : 
     174             :         bool equal(const B3DTuple& rTup, const double& rfSmallValue) const
     175             :         {
     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)));
     181             :         }
     182             : 
     183             :         // operators
     184             :         //////////////////////////////////////////////////////////////////////
     185             : 
     186             :         B3DTuple& operator+=( const B3DTuple& rTup )
     187             :         {
     188             :             mfX += rTup.mfX;
     189             :             mfY += rTup.mfY;
     190             :             mfZ += rTup.mfZ;
     191             :             return *this;
     192             :         }
     193             : 
     194           0 :         B3DTuple& operator-=( const B3DTuple& rTup )
     195             :         {
     196           0 :             mfX -= rTup.mfX;
     197           0 :             mfY -= rTup.mfY;
     198           0 :             mfZ -= rTup.mfZ;
     199           0 :             return *this;
     200             :         }
     201             : 
     202             :         B3DTuple& operator/=( const B3DTuple& rTup )
     203             :         {
     204             :             mfX /= rTup.mfX;
     205             :             mfY /= rTup.mfY;
     206             :             mfZ /= rTup.mfZ;
     207             :             return *this;
     208             :         }
     209             : 
     210             :         B3DTuple& operator*=( const B3DTuple& rTup )
     211             :         {
     212             :             mfX *= rTup.mfX;
     213             :             mfY *= rTup.mfY;
     214             :             mfZ *= rTup.mfZ;
     215             :             return *this;
     216             :         }
     217             : 
     218             :         B3DTuple& operator*=(double t)
     219             :         {
     220             :             mfX *= t;
     221             :             mfY *= t;
     222             :             mfZ *= t;
     223             :             return *this;
     224             :         }
     225             : 
     226             :         B3DTuple& operator/=(double t)
     227             :         {
     228             :             const double fVal(1.0 / t);
     229             :             mfX *= fVal;
     230             :             mfY *= fVal;
     231             :             mfZ *= fVal;
     232             :             return *this;
     233             :         }
     234             : 
     235         328 :         B3DTuple operator-(void) const
     236             :         {
     237         328 :             return B3DTuple(-mfX, -mfY, -mfZ);
     238             :         }
     239             : 
     240           0 :         bool operator==( const B3DTuple& rTup ) const
     241             :         {
     242           0 :             return equal(rTup);
     243             :         }
     244             : 
     245           0 :         bool operator!=( const B3DTuple& rTup ) const
     246             :         {
     247           0 :             return !equal(rTup);
     248             :         }
     249             : 
     250         164 :         B3DTuple& operator=( const B3DTuple& rTup )
     251             :         {
     252         164 :             mfX = rTup.mfX;
     253         164 :             mfY = rTup.mfY;
     254         164 :             mfZ = rTup.mfZ;
     255         164 :             return *this;
     256             :         }
     257             : 
     258         328 :         void correctValues(const double fCompareValue = 0.0)
     259             :         {
     260         328 :             if(0.0 == fCompareValue)
     261             :             {
     262         246 :                 if(::basegfx::fTools::equalZero(mfX))
     263             :                 {
     264          82 :                     mfX = 0.0;
     265             :                 }
     266             : 
     267         246 :                 if(::basegfx::fTools::equalZero(mfY))
     268             :                 {
     269         164 :                     mfY = 0.0;
     270             :                 }
     271             : 
     272         246 :                 if(::basegfx::fTools::equalZero(mfZ))
     273             :                 {
     274         164 :                     mfZ = 0.0;
     275             :                 }
     276             :             }
     277             :             else
     278             :             {
     279          82 :                 if(::basegfx::fTools::equal(mfX, fCompareValue))
     280             :                 {
     281           0 :                     mfX = fCompareValue;
     282             :                 }
     283             : 
     284          82 :                 if(::basegfx::fTools::equal(mfY, fCompareValue))
     285             :                 {
     286           0 :                     mfY = fCompareValue;
     287             :                 }
     288             : 
     289          82 :                 if(::basegfx::fTools::equal(mfZ, fCompareValue))
     290             :                 {
     291           0 :                     mfZ = fCompareValue;
     292             :                 }
     293             :             }
     294         328 :         }
     295             : 
     296             :         static const B3DTuple& getEmptyTuple();
     297             :     };
     298             : 
     299             :     // external operators
     300             :     //////////////////////////////////////////////////////////////////////////
     301             : 
     302             :     inline B3DTuple minimum(const B3DTuple& rTupA, const B3DTuple& rTupB)
     303             :     {
     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;
     309             :     }
     310             : 
     311             :     inline B3DTuple maximum(const B3DTuple& rTupA, const B3DTuple& rTupB)
     312             :     {
     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;
     318             :     }
     319             : 
     320             :     inline B3DTuple absolute(const B3DTuple& rTup)
     321             :     {
     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;
     327             :     }
     328             : 
     329           0 :     inline B3DTuple interpolate(const B3DTuple& rOld1, const B3DTuple& rOld2, double t)
     330             :     {
     331             :         B3DTuple aInt(
     332           0 :             ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
     333           0 :             ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY(),
     334           0 :             ((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ());
     335           0 :         return aInt;
     336             :     }
     337             : 
     338             :     inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2)
     339             :     {
     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;
     345             :     }
     346             : 
     347             :     inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2, const B3DTuple& rOld3)
     348             :     {
     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;
     354             :     }
     355             : 
     356             :     inline B3DTuple operator+(const B3DTuple& rTupA, const B3DTuple& rTupB)
     357             :     {
     358             :         B3DTuple aSum(rTupA);
     359             :         aSum += rTupB;
     360             :         return aSum;
     361             :     }
     362             : 
     363           0 :     inline B3DTuple operator-(const B3DTuple& rTupA, const B3DTuple& rTupB)
     364             :     {
     365           0 :         B3DTuple aSub(rTupA);
     366           0 :         aSub -= rTupB;
     367           0 :         return aSub;
     368             :     }
     369             : 
     370             :     inline B3DTuple operator/(const B3DTuple& rTupA, const B3DTuple& rTupB)
     371             :     {
     372             :         B3DTuple aDiv(rTupA);
     373             :         aDiv /= rTupB;
     374             :         return aDiv;
     375             :     }
     376             : 
     377             :     inline B3DTuple operator*(const B3DTuple& rTupA, const B3DTuple& rTupB)
     378             :     {
     379             :         B3DTuple aMul(rTupA);
     380             :         aMul *= rTupB;
     381             :         return aMul;
     382             :     }
     383             : 
     384             :     inline B3DTuple operator*(const B3DTuple& rTup, double t)
     385             :     {
     386             :         B3DTuple aNew(rTup);
     387             :         aNew *= t;
     388             :         return aNew;
     389             :     }
     390             : 
     391             :     inline B3DTuple operator*(double t, const B3DTuple& rTup)
     392             :     {
     393             :         B3DTuple aNew(rTup);
     394             :         aNew *= t;
     395             :         return aNew;
     396             :     }
     397             : 
     398             :     inline B3DTuple operator/(const B3DTuple& rTup, double t)
     399             :     {
     400             :         B3DTuple aNew(rTup);
     401             :         aNew /= t;
     402             :         return aNew;
     403             :     }
     404             : 
     405             :     inline B3DTuple operator/(double t, const B3DTuple& rTup)
     406             :     {
     407             :         B3DTuple aNew(rTup);
     408             :         aNew /= t;
     409             :         return aNew;
     410             :     }
     411             : 
     412             :     /** Round double to nearest integer for 3D tuple
     413             : 
     414             :         @return the nearest integer for this tuple
     415             :     */
     416             :     BASEGFX_DLLPUBLIC B3ITuple fround(const B3DTuple& rTup);
     417             : } // end of namespace basegfx
     418             : 
     419             : #endif /* _BGFX_TUPLE_B3DTUPLE_HXX */
     420             : 
     421             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10