LCOV - code coverage report
Current view: top level - chart2/source/view/main - Clipping.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 118 0.0 %
Date: 2014-04-14 Functions: 0 5 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 "Clipping.hxx"
      21             : #include "CommonConverters.hxx"
      22             : #include "BaseGFXHelper.hxx"
      23             : 
      24             : #include <com/sun/star/drawing/Position3D.hpp>
      25             : #include <com/sun/star/drawing/DoubleSequence.hpp>
      26             : 
      27             : namespace chart
      28             : {
      29             : using namespace ::com::sun::star;
      30             : using ::basegfx::B2DRectangle;
      31             : using ::basegfx::B2DTuple;
      32             : 
      33             : namespace{
      34             : /** @descr  This is a supporting function for lcl_clip2d.  It computes a new parametric
      35             :             value for an entering (dTE) or leaving (dTL) intersection point with one
      36             :             of the edges bounding the clipping area.
      37             :             For explanation of the parameters please refer to :
      38             : 
      39             :             Liang-Biarsky parametric line-clipping algorithm as described in:
      40             :             Computer Graphics: principles and practice, 2nd ed.,
      41             :             James D. Foley et al.,
      42             :             Section 3.12.4 on page 117.
      43             : */
      44           0 : bool lcl_CLIPt(double fDenom,double fNum, double & fTE, double & fTL)
      45             : {
      46             :     double  fT;
      47             : 
      48           0 :     if (fDenom > 0)             //  Intersection enters: PE
      49             :     {
      50           0 :         fT = fNum / fDenom;     //  Parametric value at the intersection.
      51           0 :         if (fT > fTL)           //  fTE and fTL crossover
      52           0 :             return false;       //    therefore reject the line.
      53           0 :         else if (fT > fTE)      //  A new fTE has been found.
      54           0 :             fTE = fT;
      55             :     }
      56           0 :     else if (fDenom < 0)        //  Intersection leaves: PL
      57             :     {
      58           0 :         fT = fNum / fDenom;     //  Parametric Value at the intersection.
      59           0 :         if (fT < fTE)           //  fTE and fTL crossover
      60           0 :             return false;       //    therefore reject the line.
      61           0 :         else if (fT < fTL)      //  A new fTL has been found.
      62           0 :             fTL = fT;
      63             :     }
      64           0 :     else if (fNum > 0)
      65           0 :         return false;           //  Line lies on the outside of the edge.
      66             : 
      67           0 :     return true;
      68             : }
      69             : 
      70             : /** @descr  The line given by it's two endpoints rP0 and rP1 is clipped at the rectangle
      71             :             rRectangle.  If there is at least a part of it visible then sal_True is returned and
      72             :             the endpoints of that part are stored in rP0 and rP1.  The points rP0 and rP1
      73             :             may have the same coordinates.
      74             :         @param  rP0 Start point of the line to clip.  Modified to contain a start point inside
      75             :             the clipping area if possible.
      76             :         @param  rP1 End point of the line to clip.  Modified to contain an end point inside
      77             :             the clipping area if possible.
      78             :         @param  rRectangle Clipping area.
      79             :         @return If the line lies completely or partly inside the clipping area then TRUE
      80             :             is returned.  If the line lies completely outside then sal_False is returned and rP0 and
      81             :             rP1 are left unmodified.
      82             : */
      83           0 : bool lcl_clip2d(B2DTuple& rPoint0, B2DTuple& rPoint1, const B2DRectangle& rRectangle)
      84             : {
      85             :     //Direction vector of the line.
      86           0 :     B2DTuple aDirection = rPoint1 - rPoint0;
      87             : 
      88           0 :     if( aDirection.getX()==0 && aDirection.getY()==0 && rRectangle.isInside(rPoint0) )
      89             :     {
      90             :         //  Degenerate case of a zero length line.
      91           0 :         return true;
      92             :     }
      93             :     else
      94             :     {
      95             :         //  Values of the line parameter where the line enters resp. leaves the rectangle.
      96           0 :         double fTE = 0,
      97           0 :                fTL = 1;
      98             : 
      99             :         //  Test whether at least a part lies in the four half-planes with respect to
     100             :         //  the rectangles four edges.
     101           0 :         if( lcl_CLIPt(aDirection.getX(), rRectangle.getMinX() - rPoint0.getX(), fTE, fTL) )
     102           0 :             if( lcl_CLIPt(-aDirection.getX(), rPoint0.getX() - rRectangle.getMaxX(), fTE, fTL) )
     103           0 :                 if( lcl_CLIPt(aDirection.getY(), rRectangle.getMinY() - rPoint0.getY(), fTE, fTL) )
     104           0 :                     if( lcl_CLIPt(-aDirection.getY(), rPoint0.getY() - rRectangle.getMaxY(), fTE, fTL) )
     105             :                     {
     106             :                         //  At least a part is visible.
     107           0 :                         if (fTL < 1)
     108             :                         {
     109             :                             //  Compute the new end point.
     110           0 :                             rPoint1.setX( rPoint0.getX() + fTL * aDirection.getX() );
     111           0 :                             rPoint1.setY( rPoint0.getY() + fTL * aDirection.getY() );
     112             :                         }
     113           0 :                         if (fTE > 0)
     114             :                         {
     115             :                             //  Compute the new starting point.
     116           0 :                             rPoint0.setX( rPoint0.getX() + fTE * aDirection.getX() );
     117           0 :                             rPoint0.setY( rPoint0.getY() + fTE * aDirection.getY() );
     118             :                         }
     119           0 :                         return true;
     120             :                     }
     121             : 
     122             :         //  Line is not visible.
     123           0 :         return false;
     124           0 :     }
     125             : }
     126             : 
     127           0 : bool lcl_clip2d_(drawing::Position3D& rPoint0, drawing::Position3D& rPoint1, const B2DRectangle& rRectangle)
     128             : {
     129           0 :     B2DTuple aP0(rPoint0.PositionX,rPoint0.PositionY);
     130           0 :     B2DTuple aP1(rPoint1.PositionX,rPoint1.PositionY);
     131           0 :     bool bRet = lcl_clip2d( aP0, aP1, rRectangle );
     132             : 
     133           0 :     rPoint0.PositionX = aP0.getX();
     134           0 :     rPoint0.PositionY = aP0.getY();
     135           0 :     rPoint1.PositionX = aP1.getX();
     136           0 :     rPoint1.PositionY = aP1.getY();
     137             : 
     138           0 :     return bRet;
     139             : }
     140             : 
     141           0 : void lcl_addPointToPoly( drawing::PolyPolygonShape3D& rPoly
     142             :         , const drawing::Position3D& rPos
     143             :         , sal_Int32 nPolygonIndex
     144             :         , std::vector< sal_Int32 >& rResultPointCount
     145             :         , sal_Int32 nReservePointCount )
     146             : {
     147           0 :     if(nPolygonIndex<0)
     148             :     {
     149             :         OSL_FAIL( "The polygon index needs to be > 0");
     150           0 :         nPolygonIndex=0;
     151             :     }
     152             : 
     153             :     //make sure that we have enough polygons
     154           0 :     if(nPolygonIndex >= rPoly.SequenceX.getLength() )
     155             :     {
     156           0 :         rPoly.SequenceX.realloc(nPolygonIndex+1);
     157           0 :         rPoly.SequenceY.realloc(nPolygonIndex+1);
     158           0 :         rPoly.SequenceZ.realloc(nPolygonIndex+1);
     159           0 :         rResultPointCount.resize(nPolygonIndex+1,0);
     160             :     }
     161             : 
     162           0 :     drawing::DoubleSequence* pOuterSequenceX = &rPoly.SequenceX.getArray()[nPolygonIndex];
     163           0 :     drawing::DoubleSequence* pOuterSequenceY = &rPoly.SequenceY.getArray()[nPolygonIndex];
     164           0 :     drawing::DoubleSequence* pOuterSequenceZ = &rPoly.SequenceZ.getArray()[nPolygonIndex];
     165             : 
     166           0 :     sal_Int32 nNewResultPointCount = rResultPointCount[nPolygonIndex]+1;
     167           0 :     sal_Int32 nSeqLength = pOuterSequenceX->getLength();
     168             : 
     169           0 :     if( nSeqLength <= nNewResultPointCount )
     170             :     {
     171           0 :         sal_Int32 nReallocLength = nReservePointCount;
     172           0 :         if( nNewResultPointCount > nReallocLength )
     173             :         {
     174           0 :             nReallocLength = nNewResultPointCount;
     175             :             OSL_FAIL("this should not be the case to avoid performance problems");
     176             :         }
     177           0 :         pOuterSequenceX->realloc(nReallocLength);
     178           0 :         pOuterSequenceY->realloc(nReallocLength);
     179           0 :         pOuterSequenceZ->realloc(nReallocLength);
     180             :     }
     181             : 
     182           0 :     double* pInnerSequenceX = pOuterSequenceX->getArray();
     183           0 :     double* pInnerSequenceY = pOuterSequenceY->getArray();
     184           0 :     double* pInnerSequenceZ = pOuterSequenceZ->getArray();
     185             : 
     186           0 :     pInnerSequenceX[nNewResultPointCount-1] = rPos.PositionX;
     187           0 :     pInnerSequenceY[nNewResultPointCount-1] = rPos.PositionY;
     188           0 :     pInnerSequenceZ[nNewResultPointCount-1] = rPos.PositionZ;
     189           0 :     rResultPointCount[nPolygonIndex]=nNewResultPointCount;
     190           0 : }
     191             : 
     192             : }//end anonymous namespace
     193             : 
     194           0 : void Clipping::clipPolygonAtRectangle( const drawing::PolyPolygonShape3D& rPolygon
     195             :                                       , const B2DRectangle& rRectangle
     196             :                                       , drawing::PolyPolygonShape3D& aResult
     197             :                                       , bool bSplitPiecesToDifferentPolygons )
     198             : {
     199           0 :     aResult.SequenceX.realloc(0);
     200           0 :     aResult.SequenceY.realloc(0);
     201           0 :     aResult.SequenceZ.realloc(0);
     202             : 
     203           0 :     if(!rPolygon.SequenceX.getLength())
     204           0 :         return;
     205             : 
     206             :     //need clipping?:
     207             :     {
     208           0 :         ::basegfx::B3DRange a3DRange( BaseGFXHelper::getBoundVolume( rPolygon ) );
     209           0 :         ::basegfx::B2DRange a2DRange( a3DRange.getMinX(), a3DRange.getMinY(), a3DRange.getMaxX(), a3DRange.getMaxY() );
     210           0 :         if( rRectangle.isInside( a2DRange ) )
     211             :         {
     212           0 :             aResult = rPolygon;
     213           0 :             return;
     214             :         }
     215             :         else
     216             :         {
     217           0 :             a2DRange.intersect( rRectangle );
     218           0 :             if( a2DRange.isEmpty() )
     219           0 :                 return;
     220             :         }
     221             :     }
     222             : 
     223             : 
     224           0 :     std::vector< sal_Int32 > aResultPointCount;//per polygon index
     225             : 
     226             :     //apply clipping:
     227           0 :     drawing::Position3D aFrom;
     228           0 :     drawing::Position3D aTo;
     229             : 
     230           0 :     sal_Int32 nNewPolyIndex = 0;
     231           0 :     sal_Int32 nOldPolyCount = rPolygon.SequenceX.getLength();
     232           0 :     for(sal_Int32 nOldPolyIndex=0; nOldPolyIndex<nOldPolyCount; nOldPolyIndex++, nNewPolyIndex++ )
     233             :     {
     234           0 :         sal_Int32 nOldPointCount = rPolygon.SequenceX[nOldPolyIndex].getLength();
     235             : 
     236             :         // set last point to a position outside the rectangle, such that the first
     237             :         // time lcl_clip2d returns true, the comparison to last will always yield false
     238           0 :         drawing::Position3D aLast(rRectangle.getMinX()-1.0,rRectangle.getMinY()-1.0, 0.0 );
     239             : 
     240           0 :         for(sal_Int32 nOldPoint=1; nOldPoint<nOldPointCount; nOldPoint++)
     241             :         {
     242           0 :             aFrom = getPointFromPoly(rPolygon,nOldPoint-1,nOldPolyIndex);
     243           0 :             aTo = getPointFromPoly(rPolygon,nOldPoint,nOldPolyIndex);
     244           0 :             if( lcl_clip2d_(aFrom, aTo, rRectangle) )
     245             :             {
     246             :                 // compose an Polygon of as many consecutive points as possible
     247           0 :                 if(aFrom == aLast)
     248             :                 {
     249           0 :                     if( !(aTo==aFrom) )
     250             :                     {
     251           0 :                         lcl_addPointToPoly( aResult, aTo, nNewPolyIndex, aResultPointCount, nOldPointCount );
     252             :                     }
     253             :                 }
     254             :                 else
     255             :                 {
     256           0 :                     if( bSplitPiecesToDifferentPolygons && nOldPoint!=1 )
     257             :                     {
     258           0 :                         if( nNewPolyIndex < aResult.SequenceX.getLength()
     259           0 :                                 && aResultPointCount[nNewPolyIndex]>0 )
     260           0 :                             nNewPolyIndex++;
     261             :                     }
     262           0 :                     lcl_addPointToPoly( aResult, aFrom, nNewPolyIndex, aResultPointCount, nOldPointCount );
     263           0 :                     if( !(aTo==aFrom) )
     264           0 :                         lcl_addPointToPoly( aResult, aTo, nNewPolyIndex, aResultPointCount, nOldPointCount );
     265             :                 }
     266           0 :                 aLast = aTo;
     267             :             }
     268             :         }
     269             :     }
     270             :     //free unused space
     271           0 :     for( sal_Int32 nPolygonIndex = aResultPointCount.size(); nPolygonIndex--; )
     272             :     {
     273           0 :         drawing::DoubleSequence* pOuterSequenceX = &aResult.SequenceX.getArray()[nPolygonIndex];
     274           0 :         drawing::DoubleSequence* pOuterSequenceY = &aResult.SequenceY.getArray()[nPolygonIndex];
     275           0 :         drawing::DoubleSequence* pOuterSequenceZ = &aResult.SequenceZ.getArray()[nPolygonIndex];
     276             : 
     277           0 :         sal_Int32 nUsedPointCount = aResultPointCount[nPolygonIndex];
     278           0 :         pOuterSequenceX->realloc(nUsedPointCount);
     279           0 :         pOuterSequenceY->realloc(nUsedPointCount);
     280           0 :         pOuterSequenceZ->realloc(nUsedPointCount);
     281           0 :     }
     282             : }
     283             : 
     284             : } //namespace chart
     285             : 
     286             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10