1ce9c7ef7SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3ce9c7ef7SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ce9c7ef7SAndrew Rist * or more contributor license agreements. See the NOTICE file 5ce9c7ef7SAndrew Rist * distributed with this work for additional information 6ce9c7ef7SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ce9c7ef7SAndrew Rist * to you under the Apache License, Version 2.0 (the 8ce9c7ef7SAndrew Rist * "License"); you may not use this file except in compliance 9ce9c7ef7SAndrew Rist * with the License. You may obtain a copy of the License at 10ce9c7ef7SAndrew Rist * 11ce9c7ef7SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12ce9c7ef7SAndrew Rist * 13ce9c7ef7SAndrew Rist * Unless required by applicable law or agreed to in writing, 14ce9c7ef7SAndrew Rist * software distributed under the License is distributed on an 15ce9c7ef7SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ce9c7ef7SAndrew Rist * KIND, either express or implied. See the License for the 17ce9c7ef7SAndrew Rist * specific language governing permissions and limitations 18ce9c7ef7SAndrew Rist * under the License. 19ce9c7ef7SAndrew Rist * 20ce9c7ef7SAndrew Rist *************************************************************/ 21ce9c7ef7SAndrew Rist 22ce9c7ef7SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _BGFX_POLYGON_B2DPOLYGONTOOLS_HXX 25cdf0e10cSrcweir #define _BGFX_POLYGON_B2DPOLYGONTOOLS_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx> 28cdf0e10cSrcweir #include <basegfx/vector/b2dvector.hxx> 29cdf0e10cSrcweir #include <basegfx/range/b2drectangle.hxx> 30cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx> 31cdf0e10cSrcweir #include <basegfx/polygon/b3dpolygon.hxx> 321f882ec4SArmin Le Grand #include <com/sun/star/drawing/PointSequence.hpp> 331f882ec4SArmin Le Grand #include <com/sun/star/drawing/FlagSequence.hpp> 34cdf0e10cSrcweir #include <vector> 35*b63233d8Sdamjan #include <basegfx/basegfxdllapi.h> 36cdf0e10cSrcweir 37cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 38cdf0e10cSrcweir 39cdf0e10cSrcweir namespace basegfx 40cdf0e10cSrcweir { 41cdf0e10cSrcweir // predefinitions 42cdf0e10cSrcweir class B2DPolygon; 43cdf0e10cSrcweir class B2DRange; 44cdf0e10cSrcweir 45cdf0e10cSrcweir namespace tools 46cdf0e10cSrcweir { 47cdf0e10cSrcweir // B2DPolygon tools 48cdf0e10cSrcweir 49cdf0e10cSrcweir // open/close with point add/remove and control point corrections 50*b63233d8Sdamjan BASEGFX_DLLPUBLIC void openWithGeometryChange(B2DPolygon& rCandidate); 51*b63233d8Sdamjan BASEGFX_DLLPUBLIC void closeWithGeometryChange(B2DPolygon& rCandidate); 52cdf0e10cSrcweir 53cdf0e10cSrcweir /** Check if given polygon is closed. 54cdf0e10cSrcweir 55cdf0e10cSrcweir This is kind of a 'classic' method to support old polygon 56cdf0e10cSrcweir definitions. Those old polygon definitions define the 57cdf0e10cSrcweir closed state of the polygon using identical start and 58cdf0e10cSrcweir endpoints. This method corrects this (removes double 59cdf0e10cSrcweir start/end points) and sets the Closed()-state of the 60cdf0e10cSrcweir polygon correctly. 61cdf0e10cSrcweir */ 62*b63233d8Sdamjan BASEGFX_DLLPUBLIC void checkClosed(B2DPolygon& rCandidate); 63cdf0e10cSrcweir 64cdf0e10cSrcweir // Get successor and predecessor indices. Returning the same index means there 65cdf0e10cSrcweir // is none. Same for successor. 66*b63233d8Sdamjan BASEGFX_DLLPUBLIC sal_uInt32 getIndexOfPredecessor(sal_uInt32 nIndex, const B2DPolygon& rCandidate); 67*b63233d8Sdamjan BASEGFX_DLLPUBLIC sal_uInt32 getIndexOfSuccessor(sal_uInt32 nIndex, const B2DPolygon& rCandidate); 68cdf0e10cSrcweir 69cdf0e10cSrcweir // Get orientation of Polygon 70*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2VectorOrientation getOrientation(const B2DPolygon& rCandidate); 71cdf0e10cSrcweir 72cdf0e10cSrcweir // isInside tests for B2dPoint and other B2dPolygon. On border is not inside as long as 73cdf0e10cSrcweir // not true is given in bWithBorder flag. 74*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isInside(const B2DPolygon& rCandidate, const B2DPoint& rPoint, bool bWithBorder = false); 75*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isInside(const B2DPolygon& rCandidate, const B2DPolygon& rPolygon, bool bWithBorder = false); 76cdf0e10cSrcweir 77cdf0e10cSrcweir /** Get the range of a polygon including bezier control points 78cdf0e10cSrcweir 79cdf0e10cSrcweir For detailed discussion, see B2DPolygon::getB2DRange() 80cdf0e10cSrcweir 81cdf0e10cSrcweir @param rCandidate 82cdf0e10cSrcweir The B2DPolygon eventually containing bezier segments 83cdf0e10cSrcweir 84cdf0e10cSrcweir @return 85cdf0e10cSrcweir The outer range of the bezier curve containing bezier control points 86cdf0e10cSrcweir */ 87*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DRange getRangeWithControlPoints(const B2DPolygon& rCandidate); 88cdf0e10cSrcweir 89cdf0e10cSrcweir /** Get the range of a polygon 90cdf0e10cSrcweir 91cdf0e10cSrcweir This method creates the outer range of the subdivided bezier curve. 92cdf0e10cSrcweir For detailed discussion see B2DPolygon::getB2DRange() 93cdf0e10cSrcweir 94cdf0e10cSrcweir @param rCandidate 95cdf0e10cSrcweir The B2DPolygon eventually containing bezier segments 96cdf0e10cSrcweir 97cdf0e10cSrcweir @return 98cdf0e10cSrcweir The outer range of the bezier curve 99cdf0e10cSrcweir */ 100*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DRange getRange(const B2DPolygon& rCandidate); 101cdf0e10cSrcweir 102cdf0e10cSrcweir // get signed area of polygon 103*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getSignedArea(const B2DPolygon& rCandidate); 104cdf0e10cSrcweir 105cdf0e10cSrcweir // get area of polygon 106*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getArea(const B2DPolygon& rCandidate); 107cdf0e10cSrcweir 108cdf0e10cSrcweir /** get length of polygon edge from point nIndex to nIndex + 1 */ 109*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getEdgeLength(const B2DPolygon& rCandidate, sal_uInt32 nIndex); 110cdf0e10cSrcweir 111cdf0e10cSrcweir /** get length of polygon */ 112*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getLength(const B2DPolygon& rCandidate); 113cdf0e10cSrcweir 114cdf0e10cSrcweir // get position on polygon for absolute given distance. If 115cdf0e10cSrcweir // length is given, it is assumed the correct polygon length, if 0.0 it is calculated 116cdf0e10cSrcweir // using getLength(...) 117*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPoint getPositionAbsolute(const B2DPolygon& rCandidate, double fDistance, double fLength = 0.0); 118cdf0e10cSrcweir 119cdf0e10cSrcweir // get position on polygon for relative given distance in range [0.0 .. 1.0]. If 120cdf0e10cSrcweir // length is given, it is assumed the correct polygon length, if 0.0 it is calculated 121cdf0e10cSrcweir // using getLength(...) 122*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPoint getPositionRelative(const B2DPolygon& rCandidate, double fDistance, double fLength = 0.0); 123cdf0e10cSrcweir 124cdf0e10cSrcweir // get a snippet from given polygon for absolute distances. The polygon is assumed 125cdf0e10cSrcweir // to be opened (not closed). fFrom and fTo need to be in range [0.0 .. fLength], where 126cdf0e10cSrcweir // fTo >= fFrom. If length is given, it is assumed the correct polygon length, 127cdf0e10cSrcweir // if 0.0 it is calculated using getLength(...) 128*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon getSnippetAbsolute(const B2DPolygon& rCandidate, double fFrom, double fTo, double fLength = 0.0); 129cdf0e10cSrcweir 130cdf0e10cSrcweir // get a snippet from given polygon for relative distances. The polygon is assumed 131cdf0e10cSrcweir // to be opened (not closed). fFrom and fTo need to be in range [0.0 .. 1.0], where 132cdf0e10cSrcweir // fTo >= fFrom. If length is given, it is assumed the correct polygon length, 133cdf0e10cSrcweir // if 0.0 it is calculated using getLength(...) 134*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon getSnippetRelative(const B2DPolygon& rCandidate, double fFrom = 0.0, double fTo = 1.0, double fLength = 0.0); 135cdf0e10cSrcweir 136cdf0e10cSrcweir // Continuity check for point with given index 137*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2VectorContinuity getContinuityInPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndex); 138cdf0e10cSrcweir 139cdf0e10cSrcweir // Subdivide all contained curves. Use distanceBound value if given. 140*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon& rCandidate, double fDistanceBound = 0.0); 141cdf0e10cSrcweir 142cdf0e10cSrcweir // Subdivide all contained curves. Use angleBound value if given. 143*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon adaptiveSubdivideByAngle(const B2DPolygon& rCandidate, double fAngleBound = 0.0); 144cdf0e10cSrcweir 145cdf0e10cSrcweir // #i37443# Subdivide all contained curves. 146*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon adaptiveSubdivideByCount(const B2DPolygon& rCandidate, sal_uInt32 nCount = 0L); 147cdf0e10cSrcweir 148cdf0e10cSrcweir // Definitions for the cut flags used from the findCut methods 149cdf0e10cSrcweir typedef sal_uInt16 CutFlagValue; 150cdf0e10cSrcweir 151cdf0e10cSrcweir #define CUTFLAG_NONE (0x0000) 152cdf0e10cSrcweir #define CUTFLAG_LINE (0x0001) 153cdf0e10cSrcweir #define CUTFLAG_START1 (0x0002) 154cdf0e10cSrcweir #define CUTFLAG_START2 (0x0004) 155cdf0e10cSrcweir #define CUTFLAG_END1 (0x0008) 156cdf0e10cSrcweir #define CUTFLAG_END2 (0x0010) 157cdf0e10cSrcweir #define CUTFLAG_ALL (CUTFLAG_LINE|CUTFLAG_START1|CUTFLAG_START2|CUTFLAG_END1|CUTFLAG_END2) 158cdf0e10cSrcweir #define CUTFLAG_DEFAULT (CUTFLAG_LINE|CUTFLAG_START2|CUTFLAG_END2) 159cdf0e10cSrcweir 160cdf0e10cSrcweir // Calculate cut between the points given by the two indices. pCut1 161cdf0e10cSrcweir // and pCut2 will contain the cut coordinate on each edge in ]0.0, 1.0] 162cdf0e10cSrcweir // (if given) and the return value will contain a cut description. 163*b63233d8Sdamjan BASEGFX_DLLPUBLIC CutFlagValue findCut( 164cdf0e10cSrcweir const B2DPolygon& rCandidate, 165cdf0e10cSrcweir sal_uInt32 nIndex1, sal_uInt32 nIndex2, 166cdf0e10cSrcweir CutFlagValue aCutFlags = CUTFLAG_DEFAULT, 167cdf0e10cSrcweir double* pCut1 = 0L, double* pCut2 = 0L); 168cdf0e10cSrcweir 169cdf0e10cSrcweir // This version is working with two indexed edges from different 170cdf0e10cSrcweir // polygons. 171*b63233d8Sdamjan BASEGFX_DLLPUBLIC CutFlagValue findCut( 172cdf0e10cSrcweir const B2DPolygon& rCandidate1, sal_uInt32 nIndex1, 173cdf0e10cSrcweir const B2DPolygon& rCandidate2, sal_uInt32 nIndex2, 174cdf0e10cSrcweir CutFlagValue aCutFlags = CUTFLAG_DEFAULT, 175cdf0e10cSrcweir double* pCut1 = 0L, double* pCut2 = 0L); 176cdf0e10cSrcweir 177cdf0e10cSrcweir // This version works with two points and vectors to define the 178cdf0e10cSrcweir // edges for the cut test. 179*b63233d8Sdamjan BASEGFX_DLLPUBLIC CutFlagValue findCut( 180cdf0e10cSrcweir const B2DPoint& rEdge1Start, const B2DVector& rEdge1Delta, 181cdf0e10cSrcweir const B2DPoint& rEdge2Start, const B2DVector& rEdge2Delta, 182cdf0e10cSrcweir CutFlagValue aCutFlags = CUTFLAG_DEFAULT, 183cdf0e10cSrcweir double* pCut1 = 0L, double* pCut2 = 0L); 184cdf0e10cSrcweir 185cdf0e10cSrcweir // test if point is on the given edge in range ]0.0..1.0[ without 186cdf0e10cSrcweir // the start/end points. If so, return true and put the parameter 187cdf0e10cSrcweir // value in pCut (if provided) 188*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isPointOnEdge( 189cdf0e10cSrcweir const B2DPoint& rPoint, 190cdf0e10cSrcweir const B2DPoint& rEdgeStart, 191cdf0e10cSrcweir const B2DVector& rEdgeDelta, 192cdf0e10cSrcweir double* pCut = 0L); 193cdf0e10cSrcweir 194cdf0e10cSrcweir /** Apply given LineDashing to given polygon 195cdf0e10cSrcweir 196cdf0e10cSrcweir This method is used to cut down line polygons to the needed 197cdf0e10cSrcweir pieces when a dashing needs to be applied. 198cdf0e10cSrcweir It is now capable of keeping contained bezier segments. 199cdf0e10cSrcweir It is also capable of delivering line and non-line portions 200cdf0e10cSrcweir depending on what target polygons You provide. This is useful 201cdf0e10cSrcweir e.g. for dashed lines with two colors. 202cdf0e10cSrcweir If the last and the first snippet in one of the results have 203cdf0e10cSrcweir a common start/end ppoint, they will be merged to achieve as 204cdf0e10cSrcweir view as needed result line snippets. This is also relevant for 205cdf0e10cSrcweir further processing the results. 206cdf0e10cSrcweir 207cdf0e10cSrcweir @param rCandidate 208cdf0e10cSrcweir The polygon based on which the snippets will be created. 209cdf0e10cSrcweir 210cdf0e10cSrcweir @param rDotDashArray 211cdf0e10cSrcweir The line pattern given as array of length values 212cdf0e10cSrcweir 213cdf0e10cSrcweir @param pLineTarget 214cdf0e10cSrcweir The target for line snippets, e.g. the first entry will be 215cdf0e10cSrcweir a line segment with length rDotDashArray[0]. The given 216cdf0e10cSrcweir polygon will be emptied as preparation. 217cdf0e10cSrcweir 218cdf0e10cSrcweir @param pGapTarget 219cdf0e10cSrcweir The target for gap snippets, e.g. the first entry will be 220cdf0e10cSrcweir a line segment with length rDotDashArray[1]. The given 221cdf0e10cSrcweir polygon will be emptied as preparation. 222cdf0e10cSrcweir 223cdf0e10cSrcweir @param fFullDashDotLen 224cdf0e10cSrcweir The sumed-up length of the rDotDashArray. If zero, it will 225cdf0e10cSrcweir be calculated internally. 226cdf0e10cSrcweir */ 227*b63233d8Sdamjan BASEGFX_DLLPUBLIC void applyLineDashing( 228cdf0e10cSrcweir const B2DPolygon& rCandidate, 229cdf0e10cSrcweir const ::std::vector<double>& rDotDashArray, 230cdf0e10cSrcweir B2DPolyPolygon* pLineTarget, 231*b63233d8Sdamjan B2DPolyPolygon* pGapTarget = 0, 232cdf0e10cSrcweir double fFullDashDotLen = 0.0); 233cdf0e10cSrcweir 234cdf0e10cSrcweir // test if point is inside epsilon-range around an edge defined 235cdf0e10cSrcweir // by the two given points. Can be used for HitTesting. The epsilon-range 236cdf0e10cSrcweir // is defined to be the rectangle centered to the given edge, using height 237cdf0e10cSrcweir // 2 x fDistance, and the circle around both points with radius fDistance. 238*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isInEpsilonRange(const B2DPoint& rEdgeStart, const B2DPoint& rEdgeEnd, const B2DPoint& rTestPosition, double fDistance); 239cdf0e10cSrcweir 240cdf0e10cSrcweir // test if point is inside epsilon-range around the given Polygon. Can be used 241cdf0e10cSrcweir // for HitTesting. The epsilon-range is defined to be the rectangle centered 242cdf0e10cSrcweir // to the given edge, using height 2 x fDistance, and the circle around both points 243cdf0e10cSrcweir // with radius fDistance. 244*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isInEpsilonRange(const B2DPolygon& rCandidate, const B2DPoint& rTestPosition, double fDistance); 245cdf0e10cSrcweir 246cdf0e10cSrcweir /** Create a polygon from a rectangle. 247cdf0e10cSrcweir 248cdf0e10cSrcweir @param rRect 249cdf0e10cSrcweir The rectangle which describes the polygon size 250cdf0e10cSrcweir 251cdf0e10cSrcweir @param fRadius 252cdf0e10cSrcweir Radius of the edge rounding, relative to the rectangle size. 0.0 means no 253cdf0e10cSrcweir rounding, 1.0 will lead to an ellipse 254cdf0e10cSrcweir */ 255*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromRect( const B2DRectangle& rRect, double fRadius ); 256cdf0e10cSrcweir 257cdf0e10cSrcweir /** Create a polygon from a rectangle. 258cdf0e10cSrcweir 259cdf0e10cSrcweir @param rRect 260cdf0e10cSrcweir The rectangle which describes the polygon size 261cdf0e10cSrcweir 262cdf0e10cSrcweir @param fRadiusX 263cdf0e10cSrcweir @param fRadiusY 264cdf0e10cSrcweir Radius of the edge rounding, relative to the rectangle size. 0.0 means no 265cdf0e10cSrcweir rounding, 1.0 will lead to an ellipse 266cdf0e10cSrcweir */ 267*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromRect( const B2DRectangle& rRect, double fRadiusX, double fRadiusY ); 268cdf0e10cSrcweir 269cdf0e10cSrcweir /** Create a polygon from a rectangle. 270cdf0e10cSrcweir */ 271*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromRect( const B2DRectangle& rRect ); 272cdf0e10cSrcweir 273cdf0e10cSrcweir /** Create the unit polygon 274cdf0e10cSrcweir */ 275*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createUnitPolygon(); 276cdf0e10cSrcweir 277cdf0e10cSrcweir /** Create a circle polygon with given radius. 278cdf0e10cSrcweir 279cdf0e10cSrcweir This method creates a circle approximation consisting of 280cdf0e10cSrcweir four cubic bezier segments, which approximate the given 281cdf0e10cSrcweir circle with an error of less than 0.5 percent. 282cdf0e10cSrcweir 283cdf0e10cSrcweir @param rCenter 284cdf0e10cSrcweir Center point of the circle 285cdf0e10cSrcweir 286cdf0e10cSrcweir @param fRadius 287cdf0e10cSrcweir Radius of the circle 288cdf0e10cSrcweir */ 289*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromCircle( const B2DPoint& rCenter, double fRadius ); 290cdf0e10cSrcweir 2915aaf853bSArmin Le Grand /// create half circle centered on (0,0) from [0 .. F_PI] 2925aaf853bSArmin Le Grand B2DPolygon createHalfUnitCircle(); 2935aaf853bSArmin Le Grand 294cdf0e10cSrcweir /** create a polygon which describes the unit circle and close it 295cdf0e10cSrcweir 296cdf0e10cSrcweir @param nStartQuadrant 297cdf0e10cSrcweir To be able to rebuild the old behaviour where the circles started at bottom, 298cdf0e10cSrcweir this parameter is used. Default is 0 which is the first quadrant and the 299cdf0e10cSrcweir polygon's start point will be the rightmost one. When using e.g. 1, the 300cdf0e10cSrcweir first created quadrant will start at the YMax-position (with Y down on screens, 301cdf0e10cSrcweir this is the lowest one). This is needed since when lines are dashed, toe old 302cdf0e10cSrcweir geometry started at bottom point, else it would look different. 303cdf0e10cSrcweir */ 304*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromUnitCircle(sal_uInt32 nStartQuadrant = 0); 305cdf0e10cSrcweir 306cdf0e10cSrcweir /** Create an ellipse polygon with given radii. 307cdf0e10cSrcweir 308cdf0e10cSrcweir This method creates an ellipse approximation consisting of 309cdf0e10cSrcweir four cubic bezier segments, which approximate the given 310cdf0e10cSrcweir ellipse with an error of less than 0.5 percent. 311cdf0e10cSrcweir 312cdf0e10cSrcweir @param rCenter 313cdf0e10cSrcweir Center point of the circle 314cdf0e10cSrcweir 315cdf0e10cSrcweir @param fRadiusX 316cdf0e10cSrcweir Radius of the ellipse in X direction 317cdf0e10cSrcweir 318cdf0e10cSrcweir @param fRadiusY 319cdf0e10cSrcweir Radius of the ellipse in Y direction 320cdf0e10cSrcweir */ 321*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromEllipse( const B2DPoint& rCenter, double fRadiusX, double fRadiusY ); 322cdf0e10cSrcweir 323cdf0e10cSrcweir /** Create an unit ellipse polygon with the given angles, from start to end 324cdf0e10cSrcweir */ 325*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromEllipseSegment( const B2DPoint& rCenter, double fRadiusX, double fRadiusY, double fStart, double fEnd ); 326cdf0e10cSrcweir 327*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createPolygonFromUnitEllipseSegment( double fStart, double fEnd ); 328cdf0e10cSrcweir 329cdf0e10cSrcweir /** Predicate whether a given polygon is a rectangle. 330cdf0e10cSrcweir 331cdf0e10cSrcweir @param rPoly 332cdf0e10cSrcweir Polygon to check 333cdf0e10cSrcweir 334cdf0e10cSrcweir @return true, if the polygon describes a rectangle 335cdf0e10cSrcweir (polygon is closed, and the points are either cw or ccw 336cdf0e10cSrcweir enumerations of a rectangle's vertices). Note that 337cdf0e10cSrcweir intermediate points and duplicate points are ignored. 338cdf0e10cSrcweir */ 339*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isRectangle( const B2DPolygon& rPoly ); 340cdf0e10cSrcweir 341cdf0e10cSrcweir // create 3d polygon from given 2d polygon. The given fZCoordinate is used to expand the 342cdf0e10cSrcweir // third coordinate. 343*b63233d8Sdamjan BASEGFX_DLLPUBLIC B3DPolygon createB3DPolygonFromB2DPolygon(const B2DPolygon& rCandidate, double fZCoordinate = 0.0); 344cdf0e10cSrcweir 345cdf0e10cSrcweir // create 2d PolyPolygon from given 3d PolyPolygon. All coordinates are transformed using the given 346cdf0e10cSrcweir // matrix and the resulting x,y is used to form the new polygon. 347*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createB2DPolygonFromB3DPolygon(const B3DPolygon& rCandidate, const B3DHomMatrix& rMat); 348cdf0e10cSrcweir 349cdf0e10cSrcweir // create simplified version of the original polygon by 350cdf0e10cSrcweir // replacing segments with spikes/loops and self intersections 351cdf0e10cSrcweir // by several trivial sub-segments 352*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createSimplifiedPolygon(const B2DPolygon&); 353cdf0e10cSrcweir 354cdf0e10cSrcweir // calculate the distance to the given endless ray and return. The relative position on the edge is returned in Cut. 355cdf0e10cSrcweir // That position may be less than 0.0 or more than 1.0 356*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getDistancePointToEndlessRay(const B2DPoint& rPointA, const B2DPoint& rPointB, const B2DPoint& rTestPoint, double& rCut); 357cdf0e10cSrcweir 358cdf0e10cSrcweir // calculate the smallest distance to given edge and return. The relative position on the edge is returned in Cut. 359cdf0e10cSrcweir // That position is in the range [0.0 .. 1.0] and the returned distance is adapted accordingly to the start or end 360cdf0e10cSrcweir // point of the edge 361*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getSmallestDistancePointToEdge(const B2DPoint& rPointA, const B2DPoint& rPointB, const B2DPoint& rTestPoint, double& rCut); 362cdf0e10cSrcweir 363cdf0e10cSrcweir // for each contained edge calculate the smallest distance. Return the index to the smallest 364cdf0e10cSrcweir // edge in rEdgeIndex. The relative position on the edge is returned in rCut. 365cdf0e10cSrcweir // If nothing was found (e.g. empty input plygon), DBL_MAX is returned. 366*b63233d8Sdamjan BASEGFX_DLLPUBLIC double getSmallestDistancePointToPolygon(const B2DPolygon& rCandidate, const B2DPoint& rTestPoint, sal_uInt32& rEdgeIndex, double& rCut); 367cdf0e10cSrcweir 368cdf0e10cSrcweir // distort single point. rOriginal describes the original range, where the given points describe the distorted corresponding points. 369*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPoint distort(const B2DPoint& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight); 370cdf0e10cSrcweir 371cdf0e10cSrcweir // distort polygon. rOriginal describes the original range, where the given points describe the distorted corresponding points. 372*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon distort(const B2DPolygon& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight); 373cdf0e10cSrcweir 374cdf0e10cSrcweir // rotate polygon around given point with given angle. 375*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon rotateAroundPoint(const B2DPolygon& rCandidate, const B2DPoint& rCenter, double fAngle); 376cdf0e10cSrcweir 377cdf0e10cSrcweir // expand all segments (which are not yet) to curve segments. This is done with setting the control 378cdf0e10cSrcweir // vectors on the 1/3 resp. 2/3 distances on each segment. 379*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon expandToCurve(const B2DPolygon& rCandidate); 380cdf0e10cSrcweir 381cdf0e10cSrcweir // expand given segment to curve segment. This is done with setting the control 382cdf0e10cSrcweir // vectors on the 1/3 resp. 2/3 distances. The return value describes if a change took place. 383*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool expandToCurveInPoint(B2DPolygon& rCandidate, sal_uInt32 nIndex); 384cdf0e10cSrcweir 385cdf0e10cSrcweir // set continuity for the whole curve. If not a curve, nothing will change. Non-curve points are not changed, too. 386*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon setContinuity(const B2DPolygon& rCandidate, B2VectorContinuity eContinuity); 387cdf0e10cSrcweir 388cdf0e10cSrcweir // set continuity for given index. If not a curve, nothing will change. Non-curve points are not changed, too. 389cdf0e10cSrcweir // The return value describes if a change took place. 390*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool setContinuityInPoint(B2DPolygon& rCandidate, sal_uInt32 nIndex, B2VectorContinuity eContinuity); 391cdf0e10cSrcweir 392cdf0e10cSrcweir // test if polygon contains neutral points. A neutral point is one whos orientation is neutral 393cdf0e10cSrcweir // e.g. positioned on the edge of it's predecessor and successor 394*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool hasNeutralPoints(const B2DPolygon& rCandidate); 395cdf0e10cSrcweir 396cdf0e10cSrcweir // remove neutral points. A neutral point is one whos orientation is neutral 397cdf0e10cSrcweir // e.g. positioned on the edge of it's predecessor and successor 398*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon removeNeutralPoints(const B2DPolygon& rCandidate); 399cdf0e10cSrcweir 400cdf0e10cSrcweir // tests if polygon is convex 401*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isConvex(const B2DPolygon& rCandidate); 402cdf0e10cSrcweir 403cdf0e10cSrcweir // calculates the orientation at edge nIndex 404*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2VectorOrientation getOrientationForIndex(const B2DPolygon& rCandidate, sal_uInt32 nIndex); 405cdf0e10cSrcweir 406cdf0e10cSrcweir // calculates if given point is on given line, taking care of the numerical epsilon 407*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isPointOnLine(const B2DPoint& rStart, const B2DPoint& rEnd, const B2DPoint& rCandidate, bool bWithPoints = false); 408cdf0e10cSrcweir 409cdf0e10cSrcweir // calculates if given point is on given polygon, taking care of the numerical epsilon. Uses 410cdf0e10cSrcweir // isPointOnLine internally 411*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isPointOnPolygon(const B2DPolygon& rCandidate, const B2DPoint& rPoint, bool bWithPoints = true); 412cdf0e10cSrcweir 413cdf0e10cSrcweir // test if candidate is inside triangle 414*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isPointInTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC, const B2DPoint& rCandidate, bool bWithBorder = false); 415cdf0e10cSrcweir 416cdf0e10cSrcweir // test if candidateA and candidateB are on the same side of the given line 417*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool arePointsOnSameSideOfLine(const B2DPoint& rStart, const B2DPoint& rEnd, const B2DPoint& rCandidateA, const B2DPoint& rCandidateB, bool bWithLine = false); 418cdf0e10cSrcweir 419cdf0e10cSrcweir // add triangles for given rCandidate to rTarget. For each triangle, 3 points will be added to rCandidate. 420cdf0e10cSrcweir // All triangles will go from the start point of rCandidate to two consecutive points, building (rCandidate.count() - 2) 421cdf0e10cSrcweir // triangles. 422*b63233d8Sdamjan BASEGFX_DLLPUBLIC void addTriangleFan(const B2DPolygon& rCandidate, B2DPolygon& rTarget); 423cdf0e10cSrcweir 424cdf0e10cSrcweir // grow for polygon. Move all geometry in each point in the direction of the normal in that point 425cdf0e10cSrcweir // with the given amount. Value may be negative. 426*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon growInNormalDirection(const B2DPolygon& rCandidate, double fValue); 427cdf0e10cSrcweir 428cdf0e10cSrcweir // force all sub-polygons to a point count of nSegments 429*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon reSegmentPolygon(const B2DPolygon& rCandidate, sal_uInt32 nSegments); 430cdf0e10cSrcweir 431cdf0e10cSrcweir // create polygon state at t from 0.0 to 1.0 between the two polygons. Both polygons must have the same 432cdf0e10cSrcweir // organisation, e.g. same amount of points 433*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon interpolate(const B2DPolygon& rOld1, const B2DPolygon& rOld2, double t); 434cdf0e10cSrcweir 435*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool isPolyPolygonEqualRectangle( const B2DPolyPolygon& rPolyPoly, const B2DRange& rRect ); 436cdf0e10cSrcweir 437cdf0e10cSrcweir // #i76891# Try to remove existing curve segments if they are simply edges 438*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon simplifyCurveSegments(const B2DPolygon& rCandidate); 439cdf0e10cSrcweir 440cdf0e10cSrcweir // makes the given indexed point the new polygon start point. To do that, the points in the 441cdf0e10cSrcweir // polygon will be rotated. This is only valid for closed polygons, for non-closed ones 442cdf0e10cSrcweir // an assertion will be triggered 443*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon makeStartPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndexOfNewStatPoint); 444cdf0e10cSrcweir 445cdf0e10cSrcweir /** create edges of given length along given B2DPolygon 446cdf0e10cSrcweir 447cdf0e10cSrcweir @param rCandidate 448cdf0e10cSrcweir The polygon to move along. Points at the given polygon are created, starting 449cdf0e10cSrcweir at position fStart and stopping at less or equal to fEnd. The closed state is 450cdf0e10cSrcweir preserved. 451cdf0e10cSrcweir The polygon is subdivided if curve segments are included. That subdivision is the base 452cdf0e10cSrcweir for the newly created points. 453cdf0e10cSrcweir If the source is closed, the indirectly existing last edge may NOT have the 454cdf0e10cSrcweir given length. 455cdf0e10cSrcweir If the source is open, all edges will have the given length. You may use the last 456cdf0e10cSrcweir point of the original when You want to add the last edge Yourself. 457cdf0e10cSrcweir 458cdf0e10cSrcweir @param fLength 459cdf0e10cSrcweir The length of the created edges. If less or equal zero, an empty polygon is returned. 460cdf0e10cSrcweir 461cdf0e10cSrcweir @param fStart 462cdf0e10cSrcweir The start distance for the first to be generated point. Use 0.0 to get the 463cdf0e10cSrcweir original start point. Negative values are truncated to 0.0. 464cdf0e10cSrcweir 465cdf0e10cSrcweir @param fEnd 466cdf0e10cSrcweir The maximum distance for the last point. No more points behind this distance will be created. 46707a3d7f1SPedro Giffuni Use 0.0 to process the whole polygon. Negative values are truncated to 0.0. It also 468cdf0e10cSrcweir needs to be more or equal to fStart, else it is truncated to fStart. 469cdf0e10cSrcweir 470cdf0e10cSrcweir @return 471cdf0e10cSrcweir The newly created polygon 472cdf0e10cSrcweir */ 473*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createEdgesOfGivenLength(const B2DPolygon& rCandidate, double fLength, double fStart = 0.0, double fEnd = 0.0); 474cdf0e10cSrcweir 475cdf0e10cSrcweir /** Create Waveline along given polygon 476cdf0e10cSrcweir The implementation is based on createEdgesOfGivenLength and creates a curve 477cdf0e10cSrcweir segment with the given dimensions for each created line segment. The polygon 478cdf0e10cSrcweir is treated as if opened (closed state will be ignored) and only for whole 479cdf0e10cSrcweir edges a curve segment will be created (no rest handling) 480cdf0e10cSrcweir 481cdf0e10cSrcweir @param rCandidate 482cdf0e10cSrcweir The polygon along which the waveline will be created 483cdf0e10cSrcweir 484cdf0e10cSrcweir @param fWaveWidth 485cdf0e10cSrcweir The length of a single waveline curve segment 486cdf0e10cSrcweir 487cdf0e10cSrcweir @param fgWaveHeight 488cdf0e10cSrcweir The height of the waveline (amplitude) 489cdf0e10cSrcweir */ 490*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon createWaveline(const B2DPolygon& rCandidate, double fWaveWidth, double fWaveHeight); 491cdf0e10cSrcweir 492cdf0e10cSrcweir /** split each edge of a polygon in exactly nSubEdges equidistant edges 493cdf0e10cSrcweir 494cdf0e10cSrcweir @param rCandidate 495cdf0e10cSrcweir The source polygon. If too small (no edges), nSubEdges too small (<2) 496cdf0e10cSrcweir or neither bHandleCurvedEdgesnor bHandleStraightEdges it will just be returned. 497cdf0e10cSrcweir Else for each edge nSubEdges will be created. Closed state is preserved. 498cdf0e10cSrcweir 499cdf0e10cSrcweir @param nSubEdges 500cdf0e10cSrcweir How many edges shall be created as replacement for each single edge 501cdf0e10cSrcweir 502cdf0e10cSrcweir @param bHandleCurvedEdges 503cdf0e10cSrcweir Process curved edges or not. If to handle the curved edges will be splitted 504cdf0e10cSrcweir into nSubEdges part curved edges of equidistant bezier distances. If not, 505cdf0e10cSrcweir curved edges will just be copied. 506cdf0e10cSrcweir 507cdf0e10cSrcweir @param bHandleStraightEdges 508cdf0e10cSrcweir Process straight edges or not. If to handle the straight edges will be splitted 509cdf0e10cSrcweir into nSubEdges part curved edges of equidistant length. If not, 510cdf0e10cSrcweir straight edges will just be copied. 511cdf0e10cSrcweir */ 512*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon reSegmentPolygonEdges(const B2DPolygon& rCandidate, sal_uInt32 nSubEdges, bool bHandleCurvedEdges, bool bHandleStraightEdges); 513cdf0e10cSrcweir 514cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////// 515cdf0e10cSrcweir // comparators with tolerance for 2D Polygons 516*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool equal(const B2DPolygon& rCandidateA, const B2DPolygon& rCandidateB, const double& rfSmallValue); 517*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool equal(const B2DPolygon& rCandidateA, const B2DPolygon& rCandidateB); 518cdf0e10cSrcweir 519cdf0e10cSrcweir /** snap some polygon coordinates to discrete coordinates 520cdf0e10cSrcweir 521cdf0e10cSrcweir This method allows to snap some polygon points to discrete (integer) values 522cdf0e10cSrcweir which equals e.g. a snap to discrete coordinates. It will snap points of 523cdf0e10cSrcweir horizontal and vertical edges 524cdf0e10cSrcweir 525cdf0e10cSrcweir @param rCandidate 526cdf0e10cSrcweir The source polygon 527cdf0e10cSrcweir 528cdf0e10cSrcweir @return 529cdf0e10cSrcweir The modified version of the source polygon 530cdf0e10cSrcweir */ 531*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon snapPointsOfHorizontalOrVerticalEdges(const B2DPolygon& rCandidate); 532cdf0e10cSrcweir 533d8ed516eSArmin Le Grand /** returns true if the Polygon only contains horizontal or vertical edges 534d8ed516eSArmin Le Grand so that it could be represented by RegionBands 535d8ed516eSArmin Le Grand */ 536*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool containsOnlyHorizontalAndVerticalEdges(const B2DPolygon& rCandidate); 537d8ed516eSArmin Le Grand 538facb0eb6SArmin Le Grand /// get the tangent with which the given point is entered seen from the previous 539facb0eb6SArmin Le Grand /// polygon path data. Take into account all stuff like closed state, zero-length edges and others. 540*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DVector getTangentEnteringPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndex); 541facb0eb6SArmin Le Grand 542facb0eb6SArmin Le Grand /// get the tangent with which the given point is left seen from the following 543facb0eb6SArmin Le Grand /// polygon path data. Take into account all stuff like closed state, zero-length edges and others. 544*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DVector getTangentLeavingPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndex); 545facb0eb6SArmin Le Grand 5461f882ec4SArmin Le Grand /// converters for com::sun::star::drawing::PointSequence 547*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon UnoPointSequenceToB2DPolygon( 5481f882ec4SArmin Le Grand const com::sun::star::drawing::PointSequence& rPointSequenceSource, 5491f882ec4SArmin Le Grand bool bCheckClosed = true); 550*b63233d8Sdamjan BASEGFX_DLLPUBLIC void B2DPolygonToUnoPointSequence( 5511f882ec4SArmin Le Grand const B2DPolygon& rPolygon, 5521f882ec4SArmin Le Grand com::sun::star::drawing::PointSequence& rPointSequenceRetval); 5531f882ec4SArmin Le Grand 5541f882ec4SArmin Le Grand /* converters for com::sun::star::drawing::PointSequence and 5551f882ec4SArmin Le Grand com::sun::star::drawing::FlagSequence to B2DPolygon (curved polygons) 5561f882ec4SArmin Le Grand */ 557*b63233d8Sdamjan BASEGFX_DLLPUBLIC B2DPolygon UnoPolygonBezierCoordsToB2DPolygon( 5581f882ec4SArmin Le Grand const com::sun::star::drawing::PointSequence& rPointSequenceSource, 5591f882ec4SArmin Le Grand const com::sun::star::drawing::FlagSequence& rFlagSequenceSource, 5601f882ec4SArmin Le Grand bool bCheckClosed = true); 561*b63233d8Sdamjan BASEGFX_DLLPUBLIC void B2DPolygonToUnoPolygonBezierCoords( 5621f882ec4SArmin Le Grand const B2DPolygon& rPolyPolygon, 5631f882ec4SArmin Le Grand com::sun::star::drawing::PointSequence& rPointSequenceRetval, 5641f882ec4SArmin Le Grand com::sun::star::drawing::FlagSequence& rFlagSequenceRetval); 5651f882ec4SArmin Le Grand 5661f882ec4SArmin Le Grand /** Read poly-polygon from SVG. 5671f882ec4SArmin Le Grand 5681f882ec4SArmin Le Grand This function imports a poly-polygon from an SVG points 5691f882ec4SArmin Le Grand attribute (a plain list of coordinate pairs). 5701f882ec4SArmin Le Grand 5711f882ec4SArmin Le Grand @param o_rPoly 5721f882ec4SArmin Le Grand The output polygon. Note that svg:points can only define a 5731f882ec4SArmin Le Grand single polygon 5741f882ec4SArmin Le Grand 5751f882ec4SArmin Le Grand @param rSvgPointsAttribute 5761f882ec4SArmin Le Grand A valid SVG points attribute string 5771f882ec4SArmin Le Grand 5781f882ec4SArmin Le Grand @return true, if the string was successfully parsed 5791f882ec4SArmin Le Grand */ 580*b63233d8Sdamjan BASEGFX_DLLPUBLIC bool importFromSvgPoints( B2DPolygon& o_rPoly, 5811f882ec4SArmin Le Grand const ::rtl::OUString& rSvgPointsAttribute ); 5821f882ec4SArmin Le Grand 5831f882ec4SArmin Le Grand /** Write poly-polygon to SVG. 5841f882ec4SArmin Le Grand 5851f882ec4SArmin Le Grand This function imports a non-bezier polygon to SVG points 5861f882ec4SArmin Le Grand (a plain list of coordinate pairs). 5871f882ec4SArmin Le Grand 5881f882ec4SArmin Le Grand @param rPoly 5891f882ec4SArmin Le Grand The polygon to export 5901f882ec4SArmin Le Grand 5911f882ec4SArmin Le Grand @param rSvgPointsAttribute 5921f882ec4SArmin Le Grand A valid SVG points attribute string 5931f882ec4SArmin Le Grand 5941f882ec4SArmin Le Grand @return true, if the string was successfully parsed 5951f882ec4SArmin Le Grand */ 596*b63233d8Sdamjan BASEGFX_DLLPUBLIC ::rtl::OUString exportToSvgPoints( const B2DPolygon& rPoly ); 5971f882ec4SArmin Le Grand 598cdf0e10cSrcweir } // end of namespace tools 599cdf0e10cSrcweir } // end of namespace basegfx 600cdf0e10cSrcweir 601cdf0e10cSrcweir #endif /* _BGFX_POLYGON_B2DPOLYGONTOOLS_HXX */ 602