LCOV - code coverage report
Current view: top level - libreoffice/basegfx/inc/basegfx/range - b2drange.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 62 65 95.4 %
Date: 2012-12-27 Functions: 21 22 95.5 %
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_RANGE_B2DRANGE_HXX
      21             : #define _BGFX_RANGE_B2DRANGE_HXX
      22             : 
      23             : #include <basegfx/vector/b2dvector.hxx>
      24             : #include <basegfx/point/b2dpoint.hxx>
      25             : #include <basegfx/tuple/b2dtuple.hxx>
      26             : #include <basegfx/range/basicrange.hxx>
      27             : #include <vector>
      28             : #include <basegfx/basegfxdllapi.h>
      29             : 
      30             : 
      31             : namespace basegfx
      32             : {
      33             :     // predeclarations
      34             :     class B2IRange;
      35             :     class B2DHomMatrix;
      36             : 
      37             :     /** A two-dimensional interval over doubles
      38             : 
      39             :         This is a set of real numbers, bounded by a lower and an upper
      40             :         pair. All inbetween values are included in the set (see also
      41             :         http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
      42             : 
      43             :         The set is closed, i.e. the upper and the lower bound are
      44             :         included (if you're used to the notation - we're talking about
      45             :         [a,b] here, compared to half-open [a,b) or open intervals
      46             :         (a,b)).
      47             : 
      48             :         That means, isInside(val) will return true also for values of
      49             :         val=a or val=b.
      50             : 
      51             :         @see B1DRange
      52             :      */
      53             :     class B2DRange
      54             :     {
      55             :     public:
      56             :         typedef double          ValueType;
      57             :         typedef DoubleTraits    TraitsType;
      58             : 
      59      165024 :         B2DRange() {}
      60             : 
      61             :         /// Create degenerate interval consisting of a single point
      62             :         explicit B2DRange(const B2DTuple& rTuple)
      63             :         :   maRangeX(rTuple.getX()),
      64             :             maRangeY(rTuple.getY())
      65             :         {
      66             :         }
      67             : 
      68             :         /// Create proper interval between the two given double pairs
      69         210 :         B2DRange(double x1,
      70             :                  double y1,
      71             :                  double x2,
      72             :                  double y2)
      73             :         :   maRangeX(x1),
      74         210 :             maRangeY(y1)
      75             :         {
      76         210 :             maRangeX.expand(x2);
      77         210 :             maRangeY.expand(y2);
      78         210 :         }
      79             : 
      80             :         /// Create proper interval between the two given points
      81       22986 :         B2DRange(const B2DTuple& rTuple1,
      82             :                  const B2DTuple& rTuple2)
      83             :         :   maRangeX(rTuple1.getX()),
      84       22986 :             maRangeY(rTuple1.getY())
      85             :         {
      86       22986 :             expand( rTuple2 );
      87       22986 :         }
      88             : 
      89             :         BASEGFX_DLLPUBLIC explicit B2DRange(const B2IRange& rRange);
      90             : 
      91             :         /** Check if the interval set is empty
      92             : 
      93             :             @return false, if no value is in this set - having a
      94             :             single point included will already return true.
      95             :          */
      96        2290 :         bool isEmpty() const
      97             :         {
      98             :             return (
      99        2290 :                 maRangeX.isEmpty()
     100        2290 :                 || maRangeY.isEmpty()
     101        4580 :                 );
     102             :         }
     103             : 
     104             :         /// reset the object to empty state again, clearing all values
     105        2251 :         void reset()
     106             :         {
     107        2251 :             maRangeX.reset();
     108        2251 :             maRangeY.reset();
     109        2251 :         }
     110             : 
     111           0 :         bool operator==( const B2DRange& rRange ) const
     112             :         {
     113           0 :             return (maRangeX == rRange.maRangeX
     114           0 :                 && maRangeY == rRange.maRangeY);
     115             :         }
     116             : 
     117             :         bool operator!=( const B2DRange& rRange ) const
     118             :         {
     119             :             return (maRangeX != rRange.maRangeX
     120             :                 || maRangeY != rRange.maRangeY);
     121             :         }
     122             : 
     123             :         bool equal(const B2DRange& rRange) const
     124             :         {
     125             :             return (maRangeX.equal(rRange.maRangeX)
     126             :                     && maRangeY.equal(rRange.maRangeY));
     127             :         }
     128             : 
     129             :         /// get lower bound of the set. returns arbitrary values for empty sets.
     130       68661 :         double getMinX() const
     131             :         {
     132       68661 :             return maRangeX.getMinimum();
     133             :         }
     134             : 
     135             :         /// get lower bound of the set. returns arbitrary values for empty sets.
     136       68895 :         double getMinY() const
     137             :         {
     138       68895 :             return maRangeY.getMinimum();
     139             :         }
     140             : 
     141             :         /// get upper bound of the set. returns arbitrary values for empty sets.
     142       68433 :         double getMaxX() const
     143             :         {
     144       68433 :             return maRangeX.getMaximum();
     145             :         }
     146             : 
     147             :         /// get upper bound of the set. returns arbitrary values for empty sets.
     148       68809 :         double getMaxY() const
     149             :         {
     150       68809 :             return maRangeY.getMaximum();
     151             :         }
     152             : 
     153             :         /// return difference between upper and lower X value. returns 0 for empty sets.
     154          26 :         double getWidth() const
     155             :         {
     156          26 :             return maRangeX.getRange();
     157             :         }
     158             : 
     159             :         /// return difference between upper and lower Y value. returns 0 for empty sets.
     160         234 :         double getHeight() const
     161             :         {
     162         234 :             return maRangeY.getRange();
     163             :         }
     164             : 
     165             :         /// get lower bound of the set. returns arbitrary values for empty sets.
     166         211 :         B2DPoint getMinimum() const
     167             :         {
     168             :             return B2DPoint(
     169             :                 maRangeX.getMinimum(),
     170             :                 maRangeY.getMinimum()
     171         211 :                 );
     172             :         }
     173             : 
     174             :         /// get upper bound of the set. returns arbitrary values for empty sets.
     175         211 :         B2DPoint getMaximum() const
     176             :         {
     177             :             return B2DPoint(
     178             :                 maRangeX.getMaximum(),
     179             :                 maRangeY.getMaximum()
     180         211 :                 );
     181             :         }
     182             : 
     183             :         /// return difference between upper and lower point. returns (0,0) for empty sets.
     184           2 :         B2DVector getRange() const
     185             :         {
     186             :             return B2DVector(
     187             :                 maRangeX.getRange(),
     188             :                 maRangeY.getRange()
     189           2 :                 );
     190             :         }
     191             : 
     192             :         /// return center point of set. returns (0,0) for empty sets.
     193         146 :         B2DPoint getCenter() const
     194             :         {
     195             :             return B2DPoint(
     196             :                 maRangeX.getCenter(),
     197             :                 maRangeY.getCenter()
     198         146 :                 );
     199             :         }
     200             : 
     201             :         /// return center X value of set. returns 0 for empty sets.
     202             :         double getCenterX() const
     203             :         {
     204             :             return maRangeX.getCenter();
     205             :         }
     206             : 
     207             :         /// return center Y value of set. returns 0 for empty sets.
     208             :         double getCenterY() const
     209             :         {
     210             :             return maRangeY.getCenter();
     211             :         }
     212             : 
     213             :         /// yields true if given point is contained in set
     214       22749 :         bool isInside(const B2DTuple& rTuple) const
     215             :         {
     216             :             return (
     217       22749 :                 maRangeX.isInside(rTuple.getX())
     218        9621 :                 && maRangeY.isInside(rTuple.getY())
     219       32370 :                 );
     220             :         }
     221             : 
     222             :         /// yields true if rRange is inside, or equal to set
     223         836 :         bool isInside(const B2DRange& rRange) const
     224             :         {
     225             :             return (
     226         836 :                 maRangeX.isInside(rRange.maRangeX)
     227         543 :                 && maRangeY.isInside(rRange.maRangeY)
     228        1379 :                 );
     229             :         }
     230             : 
     231             :         /// yields true if rRange at least partly inside set
     232       10061 :         bool overlaps(const B2DRange& rRange) const
     233             :         {
     234             :             return (
     235       10061 :                 maRangeX.overlaps(rRange.maRangeX)
     236        3232 :                 && maRangeY.overlaps(rRange.maRangeY)
     237       13293 :                 );
     238             :         }
     239             : 
     240             :         /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
     241        5937 :         bool overlapsMore(const B2DRange& rRange) const
     242             :         {
     243             :             return (
     244        5937 :                 maRangeX.overlapsMore(rRange.maRangeX)
     245        2054 :                 && maRangeY.overlapsMore(rRange.maRangeY)
     246        7991 :                 );
     247             :         }
     248             : 
     249             :         /// add point to the set, expanding as necessary
     250      369547 :         void expand(const B2DTuple& rTuple)
     251             :         {
     252      369547 :             maRangeX.expand(rTuple.getX());
     253      369547 :             maRangeY.expand(rTuple.getY());
     254      369547 :         }
     255             : 
     256             :         /// add rRange to the set, expanding as necessary
     257       82377 :         void expand(const B2DRange& rRange)
     258             :         {
     259       82377 :             maRangeX.expand(rRange.maRangeX);
     260       82377 :             maRangeY.expand(rRange.maRangeY);
     261       82377 :         }
     262             : 
     263             :         /// calc set intersection
     264             :         void intersect(const B2DRange& rRange)
     265             :         {
     266             :             maRangeX.intersect(rRange.maRangeX);
     267             :             maRangeY.intersect(rRange.maRangeY);
     268             :         }
     269             : 
     270             :         /// grow set by fValue on all sides
     271             :         void grow(double fValue)
     272             :         {
     273             :             maRangeX.grow(fValue);
     274             :             maRangeY.grow(fValue);
     275             :         }
     276             : 
     277             :         BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix& rMatrix);
     278             : 
     279             :     private:
     280             :         typedef ::basegfx::BasicRange< ValueType, TraitsType >  MyBasicRange;
     281             : 
     282             :         MyBasicRange        maRangeX;
     283             :         MyBasicRange        maRangeY;
     284             :     };
     285             : 
     286             :     /** Round double to nearest integer for 2D range
     287             : 
     288             :         @return the nearest integer for this range
     289             :     */
     290             :     BASEGFX_DLLPUBLIC B2IRange fround(const B2DRange& rRange);
     291             : 
     292             :     /** Compute the set difference of the two given ranges
     293             : 
     294             :         This method calculates the symmetric difference (aka XOR)
     295             :         between the two given ranges, and returning the resulting
     296             :         ranges. Thus, the result will contain all areas where one, but
     297             :         not both ranges lie.
     298             : 
     299             :         @param o_rResult
     300             :         Result vector. The up to four difference ranges are returned
     301             :         within this vector
     302             : 
     303             :         @param rFirst
     304             :         The first range
     305             : 
     306             :         @param rSecond
     307             :         The second range
     308             : 
     309             :         @return the input vector
     310             :      */
     311             :     BASEGFX_DLLPUBLIC ::std::vector< B2DRange >& computeSetDifference( ::std::vector< B2DRange >&   o_rResult,
     312             :                                                      const B2DRange&            rFirst,
     313             :                                                      const B2DRange&            rSecond );
     314             : 
     315             : } // end of namespace basegfx
     316             : 
     317             : 
     318             : #endif /* _BGFX_RANGE_B2DRANGE_HXX */
     319             : 
     320             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10