LCOV - code coverage report
Current view: top level - basegfx/source/tools - b2dclipstate.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 212 0.0 %
Date: 2014-04-14 Functions: 0 40 0.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             : #include <basegfx/tools/b2dclipstate.hxx>
      21             : 
      22             : #include <basegfx/range/b2drange.hxx>
      23             : #include <basegfx/range/b2dpolyrange.hxx>
      24             : #include <basegfx/range/b2drangeclipper.hxx>
      25             : #include <basegfx/polygon/b2dpolygon.hxx>
      26             : #include <basegfx/polygon/b2dpolygontools.hxx>
      27             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      28             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      29             : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
      30             : 
      31             : namespace basegfx
      32             : {
      33             : namespace tools
      34             : {
      35           0 :     class ImplB2DClipState
      36             :     {
      37             :     public:
      38             :         enum Operation {UNION, INTERSECT, XOR, SUBTRACT};
      39             : 
      40           0 :         ImplB2DClipState() :
      41             :             maPendingPolygons(),
      42             :             maPendingRanges(),
      43             :             maClipPoly(),
      44           0 :             mePendingOps(UNION)
      45           0 :         {}
      46             : 
      47           0 :         explicit ImplB2DClipState( const B2DPolyPolygon& rPoly ) :
      48             :             maPendingPolygons(),
      49             :             maPendingRanges(),
      50             :             maClipPoly(rPoly),
      51           0 :             mePendingOps(UNION)
      52           0 :         {}
      53             : 
      54           0 :         bool isCleared() const
      55             :         {
      56           0 :             return !maClipPoly.count()
      57           0 :                 && !maPendingPolygons.count()
      58           0 :                 && !maPendingRanges.count();
      59             :         }
      60             : 
      61           0 :         bool isNullClipPoly() const
      62             :         {
      63           0 :             return maClipPoly.count() == 1
      64           0 :                 && !maClipPoly.getB2DPolygon(0).count();
      65             :         }
      66             : 
      67           0 :         bool isNull() const
      68             :         {
      69           0 :             return !maPendingPolygons.count()
      70           0 :                 && !maPendingRanges.count()
      71           0 :                 && isNullClipPoly();
      72             :         }
      73             : 
      74           0 :         void makeNull()
      75             :         {
      76           0 :             maPendingPolygons.clear();
      77           0 :             maPendingRanges.clear();
      78           0 :             maClipPoly.clear();
      79           0 :             maClipPoly.append(B2DPolygon());
      80           0 :             mePendingOps = UNION;
      81           0 :         }
      82             : 
      83           0 :         bool operator==(const ImplB2DClipState& rRHS) const
      84             :         {
      85           0 :             return maPendingPolygons == rRHS.maPendingPolygons
      86           0 :                 && maPendingRanges == rRHS.maPendingRanges
      87           0 :                 && maClipPoly == rRHS.maClipPoly
      88           0 :                 && mePendingOps == rRHS.mePendingOps;
      89             :         }
      90             : 
      91           0 :         void addRange(const B2DRange& rRange, Operation eOp)
      92             :         {
      93           0 :             if( rRange.isEmpty() )
      94           0 :                 return;
      95             : 
      96           0 :             commitPendingPolygons();
      97           0 :             if( mePendingOps != eOp )
      98           0 :                 commitPendingRanges();
      99             : 
     100           0 :             mePendingOps = eOp;
     101             :             maPendingRanges.appendElement(
     102             :                 rRange,
     103           0 :                 ORIENTATION_POSITIVE);
     104             :         }
     105             : 
     106           0 :         void addPolyPolygon(B2DPolyPolygon aPoly, Operation eOp)
     107             :         {
     108           0 :             commitPendingRanges();
     109           0 :             if( mePendingOps != eOp )
     110           0 :                 commitPendingPolygons();
     111             : 
     112           0 :             mePendingOps = eOp;
     113           0 :             maPendingPolygons.append(aPoly);
     114           0 :         }
     115             : 
     116           0 :         void unionRange(const B2DRange& rRange)
     117             :         {
     118           0 :             if( isCleared() )
     119           0 :                 return;
     120             : 
     121           0 :             addRange(rRange,UNION);
     122             :         }
     123             : 
     124           0 :         void unionPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     125             :         {
     126           0 :             if( isCleared() )
     127           0 :                 return;
     128             : 
     129           0 :             addPolyPolygon(rPolyPoly,UNION);
     130             :         }
     131             : 
     132           0 :         void intersectRange(const B2DRange& rRange)
     133             :         {
     134           0 :             if( isNull() )
     135           0 :                 return;
     136             : 
     137           0 :             addRange(rRange,INTERSECT);
     138             :         }
     139             : 
     140           0 :         void intersectPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     141             :         {
     142           0 :             if( isNull() )
     143           0 :                 return;
     144             : 
     145           0 :             addPolyPolygon(rPolyPoly,INTERSECT);
     146             :         }
     147             : 
     148           0 :         void subtractRange(const B2DRange& rRange )
     149             :         {
     150           0 :             if( isNull() )
     151           0 :                 return;
     152             : 
     153           0 :             addRange(rRange,SUBTRACT);
     154             :         }
     155             : 
     156           0 :         void subtractPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     157             :         {
     158           0 :             if( isNull() )
     159           0 :                 return;
     160             : 
     161           0 :             addPolyPolygon(rPolyPoly,SUBTRACT);
     162             :         }
     163             : 
     164           0 :         void xorRange(const B2DRange& rRange)
     165             :         {
     166           0 :             addRange(rRange,XOR);
     167           0 :         }
     168             : 
     169           0 :         void xorPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     170             :         {
     171           0 :             addPolyPolygon(rPolyPoly,XOR);
     172           0 :         }
     173             : 
     174           0 :         B2DPolyPolygon getClipPoly() const
     175             :         {
     176           0 :             commitPendingRanges();
     177           0 :             commitPendingPolygons();
     178             : 
     179           0 :             return maClipPoly;
     180             :         }
     181             : 
     182             :     private:
     183           0 :         void commitPendingPolygons() const
     184             :         {
     185           0 :             if( !maPendingPolygons.count() )
     186           0 :                 return;
     187             : 
     188             :             // assumption: maClipPoly has kept polygons prepared for
     189             :             // clipping; i.e. no neutral polygons & correct
     190             :             // orientation
     191           0 :             maPendingPolygons = tools::prepareForPolygonOperation(maPendingPolygons);
     192           0 :             const bool bIsEmpty=isNullClipPoly();
     193           0 :             const bool bIsCleared=!maClipPoly.count();
     194           0 :             switch(mePendingOps)
     195             :             {
     196             :                 case UNION:
     197             :                     OSL_ASSERT( !bIsCleared );
     198             : 
     199           0 :                     if( bIsEmpty )
     200           0 :                         maClipPoly = maPendingPolygons;
     201             :                     else
     202           0 :                         maClipPoly = tools::solvePolygonOperationOr(
     203             :                             maClipPoly,
     204           0 :                             maPendingPolygons);
     205           0 :                     break;
     206             :                 case INTERSECT:
     207             :                     OSL_ASSERT( !bIsEmpty );
     208             : 
     209           0 :                     if( bIsCleared )
     210           0 :                         maClipPoly = maPendingPolygons;
     211             :                     else
     212           0 :                         maClipPoly = tools::solvePolygonOperationAnd(
     213             :                             maClipPoly,
     214           0 :                             maPendingPolygons);
     215           0 :                     break;
     216             :                 case XOR:
     217           0 :                     if( bIsEmpty )
     218           0 :                         maClipPoly = maPendingPolygons;
     219           0 :                     else if( bIsCleared )
     220             :                     {
     221             :                         // not representable, strictly speaking,
     222             :                         // using polygons with the common even/odd
     223             :                         // or nonzero winding number fill rule. If
     224             :                         // we'd want to represent it, fill rule
     225             :                         // would need to be "non-negative winding
     226             :                         // number" (and we then would return
     227             :                         // 'holes' here)
     228             : 
     229             :                         // going for an ugly hack meanwhile
     230           0 :                         maClipPoly = tools::solvePolygonOperationXor(
     231             :                             B2DPolyPolygon(
     232             :                                 tools::createPolygonFromRect(B2DRange(-1E20,-1E20,1E20,1E20))),
     233           0 :                             maPendingPolygons);
     234             :                     }
     235             :                     else
     236           0 :                         maClipPoly = tools::solvePolygonOperationXor(
     237             :                             maClipPoly,
     238           0 :                             maPendingPolygons);
     239           0 :                     break;
     240             :                 case SUBTRACT:
     241             :                     OSL_ASSERT( !bIsEmpty );
     242             : 
     243             :                     // first union all pending ones, subtract en bloc then
     244           0 :                     maPendingPolygons = solveCrossovers(maPendingPolygons);
     245           0 :                     maPendingPolygons = stripNeutralPolygons(maPendingPolygons);
     246           0 :                     maPendingPolygons = stripDispensablePolygons(maPendingPolygons, false);
     247             : 
     248           0 :                     if( bIsCleared )
     249             :                     {
     250             :                         // not representable, strictly speaking,
     251             :                         // using polygons with the common even/odd
     252             :                         // or nonzero winding number fill rule. If
     253             :                         // we'd want to represent it, fill rule
     254             :                         // would need to be "non-negative winding
     255             :                         // number" (and we then would return
     256             :                         // 'holes' here)
     257             : 
     258             :                         // going for an ugly hack meanwhile
     259           0 :                         maClipPoly = tools::solvePolygonOperationDiff(
     260             :                             B2DPolyPolygon(
     261             :                                 tools::createPolygonFromRect(B2DRange(-1E20,-1E20,1E20,1E20))),
     262           0 :                             maPendingPolygons);
     263             :                     }
     264             :                     else
     265           0 :                         maClipPoly = tools::solvePolygonOperationDiff(
     266             :                             maClipPoly,
     267           0 :                             maPendingPolygons);
     268           0 :                     break;
     269             :             }
     270             : 
     271           0 :             maPendingPolygons.clear();
     272           0 :             mePendingOps = UNION;
     273             :         }
     274             : 
     275           0 :         void commitPendingRanges() const
     276             :         {
     277           0 :             if( !maPendingRanges.count() )
     278           0 :                 return;
     279             : 
     280             :             // use the specialized range clipper for the win
     281           0 :             B2DPolyPolygon aCollectedRanges;
     282           0 :             const bool bIsEmpty=isNullClipPoly();
     283           0 :             const bool bIsCleared=!maClipPoly.count();
     284           0 :             switch(mePendingOps)
     285             :             {
     286             :                 case UNION:
     287             :                     OSL_ASSERT( !bIsCleared );
     288             : 
     289           0 :                     aCollectedRanges = maPendingRanges.solveCrossovers();
     290           0 :                     aCollectedRanges = stripNeutralPolygons(aCollectedRanges);
     291           0 :                     aCollectedRanges = stripDispensablePolygons(aCollectedRanges, false);
     292           0 :                     if( bIsEmpty )
     293           0 :                         maClipPoly = aCollectedRanges;
     294             :                     else
     295           0 :                         maClipPoly = tools::solvePolygonOperationOr(
     296             :                             maClipPoly,
     297           0 :                             aCollectedRanges);
     298           0 :                     break;
     299             :                 case INTERSECT:
     300             :                     OSL_ASSERT( !bIsEmpty );
     301             : 
     302           0 :                     aCollectedRanges = maPendingRanges.solveCrossovers();
     303           0 :                     aCollectedRanges = stripNeutralPolygons(aCollectedRanges);
     304           0 :                     if( maPendingRanges.count() > 1 )
     305           0 :                         aCollectedRanges = stripDispensablePolygons(aCollectedRanges, true);
     306             : 
     307           0 :                     if( bIsCleared )
     308           0 :                         maClipPoly = aCollectedRanges;
     309             :                     else
     310           0 :                         maClipPoly = tools::solvePolygonOperationAnd(
     311             :                             maClipPoly,
     312           0 :                             aCollectedRanges);
     313           0 :                     break;
     314             :                 case XOR:
     315           0 :                     aCollectedRanges = maPendingRanges.solveCrossovers();
     316           0 :                     aCollectedRanges = stripNeutralPolygons(aCollectedRanges);
     317           0 :                     aCollectedRanges = correctOrientations(aCollectedRanges);
     318             : 
     319           0 :                     if( bIsEmpty )
     320           0 :                         maClipPoly = aCollectedRanges;
     321           0 :                     else if( bIsCleared )
     322             :                     {
     323             :                         // not representable, strictly speaking,
     324             :                         // using polygons with the common even/odd
     325             :                         // or nonzero winding number fill rule. If
     326             :                         // we'd want to represent it, fill rule
     327             :                         // would need to be "non-negative winding
     328             :                         // number" (and we then would return
     329             :                         // 'holes' here)
     330             : 
     331             :                         // going for an ugly hack meanwhile
     332           0 :                         maClipPoly = tools::solvePolygonOperationXor(
     333             :                             B2DPolyPolygon(
     334             :                                 tools::createPolygonFromRect(B2DRange(-1E20,-1E20,1E20,1E20))),
     335           0 :                             aCollectedRanges);
     336             :                     }
     337             :                     else
     338           0 :                         maClipPoly = tools::solvePolygonOperationXor(
     339             :                             maClipPoly,
     340           0 :                             aCollectedRanges);
     341           0 :                     break;
     342             :                 case SUBTRACT:
     343             :                     OSL_ASSERT( !bIsEmpty );
     344             : 
     345             :                     // first union all pending ranges, subtract en bloc then
     346           0 :                     aCollectedRanges = maPendingRanges.solveCrossovers();
     347           0 :                     aCollectedRanges = stripNeutralPolygons(aCollectedRanges);
     348           0 :                     aCollectedRanges = stripDispensablePolygons(aCollectedRanges, false);
     349             : 
     350           0 :                     if( bIsCleared )
     351             :                     {
     352             :                         // not representable, strictly speaking,
     353             :                         // using polygons with the common even/odd
     354             :                         // or nonzero winding number fill rule. If
     355             :                         // we'd want to represent it, fill rule
     356             :                         // would need to be "non-negative winding
     357             :                         // number" (and we then would return
     358             :                         // 'holes' here)
     359             : 
     360             :                         // going for an ugly hack meanwhile
     361           0 :                         maClipPoly = tools::solvePolygonOperationDiff(
     362             :                             B2DPolyPolygon(
     363             :                                 tools::createPolygonFromRect(B2DRange(-1E20,-1E20,1E20,1E20))),
     364           0 :                             aCollectedRanges);
     365             :                     }
     366             :                     else
     367           0 :                         maClipPoly = tools::solvePolygonOperationDiff(
     368             :                             maClipPoly,
     369           0 :                             aCollectedRanges);
     370           0 :                     break;
     371             :             }
     372             : 
     373           0 :             maPendingRanges.clear();
     374           0 :             mePendingOps = UNION;
     375             :         }
     376             : 
     377             :         mutable B2DPolyPolygon maPendingPolygons;
     378             :         mutable B2DPolyRange   maPendingRanges;
     379             :         mutable B2DPolyPolygon maClipPoly;
     380             :         mutable Operation      mePendingOps;
     381             :     };
     382             : 
     383           0 :     B2DClipState::B2DClipState() :
     384           0 :         mpImpl()
     385           0 :     {}
     386             : 
     387           0 :     B2DClipState::~B2DClipState()
     388           0 :     {}
     389             : 
     390           0 :     B2DClipState::B2DClipState( const B2DClipState& rOrig ) :
     391           0 :         mpImpl(rOrig.mpImpl)
     392           0 :     {}
     393             : 
     394           0 :     B2DClipState::B2DClipState( const B2DPolyPolygon& rPolyPoly ) :
     395           0 :         mpImpl( ImplB2DClipState(rPolyPoly) )
     396           0 :     {}
     397             : 
     398           0 :     B2DClipState& B2DClipState::operator=( const B2DClipState& rRHS )
     399             :     {
     400           0 :         mpImpl = rRHS.mpImpl;
     401           0 :         return *this;
     402             :     }
     403             : 
     404           0 :     void B2DClipState::makeNull()
     405             :     {
     406           0 :         mpImpl->makeNull();
     407           0 :     }
     408             : 
     409           0 :     bool B2DClipState::isCleared() const
     410             :     {
     411           0 :         return mpImpl->isCleared();
     412             :     }
     413             : 
     414           0 :     bool B2DClipState::operator==(const B2DClipState& rRHS) const
     415             :     {
     416           0 :         if(mpImpl.same_object(rRHS.mpImpl))
     417           0 :             return true;
     418             : 
     419           0 :         return ((*mpImpl) == (*rRHS.mpImpl));
     420             :     }
     421             : 
     422           0 :     bool B2DClipState::operator!=(const B2DClipState& rRHS) const
     423             :     {
     424           0 :         return !(*this == rRHS);
     425             :     }
     426             : 
     427           0 :     void B2DClipState::unionRange(const B2DRange& rRange)
     428             :     {
     429           0 :         mpImpl->unionRange(rRange);
     430           0 :     }
     431             : 
     432           0 :     void B2DClipState::unionPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     433             :     {
     434           0 :         mpImpl->unionPolyPolygon(rPolyPoly);
     435           0 :     }
     436             : 
     437           0 :     void B2DClipState::intersectRange(const B2DRange& rRange)
     438             :     {
     439           0 :         mpImpl->intersectRange(rRange);
     440           0 :     }
     441             : 
     442           0 :     void B2DClipState::intersectPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     443             :     {
     444           0 :         mpImpl->intersectPolyPolygon(rPolyPoly);
     445           0 :     }
     446             : 
     447           0 :     void B2DClipState::subtractRange(const B2DRange& rRange)
     448             :     {
     449           0 :         mpImpl->subtractRange(rRange);
     450           0 :     }
     451             : 
     452           0 :     void B2DClipState::subtractPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     453             :     {
     454           0 :         mpImpl->subtractPolyPolygon(rPolyPoly);
     455           0 :     }
     456             : 
     457           0 :     void B2DClipState::xorRange(const B2DRange& rRange)
     458             :     {
     459           0 :         mpImpl->xorRange(rRange);
     460           0 :     }
     461             : 
     462           0 :     void B2DClipState::xorPolyPolygon(const B2DPolyPolygon& rPolyPoly)
     463             :     {
     464           0 :         mpImpl->xorPolyPolygon(rPolyPoly);
     465           0 :     }
     466             : 
     467           0 :     B2DPolyPolygon B2DClipState::getClipPoly() const
     468             :     {
     469           0 :         return mpImpl->getClipPoly();
     470             :     }
     471             : 
     472             : } // end of namespace tools
     473             : } // end of namespace basegfx
     474             : 
     475             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10