LCOV - code coverage report
Current view: top level - basegfx/inc/basegfx/tuple - b3dtuple.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 70 75 93.3 %
Date: 2012-08-25 Functions: 19 20 95.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 28 30 93.3 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #ifndef _BGFX_TUPLE_B3DTUPLE_HXX
      30                 :            : #define _BGFX_TUPLE_B3DTUPLE_HXX
      31                 :            : 
      32                 :            : #include <sal/types.h>
      33                 :            : #include <basegfx/numeric/ftools.hxx>
      34                 :            : #include <basegfx/basegfxdllapi.h>
      35                 :            : 
      36                 :            : namespace basegfx
      37                 :            : {
      38                 :            :     // predeclarations
      39                 :            :     class B3ITuple;
      40                 :            : 
      41                 :            :     /** Base class for all Points/Vectors with three double values
      42                 :            : 
      43                 :            :         This class provides all methods common to Point
      44                 :            :         avd Vector classes which are derived from here.
      45                 :            : 
      46                 :            :         @derive Use this class to implement Points or Vectors
      47                 :            :         which are based on three double values
      48                 :            :     */
      49                 :            :     class BASEGFX_DLLPUBLIC B3DTuple
      50                 :            :     {
      51                 :            :     protected:
      52                 :            :         double                                      mfX;
      53                 :            :         double                                      mfY;
      54                 :            :         double                                      mfZ;
      55                 :            : 
      56                 :            :     public:
      57                 :            :         /** Create a 3D Tuple
      58                 :            : 
      59                 :            :             The tuple is initialized to (0.0, 0.0, 0.0)
      60                 :            :         */
      61                 :      40731 :         B3DTuple()
      62                 :            :         :   mfX(0.0),
      63                 :            :             mfY(0.0),
      64                 :      40731 :             mfZ(0.0)
      65                 :      40731 :         {}
      66                 :            : 
      67                 :            :         /** Create a 3D Tuple
      68                 :            : 
      69                 :            :             @param fX
      70                 :            :             This parameter is used to initialize the X-coordinate
      71                 :            :             of the 3D Tuple.
      72                 :            : 
      73                 :            :             @param fY
      74                 :            :             This parameter is used to initialize the Y-coordinate
      75                 :            :             of the 3D Tuple.
      76                 :            : 
      77                 :            :             @param fZ
      78                 :            :             This parameter is used to initialize the Z-coordinate
      79                 :            :             of the 3D Tuple.
      80                 :            :         */
      81                 :     126145 :         B3DTuple(double fX, double fY, double fZ)
      82                 :            :         :   mfX(fX),
      83                 :            :             mfY(fY),
      84                 :     126145 :             mfZ(fZ)
      85                 :     126145 :         {}
      86                 :            : 
      87                 :            :         /** Create a copy of a 3D Tuple
      88                 :            : 
      89                 :            :             @param rTup
      90                 :            :             The 3D Tuple which will be copied.
      91                 :            :         */
      92                 :     634260 :         B3DTuple(const B3DTuple& rTup)
      93                 :            :         :   mfX( rTup.mfX ),
      94                 :            :             mfY( rTup.mfY ),
      95                 :     634260 :             mfZ( rTup.mfZ )
      96                 :     634260 :         {}
      97                 :            : 
      98                 :     724279 :         ~B3DTuple()
      99                 :     724279 :         {}
     100                 :            : 
     101                 :            :         /// get X-Coordinate of 3D Tuple
     102                 :     301322 :         double getX() const
     103                 :            :         {
     104                 :     301322 :             return mfX;
     105                 :            :         }
     106                 :            : 
     107                 :            :         /// get Y-Coordinate of 3D Tuple
     108                 :     282516 :         double getY() const
     109                 :            :         {
     110                 :     282516 :             return mfY;
     111                 :            :         }
     112                 :            : 
     113                 :            :         /// get Z-Coordinate of 3D Tuple
     114                 :     282826 :         double getZ() const
     115                 :            :         {
     116                 :     282826 :             return mfZ;
     117                 :            :         }
     118                 :            : 
     119                 :            :         /// set X-Coordinate of 3D Tuple
     120                 :       6385 :         void setX(double fX)
     121                 :            :         {
     122                 :       6385 :             mfX = fX;
     123                 :       6385 :         }
     124                 :            : 
     125                 :            :         /// set Y-Coordinate of 3D Tuple
     126                 :       6385 :         void setY(double fY)
     127                 :            :         {
     128                 :       6385 :             mfY = fY;
     129                 :       6385 :         }
     130                 :            : 
     131                 :            :         /// set Z-Coordinate of 3D Tuple
     132                 :       6385 :         void setZ(double fZ)
     133                 :            :         {
     134                 :       6385 :             mfZ = fZ;
     135                 :       6385 :         }
     136                 :            : 
     137                 :            :         /// Array-access to 3D Tuple
     138                 :            :         const double& operator[] (int nPos) const
     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                 :            :         /// Array-access to 3D Tuple
     147                 :            :         double& operator[] (int nPos)
     148                 :            :         {
     149                 :            :             // Here, normally two if(...)'s should be used. In the assumption that
     150                 :            :             // both double members can be accessed as an array a shortcut is used here.
     151                 :            :             // if(0 == nPos) return mfX; if(1 == nPos) return mfY; return mfZ;
     152                 :            :             return *((&mfX) + nPos);
     153                 :            :         }
     154                 :            : 
     155                 :            :         // comparators with tolerance
     156                 :            :         //////////////////////////////////////////////////////////////////////
     157                 :            : 
     158                 :      58062 :         bool equalZero() const
     159                 :            :         {
     160                 :      58062 :             return (this == &getEmptyTuple() ||
     161                 :      58062 :                 (::basegfx::fTools::equalZero(mfX)
     162                 :      24568 :                 && ::basegfx::fTools::equalZero(mfY)
     163 [ +  + ][ +  -  :     140692 :                 && ::basegfx::fTools::equalZero(mfZ)));
             +  +  +  + ]
     164                 :            :         }
     165                 :            : 
     166                 :            :         bool equalZero(const double& rfSmallValue) const
     167                 :            :         {
     168                 :            :             return (this == &getEmptyTuple() ||
     169                 :            :                 (::basegfx::fTools::equalZero(mfX, rfSmallValue)
     170                 :            :                 && ::basegfx::fTools::equalZero(mfY, rfSmallValue)
     171                 :            :                 && ::basegfx::fTools::equalZero(mfZ, rfSmallValue)));
     172                 :            :         }
     173                 :            : 
     174                 :     152826 :         bool equal(const B3DTuple& rTup) const
     175                 :            :         {
     176                 :            :             return (
     177                 :            :                 this == &rTup ||
     178                 :     152826 :                 (::basegfx::fTools::equal(mfX, rTup.mfX) &&
     179                 :     125784 :                 ::basegfx::fTools::equal(mfY, rTup.mfY) &&
     180   [ +  -  +  +  :     431436 :                 ::basegfx::fTools::equal(mfZ, rTup.mfZ)));
           +  + ][ +  + ]
     181                 :            :         }
     182                 :            : 
     183                 :            :         bool equal(const B3DTuple& rTup, const double& rfSmallValue) const
     184                 :            :         {
     185                 :            :             return (
     186                 :            :                 this == &rTup ||
     187                 :            :                 (::basegfx::fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
     188                 :            :                 ::basegfx::fTools::equal(mfY, rTup.mfY, rfSmallValue) &&
     189                 :            :                 ::basegfx::fTools::equal(mfZ, rTup.mfZ, rfSmallValue)));
     190                 :            :         }
     191                 :            : 
     192                 :            :         // operators
     193                 :            :         //////////////////////////////////////////////////////////////////////
     194                 :            : 
     195                 :            :         B3DTuple& operator+=( const B3DTuple& rTup )
     196                 :            :         {
     197                 :            :             mfX += rTup.mfX;
     198                 :            :             mfY += rTup.mfY;
     199                 :            :             mfZ += rTup.mfZ;
     200                 :            :             return *this;
     201                 :            :         }
     202                 :            : 
     203                 :       2688 :         B3DTuple& operator-=( const B3DTuple& rTup )
     204                 :            :         {
     205                 :       2688 :             mfX -= rTup.mfX;
     206                 :       2688 :             mfY -= rTup.mfY;
     207                 :       2688 :             mfZ -= rTup.mfZ;
     208                 :       2688 :             return *this;
     209                 :            :         }
     210                 :            : 
     211                 :            :         B3DTuple& operator/=( const B3DTuple& rTup )
     212                 :            :         {
     213                 :            :             mfX /= rTup.mfX;
     214                 :            :             mfY /= rTup.mfY;
     215                 :            :             mfZ /= rTup.mfZ;
     216                 :            :             return *this;
     217                 :            :         }
     218                 :            : 
     219                 :            :         B3DTuple& operator*=( const B3DTuple& rTup )
     220                 :            :         {
     221                 :            :             mfX *= rTup.mfX;
     222                 :            :             mfY *= rTup.mfY;
     223                 :            :             mfZ *= rTup.mfZ;
     224                 :            :             return *this;
     225                 :            :         }
     226                 :            : 
     227                 :            :         B3DTuple& operator*=(double t)
     228                 :            :         {
     229                 :            :             mfX *= t;
     230                 :            :             mfY *= t;
     231                 :            :             mfZ *= t;
     232                 :            :             return *this;
     233                 :            :         }
     234                 :            : 
     235                 :            :         B3DTuple& operator/=(double t)
     236                 :            :         {
     237                 :            :             const double fVal(1.0 / t);
     238                 :            :             mfX *= fVal;
     239                 :            :             mfY *= fVal;
     240                 :            :             mfZ *= fVal;
     241                 :            :             return *this;
     242                 :            :         }
     243                 :            : 
     244                 :       3956 :         B3DTuple operator-(void) const
     245                 :            :         {
     246                 :       3956 :             return B3DTuple(-mfX, -mfY, -mfZ);
     247                 :            :         }
     248                 :            : 
     249                 :     108836 :         bool operator==( const B3DTuple& rTup ) const
     250                 :            :         {
     251                 :     108836 :             return equal(rTup);
     252                 :            :         }
     253                 :            : 
     254                 :      38300 :         bool operator!=( const B3DTuple& rTup ) const
     255                 :            :         {
     256                 :      38300 :             return !equal(rTup);
     257                 :            :         }
     258                 :            : 
     259                 :     126667 :         B3DTuple& operator=( const B3DTuple& rTup )
     260                 :            :         {
     261                 :     126667 :             mfX = rTup.mfX;
     262                 :     126667 :             mfY = rTup.mfY;
     263                 :     126667 :             mfZ = rTup.mfZ;
     264                 :     126667 :             return *this;
     265                 :            :         }
     266                 :            : 
     267                 :       5108 :         void correctValues(const double fCompareValue = 0.0)
     268                 :            :         {
     269         [ +  + ]:       5108 :             if(0.0 == fCompareValue)
     270                 :            :             {
     271         [ +  + ]:       3831 :                 if(::basegfx::fTools::equalZero(mfX))
     272                 :            :                 {
     273                 :       1669 :                     mfX = 0.0;
     274                 :            :                 }
     275                 :            : 
     276         [ +  + ]:       3831 :                 if(::basegfx::fTools::equalZero(mfY))
     277                 :            :                 {
     278                 :       2652 :                     mfY = 0.0;
     279                 :            :                 }
     280                 :            : 
     281         [ +  + ]:       3831 :                 if(::basegfx::fTools::equalZero(mfZ))
     282                 :            :                 {
     283                 :       2652 :                     mfZ = 0.0;
     284                 :            :                 }
     285                 :            :             }
     286                 :            :             else
     287                 :            :             {
     288         [ +  + ]:       1277 :                 if(::basegfx::fTools::equal(mfX, fCompareValue))
     289                 :            :                 {
     290                 :        294 :                     mfX = fCompareValue;
     291                 :            :                 }
     292                 :            : 
     293         [ +  + ]:       1277 :                 if(::basegfx::fTools::equal(mfY, fCompareValue))
     294                 :            :                 {
     295                 :        294 :                     mfY = fCompareValue;
     296                 :            :                 }
     297                 :            : 
     298         [ +  + ]:       1277 :                 if(::basegfx::fTools::equal(mfZ, fCompareValue))
     299                 :            :                 {
     300                 :        294 :                     mfZ = fCompareValue;
     301                 :            :                 }
     302                 :            :             }
     303                 :       5108 :         }
     304                 :            : 
     305                 :            :         static const B3DTuple& getEmptyTuple();
     306                 :            :     };
     307                 :            : 
     308                 :            :     // external operators
     309                 :            :     //////////////////////////////////////////////////////////////////////////
     310                 :            : 
     311                 :            :     inline B3DTuple minimum(const B3DTuple& rTupA, const B3DTuple& rTupB)
     312                 :            :     {
     313                 :            :         B3DTuple aMin(
     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 aMin;
     318                 :            :     }
     319                 :            : 
     320                 :            :     inline B3DTuple maximum(const B3DTuple& rTupA, const B3DTuple& rTupB)
     321                 :            :     {
     322                 :            :         B3DTuple aMax(
     323                 :            :             (rTupB.getX() > rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
     324                 :            :             (rTupB.getY() > rTupA.getY()) ? rTupB.getY() : rTupA.getY(),
     325                 :            :             (rTupB.getZ() > rTupA.getZ()) ? rTupB.getZ() : rTupA.getZ());
     326                 :            :         return aMax;
     327                 :            :     }
     328                 :            : 
     329                 :            :     inline B3DTuple absolute(const B3DTuple& rTup)
     330                 :            :     {
     331                 :            :         B3DTuple aAbs(
     332                 :            :             (0.0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
     333                 :            :             (0.0 > rTup.getY()) ? -rTup.getY() : rTup.getY(),
     334                 :            :             (0.0 > rTup.getZ()) ? -rTup.getZ() : rTup.getZ());
     335                 :            :         return aAbs;
     336                 :            :     }
     337                 :            : 
     338                 :          0 :     inline B3DTuple interpolate(const B3DTuple& rOld1, const B3DTuple& rOld2, double t)
     339                 :            :     {
     340                 :            :         B3DTuple aInt(
     341                 :          0 :             ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
     342                 :          0 :             ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY(),
     343                 :          0 :             ((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ());
     344                 :          0 :         return aInt;
     345                 :            :     }
     346                 :            : 
     347                 :            :     inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2)
     348                 :            :     {
     349                 :            :         B3DTuple aAvg(
     350                 :            :             (rOld1.getX() + rOld2.getX()) * 0.5,
     351                 :            :             (rOld1.getY() + rOld2.getY()) * 0.5,
     352                 :            :             (rOld1.getZ() + rOld2.getZ()) * 0.5);
     353                 :            :         return aAvg;
     354                 :            :     }
     355                 :            : 
     356                 :            :     inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2, const B3DTuple& rOld3)
     357                 :            :     {
     358                 :            :         B3DTuple aAvg(
     359                 :            :             (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
     360                 :            :             (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0),
     361                 :            :             (rOld1.getZ() + rOld2.getZ() + rOld3.getZ()) * (1.0 / 3.0));
     362                 :            :         return aAvg;
     363                 :            :     }
     364                 :            : 
     365                 :            :     inline B3DTuple operator+(const B3DTuple& rTupA, const B3DTuple& rTupB)
     366                 :            :     {
     367                 :            :         B3DTuple aSum(rTupA);
     368                 :            :         aSum += rTupB;
     369                 :            :         return aSum;
     370                 :            :     }
     371                 :            : 
     372                 :       2688 :     inline B3DTuple operator-(const B3DTuple& rTupA, const B3DTuple& rTupB)
     373                 :            :     {
     374                 :       2688 :         B3DTuple aSub(rTupA);
     375                 :       2688 :         aSub -= rTupB;
     376                 :       2688 :         return aSub;
     377                 :            :     }
     378                 :            : 
     379                 :            :     inline B3DTuple operator/(const B3DTuple& rTupA, const B3DTuple& rTupB)
     380                 :            :     {
     381                 :            :         B3DTuple aDiv(rTupA);
     382                 :            :         aDiv /= rTupB;
     383                 :            :         return aDiv;
     384                 :            :     }
     385                 :            : 
     386                 :            :     inline B3DTuple operator*(const B3DTuple& rTupA, const B3DTuple& rTupB)
     387                 :            :     {
     388                 :            :         B3DTuple aMul(rTupA);
     389                 :            :         aMul *= rTupB;
     390                 :            :         return aMul;
     391                 :            :     }
     392                 :            : 
     393                 :            :     inline B3DTuple operator*(const B3DTuple& rTup, double t)
     394                 :            :     {
     395                 :            :         B3DTuple aNew(rTup);
     396                 :            :         aNew *= t;
     397                 :            :         return aNew;
     398                 :            :     }
     399                 :            : 
     400                 :            :     inline B3DTuple operator*(double t, const B3DTuple& rTup)
     401                 :            :     {
     402                 :            :         B3DTuple aNew(rTup);
     403                 :            :         aNew *= t;
     404                 :            :         return aNew;
     405                 :            :     }
     406                 :            : 
     407                 :            :     inline B3DTuple operator/(const B3DTuple& rTup, double t)
     408                 :            :     {
     409                 :            :         B3DTuple aNew(rTup);
     410                 :            :         aNew /= t;
     411                 :            :         return aNew;
     412                 :            :     }
     413                 :            : 
     414                 :            :     inline B3DTuple operator/(double t, const B3DTuple& rTup)
     415                 :            :     {
     416                 :            :         B3DTuple aNew(rTup);
     417                 :            :         aNew /= t;
     418                 :            :         return aNew;
     419                 :            :     }
     420                 :            : 
     421                 :            :     /** Round double to nearest integer for 3D tuple
     422                 :            : 
     423                 :            :         @return the nearest integer for this tuple
     424                 :            :     */
     425                 :            :     BASEGFX_DLLPUBLIC B3ITuple fround(const B3DTuple& rTup);
     426                 :            : } // end of namespace basegfx
     427                 :            : 
     428                 :            : #endif /* _BGFX_TUPLE_B3DTUPLE_HXX */
     429                 :            : 
     430                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10