LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/basegfx/numeric - ftools.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 21 33 63.6 %
Date: 2013-07-09 Functions: 9 15 60.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_NUMERIC_FTOOLS_HXX
      21             : #define _BGFX_NUMERIC_FTOOLS_HXX
      22             : 
      23             : #include <rtl/math.hxx>
      24             : #include <basegfx/basegfxdllapi.h>
      25             : 
      26             : //////////////////////////////////////////////////////////////////////////////
      27             : // standard PI defines from solar.h, but we do not want to link against tools
      28             : 
      29             : #ifndef F_PI
      30             : #define F_PI        M_PI
      31             : #endif
      32             : #ifndef F_PI2
      33             : #define F_PI2       M_PI_2
      34             : #endif
      35             : #ifndef F_PI4
      36             : #define F_PI4       M_PI_4
      37             : #endif
      38             : #ifndef F_PI180
      39             : #define F_PI180     (M_PI/180.0)
      40             : #endif
      41             : #ifndef F_PI1800
      42             : #define F_PI1800    (M_PI/1800.0)
      43             : #endif
      44             : #ifndef F_PI18000
      45             : #define F_PI18000   (M_PI/18000.0)
      46             : #endif
      47             : #ifndef F_2PI
      48             : #define F_2PI       (2.0*M_PI)
      49             : #endif
      50             : 
      51             : //////////////////////////////////////////////////////////////////////////////
      52             : // fTools defines
      53             : 
      54             : namespace basegfx
      55             : {
      56             :     /** Round double to nearest integer
      57             : 
      58             :         @return the nearest integer
      59             :     */
      60    42891380 :     inline sal_Int32 fround( double fVal )
      61             :     {
      62    42891380 :         return fVal > 0.0 ? static_cast<sal_Int32>( fVal + .5 ) : -static_cast<sal_Int32>( -fVal + .5 );
      63             :     }
      64             : 
      65             :     /** Round double to nearest integer
      66             : 
      67             :         @return the nearest 64 bit integer
      68             :     */
      69           0 :     inline sal_Int64 fround64( double fVal )
      70             :     {
      71           0 :         return fVal > 0.0 ? static_cast<sal_Int64>( fVal + .5 ) : -static_cast<sal_Int64>( -fVal + .5 );
      72             :     }
      73             : 
      74             :     /** Prune a small epsilon range around zero.
      75             : 
      76             :         Use this method e.g. for calculating scale values. There, it
      77             :         is usually advisable not to set a scaling to 0.0, because that
      78             :         yields singular transformation matrices.
      79             : 
      80             :         @param fVal
      81             :         An arbitrary, but finite and valid number
      82             : 
      83             :         @return either fVal, or a small value slightly above (when
      84             :         fVal>0) or below (when fVal<0) zero.
      85             :      */
      86           0 :     inline double pruneScaleValue( double fVal )
      87             :     {
      88             :         // old version used ::std::min/max, but this collides if min is defined as preprocessor
      89             :         // macro which is the case e.g with windows.h headers. The simplest way to avoid this is to
      90             :         // just use the full comparison. I keep the original here, maybe there will be a better
      91             :         // solution some day.
      92             :         //
      93             :         //return fVal < 0.0 ?
      94             :         //    (::std::min(fVal,-0.00001)) :
      95             :         //    (::std::max(fVal,0.00001));
      96             : 
      97           0 :         if(fVal < 0.0)
      98           0 :             return (fVal < -0.00001 ? fVal : -0.00001);
      99             :         else
     100           0 :             return (fVal > 0.00001 ? fVal : 0.00001);
     101             :     }
     102             : 
     103             :     /** clamp given value against given minimum and maximum values
     104             :     */
     105     2669610 :     template <class T> inline const T& clamp(const T& value, const T& minimum, const T& maximum)
     106             :     {
     107     2669610 :         if(value < minimum)
     108             :         {
     109           1 :             return minimum;
     110             :         }
     111     2669609 :         else if(value > maximum)
     112             :         {
     113         154 :             return maximum;
     114             :         }
     115             :         else
     116             :         {
     117     2669455 :             return value;
     118             :         }
     119             :     }
     120             : 
     121             :     /** Convert value from degrees to radians
     122             :      */
     123           0 :     inline double deg2rad( double v )
     124             :     {
     125             :         // divide first, to get exact values for v being a multiple of
     126             :         // 90 degrees
     127           0 :         return v / 90.0 * M_PI_2;
     128             :     }
     129             : 
     130             :     /** Convert value radians to degrees
     131             :      */
     132             :     inline double rad2deg( double v )
     133             :     {
     134             :         // divide first, to get exact values for v being a multiple of
     135             :         // pi/2
     136             :         return v / M_PI_2 * 90.0;
     137             :     }
     138             : 
     139             :     /** Snap v to nearest multiple of fStep, from negative and
     140             :         positive side.
     141             : 
     142             :         Examples:
     143             : 
     144             :         snapToNearestMultiple(-0.1, 0.5) = 0.0
     145             :         snapToNearestMultiple(0.1, 0.5) = 0.0
     146             :         snapToNearestMultiple(0.25, 0.5) = 0.0
     147             :         snapToNearestMultiple(0.26, 0.5) = 0.5
     148             :      */
     149             :     double snapToNearestMultiple(double v, const double fStep);
     150             : 
     151             :     /** Snap v to the range [0.0 .. fWidth] using modulo
     152             :      */
     153             :     double snapToZeroRange(double v, double fWidth);
     154             : 
     155             :     /** Snap v to the range [fLow .. fHigh] using modulo
     156             :      */
     157             :     double snapToRange(double v, double fLow, double fHigh);
     158             : 
     159             :     /** return fValue with the sign of fSignCarrier, thus evtl. changed
     160             :     */
     161             :     inline double copySign(double fValue, double fSignCarrier)
     162             :     {
     163             : #ifdef WNT
     164             :         return _copysign(fValue, fSignCarrier);
     165             : #else
     166             :         return copysign(fValue, fSignCarrier);
     167             : #endif
     168             :     }
     169             : 
     170             :     class BASEGFX_DLLPUBLIC fTools
     171             :     {
     172             :         /// Threshold value for equalZero()
     173             :         static double                                   mfSmallValue;
     174             : 
     175             :     public:
     176             :         /// Get threshold value for equalZero and friends
     177    11983200 :         static double getSmallValue() { return mfSmallValue; }
     178             :         /// Set threshold value for equalZero and friends
     179             :         static void setSmallValue(const double& rfNew) { mfSmallValue = rfNew; }
     180             : 
     181             :         /// Compare against small value
     182    11983200 :         static bool equalZero(const double& rfVal)
     183             :         {
     184    11983200 :             return (fabs(rfVal) <= getSmallValue());
     185             :         }
     186             : 
     187             :         /// Compare against given small value
     188           0 :         static bool equalZero(const double& rfVal, const double& rfSmallValue)
     189             :         {
     190           0 :             return (fabs(rfVal) <= rfSmallValue);
     191             :         }
     192             : 
     193    65690194 :         static bool equal(const double& rfValA, const double& rfValB)
     194             :         {
     195             :             // changed to approxEqual usage for better numerical correctness
     196    65690194 :             return rtl::math::approxEqual(rfValA, rfValB);
     197             :         }
     198             : 
     199           0 :         static bool equal(const double& rfValA, const double& rfValB, const double& rfSmallValue)
     200             :         {
     201           0 :             return (fabs(rfValA - rfValB) <= rfSmallValue);
     202             :         }
     203             : 
     204      804394 :         static bool less(const double& rfValA, const double& rfValB)
     205             :         {
     206      804394 :             return (rfValA < rfValB && !equal(rfValA, rfValB));
     207             :         }
     208             : 
     209      261896 :         static bool lessOrEqual(const double& rfValA, const double& rfValB)
     210             :         {
     211      261896 :             return (rfValA < rfValB || equal(rfValA, rfValB));
     212             :         }
     213             : 
     214     9521766 :         static bool more(const double& rfValA, const double& rfValB)
     215             :         {
     216     9521766 :             return (rfValA > rfValB && !equal(rfValA, rfValB));
     217             :         }
     218             : 
     219      624187 :         static bool moreOrEqual(const double& rfValA, const double& rfValB)
     220             :         {
     221      624187 :             return (rfValA > rfValB || equal(rfValA, rfValB));
     222             :         }
     223             :     };
     224             : } // end of namespace basegfx
     225             : 
     226             : #endif /* _BGFX_NUMERIC_FTOOLS_HXX */
     227             : 
     228             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10