LCOV - code coverage report
Current view: top level - include/basegfx/range - b2drange.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 82 86 95.3 %
Date: 2015-06-13 12:38:46 Functions: 27 29 93.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 INCLUDED_BASEGFX_RANGE_B2DRANGE_HXX
      21             : #define INCLUDED_BASEGFX_RANGE_B2DRANGE_HXX
      22             : 
      23             : #include <ostream>
      24             : #include <vector>
      25             : 
      26             : #include <basegfx/vector/b2dvector.hxx>
      27             : #include <basegfx/point/b2dpoint.hxx>
      28             : #include <basegfx/tuple/b2dtuple.hxx>
      29             : #include <basegfx/range/basicrange.hxx>
      30             : #include <basegfx/basegfxdllapi.h>
      31             : 
      32             : namespace basegfx
      33             : {
      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    13007236 :         B2DRange() {}
      60             : 
      61             :         /// Create degenerate interval consisting of a single point
      62       17987 :         explicit B2DRange(const B2DTuple& rTuple)
      63             :         :   maRangeX(rTuple.getX()),
      64       17987 :             maRangeY(rTuple.getY())
      65             :         {
      66       17987 :         }
      67             : 
      68             :         /// Create proper interval between the two given double pairs
      69     2895027 :         B2DRange(double x1,
      70             :                  double y1,
      71             :                  double x2,
      72             :                  double y2)
      73             :         :   maRangeX(x1),
      74     2895027 :             maRangeY(y1)
      75             :         {
      76     2895027 :             maRangeX.expand(x2);
      77     2895027 :             maRangeY.expand(y2);
      78     2895027 :         }
      79             : 
      80             :         /// Create proper interval between the two given points
      81    21350205 :         B2DRange(const B2DTuple& rTuple1,
      82             :                  const B2DTuple& rTuple2)
      83             :         :   maRangeX(rTuple1.getX()),
      84    21350205 :             maRangeY(rTuple1.getY())
      85             :         {
      86    21350205 :             expand( rTuple2 );
      87    21350205 :         }
      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     1076750 :         bool isEmpty() const
      97             :         {
      98             :             return (
      99     1076750 :                 maRangeX.isEmpty()
     100     1076750 :                 || maRangeY.isEmpty()
     101     1076750 :                 );
     102             :         }
     103             : 
     104             :         /// reset the object to empty state again, clearing all values
     105      248482 :         void reset()
     106             :         {
     107      248482 :             maRangeX.reset();
     108      248482 :             maRangeY.reset();
     109      248482 :         }
     110             : 
     111        4482 :         bool operator==( const B2DRange& rRange ) const
     112             :         {
     113        4482 :             return (maRangeX == rRange.maRangeX
     114        4482 :                 && maRangeY == rRange.maRangeY);
     115             :         }
     116             : 
     117        4584 :         bool operator!=( const B2DRange& rRange ) const
     118             :         {
     119        4584 :             return (maRangeX != rRange.maRangeX
     120        4584 :                 || maRangeY != rRange.maRangeY);
     121             :         }
     122             : 
     123        9785 :         bool equal(const B2DRange& rRange) const
     124             :         {
     125        9785 :             return (maRangeX.equal(rRange.maRangeX)
     126        9785 :                     && maRangeY.equal(rRange.maRangeY));
     127             :         }
     128             : 
     129             :         /// get lower bound of the set. returns arbitrary values for empty sets.
     130     6078467 :         double getMinX() const
     131             :         {
     132     6078467 :             return maRangeX.getMinimum();
     133             :         }
     134             : 
     135             :         /// get lower bound of the set. returns arbitrary values for empty sets.
     136     9646508 :         double getMinY() const
     137             :         {
     138     9646508 :             return maRangeY.getMinimum();
     139             :         }
     140             : 
     141             :         /// get upper bound of the set. returns arbitrary values for empty sets.
     142     5936654 :         double getMaxX() const
     143             :         {
     144     5936654 :             return maRangeX.getMaximum();
     145             :         }
     146             : 
     147             :         /// get upper bound of the set. returns arbitrary values for empty sets.
     148     9529253 :         double getMaxY() const
     149             :         {
     150     9529253 :             return maRangeY.getMaximum();
     151             :         }
     152             : 
     153             :         /// return difference between upper and lower X value. returns 0 for empty sets.
     154      506114 :         double getWidth() const
     155             :         {
     156      506114 :             return maRangeX.getRange();
     157             :         }
     158             : 
     159             :         /// return difference between upper and lower Y value. returns 0 for empty sets.
     160      490927 :         double getHeight() const
     161             :         {
     162      490927 :             return maRangeY.getRange();
     163             :         }
     164             : 
     165             :         /// get lower bound of the set. returns arbitrary values for empty sets.
     166        2548 :         B2DPoint getMinimum() const
     167             :         {
     168             :             return B2DPoint(
     169             :                 maRangeX.getMinimum(),
     170             :                 maRangeY.getMinimum()
     171        2548 :                 );
     172             :         }
     173             : 
     174             :         /// get upper bound of the set. returns arbitrary values for empty sets.
     175         926 :         B2DPoint getMaximum() const
     176             :         {
     177             :             return B2DPoint(
     178             :                 maRangeX.getMaximum(),
     179             :                 maRangeY.getMaximum()
     180         926 :                 );
     181             :         }
     182             : 
     183             :         /// return difference between upper and lower point. returns (0,0) for empty sets.
     184        1824 :         B2DVector getRange() const
     185             :         {
     186             :             return B2DVector(
     187             :                 maRangeX.getRange(),
     188             :                 maRangeY.getRange()
     189        1824 :                 );
     190             :         }
     191             : 
     192             :         /// return center point of set. returns (0,0) for empty sets.
     193       29243 :         B2DPoint getCenter() const
     194             :         {
     195             :             return B2DPoint(
     196             :                 maRangeX.getCenter(),
     197             :                 maRangeY.getCenter()
     198       29243 :                 );
     199             :         }
     200             : 
     201             :         /// return center X value of set. returns 0 for empty sets.
     202           0 :         double getCenterX() const
     203             :         {
     204           0 :             return maRangeX.getCenter();
     205             :         }
     206             : 
     207             :         /// return center Y value of set. returns 0 for empty sets.
     208           0 :         double getCenterY() const
     209             :         {
     210           0 :             return maRangeY.getCenter();
     211             :         }
     212             : 
     213             :         /// yields true if given point is contained in set
     214    34445881 :         bool isInside(const B2DTuple& rTuple) const
     215             :         {
     216             :             return (
     217    34445881 :                 maRangeX.isInside(rTuple.getX())
     218    34445881 :                 && maRangeY.isInside(rTuple.getY())
     219    34445881 :                 );
     220             :         }
     221             : 
     222             :         /// yields true if rRange is inside, or equal to set
     223      181544 :         bool isInside(const B2DRange& rRange) const
     224             :         {
     225             :             return (
     226      181544 :                 maRangeX.isInside(rRange.maRangeX)
     227      181544 :                 && maRangeY.isInside(rRange.maRangeY)
     228      181544 :                 );
     229             :         }
     230             : 
     231             :         /// yields true if rRange at least partly inside set
     232    18420857 :         bool overlaps(const B2DRange& rRange) const
     233             :         {
     234             :             return (
     235    18420857 :                 maRangeX.overlaps(rRange.maRangeX)
     236    18420857 :                 && maRangeY.overlaps(rRange.maRangeY)
     237    18420857 :                 );
     238             :         }
     239             : 
     240             :         /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
     241      419488 :         bool overlapsMore(const B2DRange& rRange) const
     242             :         {
     243             :             return (
     244      419488 :                 maRangeX.overlapsMore(rRange.maRangeX)
     245      419488 :                 && maRangeY.overlapsMore(rRange.maRangeY)
     246      419488 :                 );
     247             :         }
     248             : 
     249             :         /// add point to the set, expanding as necessary
     250    43744186 :         void expand(const B2DTuple& rTuple)
     251             :         {
     252    43744186 :             maRangeX.expand(rTuple.getX());
     253    43744186 :             maRangeY.expand(rTuple.getY());
     254    43744186 :         }
     255             : 
     256             :         /// add rRange to the set, expanding as necessary
     257     8974855 :         void expand(const B2DRange& rRange)
     258             :         {
     259     8974855 :             maRangeX.expand(rRange.maRangeX);
     260     8974855 :             maRangeY.expand(rRange.maRangeY);
     261     8974855 :         }
     262             : 
     263             :         /// calc set intersection
     264       49852 :         void intersect(const B2DRange& rRange)
     265             :         {
     266       49852 :             maRangeX.intersect(rRange.maRangeX);
     267       49852 :             maRangeY.intersect(rRange.maRangeY);
     268       49852 :         }
     269             : 
     270             :         /// grow set by fValue on all sides
     271      357162 :         void grow(double fValue)
     272             :         {
     273      357162 :             maRangeX.grow(fValue);
     274      357162 :             maRangeY.grow(fValue);
     275      357162 :         }
     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             : template< typename charT, typename traits >
     319             : inline std::basic_ostream<charT, traits> & operator <<(
     320             :     std::basic_ostream<charT, traits> & stream, const basegfx::B2DRange& range )
     321             : {
     322             :     return stream << range.getWidth() << "x" << range.getHeight() << "@" << range.getMinimum();
     323             : }
     324             : 
     325             : #endif // INCLUDED_BASEGFX_RANGE_B2DRANGE_HXX
     326             : 
     327             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11