LCOV - code coverage report
Current view: top level - basegfx/inc/basegfx/tuple - b2dtuple.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 80 93 86.0 %
Date: 2012-08-25 Functions: 24 28 85.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 11 18 61.1 %

           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_B2DTUPLE_HXX
      30                 :            : #define _BGFX_TUPLE_B2DTUPLE_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 B2ITuple;
      40                 :            : 
      41                 :            :     /** Base class for all Points/Vectors with two 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 two double values
      48                 :            :     */
      49                 :            :     class B2DTuple
      50                 :            :     {
      51                 :            :     protected:
      52                 :            :         double                                      mfX;
      53                 :            :         double                                      mfY;
      54                 :            : 
      55                 :            :     public:
      56                 :            :         /** Create a 2D Tuple
      57                 :            : 
      58                 :            :             The tuple is initialized to (0.0, 0.0)
      59                 :            :         */
      60                 :     296456 :         B2DTuple()
      61                 :            :         :   mfX(0.0),
      62                 :     296456 :             mfY(0.0)
      63                 :     296456 :         {}
      64                 :            : 
      65                 :            :         /** Create a 2D Tuple
      66                 :            : 
      67                 :            :             @param fX
      68                 :            :             This parameter is used to initialize the X-coordinate
      69                 :            :             of the 2D Tuple.
      70                 :            : 
      71                 :            :             @param fY
      72                 :            :             This parameter is used to initialize the Y-coordinate
      73                 :            :             of the 2D Tuple.
      74                 :            :         */
      75                 :    5486038 :         B2DTuple(double fX, double fY)
      76                 :            :         :   mfX( fX ),
      77                 :    5486038 :             mfY( fY )
      78                 :    5486038 :         {}
      79                 :            : 
      80                 :            :         /** Create a copy of a 2D Tuple
      81                 :            : 
      82                 :            :             @param rTup
      83                 :            :             The 2D Tuple which will be copied.
      84                 :            :         */
      85                 :  103224298 :         B2DTuple(const B2DTuple& rTup)
      86                 :            :         :   mfX( rTup.mfX ),
      87                 :  103224298 :             mfY( rTup.mfY )
      88                 :  103224298 :         {}
      89                 :            : 
      90                 :            :         /** Create a copy of a 2D integer Tuple
      91                 :            : 
      92                 :            :             @param rTup
      93                 :            :             The 2D Tuple which will be copied.
      94                 :            :         */
      95                 :            :         BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
      96                 :            : 
      97                 :   75690689 :         ~B2DTuple()
      98                 :   75690689 :         {}
      99                 :            : 
     100                 :            :         /// Get X-Coordinate of 2D Tuple
     101                 :   24273026 :         double getX() const
     102                 :            :         {
     103                 :   24273026 :             return mfX;
     104                 :            :         }
     105                 :            : 
     106                 :            :         /// Get Y-Coordinate of 2D Tuple
     107                 :   23736692 :         double getY() const
     108                 :            :         {
     109                 :   23736692 :             return mfY;
     110                 :            :         }
     111                 :            : 
     112                 :            :         /// Set X-Coordinate of 2D Tuple
     113                 :     645326 :         void setX(double fX)
     114                 :            :         {
     115                 :     645326 :             mfX = fX;
     116                 :     645326 :         }
     117                 :            : 
     118                 :            :         /// Set Y-Coordinate of 2D Tuple
     119                 :     642807 :         void setY(double fY)
     120                 :            :         {
     121                 :     642807 :             mfY = fY;
     122                 :     642807 :         }
     123                 :            : 
     124                 :            :         /// Array-access to 2D Tuple
     125                 :            :         const double& operator[] (int nPos) const
     126                 :            :         {
     127                 :            :             // Here, normally one if(...) should be used. In the assumption that
     128                 :            :             // both double members can be accessed as an array a shortcut is used here.
     129                 :            :             // if(0 == nPos) return mfX; return mfY;
     130                 :            :             return *((&mfX) + nPos);
     131                 :            :         }
     132                 :            : 
     133                 :            :         /// Array-access to 2D Tuple
     134                 :            :         double& operator[] (int nPos)
     135                 :            :         {
     136                 :            :             // Here, normally one if(...) should be used. In the assumption that
     137                 :            :             // both double members can be accessed as an array a shortcut is used here.
     138                 :            :             // if(0 == nPos) return mfX; return mfY;
     139                 :            :             return *((&mfX) + nPos);
     140                 :            :         }
     141                 :            : 
     142                 :            :         // comparators with tolerance
     143                 :            :         //////////////////////////////////////////////////////////////////////
     144                 :            : 
     145                 :    1303195 :         bool equalZero() const
     146                 :            :         {
     147                 :    1303195 :             return (this == &getEmptyTuple() ||
     148 [ +  + ][ +  + ]:    1303195 :                     (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
                 [ +  - ]
     149                 :            :         }
     150                 :            : 
     151                 :            :         bool equalZero(const double& rfSmallValue) const
     152                 :            :         {
     153                 :            :             return (this == &getEmptyTuple() ||
     154                 :            :                     (fTools::equalZero(mfX, rfSmallValue) && fTools::equalZero(mfY, rfSmallValue)));
     155                 :            :         }
     156                 :            : 
     157                 :   12766921 :         bool equal(const B2DTuple& rTup) const
     158                 :            :         {
     159                 :            :             return (
     160                 :            :                 this == &rTup ||
     161                 :   12766916 :                 (fTools::equal(mfX, rTup.mfX) &&
     162   [ +  +  +  + ]:   25533837 :                 fTools::equal(mfY, rTup.mfY)));
                 [ +  + ]
     163                 :            :         }
     164                 :            : 
     165                 :          0 :         bool equal(const B2DTuple& rTup, const double& rfSmallValue) const
     166                 :            :         {
     167                 :            :             return (
     168                 :            :                 this == &rTup ||
     169                 :          0 :                 (fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
     170   [ #  #  #  # ]:          0 :                 fTools::equal(mfY, rTup.mfY, rfSmallValue)));
                 [ #  # ]
     171                 :            :         }
     172                 :            : 
     173                 :            :         // operators
     174                 :            :         //////////////////////////////////////////////////////////////////////
     175                 :            : 
     176                 :     451152 :         B2DTuple& operator+=( const B2DTuple& rTup )
     177                 :            :         {
     178                 :     451152 :             mfX += rTup.mfX;
     179                 :     451152 :             mfY += rTup.mfY;
     180                 :     451152 :             return *this;
     181                 :            :         }
     182                 :            : 
     183                 :     284961 :         B2DTuple& operator-=( const B2DTuple& rTup )
     184                 :            :         {
     185                 :     284961 :             mfX -= rTup.mfX;
     186                 :     284961 :             mfY -= rTup.mfY;
     187                 :     284961 :             return *this;
     188                 :            :         }
     189                 :            : 
     190                 :          0 :         B2DTuple& operator/=( const B2DTuple& rTup )
     191                 :            :         {
     192                 :          0 :             mfX /= rTup.mfX;
     193                 :          0 :             mfY /= rTup.mfY;
     194                 :          0 :             return *this;
     195                 :            :         }
     196                 :            : 
     197                 :            :         B2DTuple& operator*=( const B2DTuple& rTup )
     198                 :            :         {
     199                 :            :             mfX *= rTup.mfX;
     200                 :            :             mfY *= rTup.mfY;
     201                 :            :             return *this;
     202                 :            :         }
     203                 :            : 
     204                 :      31622 :         B2DTuple& operator*=(double t)
     205                 :            :         {
     206                 :      31622 :             mfX *= t;
     207                 :      31622 :             mfY *= t;
     208                 :      31622 :             return *this;
     209                 :            :         }
     210                 :            : 
     211                 :       1090 :         B2DTuple& operator/=(double t)
     212                 :            :         {
     213                 :       1090 :             const double fVal(1.0 / t);
     214                 :       1090 :             mfX *= fVal;
     215                 :       1090 :             mfY *= fVal;
     216                 :       1090 :             return *this;
     217                 :            :         }
     218                 :            : 
     219                 :          0 :         B2DTuple operator-(void) const
     220                 :            :         {
     221                 :          0 :             return B2DTuple(-mfX, -mfY);
     222                 :            :         }
     223                 :            : 
     224                 :     325478 :         bool operator==( const B2DTuple& rTup ) const
     225                 :            :         {
     226                 :     325478 :             return equal(rTup);
     227                 :            :         }
     228                 :            : 
     229                 :   12019333 :         bool operator!=( const B2DTuple& rTup ) const
     230                 :            :         {
     231                 :   12019333 :             return !equal(rTup);
     232                 :            :         }
     233                 :            : 
     234                 :   11846226 :         B2DTuple& operator=( const B2DTuple& rTup )
     235                 :            :         {
     236                 :   11846226 :             mfX = rTup.mfX;
     237                 :   11846226 :             mfY = rTup.mfY;
     238                 :   11846226 :             return *this;
     239                 :            :         }
     240                 :            : 
     241                 :            :         BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
     242                 :            :     };
     243                 :            : 
     244                 :            :     // external operators
     245                 :            :     //////////////////////////////////////////////////////////////////////////
     246                 :            : 
     247                 :            :     inline B2DTuple minimum(const B2DTuple& rTupA, const B2DTuple& rTupB)
     248                 :            :     {
     249                 :            :         B2DTuple aMin(
     250                 :            :             (rTupB.getX() < rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
     251                 :            :             (rTupB.getY() < rTupA.getY()) ? rTupB.getY() : rTupA.getY());
     252                 :            :         return aMin;
     253                 :            :     }
     254                 :            : 
     255                 :            :     inline B2DTuple maximum(const B2DTuple& rTupA, const B2DTuple& rTupB)
     256                 :            :     {
     257                 :            :         B2DTuple aMax(
     258                 :            :             (rTupB.getX() > rTupA.getX()) ? rTupB.getX() : rTupA.getX(),
     259                 :            :             (rTupB.getY() > rTupA.getY()) ? rTupB.getY() : rTupA.getY());
     260                 :            :         return aMax;
     261                 :            :     }
     262                 :            : 
     263                 :            :     inline B2DTuple absolute(const B2DTuple& rTup)
     264                 :            :     {
     265                 :            :         B2DTuple aAbs(
     266                 :            :             (0.0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
     267                 :            :             (0.0 > rTup.getY()) ? -rTup.getY() : rTup.getY());
     268                 :            :         return aAbs;
     269                 :            :     }
     270                 :            : 
     271                 :     208656 :     inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
     272                 :            :     {
     273                 :            :         B2DTuple aInt(
     274                 :     208656 :             ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
     275                 :     417312 :             ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
     276                 :     208656 :         return aInt;
     277                 :            :     }
     278                 :            : 
     279                 :       9000 :     inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
     280                 :            :     {
     281                 :            :         B2DTuple aAvg(
     282                 :       9000 :             (rOld1.getX() + rOld2.getX()) * 0.5,
     283                 :      18000 :             (rOld1.getY() + rOld2.getY()) * 0.5);
     284                 :       9000 :         return aAvg;
     285                 :            :     }
     286                 :            : 
     287                 :            :     inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2, const B2DTuple& rOld3)
     288                 :            :     {
     289                 :            :         B2DTuple aAvg(
     290                 :            :             (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
     291                 :            :             (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
     292                 :            :         return aAvg;
     293                 :            :     }
     294                 :            : 
     295                 :     451152 :     inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
     296                 :            :     {
     297                 :     451152 :         B2DTuple aSum(rTupA);
     298                 :     451152 :         aSum += rTupB;
     299                 :     451152 :         return aSum;
     300                 :            :     }
     301                 :            : 
     302                 :     284961 :     inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
     303                 :            :     {
     304                 :     284961 :         B2DTuple aSub(rTupA);
     305                 :     284961 :         aSub -= rTupB;
     306                 :     284961 :         return aSub;
     307                 :            :     }
     308                 :            : 
     309                 :          0 :     inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
     310                 :            :     {
     311                 :          0 :         B2DTuple aDiv(rTupA);
     312                 :          0 :         aDiv /= rTupB;
     313                 :          0 :         return aDiv;
     314                 :            :     }
     315                 :            : 
     316                 :            :     inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
     317                 :            :     {
     318                 :            :         B2DTuple aMul(rTupA);
     319                 :            :         aMul *= rTupB;
     320                 :            :         return aMul;
     321                 :            :     }
     322                 :            : 
     323                 :      30522 :     inline B2DTuple operator*(const B2DTuple& rTup, double t)
     324                 :            :     {
     325                 :      30522 :         B2DTuple aNew(rTup);
     326                 :      30522 :         aNew *= t;
     327                 :      30522 :         return aNew;
     328                 :            :     }
     329                 :            : 
     330                 :       1090 :     inline B2DTuple operator*(double t, const B2DTuple& rTup)
     331                 :            :     {
     332                 :       1090 :         B2DTuple aNew(rTup);
     333                 :       1090 :         aNew *= t;
     334                 :       1090 :         return aNew;
     335                 :            :     }
     336                 :            : 
     337                 :       1090 :     inline B2DTuple operator/(const B2DTuple& rTup, double t)
     338                 :            :     {
     339                 :       1090 :         B2DTuple aNew(rTup);
     340                 :       1090 :         aNew /= t;
     341                 :       1090 :         return aNew;
     342                 :            :     }
     343                 :            : 
     344                 :            :     inline B2DTuple operator/(double t, const B2DTuple& rTup)
     345                 :            :     {
     346                 :            :         B2DTuple aNew(t, t);
     347                 :            :         B2DTuple aTmp(rTup);
     348                 :            :         aNew /= aTmp;
     349                 :            :         return aNew;
     350                 :            :     }
     351                 :            : 
     352                 :            :     /** Round double to nearest integer for 2D tuple
     353                 :            : 
     354                 :            :         @return the nearest integer for this tuple
     355                 :            :     */
     356                 :            :     BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
     357                 :            : } // end of namespace basegfx
     358                 :            : 
     359                 :            : #endif /* _BGFX_TUPLE_B2DTUPLE_HXX */
     360                 :            : 
     361                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10