LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/basegfx/tuple - b2dtuple.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 88 113 77.9 %
Date: 2013-07-09 Functions: 25 32 78.1 %
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 <algorithm>
      26             : #include <basegfx/basegfxdllapi.h>
      27             : 
      28             : namespace basegfx
      29             : {
      30             :     // predeclarations
      31             :     class B2ITuple;
      32             : 
      33             :     /** Base class for all Points/Vectors with two double values
      34             : 
      35             :         This class provides all methods common to Point
      36             :         avd Vector classes which are derived from here.
      37             : 
      38             :         @derive Use this class to implement Points or Vectors
      39             :         which are based on two double values
      40             :     */
      41             :     class SAL_WARN_UNUSED B2DTuple
      42             :     {
      43             :     protected:
      44             :         double                                      mfX;
      45             :         double                                      mfY;
      46             : 
      47             :     public:
      48             :         /** Create a 2D Tuple
      49             : 
      50             :             The tuple is initialized to (0.0, 0.0)
      51             :         */
      52      911446 :         B2DTuple()
      53             :         :   mfX(0.0),
      54      911446 :             mfY(0.0)
      55      911446 :         {}
      56             : 
      57             :         /** Create a 2D Tuple
      58             : 
      59             :             @param fX
      60             :             This parameter is used to initialize the X-coordinate
      61             :             of the 2D Tuple.
      62             : 
      63             :             @param fY
      64             :             This parameter is used to initialize the Y-coordinate
      65             :             of the 2D Tuple.
      66             :         */
      67    29167098 :         B2DTuple(double fX, double fY)
      68             :         :   mfX( fX ),
      69    29167098 :             mfY( fY )
      70    29167098 :         {}
      71             : 
      72             :         /** Create a copy of a 2D Tuple
      73             : 
      74             :             @param rTup
      75             :             The 2D Tuple which will be copied.
      76             :         */
      77   184309191 :         B2DTuple(const B2DTuple& rTup)
      78             :         :   mfX( rTup.mfX ),
      79   184309191 :             mfY( rTup.mfY )
      80   184309191 :         {}
      81             : 
      82             :         /** Create a copy of a 2D integer Tuple
      83             : 
      84             :             @param rTup
      85             :             The 2D Tuple which will be copied.
      86             :         */
      87             :         BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
      88             : 
      89   214387654 :         ~B2DTuple()
      90   214387654 :         {}
      91             : 
      92             :         /// Get X-Coordinate of 2D Tuple
      93   165866491 :         double getX() const
      94             :         {
      95   165866491 :             return mfX;
      96             :         }
      97             : 
      98             :         /// Get Y-Coordinate of 2D Tuple
      99   167512370 :         double getY() const
     100             :         {
     101   167512370 :             return mfY;
     102             :         }
     103             : 
     104             :         /// Set X-Coordinate of 2D Tuple
     105      604455 :         void setX(double fX)
     106             :         {
     107      604455 :             mfX = fX;
     108      604455 :         }
     109             : 
     110             :         /// Set Y-Coordinate of 2D Tuple
     111      599562 :         void setY(double fY)
     112             :         {
     113      599562 :             mfY = fY;
     114      599562 :         }
     115             : 
     116             :         /// Array-access to 2D Tuple
     117             :         const double& operator[] (int nPos) const
     118             :         {
     119             :             // Here, normally one if(...) should be used. In the assumption that
     120             :             // both double members can be accessed as an array a shortcut is used here.
     121             :             // if(0 == nPos) return mfX; return mfY;
     122             :             return *((&mfX) + nPos);
     123             :         }
     124             : 
     125             :         /// Array-access to 2D Tuple
     126           0 :         double& operator[] (int nPos)
     127             :         {
     128             :             // Here, normally one if(...) should be used. In the assumption that
     129             :             // both double members can be accessed as an array a shortcut is used here.
     130             :             // if(0 == nPos) return mfX; return mfY;
     131           0 :             return *((&mfX) + nPos);
     132             :         }
     133             : 
     134             :         // comparators with tolerance
     135             :         //////////////////////////////////////////////////////////////////////
     136             : 
     137     2829241 :         bool equalZero() const
     138             :         {
     139     6140316 :             return (this == &getEmptyTuple() ||
     140     6725757 :                     (fTools::equalZero(mfX) && fTools::equalZero(mfY)));
     141             :         }
     142             : 
     143             :         bool equalZero(const double& rfSmallValue) const
     144             :         {
     145             :             return (this == &getEmptyTuple() ||
     146             :                     (fTools::equalZero(mfX, rfSmallValue) && fTools::equalZero(mfY, rfSmallValue)));
     147             :         }
     148             : 
     149    42103653 :         bool equal(const B2DTuple& rTup) const
     150             :         {
     151             :             return (
     152    86500533 :                 this == &rTup ||
     153    48412722 :                 (fTools::equal(mfX, rTup.mfX) &&
     154    48413740 :                 fTools::equal(mfY, rTup.mfY)));
     155             :         }
     156             : 
     157           0 :         bool equal(const B2DTuple& rTup, const double& rfSmallValue) const
     158             :         {
     159             :             return (
     160           0 :                 this == &rTup ||
     161           0 :                 (fTools::equal(mfX, rTup.mfX, rfSmallValue) &&
     162           0 :                 fTools::equal(mfY, rTup.mfY, rfSmallValue)));
     163             :         }
     164             : 
     165             :         // operators
     166             :         //////////////////////////////////////////////////////////////////////
     167             : 
     168     2411530 :         B2DTuple& operator+=( const B2DTuple& rTup )
     169             :         {
     170     2411530 :             mfX += rTup.mfX;
     171     2411530 :             mfY += rTup.mfY;
     172     2411530 :             return *this;
     173             :         }
     174             : 
     175    11215629 :         B2DTuple& operator-=( const B2DTuple& rTup )
     176             :         {
     177    11215629 :             mfX -= rTup.mfX;
     178    11215629 :             mfY -= rTup.mfY;
     179    11215629 :             return *this;
     180             :         }
     181             : 
     182           0 :         B2DTuple& operator/=( const B2DTuple& rTup )
     183             :         {
     184           0 :             mfX /= rTup.mfX;
     185           0 :             mfY /= rTup.mfY;
     186           0 :             return *this;
     187             :         }
     188             : 
     189           0 :         B2DTuple& operator*=( const B2DTuple& rTup )
     190             :         {
     191           0 :             mfX *= rTup.mfX;
     192           0 :             mfY *= rTup.mfY;
     193           0 :             return *this;
     194             :         }
     195             : 
     196      274642 :         B2DTuple& operator*=(double t)
     197             :         {
     198      274642 :             mfX *= t;
     199      274642 :             mfY *= t;
     200      274642 :             return *this;
     201             :         }
     202             : 
     203       28559 :         B2DTuple& operator/=(double t)
     204             :         {
     205       28559 :             const double fVal(1.0 / t);
     206       28559 :             mfX *= fVal;
     207       28559 :             mfY *= fVal;
     208       28559 :             return *this;
     209             :         }
     210             : 
     211           0 :         B2DTuple operator-(void) const
     212             :         {
     213           0 :             return B2DTuple(-mfX, -mfY);
     214             :         }
     215             : 
     216    15829485 :         bool operator==( const B2DTuple& rTup ) const
     217             :         {
     218    15829485 :             return equal(rTup);
     219             :         }
     220             : 
     221    11750933 :         bool operator!=( const B2DTuple& rTup ) const
     222             :         {
     223    11750933 :             return !equal(rTup);
     224             :         }
     225             : 
     226    39035564 :         B2DTuple& operator=( const B2DTuple& rTup )
     227             :         {
     228    39035564 :             mfX = rTup.mfX;
     229    39035564 :             mfY = rTup.mfY;
     230    39035564 :             return *this;
     231             :         }
     232             : 
     233             :         BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
     234             :     };
     235             : 
     236             :     // external operators
     237             :     //////////////////////////////////////////////////////////////////////////
     238             : 
     239             :     inline B2DTuple minimum(const B2DTuple& rTupA, const B2DTuple& rTupB)
     240             :     {
     241             :         return B2DTuple(
     242             :             std::min(rTupB.getX(), rTupA.getX()),
     243             :             std::min(rTupB.getY(), rTupA.getY()));
     244             :     }
     245             : 
     246             :     inline B2DTuple maximum(const B2DTuple& rTupA, const B2DTuple& rTupB)
     247             :     {
     248             :         return B2DTuple(
     249             :             std::max(rTupB.getX(), rTupA.getX()),
     250             :             std::max(rTupB.getY(), rTupA.getY()));
     251             :     }
     252             : 
     253        7008 :     inline B2DTuple absolute(const B2DTuple& rTup)
     254             :     {
     255             :         B2DTuple aAbs(
     256        7008 :             fabs(rTup.getX()),
     257       14016 :             fabs(rTup.getY()));
     258        7008 :         return aAbs;
     259             :     }
     260             : 
     261    15552216 :     inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
     262             :     {
     263    15552216 :         if(rOld1 == rOld2)
     264             :         {
     265      248954 :             return rOld1;
     266             :         }
     267    15303262 :         else if(0.0 >= t)
     268             :         {
     269        2596 :             return rOld1;
     270             :         }
     271    15300666 :         else if(1.0 <= t)
     272             :         {
     273           0 :             return rOld2;
     274             :         }
     275             :         else
     276             :         {
     277             :             return B2DTuple(
     278    15300666 :                 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
     279    30601332 :                 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
     280             :         }
     281             :     }
     282             : 
     283        1800 :     inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
     284             :     {
     285             :         return B2DTuple(
     286        3520 :             rOld1.getX() == rOld2.getX() ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
     287        5320 :             rOld1.getY() == rOld2.getY() ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
     288             :     }
     289             : 
     290             :     inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2, const B2DTuple& rOld3)
     291             :     {
     292             :         return B2DTuple(
     293             :             (rOld1.getX() == rOld2.getX() && rOld2.getX() == rOld3.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
     294             :             (rOld1.getY() == rOld2.getY() && rOld2.getY() == rOld3.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
     295             :     }
     296             : 
     297     2335933 :     inline B2DTuple operator+(const B2DTuple& rTupA, const B2DTuple& rTupB)
     298             :     {
     299     2335933 :         B2DTuple aSum(rTupA);
     300     2335933 :         aSum += rTupB;
     301     2335933 :         return aSum;
     302             :     }
     303             : 
     304    11215627 :     inline B2DTuple operator-(const B2DTuple& rTupA, const B2DTuple& rTupB)
     305             :     {
     306    11215627 :         B2DTuple aSub(rTupA);
     307    11215627 :         aSub -= rTupB;
     308    11215627 :         return aSub;
     309             :     }
     310             : 
     311           0 :     inline B2DTuple operator/(const B2DTuple& rTupA, const B2DTuple& rTupB)
     312             :     {
     313           0 :         B2DTuple aDiv(rTupA);
     314           0 :         aDiv /= rTupB;
     315           0 :         return aDiv;
     316             :     }
     317             : 
     318           0 :     inline B2DTuple operator*(const B2DTuple& rTupA, const B2DTuple& rTupB)
     319             :     {
     320           0 :         B2DTuple aMul(rTupA);
     321           0 :         aMul *= rTupB;
     322           0 :         return aMul;
     323             :     }
     324             : 
     325      228083 :     inline B2DTuple operator*(const B2DTuple& rTup, double t)
     326             :     {
     327      228083 :         B2DTuple aNew(rTup);
     328      228083 :         aNew *= t;
     329      228083 :         return aNew;
     330             :     }
     331             : 
     332       46555 :     inline B2DTuple operator*(double t, const B2DTuple& rTup)
     333             :     {
     334       46555 :         B2DTuple aNew(rTup);
     335       46555 :         aNew *= t;
     336       46555 :         return aNew;
     337             :     }
     338             : 
     339       28558 :     inline B2DTuple operator/(const B2DTuple& rTup, double t)
     340             :     {
     341       28558 :         B2DTuple aNew(rTup);
     342       28558 :         aNew /= t;
     343       28558 :         return aNew;
     344             :     }
     345             : 
     346             :     inline B2DTuple operator/(double t, const B2DTuple& rTup)
     347             :     {
     348             :         B2DTuple aNew(t, t);
     349             :         B2DTuple aTmp(rTup);
     350             :         aNew /= aTmp;
     351             :         return aNew;
     352             :     }
     353             : 
     354             :     /** Round double to nearest integer for 2D tuple
     355             : 
     356             :         @return the nearest integer for this tuple
     357             :     */
     358             :     BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
     359             : } // end of namespace basegfx
     360             : 
     361             : #endif /* _BGFX_TUPLE_B2DTUPLE_HXX */
     362             : 
     363             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10