LCOV - code coverage report
Current view: top level - basegfx/inc/basegfx/range - b2irange.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 8 28 28.6 %
Date: 2012-08-25 Functions: 2 11 18.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 4 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #ifndef _BGFX_RANGE_B2IRANGE_HXX
      30                 :            : #define _BGFX_RANGE_B2IRANGE_HXX
      31                 :            : 
      32                 :            : #include <basegfx/point/b2ipoint.hxx>
      33                 :            : #include <basegfx/point/b2dpoint.hxx>
      34                 :            : #include <basegfx/tuple/b2ituple.hxx>
      35                 :            : #include <basegfx/tuple/b2i64tuple.hxx>
      36                 :            : #include <basegfx/range/basicrange.hxx>
      37                 :            : #include <vector>
      38                 :            : #include <basegfx/basegfxdllapi.h>
      39                 :            : 
      40                 :            : 
      41                 :            : namespace basegfx
      42                 :            : {
      43                 :            :     /** A two-dimensional interval over integers
      44                 :            : 
      45                 :            :         This is a set of real numbers, bounded by a lower and an upper
      46                 :            :         pair. All inbetween values are included in the set (see also
      47                 :            :         http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
      48                 :            : 
      49                 :            :         Probably you rather want B2IBox for integers.
      50                 :            : 
      51                 :            :         The set is closed, i.e. the upper and the lower bound are
      52                 :            :         included (if you're used to the notation - we're talking about
      53                 :            :         [a,b] here, compared to half-open [a,b) or open intervals
      54                 :            :         (a,b)).
      55                 :            : 
      56                 :            :         That means, isInside(val) will return true also for values of
      57                 :            :         val=a or val=b.
      58                 :            : 
      59                 :            :         @see B2IBox
      60                 :            :      */
      61                 :            :     class B2IRange
      62                 :            :     {
      63                 :            :     public:
      64                 :            :         typedef sal_Int32       ValueType;
      65                 :            :         typedef Int32Traits     TraitsType;
      66                 :            : 
      67                 :          0 :         B2IRange() {}
      68                 :            : 
      69                 :            :         /// Create degenerate interval consisting of a single point
      70                 :            :         explicit B2IRange(const B2ITuple& rTuple)
      71                 :            :         :   maRangeX(rTuple.getX()),
      72                 :            :             maRangeY(rTuple.getY())
      73                 :            :         {
      74                 :            :         }
      75                 :            : 
      76                 :            :         /// Create proper interval between the two given integer pairs
      77                 :          0 :         B2IRange(sal_Int32 x1,
      78                 :            :                  sal_Int32 y1,
      79                 :            :                  sal_Int32 x2,
      80                 :            :                  sal_Int32 y2)
      81                 :            :         :   maRangeX(x1),
      82                 :          0 :             maRangeY(y1)
      83                 :            :         {
      84                 :          0 :             maRangeX.expand(x2);
      85                 :          0 :             maRangeY.expand(y2);
      86                 :          0 :         }
      87                 :            : 
      88                 :            :         /// Create proper interval between the two given points
      89                 :          5 :         B2IRange(const B2ITuple& rTuple1,
      90                 :            :                  const B2ITuple& rTuple2)
      91                 :            :         :   maRangeX(rTuple1.getX()),
      92                 :          5 :             maRangeY(rTuple1.getY())
      93                 :            :         {
      94                 :          5 :             expand( rTuple2 );
      95                 :          5 :         }
      96                 :            : 
      97                 :            :         /** Check if the interval set is empty
      98                 :            : 
      99                 :            :             @return false, if no value is in this set - having a
     100                 :            :             single point included will already return true.
     101                 :            :          */
     102                 :          0 :         bool isEmpty() const
     103                 :            :         {
     104 [ #  # ][ #  # ]:          0 :             return maRangeX.isEmpty() || maRangeY.isEmpty();
     105                 :            :         }
     106                 :            : 
     107                 :            :         /// reset the object to empty state again, clearing all values
     108                 :            :         void reset()
     109                 :            :         {
     110                 :            :             maRangeX.reset();
     111                 :            :             maRangeY.reset();
     112                 :            :         }
     113                 :            : 
     114                 :            :         bool operator==( const B2IRange& rRange ) const
     115                 :            :         {
     116                 :            :             return (maRangeX == rRange.maRangeX
     117                 :            :                 && maRangeY == rRange.maRangeY);
     118                 :            :         }
     119                 :            : 
     120                 :            :         bool operator!=( const B2IRange& rRange ) const
     121                 :            :         {
     122                 :            :             return (maRangeX != rRange.maRangeX
     123                 :            :                 || maRangeY != rRange.maRangeY);
     124                 :            :         }
     125                 :            : 
     126                 :            :         /// get lower bound of the set. returns arbitrary values for empty sets.
     127                 :          0 :         sal_Int32 getMinX() const
     128                 :            :         {
     129                 :          0 :             return maRangeX.getMinimum();
     130                 :            :         }
     131                 :            : 
     132                 :            :         /// get lower bound of the set. returns arbitrary values for empty sets.
     133                 :          0 :         sal_Int32 getMinY() const
     134                 :            :         {
     135                 :          0 :             return maRangeY.getMinimum();
     136                 :            :         }
     137                 :            : 
     138                 :            :         /// get upper bound of the set. returns arbitrary values for empty sets.
     139                 :          0 :         sal_Int32 getMaxX() const
     140                 :            :         {
     141                 :          0 :             return maRangeX.getMaximum();
     142                 :            :         }
     143                 :            : 
     144                 :            :         /// get upper bound of the set. returns arbitrary values for empty sets.
     145                 :          0 :         sal_Int32 getMaxY() const
     146                 :            :         {
     147                 :          0 :             return maRangeY.getMaximum();
     148                 :            :         }
     149                 :            : 
     150                 :            :         /// return difference between upper and lower X value. returns 0 for empty sets.
     151                 :          0 :         sal_Int64 getWidth() const
     152                 :            :         {
     153                 :          0 :             return maRangeX.getRange();
     154                 :            :         }
     155                 :            : 
     156                 :            :         /// return difference between upper and lower Y value. returns 0 for empty sets.
     157                 :          0 :         sal_Int64 getHeight() const
     158                 :            :         {
     159                 :          0 :             return maRangeY.getRange();
     160                 :            :         }
     161                 :            : 
     162                 :            :         /// get lower bound of the set. returns arbitrary values for empty sets.
     163                 :            :         B2IPoint getMinimum() const
     164                 :            :         {
     165                 :            :             return B2IPoint(
     166                 :            :                 maRangeX.getMinimum(),
     167                 :            :                 maRangeY.getMinimum()
     168                 :            :                 );
     169                 :            :         }
     170                 :            : 
     171                 :            :         /// get upper bound of the set. returns arbitrary values for empty sets.
     172                 :            :         B2IPoint getMaximum() const
     173                 :            :         {
     174                 :            :             return B2IPoint(
     175                 :            :                 maRangeX.getMaximum(),
     176                 :            :                 maRangeY.getMaximum()
     177                 :            :                 );
     178                 :            :         }
     179                 :            : 
     180                 :            :         /// return difference between upper and lower point. returns (0,0) for empty sets.
     181                 :            :         B2I64Tuple getRange() const
     182                 :            :         {
     183                 :            :             return B2I64Tuple(
     184                 :            :                 maRangeX.getRange(),
     185                 :            :                 maRangeY.getRange()
     186                 :            :                 );
     187                 :            :         }
     188                 :            : 
     189                 :            :         /// return center point of set. returns (0,0) for empty sets.
     190                 :            :         B2DPoint getCenter() const
     191                 :            :         {
     192                 :            :             return B2DPoint(
     193                 :            :                 maRangeX.getCenter(),
     194                 :            :                 maRangeY.getCenter()
     195                 :            :                 );
     196                 :            :         }
     197                 :            : 
     198                 :            :         /// yields true if given point is contained in set
     199                 :            :         bool isInside(const B2ITuple& rTuple) const
     200                 :            :         {
     201                 :            :             return (
     202                 :            :                 maRangeX.isInside(rTuple.getX())
     203                 :            :                 && maRangeY.isInside(rTuple.getY())
     204                 :            :                 );
     205                 :            :         }
     206                 :            : 
     207                 :            :         /// yields true if rRange is inside, or equal to set
     208                 :            :         bool isInside(const B2IRange& rRange) const
     209                 :            :         {
     210                 :            :             return (
     211                 :            :                 maRangeX.isInside(rRange.maRangeX)
     212                 :            :                 && maRangeY.isInside(rRange.maRangeY)
     213                 :            :                 );
     214                 :            :         }
     215                 :            : 
     216                 :            :         /// yields true if rRange at least partly inside set
     217                 :            :         bool overlaps(const B2IRange& rRange) const
     218                 :            :         {
     219                 :            :             return (
     220                 :            :                 maRangeX.overlaps(rRange.maRangeX)
     221                 :            :                 && maRangeY.overlaps(rRange.maRangeY)
     222                 :            :                 );
     223                 :            :         }
     224                 :            : 
     225                 :            :         /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
     226                 :            :         bool overlapsMore(const B2IRange& rRange) const
     227                 :            :         {
     228                 :            :             return (
     229                 :            :                 maRangeX.overlapsMore(rRange.maRangeX)
     230                 :            :                 && maRangeY.overlapsMore(rRange.maRangeY)
     231                 :            :                 );
     232                 :            :         }
     233                 :            : 
     234                 :            :         /// add point to the set, expanding as necessary
     235                 :          5 :         void expand(const B2ITuple& rTuple)
     236                 :            :         {
     237                 :          5 :             maRangeX.expand(rTuple.getX());
     238                 :          5 :             maRangeY.expand(rTuple.getY());
     239                 :          5 :         }
     240                 :            : 
     241                 :            :         /// add rRange to the set, expanding as necessary
     242                 :            :         void expand(const B2IRange& rRange)
     243                 :            :         {
     244                 :            :             maRangeX.expand(rRange.maRangeX);
     245                 :            :             maRangeY.expand(rRange.maRangeY);
     246                 :            :         }
     247                 :            : 
     248                 :            :         /// calc set intersection
     249                 :            :         void intersect(const B2IRange& rRange)
     250                 :            :         {
     251                 :            :             maRangeX.intersect(rRange.maRangeX);
     252                 :            :             maRangeY.intersect(rRange.maRangeY);
     253                 :            :         }
     254                 :            : 
     255                 :            :         /// grow set by nValue on all sides
     256                 :            :         void grow(sal_Int32 nValue)
     257                 :            :         {
     258                 :            :             maRangeX.grow(nValue);
     259                 :            :             maRangeY.grow(nValue);
     260                 :            :         }
     261                 :            : 
     262                 :            :     private:
     263                 :            :         typedef ::basegfx::BasicRange< ValueType, TraitsType >  MyBasicRange;
     264                 :            : 
     265                 :            :         MyBasicRange        maRangeX;
     266                 :            :         MyBasicRange        maRangeY;
     267                 :            :     };
     268                 :            : 
     269                 :            :     /** Compute the set difference of the two given ranges
     270                 :            : 
     271                 :            :         This method calculates the symmetric difference (aka XOR)
     272                 :            :         between the two given ranges, and returning the resulting
     273                 :            :         ranges. Thus, the result will contain all areas where one, but
     274                 :            :         not both ranges lie.
     275                 :            : 
     276                 :            :         @param o_rResult
     277                 :            :         Result vector. The up to four difference ranges are returned
     278                 :            :         within this vector
     279                 :            : 
     280                 :            :         @param rFirst
     281                 :            :         The first range
     282                 :            : 
     283                 :            :         @param rSecond
     284                 :            :         The second range
     285                 :            : 
     286                 :            :         @return the input vector
     287                 :            :      */
     288                 :            :     BASEGFX_DLLPUBLIC ::std::vector< B2IRange >& computeSetDifference( ::std::vector< B2IRange >&   o_rResult,
     289                 :            :                                                      const B2IRange&            rFirst,
     290                 :            :                                                      const B2IRange&            rSecond );
     291                 :            : 
     292                 :            : } // end of namespace basegfx
     293                 :            : 
     294                 :            : #endif /* _BGFX_RANGE_B2IRANGE_HXX */
     295                 :            : 
     296                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10