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

Generated by: LCOV version 1.10