xref: /trunk/main/basegfx/source/polygon/b2dpolygon.cxx (revision cea84f56765a2ac06f0f5a2718f45f6430edc781)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basegfx.hxx"
26 #include <osl/diagnose.h>
27 #include <basegfx/polygon/b2dpolygon.hxx>
28 #include <basegfx/point/b2dpoint.hxx>
29 #include <basegfx/vector/b2dvector.hxx>
30 #include <basegfx/matrix/b2dhommatrix.hxx>
31 #include <basegfx/curve/b2dcubicbezier.hxx>
32 #include <rtl/instance.hxx>
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <boost/scoped_ptr.hpp>
35 #include <vector>
36 #include <algorithm>
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 struct CoordinateData2D : public basegfx::B2DPoint
41 {
42 public:
CoordinateData2DCoordinateData2D43     CoordinateData2D() {}
44 
CoordinateData2DCoordinateData2D45     explicit CoordinateData2D(const basegfx::B2DPoint& rData)
46     :   B2DPoint(rData)
47     {}
48 
operator =CoordinateData2D49     CoordinateData2D& operator=(const basegfx::B2DPoint& rData)
50     {
51         B2DPoint::operator=(rData);
52         return *this;
53     }
54 
transformCoordinateData2D55     void transform(const basegfx::B2DHomMatrix& rMatrix)
56     {
57         *this *= rMatrix;
58     }
59 };
60 
61 //////////////////////////////////////////////////////////////////////////////
62 
63 class CoordinateDataArray2D
64 {
65     typedef ::std::vector< CoordinateData2D > CoordinateData2DVector;
66 
67     CoordinateData2DVector                          maVector;
68 
69 public:
CoordinateDataArray2D(sal_uInt32 nCount)70     explicit CoordinateDataArray2D(sal_uInt32 nCount)
71     :   maVector(nCount)
72     {
73     }
74 
CoordinateDataArray2D(const CoordinateDataArray2D & rOriginal)75     explicit CoordinateDataArray2D(const CoordinateDataArray2D& rOriginal)
76     :   maVector(rOriginal.maVector)
77     {
78     }
79     CoordinateDataArray2D& operator=(const CoordinateDataArray2D&) = default;
80 
CoordinateDataArray2D(const CoordinateDataArray2D & rOriginal,sal_uInt32 nIndex,sal_uInt32 nCount)81     CoordinateDataArray2D(const CoordinateDataArray2D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
82     :   maVector(rOriginal.maVector.begin() + nIndex, rOriginal.maVector.begin() + (nIndex + nCount))
83     {
84     }
85 
count() const86     sal_uInt32 count() const
87     {
88         return maVector.size();
89     }
90 
operator ==(const CoordinateDataArray2D & rCandidate) const91     bool operator==(const CoordinateDataArray2D& rCandidate) const
92     {
93         return (maVector == rCandidate.maVector);
94     }
95 
getCoordinate(sal_uInt32 nIndex) const96     const basegfx::B2DPoint& getCoordinate(sal_uInt32 nIndex) const
97     {
98         return maVector[nIndex];
99     }
100 
setCoordinate(sal_uInt32 nIndex,const basegfx::B2DPoint & rValue)101     void setCoordinate(sal_uInt32 nIndex, const basegfx::B2DPoint& rValue)
102     {
103         maVector[nIndex] = rValue;
104     }
105 
reserve(sal_uInt32 nCount)106     void reserve(sal_uInt32 nCount)
107     {
108         maVector.reserve(nCount);
109     }
110 
append(const CoordinateData2D & rValue)111     void append(const CoordinateData2D& rValue)
112     {
113         maVector.push_back(rValue);
114     }
115 
insert(sal_uInt32 nIndex,const CoordinateData2D & rValue,sal_uInt32 nCount)116     void insert(sal_uInt32 nIndex, const CoordinateData2D& rValue, sal_uInt32 nCount)
117     {
118         if(nCount)
119         {
120             // add nCount copies of rValue
121             CoordinateData2DVector::iterator aIndex(maVector.begin());
122             aIndex += nIndex;
123             maVector.insert(aIndex, nCount, rValue);
124         }
125     }
126 
insert(sal_uInt32 nIndex,const CoordinateDataArray2D & rSource)127     void insert(sal_uInt32 nIndex, const CoordinateDataArray2D& rSource)
128     {
129         const sal_uInt32 nCount(rSource.maVector.size());
130 
131         if(nCount)
132         {
133             // insert data
134             CoordinateData2DVector::iterator aIndex(maVector.begin());
135             aIndex += nIndex;
136             CoordinateData2DVector::const_iterator aStart(rSource.maVector.begin());
137             CoordinateData2DVector::const_iterator aEnd(rSource.maVector.end());
138             maVector.insert(aIndex, aStart, aEnd);
139         }
140     }
141 
remove(sal_uInt32 nIndex,sal_uInt32 nCount)142     void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
143     {
144         if(nCount)
145         {
146             // remove point data
147             CoordinateData2DVector::iterator aStart(maVector.begin());
148             aStart += nIndex;
149             const CoordinateData2DVector::iterator aEnd(aStart + nCount);
150             maVector.erase(aStart, aEnd);
151         }
152     }
153 
flip(bool bIsClosed)154     void flip(bool bIsClosed)
155     {
156         if(maVector.size() > 1)
157         {
158             // to keep the same point at index 0, just flip all points except the
159             // first one when closed
160             const sal_uInt32 nHalfSize(bIsClosed ? (maVector.size() - 1) >> 1 : maVector.size() >> 1);
161             CoordinateData2DVector::iterator aStart(bIsClosed ? maVector.begin() + 1 : maVector.begin());
162             CoordinateData2DVector::iterator aEnd(maVector.end() - 1);
163 
164             for(sal_uInt32 a(0); a < nHalfSize; a++)
165             {
166                 ::std::swap(*aStart, *aEnd);
167                 aStart++;
168                 aEnd--;
169             }
170         }
171     }
172 
removeDoublePointsAtBeginEnd()173     void removeDoublePointsAtBeginEnd()
174     {
175         // remove from end as long as there are at least two points
176         // and begin/end are equal
177         while((maVector.size() > 1) && (maVector[0] == maVector[maVector.size() - 1]))
178         {
179             maVector.pop_back();
180         }
181     }
182 
removeDoublePointsWholeTrack()183     void removeDoublePointsWholeTrack()
184     {
185         sal_uInt32 nIndex(0);
186 
187         // test as long as there are at least two points and as long as the index
188         // is smaller or equal second last point
189         while((maVector.size() > 1) && (nIndex <= maVector.size() - 2))
190         {
191             if(maVector[nIndex] == maVector[nIndex + 1])
192             {
193                 // if next is same as index, delete next
194                 maVector.erase(maVector.begin() + (nIndex + 1));
195             }
196             else
197             {
198                 // if different, step forward
199                 nIndex++;
200             }
201         }
202     }
203 
transform(const basegfx::B2DHomMatrix & rMatrix)204     void transform(const basegfx::B2DHomMatrix& rMatrix)
205     {
206         CoordinateData2DVector::iterator aStart(maVector.begin());
207         CoordinateData2DVector::iterator aEnd(maVector.end());
208 
209         for(; aStart != aEnd; aStart++)
210         {
211             aStart->transform(rMatrix);
212         }
213     }
214 
begin() const215     const basegfx::B2DPoint* begin() const
216     {
217         if(maVector.empty())
218             return 0;
219         else
220             return &maVector.front();
221     }
222 
end() const223     const basegfx::B2DPoint* end() const
224     {
225         if(maVector.empty())
226             return 0;
227         else
228             return (&maVector.back())+1;
229     }
230 
begin()231     basegfx::B2DPoint* begin()
232     {
233         if(maVector.empty())
234             return 0;
235         else
236             return &maVector.front();
237     }
238 
end()239     basegfx::B2DPoint* end()
240     {
241         if(maVector.empty())
242             return 0;
243         else
244             return (&maVector.back())+1;
245     }
246 };
247 
248 //////////////////////////////////////////////////////////////////////////////
249 
250 class ControlVectorPair2D
251 {
252     basegfx::B2DVector                          maPrevVector;
253     basegfx::B2DVector                          maNextVector;
254 
255 public:
ControlVectorPair2D()256     explicit ControlVectorPair2D () { }
257 
getPrevVector() const258     const basegfx::B2DVector& getPrevVector() const
259     {
260         return maPrevVector;
261     }
262 
setPrevVector(const basegfx::B2DVector & rValue)263     void setPrevVector(const basegfx::B2DVector& rValue)
264     {
265         if(rValue != maPrevVector)
266             maPrevVector = rValue;
267     }
268 
getNextVector() const269     const basegfx::B2DVector& getNextVector() const
270     {
271         return maNextVector;
272     }
273 
setNextVector(const basegfx::B2DVector & rValue)274     void setNextVector(const basegfx::B2DVector& rValue)
275     {
276         if(rValue != maNextVector)
277             maNextVector = rValue;
278     }
279 
operator ==(const ControlVectorPair2D & rData) const280     bool operator==(const ControlVectorPair2D& rData) const
281     {
282         return (maPrevVector == rData.getPrevVector() && maNextVector == rData.getNextVector());
283     }
284 
flip()285     void flip()
286     {
287         ::std::swap(maPrevVector, maNextVector);
288     }
289 };
290 
291 //////////////////////////////////////////////////////////////////////////////
292 
293 class ControlVectorArray2D
294 {
295     typedef ::std::vector< ControlVectorPair2D > ControlVectorPair2DVector;
296 
297     ControlVectorPair2DVector                           maVector;
298     sal_uInt32                                          mnUsedVectors;
299 
300 public:
ControlVectorArray2D(sal_uInt32 nCount)301     explicit ControlVectorArray2D(sal_uInt32 nCount)
302     :   maVector(nCount),
303         mnUsedVectors(0)
304     {}
305 
ControlVectorArray2D(const ControlVectorArray2D & rOriginal,sal_uInt32 nIndex,sal_uInt32 nCount)306     ControlVectorArray2D(const ControlVectorArray2D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
307     :   maVector(),
308         mnUsedVectors(0)
309     {
310         ControlVectorPair2DVector::const_iterator aStart(rOriginal.maVector.begin());
311         aStart += nIndex;
312         ControlVectorPair2DVector::const_iterator aEnd(aStart);
313         aEnd += nCount;
314         maVector.reserve(nCount);
315 
316         for(; aStart != aEnd; aStart++)
317         {
318             if(!aStart->getPrevVector().equalZero())
319                 mnUsedVectors++;
320 
321             if(!aStart->getNextVector().equalZero())
322                 mnUsedVectors++;
323 
324             maVector.push_back(*aStart);
325         }
326     }
327 
count() const328     sal_uInt32 count() const
329     {
330         return maVector.size();
331     }
332 
operator ==(const ControlVectorArray2D & rCandidate) const333     bool operator==(const ControlVectorArray2D& rCandidate) const
334     {
335         return (maVector == rCandidate.maVector);
336     }
337 
isUsed() const338     bool isUsed() const
339     {
340         return (0 != mnUsedVectors);
341     }
342 
getPrevVector(sal_uInt32 nIndex) const343     const basegfx::B2DVector& getPrevVector(sal_uInt32 nIndex) const
344     {
345         return maVector[nIndex].getPrevVector();
346     }
347 
setPrevVector(sal_uInt32 nIndex,const basegfx::B2DVector & rValue)348     void setPrevVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
349     {
350         bool bWasUsed(mnUsedVectors && !maVector[nIndex].getPrevVector().equalZero());
351         bool bIsUsed(!rValue.equalZero());
352 
353         if(bWasUsed)
354         {
355             if(bIsUsed)
356             {
357                 maVector[nIndex].setPrevVector(rValue);
358             }
359             else
360             {
361                 maVector[nIndex].setPrevVector(basegfx::B2DVector::getEmptyVector());
362                 mnUsedVectors--;
363             }
364         }
365         else
366         {
367             if(bIsUsed)
368             {
369                 maVector[nIndex].setPrevVector(rValue);
370                 mnUsedVectors++;
371             }
372         }
373     }
374 
getNextVector(sal_uInt32 nIndex) const375     const basegfx::B2DVector& getNextVector(sal_uInt32 nIndex) const
376     {
377         return maVector[nIndex].getNextVector();
378     }
379 
setNextVector(sal_uInt32 nIndex,const basegfx::B2DVector & rValue)380     void setNextVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
381     {
382         bool bWasUsed(mnUsedVectors && !maVector[nIndex].getNextVector().equalZero());
383         bool bIsUsed(!rValue.equalZero());
384 
385         if(bWasUsed)
386         {
387             if(bIsUsed)
388             {
389                 maVector[nIndex].setNextVector(rValue);
390             }
391             else
392             {
393                 maVector[nIndex].setNextVector(basegfx::B2DVector::getEmptyVector());
394                 mnUsedVectors--;
395             }
396         }
397         else
398         {
399             if(bIsUsed)
400             {
401                 maVector[nIndex].setNextVector(rValue);
402                 mnUsedVectors++;
403             }
404         }
405     }
406 
append(const ControlVectorPair2D & rValue)407     void append(const ControlVectorPair2D& rValue)
408     {
409         maVector.push_back(rValue);
410 
411         if(!rValue.getPrevVector().equalZero())
412             mnUsedVectors += 1;
413 
414         if(!rValue.getNextVector().equalZero())
415             mnUsedVectors += 1;
416     }
417 
insert(sal_uInt32 nIndex,const ControlVectorPair2D & rValue,sal_uInt32 nCount)418     void insert(sal_uInt32 nIndex, const ControlVectorPair2D& rValue, sal_uInt32 nCount)
419     {
420         if(nCount)
421         {
422             // add nCount copies of rValue
423             ControlVectorPair2DVector::iterator aIndex(maVector.begin());
424             aIndex += nIndex;
425             maVector.insert(aIndex, nCount, rValue);
426 
427             if(!rValue.getPrevVector().equalZero())
428                 mnUsedVectors += nCount;
429 
430             if(!rValue.getNextVector().equalZero())
431                 mnUsedVectors += nCount;
432         }
433     }
434 
insert(sal_uInt32 nIndex,const ControlVectorArray2D & rSource)435     void insert(sal_uInt32 nIndex, const ControlVectorArray2D& rSource)
436     {
437         const sal_uInt32 nCount(rSource.maVector.size());
438 
439         if(nCount)
440         {
441             // insert data
442             ControlVectorPair2DVector::iterator aIndex(maVector.begin());
443             aIndex += nIndex;
444             ControlVectorPair2DVector::const_iterator aStart(rSource.maVector.begin());
445             ControlVectorPair2DVector::const_iterator aEnd(rSource.maVector.end());
446             maVector.insert(aIndex, aStart, aEnd);
447 
448             for(; aStart != aEnd; aStart++)
449             {
450                 if(!aStart->getPrevVector().equalZero())
451                     mnUsedVectors++;
452 
453                 if(!aStart->getNextVector().equalZero())
454                     mnUsedVectors++;
455             }
456         }
457     }
458 
remove(sal_uInt32 nIndex,sal_uInt32 nCount)459     void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
460     {
461         if(nCount)
462         {
463             const ControlVectorPair2DVector::iterator aDeleteStart(maVector.begin() + nIndex);
464             const ControlVectorPair2DVector::iterator aDeleteEnd(aDeleteStart + nCount);
465             ControlVectorPair2DVector::const_iterator aStart(aDeleteStart);
466 
467             for(; mnUsedVectors && aStart != aDeleteEnd; aStart++)
468             {
469                 if(!aStart->getPrevVector().equalZero())
470                     mnUsedVectors--;
471 
472                 if(mnUsedVectors && !aStart->getNextVector().equalZero())
473                     mnUsedVectors--;
474             }
475 
476             // remove point data
477             maVector.erase(aDeleteStart, aDeleteEnd);
478         }
479     }
480 
flip(bool bIsClosed)481     void flip(bool bIsClosed)
482     {
483         if(maVector.size() > 1)
484         {
485             // to keep the same point at index 0, just flip all points except the
486             // first one when closed
487             const sal_uInt32 nHalfSize(bIsClosed ? (maVector.size() - 1) >> 1 : maVector.size() >> 1);
488             ControlVectorPair2DVector::iterator aStart(bIsClosed ? maVector.begin() + 1 : maVector.begin());
489             ControlVectorPair2DVector::iterator aEnd(maVector.end() - 1);
490 
491             for(sal_uInt32 a(0); a < nHalfSize; a++)
492             {
493                 // swap Prev and Next
494                 aStart->flip();
495                 aEnd->flip();
496 
497                 // swap entries
498                 ::std::swap(*aStart, *aEnd);
499 
500                 aStart++;
501                 aEnd--;
502             }
503 
504             if(aStart == aEnd)
505             {
506                 // swap Prev and Next at middle element (if exists)
507                 aStart->flip();
508             }
509 
510             if(bIsClosed)
511             {
512                 // swap Prev and Next at start element
513                 maVector.begin()->flip();
514             }
515         }
516     }
517 };
518 
519 //////////////////////////////////////////////////////////////////////////////
520 
521 class ImplBufferedData
522 {
523 private:
524     // Possibility to hold the last subdivision
525     boost::scoped_ptr< basegfx::B2DPolygon >        mpDefaultSubdivision;
526 
527     // Possibility to hold the last B2DRange calculation
528     boost::scoped_ptr< basegfx::B2DRange >          mpB2DRange;
529 
530 public:
ImplBufferedData()531     ImplBufferedData()
532     :   mpDefaultSubdivision(),
533         mpB2DRange()
534     {}
535 
getDefaultAdaptiveSubdivision(const basegfx::B2DPolygon & rSource) const536     const basegfx::B2DPolygon& getDefaultAdaptiveSubdivision(const basegfx::B2DPolygon& rSource) const
537     {
538         if(!mpDefaultSubdivision)
539         {
540             const_cast< ImplBufferedData* >(this)->mpDefaultSubdivision.reset(new basegfx::B2DPolygon(basegfx::tools::adaptiveSubdivideByCount(rSource, 9)));
541         }
542 
543         return *mpDefaultSubdivision;
544     }
545 
getB2DRange(const basegfx::B2DPolygon & rSource) const546     const basegfx::B2DRange& getB2DRange(const basegfx::B2DPolygon& rSource) const
547     {
548         if(!mpB2DRange)
549         {
550             basegfx::B2DRange aNewRange;
551             const sal_uInt32 nPointCount(rSource.count());
552 
553             if(nPointCount)
554             {
555                 for(sal_uInt32 a(0); a < nPointCount; a++)
556                 {
557                     aNewRange.expand(rSource.getB2DPoint(a));
558                 }
559 
560                 if(rSource.areControlPointsUsed())
561                 {
562                     const sal_uInt32 nEdgeCount(rSource.isClosed() ? nPointCount : nPointCount - 1);
563 
564                     if(nEdgeCount)
565                     {
566                         basegfx::B2DCubicBezier aEdge;
567                         aEdge.setStartPoint(rSource.getB2DPoint(0));
568 
569                         for(sal_uInt32 b(0); b < nEdgeCount; b++)
570                         {
571                             const sal_uInt32 nNextIndex((b + 1) % nPointCount);
572                             aEdge.setControlPointA(rSource.getNextControlPoint(b));
573                             aEdge.setControlPointB(rSource.getPrevControlPoint(nNextIndex));
574                             aEdge.setEndPoint(rSource.getB2DPoint(nNextIndex));
575 
576                             if(aEdge.isBezier())
577                             {
578                                 const basegfx::B2DRange aBezierRangeWithControlPoints(aEdge.getRange());
579 
580                                 if(!aNewRange.isInside(aBezierRangeWithControlPoints))
581                                 {
582                                     // the range with control points of the current edge is not completely
583                                     // inside the current range without control points. Expand current range by
584                                     // subdividing the bezier segment.
585                                     // Ideal here is a subdivision at the extreme values, so use
586                                     // getAllExtremumPositions to get all extremas in one run
587                                     ::std::vector< double > aExtremas;
588 
589                                     aExtremas.reserve(4);
590                                     aEdge.getAllExtremumPositions(aExtremas);
591 
592                                     const sal_uInt32 nExtremaCount(aExtremas.size());
593 
594                                     for(sal_uInt32 c(0); c < nExtremaCount; c++)
595                                     {
596                                         aNewRange.expand(aEdge.interpolatePoint(aExtremas[c]));
597                                     }
598                                 }
599                             }
600 
601                             // prepare next edge
602                             aEdge.setStartPoint(aEdge.getEndPoint());
603                         }
604                     }
605                 }
606             }
607 
608             const_cast< ImplBufferedData* >(this)->mpB2DRange.reset(new basegfx::B2DRange(aNewRange));
609         }
610 
611         return *mpB2DRange;
612     }
613 };
614 
615 //////////////////////////////////////////////////////////////////////////////
616 
617 class ImplB2DPolygon
618 {
619 private:
620     // The point vector. This vector exists always and defines the
621     // count of members.
622     CoordinateDataArray2D                           maPoints;
623 
624     // The control point vectors. This vectors are created on demand
625     // and may be zero.
626     boost::scoped_ptr< ControlVectorArray2D >       mpControlVector;
627 
628     // buffered data for e.g. default subdivision and range
629     boost::scoped_ptr< ImplBufferedData >           mpBufferedData;
630 
631     // flag which decides if this polygon is opened or closed
632     bool                                            mbIsClosed;
633 
634 public:
getDefaultAdaptiveSubdivision(const basegfx::B2DPolygon & rSource) const635     const basegfx::B2DPolygon& getDefaultAdaptiveSubdivision(const basegfx::B2DPolygon& rSource) const
636     {
637         if(!mpControlVector || !mpControlVector->isUsed())
638         {
639             return rSource;
640         }
641 
642         if(!mpBufferedData)
643         {
644             const_cast< ImplB2DPolygon* >(this)->mpBufferedData.reset(new ImplBufferedData);
645         }
646 
647         return mpBufferedData->getDefaultAdaptiveSubdivision(rSource);
648     }
649 
getB2DRange(const basegfx::B2DPolygon & rSource) const650     const basegfx::B2DRange& getB2DRange(const basegfx::B2DPolygon& rSource) const
651     {
652         if(!mpBufferedData)
653         {
654             const_cast< ImplB2DPolygon* >(this)->mpBufferedData.reset(new ImplBufferedData);
655         }
656 
657         return mpBufferedData->getB2DRange(rSource);
658     }
659 
ImplB2DPolygon()660     ImplB2DPolygon()
661     :   maPoints(0),
662         mpControlVector(),
663         mpBufferedData(),
664         mbIsClosed(false)
665     {}
666 
ImplB2DPolygon(const ImplB2DPolygon & rToBeCopied)667     ImplB2DPolygon(const ImplB2DPolygon& rToBeCopied)
668     :   maPoints(rToBeCopied.maPoints),
669         mpControlVector(),
670         mpBufferedData(),
671         mbIsClosed(rToBeCopied.mbIsClosed)
672     {
673         // complete initialization using copy
674         if(rToBeCopied.mpControlVector && rToBeCopied.mpControlVector->isUsed())
675         {
676             mpControlVector.reset( new ControlVectorArray2D(*rToBeCopied.mpControlVector) );
677         }
678     }
679 
ImplB2DPolygon(const ImplB2DPolygon & rToBeCopied,sal_uInt32 nIndex,sal_uInt32 nCount)680     ImplB2DPolygon(const ImplB2DPolygon& rToBeCopied, sal_uInt32 nIndex, sal_uInt32 nCount)
681     :   maPoints(rToBeCopied.maPoints, nIndex, nCount),
682         mpControlVector(),
683         mpBufferedData(),
684         mbIsClosed(rToBeCopied.mbIsClosed)
685     {
686         // complete initialization using partly copy
687         if(rToBeCopied.mpControlVector && rToBeCopied.mpControlVector->isUsed())
688         {
689             mpControlVector.reset( new ControlVectorArray2D(*rToBeCopied.mpControlVector, nIndex, nCount) );
690 
691             if(!mpControlVector->isUsed())
692                 mpControlVector.reset();
693         }
694     }
695 
operator =(const ImplB2DPolygon & rToBeCopied)696     ImplB2DPolygon& operator=( const ImplB2DPolygon& rToBeCopied )
697     {
698         maPoints = rToBeCopied.maPoints;
699         mpControlVector.reset();
700         mpBufferedData.reset();
701         mbIsClosed = rToBeCopied.mbIsClosed;
702 
703         // complete initialization using copy
704         if(rToBeCopied.mpControlVector && rToBeCopied.mpControlVector->isUsed())
705             mpControlVector.reset( new ControlVectorArray2D(*rToBeCopied.mpControlVector) );
706 
707         return *this;
708     }
709 
count() const710     sal_uInt32 count() const
711     {
712         return maPoints.count();
713     }
714 
isClosed() const715     bool isClosed() const
716     {
717         return mbIsClosed;
718     }
719 
setClosed(bool bNew)720     void setClosed(bool bNew)
721     {
722         if(bNew != mbIsClosed)
723         {
724             mpBufferedData.reset();
725             mbIsClosed = bNew;
726         }
727     }
728 
operator ==(const ImplB2DPolygon & rCandidate) const729     bool operator==(const ImplB2DPolygon& rCandidate) const
730     {
731         if(mbIsClosed == rCandidate.mbIsClosed)
732         {
733             if(maPoints == rCandidate.maPoints)
734             {
735                 bool bControlVectorsAreEqual(true);
736 
737                 if(mpControlVector)
738                 {
739                     if(rCandidate.mpControlVector)
740                     {
741                         bControlVectorsAreEqual = ((*mpControlVector) == (*rCandidate.mpControlVector));
742                     }
743                     else
744                     {
745                         // candidate has no control vector, so it's assumed all unused.
746                         bControlVectorsAreEqual = !mpControlVector->isUsed();
747                     }
748                 }
749                 else
750                 {
751                     if(rCandidate.mpControlVector)
752                     {
753                         // we have no control vector, so it's assumed all unused.
754                         bControlVectorsAreEqual = !rCandidate.mpControlVector->isUsed();
755                     }
756                 }
757 
758                 if(bControlVectorsAreEqual)
759                 {
760                     return true;
761                 }
762             }
763         }
764 
765         return false;
766     }
767 
getPoint(sal_uInt32 nIndex) const768     const basegfx::B2DPoint& getPoint(sal_uInt32 nIndex) const
769     {
770         return maPoints.getCoordinate(nIndex);
771     }
772 
setPoint(sal_uInt32 nIndex,const basegfx::B2DPoint & rValue)773     void setPoint(sal_uInt32 nIndex, const basegfx::B2DPoint& rValue)
774     {
775         mpBufferedData.reset();
776         maPoints.setCoordinate(nIndex, rValue);
777     }
778 
reserve(sal_uInt32 nCount)779     void reserve(sal_uInt32 nCount)
780     {
781         maPoints.reserve(nCount);
782     }
783 
append(const basegfx::B2DPoint & rPoint)784     void append(const basegfx::B2DPoint& rPoint)
785     {
786         mpBufferedData.reset(); // TODO: is this needed?
787         const CoordinateData2D aCoordinate(rPoint);
788         maPoints.append(aCoordinate);
789 
790         if(mpControlVector)
791         {
792             const ControlVectorPair2D aVectorPair;
793             mpControlVector->append(aVectorPair);
794         }
795     }
796 
insert(sal_uInt32 nIndex,const basegfx::B2DPoint & rPoint,sal_uInt32 nCount)797     void insert(sal_uInt32 nIndex, const basegfx::B2DPoint& rPoint, sal_uInt32 nCount)
798     {
799         if(nCount)
800         {
801             mpBufferedData.reset();
802             CoordinateData2D aCoordinate(rPoint);
803             maPoints.insert(nIndex, aCoordinate, nCount);
804 
805             if(mpControlVector)
806             {
807                 ControlVectorPair2D aVectorPair;
808                 mpControlVector->insert(nIndex, aVectorPair, nCount);
809             }
810         }
811     }
812 
getPrevControlVector(sal_uInt32 nIndex) const813     const basegfx::B2DVector& getPrevControlVector(sal_uInt32 nIndex) const
814     {
815         if(mpControlVector)
816         {
817             return mpControlVector->getPrevVector(nIndex);
818         }
819         else
820         {
821             return basegfx::B2DVector::getEmptyVector();
822         }
823     }
824 
setPrevControlVector(sal_uInt32 nIndex,const basegfx::B2DVector & rValue)825     void setPrevControlVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
826     {
827         if(!mpControlVector)
828         {
829             if(!rValue.equalZero())
830             {
831                 mpBufferedData.reset();
832                 mpControlVector.reset( new ControlVectorArray2D(maPoints.count()) );
833                 mpControlVector->setPrevVector(nIndex, rValue);
834             }
835         }
836         else
837         {
838             mpBufferedData.reset();
839             mpControlVector->setPrevVector(nIndex, rValue);
840 
841             if(!mpControlVector->isUsed())
842                 mpControlVector.reset();
843         }
844     }
845 
getNextControlVector(sal_uInt32 nIndex) const846     const basegfx::B2DVector& getNextControlVector(sal_uInt32 nIndex) const
847     {
848         if(mpControlVector)
849         {
850             return mpControlVector->getNextVector(nIndex);
851         }
852         else
853         {
854             return basegfx::B2DVector::getEmptyVector();
855         }
856     }
857 
setNextControlVector(sal_uInt32 nIndex,const basegfx::B2DVector & rValue)858     void setNextControlVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
859     {
860         if(!mpControlVector)
861         {
862             if(!rValue.equalZero())
863             {
864                 mpBufferedData.reset();
865                 mpControlVector.reset( new ControlVectorArray2D(maPoints.count()) );
866                 mpControlVector->setNextVector(nIndex, rValue);
867             }
868         }
869         else
870         {
871             mpBufferedData.reset();
872             mpControlVector->setNextVector(nIndex, rValue);
873 
874             if(!mpControlVector->isUsed())
875                 mpControlVector.reset();
876         }
877     }
878 
areControlPointsUsed() const879     bool areControlPointsUsed() const
880     {
881         return (mpControlVector && mpControlVector->isUsed());
882     }
883 
resetControlVectors(sal_uInt32 nIndex)884     void resetControlVectors(sal_uInt32 nIndex)
885     {
886         setPrevControlVector(nIndex, basegfx::B2DVector::getEmptyVector());
887         setNextControlVector(nIndex, basegfx::B2DVector::getEmptyVector());
888     }
889 
resetControlVectors()890     void resetControlVectors()
891     {
892         mpBufferedData.reset();
893         mpControlVector.reset();
894     }
895 
setControlVectors(sal_uInt32 nIndex,const basegfx::B2DVector & rPrev,const basegfx::B2DVector & rNext)896     void setControlVectors(sal_uInt32 nIndex, const basegfx::B2DVector& rPrev, const basegfx::B2DVector& rNext)
897     {
898         setPrevControlVector(nIndex, rPrev);
899         setNextControlVector(nIndex, rNext);
900     }
901 
appendBezierSegment(const basegfx::B2DVector & rNext,const basegfx::B2DVector & rPrev,const basegfx::B2DPoint & rPoint)902     void appendBezierSegment(const basegfx::B2DVector& rNext, const basegfx::B2DVector& rPrev, const basegfx::B2DPoint& rPoint)
903     {
904         mpBufferedData.reset();
905         const sal_uInt32 nCount(maPoints.count());
906 
907         if(nCount)
908         {
909             setNextControlVector(nCount - 1, rNext);
910         }
911 
912         insert(nCount, rPoint, 1);
913         setPrevControlVector(nCount, rPrev);
914     }
915 
insert(sal_uInt32 nIndex,const ImplB2DPolygon & rSource)916     void insert(sal_uInt32 nIndex, const ImplB2DPolygon& rSource)
917     {
918         const sal_uInt32 nCount(rSource.maPoints.count());
919 
920         if(nCount)
921         {
922             mpBufferedData.reset();
923 
924             if(rSource.mpControlVector && rSource.mpControlVector->isUsed() && !mpControlVector)
925             {
926                 mpControlVector.reset( new ControlVectorArray2D(maPoints.count()) );
927             }
928 
929             maPoints.insert(nIndex, rSource.maPoints);
930 
931             if(rSource.mpControlVector)
932             {
933                 mpControlVector->insert(nIndex, *rSource.mpControlVector);
934 
935                 if(!mpControlVector->isUsed())
936                     mpControlVector.reset();
937             }
938             else if(mpControlVector)
939             {
940                 ControlVectorPair2D aVectorPair;
941                 mpControlVector->insert(nIndex, aVectorPair, nCount);
942             }
943         }
944     }
945 
remove(sal_uInt32 nIndex,sal_uInt32 nCount)946     void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
947     {
948         if(nCount)
949         {
950             mpBufferedData.reset();
951             maPoints.remove(nIndex, nCount);
952 
953             if(mpControlVector)
954             {
955                 mpControlVector->remove(nIndex, nCount);
956 
957                 if(!mpControlVector->isUsed())
958                     mpControlVector.reset();
959             }
960         }
961     }
962 
flip()963     void flip()
964     {
965         if(maPoints.count() > 1)
966         {
967             mpBufferedData.reset();
968 
969             // flip points
970             maPoints.flip(mbIsClosed);
971 
972             if(mpControlVector)
973             {
974                 // flip control vector
975                 mpControlVector->flip(mbIsClosed);
976             }
977         }
978     }
979 
hasDoublePoints() const980     bool hasDoublePoints() const
981     {
982         if(mbIsClosed)
983         {
984             // check for same start and end point
985             const sal_uInt32 nIndex(maPoints.count() - 1);
986 
987             if(maPoints.getCoordinate(0) == maPoints.getCoordinate(nIndex))
988             {
989                 if(mpControlVector)
990                 {
991                     if(mpControlVector->getNextVector(nIndex).equalZero() && mpControlVector->getPrevVector(0).equalZero())
992                     {
993                         return true;
994                     }
995                 }
996                 else
997                 {
998                     return true;
999                 }
1000             }
1001         }
1002 
1003         // test for range
1004         for(sal_uInt32 a(0); a < maPoints.count() - 1; a++)
1005         {
1006             if(maPoints.getCoordinate(a) == maPoints.getCoordinate(a + 1))
1007             {
1008                 if(mpControlVector)
1009                 {
1010                     if(mpControlVector->getNextVector(a).equalZero() && mpControlVector->getPrevVector(a + 1).equalZero())
1011                     {
1012                         return true;
1013                     }
1014                 }
1015                 else
1016                 {
1017                     return true;
1018                 }
1019             }
1020         }
1021 
1022         return false;
1023     }
1024 
removeDoublePointsAtBeginEnd()1025     void removeDoublePointsAtBeginEnd()
1026     {
1027         // Only remove DoublePoints at Begin and End when poly is closed
1028         if(mbIsClosed)
1029         {
1030             mpBufferedData.reset();
1031 
1032             if(mpControlVector)
1033             {
1034                 bool bRemove;
1035 
1036                 do
1037                 {
1038                     bRemove = false;
1039 
1040                     if(maPoints.count() > 1)
1041                     {
1042                         const sal_uInt32 nIndex(maPoints.count() - 1);
1043 
1044                         if(maPoints.getCoordinate(0) == maPoints.getCoordinate(nIndex))
1045                         {
1046                             if(mpControlVector)
1047                             {
1048                                 if(mpControlVector->getNextVector(nIndex).equalZero() && mpControlVector->getPrevVector(0).equalZero())
1049                                 {
1050                                     bRemove = true;
1051                                 }
1052                             }
1053                             else
1054                             {
1055                                 bRemove = true;
1056                             }
1057                         }
1058                     }
1059 
1060                     if(bRemove)
1061                     {
1062                         const sal_uInt32 nIndex(maPoints.count() - 1);
1063 
1064                         if(mpControlVector && !mpControlVector->getPrevVector(nIndex).equalZero())
1065                         {
1066                             mpControlVector->setPrevVector(0, mpControlVector->getPrevVector(nIndex));
1067                         }
1068 
1069                         remove(nIndex, 1);
1070                     }
1071                 }
1072                 while(bRemove);
1073             }
1074             else
1075             {
1076                 maPoints.removeDoublePointsAtBeginEnd();
1077             }
1078         }
1079     }
1080 
removeDoublePointsWholeTrack()1081     void removeDoublePointsWholeTrack()
1082     {
1083         mpBufferedData.reset();
1084 
1085         if(mpControlVector)
1086         {
1087             sal_uInt32 nIndex(0);
1088 
1089             // test as long as there are at least two points and as long as the index
1090             // is smaller or equal second last point
1091             while((maPoints.count() > 1) && (nIndex <= maPoints.count() - 2))
1092             {
1093                 bool bRemove(maPoints.getCoordinate(nIndex) == maPoints.getCoordinate(nIndex + 1));
1094 
1095                 if(bRemove)
1096                 {
1097                     if(mpControlVector)
1098                     {
1099                         if(!mpControlVector->getNextVector(nIndex).equalZero() || !mpControlVector->getPrevVector(nIndex + 1).equalZero())
1100                         {
1101                             bRemove = false;
1102                         }
1103                     }
1104                 }
1105 
1106                 if(bRemove)
1107                 {
1108                     if(mpControlVector && !mpControlVector->getPrevVector(nIndex).equalZero())
1109                     {
1110                         mpControlVector->setPrevVector(nIndex + 1, mpControlVector->getPrevVector(nIndex));
1111                     }
1112 
1113                     // if next is same as index and the control vectors are unused, delete index
1114                     remove(nIndex, 1);
1115                 }
1116                 else
1117                 {
1118                     // if different, step forward
1119                     nIndex++;
1120                 }
1121             }
1122         }
1123         else
1124         {
1125             maPoints.removeDoublePointsWholeTrack();
1126         }
1127     }
1128 
transform(const basegfx::B2DHomMatrix & rMatrix)1129     void transform(const basegfx::B2DHomMatrix& rMatrix)
1130     {
1131         mpBufferedData.reset();
1132 
1133         if(mpControlVector)
1134         {
1135             for(sal_uInt32 a(0); a < maPoints.count(); a++)
1136             {
1137                 basegfx::B2DPoint aCandidate = maPoints.getCoordinate(a);
1138 
1139                 if(mpControlVector->isUsed())
1140                 {
1141                     const basegfx::B2DVector& rPrevVector(mpControlVector->getPrevVector(a));
1142                     const basegfx::B2DVector& rNextVector(mpControlVector->getNextVector(a));
1143 
1144                     if(!rPrevVector.equalZero())
1145                     {
1146                         basegfx::B2DVector aPrevVector(rMatrix * rPrevVector);
1147                         mpControlVector->setPrevVector(a, aPrevVector);
1148                     }
1149 
1150                     if(!rNextVector.equalZero())
1151                     {
1152                         basegfx::B2DVector aNextVector(rMatrix * rNextVector);
1153                         mpControlVector->setNextVector(a, aNextVector);
1154                     }
1155                 }
1156 
1157                 aCandidate *= rMatrix;
1158                 maPoints.setCoordinate(a, aCandidate);
1159             }
1160 
1161             if(!mpControlVector->isUsed())
1162                 mpControlVector.reset();
1163         }
1164         else
1165         {
1166             maPoints.transform(rMatrix);
1167         }
1168     }
1169 
begin() const1170     const basegfx::B2DPoint* begin() const
1171     {
1172         return maPoints.begin();
1173     }
1174 
end() const1175     const basegfx::B2DPoint* end() const
1176     {
1177         return maPoints.end();
1178     }
1179 
begin()1180     basegfx::B2DPoint* begin()
1181     {
1182        mpBufferedData.reset();
1183        return maPoints.begin();
1184     }
1185 
end()1186     basegfx::B2DPoint* end()
1187     {
1188         mpBufferedData.reset();
1189         return maPoints.end();
1190     }
1191 };
1192 
1193 //////////////////////////////////////////////////////////////////////////////
1194 
1195 namespace basegfx
1196 {
1197     namespace
1198     {
1199         struct DefaultPolygon: public rtl::Static<B2DPolygon::ImplType, DefaultPolygon> {};
1200     }
1201 
B2DPolygon()1202     B2DPolygon::B2DPolygon()
1203     :   mpPolygon(DefaultPolygon::get())
1204     {}
1205 
B2DPolygon(const B2DPolygon & rPolygon)1206     B2DPolygon::B2DPolygon(const B2DPolygon& rPolygon)
1207     :   mpPolygon(rPolygon.mpPolygon)
1208     {}
1209 
B2DPolygon(const B2DPolygon & rPolygon,sal_uInt32 nIndex,sal_uInt32 nCount)1210     B2DPolygon::B2DPolygon(const B2DPolygon& rPolygon, sal_uInt32 nIndex, sal_uInt32 nCount)
1211     :   mpPolygon(ImplB2DPolygon(*rPolygon.mpPolygon, nIndex, nCount))
1212     {
1213         // TODO(P2): one extra temporary here (cow_wrapper copies
1214         // given ImplB2DPolygon into its internal impl_t wrapper type)
1215         OSL_ENSURE(nIndex + nCount <= rPolygon.mpPolygon->count(), "B2DPolygon constructor outside range (!)");
1216     }
1217 
~B2DPolygon()1218     B2DPolygon::~B2DPolygon()
1219     {
1220     }
1221 
operator =(const B2DPolygon & rPolygon)1222     B2DPolygon& B2DPolygon::operator=(const B2DPolygon& rPolygon)
1223     {
1224         mpPolygon = rPolygon.mpPolygon;
1225         return *this;
1226     }
1227 
makeUnique()1228     void B2DPolygon::makeUnique()
1229     {
1230         mpPolygon.make_unique();
1231     }
1232 
operator ==(const B2DPolygon & rPolygon) const1233     bool B2DPolygon::operator==(const B2DPolygon& rPolygon) const
1234     {
1235         if(mpPolygon.same_object(rPolygon.mpPolygon))
1236             return true;
1237 
1238         return ((*mpPolygon) == (*rPolygon.mpPolygon));
1239     }
1240 
operator !=(const B2DPolygon & rPolygon) const1241     bool B2DPolygon::operator!=(const B2DPolygon& rPolygon) const
1242     {
1243         return !(*this == rPolygon);
1244     }
1245 
count() const1246     sal_uInt32 B2DPolygon::count() const
1247     {
1248         return mpPolygon->count();
1249     }
1250 
getB2DPoint(sal_uInt32 nIndex) const1251     B2DPoint B2DPolygon::getB2DPoint(sal_uInt32 nIndex) const
1252     {
1253         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1254 
1255         return mpPolygon->getPoint(nIndex);
1256     }
1257 
setB2DPoint(sal_uInt32 nIndex,const B2DPoint & rValue)1258     void B2DPolygon::setB2DPoint(sal_uInt32 nIndex, const B2DPoint& rValue)
1259     {
1260         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1261 
1262         if(mpPolygon->getPoint(nIndex) != rValue)
1263         {
1264             mpPolygon->setPoint(nIndex, rValue);
1265         }
1266     }
1267 
reserve(sal_uInt32 nCount)1268     void B2DPolygon::reserve(sal_uInt32 nCount)
1269     {
1270         mpPolygon->reserve(nCount);
1271     }
1272 
insert(sal_uInt32 nIndex,const B2DPoint & rPoint,sal_uInt32 nCount)1273     void B2DPolygon::insert(sal_uInt32 nIndex, const B2DPoint& rPoint, sal_uInt32 nCount)
1274     {
1275         OSL_ENSURE(nIndex <= mpPolygon->count(), "B2DPolygon Insert outside range (!)");
1276 
1277         if(nCount)
1278         {
1279             mpPolygon->insert(nIndex, rPoint, nCount);
1280         }
1281     }
1282 
append(const B2DPoint & rPoint,sal_uInt32 nCount)1283     void B2DPolygon::append(const B2DPoint& rPoint, sal_uInt32 nCount)
1284     {
1285         if(nCount)
1286         {
1287             mpPolygon->insert(mpPolygon->count(), rPoint, nCount);
1288         }
1289     }
1290 
append(const B2DPoint & rPoint)1291     void B2DPolygon::append(const B2DPoint& rPoint)
1292     {
1293         mpPolygon->append(rPoint);
1294     }
1295 
getPrevControlPoint(sal_uInt32 nIndex) const1296     B2DPoint B2DPolygon::getPrevControlPoint(sal_uInt32 nIndex) const
1297     {
1298         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1299 
1300         if(mpPolygon->areControlPointsUsed())
1301         {
1302             return mpPolygon->getPoint(nIndex) + mpPolygon->getPrevControlVector(nIndex);
1303         }
1304         else
1305         {
1306             return mpPolygon->getPoint(nIndex);
1307         }
1308     }
1309 
getNextControlPoint(sal_uInt32 nIndex) const1310     B2DPoint B2DPolygon::getNextControlPoint(sal_uInt32 nIndex) const
1311     {
1312         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1313 
1314         if(mpPolygon->areControlPointsUsed())
1315         {
1316             return mpPolygon->getPoint(nIndex) + mpPolygon->getNextControlVector(nIndex);
1317         }
1318         else
1319         {
1320             return mpPolygon->getPoint(nIndex);
1321         }
1322     }
1323 
setPrevControlPoint(sal_uInt32 nIndex,const B2DPoint & rValue)1324     void B2DPolygon::setPrevControlPoint(sal_uInt32 nIndex, const B2DPoint& rValue)
1325     {
1326         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1327         const basegfx::B2DVector aNewVector(rValue - mpPolygon->getPoint(nIndex));
1328 
1329         if(mpPolygon->getPrevControlVector(nIndex) != aNewVector)
1330         {
1331             mpPolygon->setPrevControlVector(nIndex, aNewVector);
1332         }
1333     }
1334 
setNextControlPoint(sal_uInt32 nIndex,const B2DPoint & rValue)1335     void B2DPolygon::setNextControlPoint(sal_uInt32 nIndex, const B2DPoint& rValue)
1336     {
1337         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1338         const basegfx::B2DVector aNewVector(rValue - mpPolygon->getPoint(nIndex));
1339 
1340         if(mpPolygon->getNextControlVector(nIndex) != aNewVector)
1341         {
1342             mpPolygon->setNextControlVector(nIndex, aNewVector);
1343         }
1344     }
1345 
setControlPoints(sal_uInt32 nIndex,const basegfx::B2DPoint & rPrev,const basegfx::B2DPoint & rNext)1346     void B2DPolygon::setControlPoints(sal_uInt32 nIndex, const basegfx::B2DPoint& rPrev, const basegfx::B2DPoint& rNext)
1347     {
1348         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1349         const B2DPoint aPoint(mpPolygon->getPoint(nIndex));
1350         const basegfx::B2DVector aNewPrev(rPrev - aPoint);
1351         const basegfx::B2DVector aNewNext(rNext - aPoint);
1352 
1353         if(mpPolygon->getPrevControlVector(nIndex) != aNewPrev || mpPolygon->getNextControlVector(nIndex) != aNewNext)
1354         {
1355             mpPolygon->setControlVectors(nIndex, aNewPrev, aNewNext);
1356         }
1357     }
1358 
resetPrevControlPoint(sal_uInt32 nIndex)1359     void B2DPolygon::resetPrevControlPoint(sal_uInt32 nIndex)
1360     {
1361         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1362 
1363         if(mpPolygon->areControlPointsUsed() && !mpPolygon->getPrevControlVector(nIndex).equalZero())
1364         {
1365             mpPolygon->setPrevControlVector(nIndex, B2DVector::getEmptyVector());
1366         }
1367     }
1368 
resetNextControlPoint(sal_uInt32 nIndex)1369     void B2DPolygon::resetNextControlPoint(sal_uInt32 nIndex)
1370     {
1371         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1372 
1373         if(mpPolygon->areControlPointsUsed() && !mpPolygon->getNextControlVector(nIndex).equalZero())
1374         {
1375             mpPolygon->setNextControlVector(nIndex, B2DVector::getEmptyVector());
1376         }
1377     }
1378 
resetControlPoints(sal_uInt32 nIndex)1379     void B2DPolygon::resetControlPoints(sal_uInt32 nIndex)
1380     {
1381         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1382 
1383         if(mpPolygon->areControlPointsUsed() &&
1384             (!mpPolygon->getPrevControlVector(nIndex).equalZero() || !mpPolygon->getNextControlVector(nIndex).equalZero()))
1385         {
1386             mpPolygon->resetControlVectors(nIndex);
1387         }
1388     }
1389 
resetControlPoints()1390     void B2DPolygon::resetControlPoints()
1391     {
1392         if(mpPolygon->areControlPointsUsed())
1393         {
1394             mpPolygon->resetControlVectors();
1395         }
1396     }
1397 
appendBezierSegment(const B2DPoint & rNextControlPoint,const B2DPoint & rPrevControlPoint,const B2DPoint & rPoint)1398     void B2DPolygon::appendBezierSegment(
1399         const B2DPoint& rNextControlPoint,
1400         const B2DPoint& rPrevControlPoint,
1401         const B2DPoint& rPoint)
1402     {
1403         const B2DVector aNewNextVector(mpPolygon->count() ? B2DVector(rNextControlPoint - mpPolygon->getPoint(mpPolygon->count() - 1)) : B2DVector::getEmptyVector());
1404         const B2DVector aNewPrevVector(rPrevControlPoint - rPoint);
1405 
1406         if(aNewNextVector.equalZero() && aNewPrevVector.equalZero())
1407         {
1408             mpPolygon->insert(mpPolygon->count(), rPoint, 1);
1409         }
1410         else
1411         {
1412             mpPolygon->appendBezierSegment(aNewNextVector, aNewPrevVector, rPoint);
1413         }
1414     }
1415 
areControlPointsUsed() const1416     bool B2DPolygon::areControlPointsUsed() const
1417     {
1418         return mpPolygon->areControlPointsUsed();
1419     }
1420 
isPrevControlPointUsed(sal_uInt32 nIndex) const1421     bool B2DPolygon::isPrevControlPointUsed(sal_uInt32 nIndex) const
1422     {
1423         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1424 
1425         return (mpPolygon->areControlPointsUsed() && !mpPolygon->getPrevControlVector(nIndex).equalZero());
1426     }
1427 
isNextControlPointUsed(sal_uInt32 nIndex) const1428     bool B2DPolygon::isNextControlPointUsed(sal_uInt32 nIndex) const
1429     {
1430         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1431 
1432         return (mpPolygon->areControlPointsUsed() && !mpPolygon->getNextControlVector(nIndex).equalZero());
1433     }
1434 
getContinuityInPoint(sal_uInt32 nIndex) const1435     B2VectorContinuity B2DPolygon::getContinuityInPoint(sal_uInt32 nIndex) const
1436     {
1437         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1438 
1439         if(mpPolygon->areControlPointsUsed())
1440         {
1441             const B2DVector& rPrev(mpPolygon->getPrevControlVector(nIndex));
1442             const B2DVector& rNext(mpPolygon->getNextControlVector(nIndex));
1443 
1444             return getContinuity(rPrev, rNext);
1445         }
1446         else
1447         {
1448             return CONTINUITY_NONE;
1449         }
1450     }
1451 
isBezierSegment(sal_uInt32 nIndex) const1452     bool B2DPolygon::isBezierSegment(sal_uInt32 nIndex) const
1453     {
1454         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1455 
1456         if(mpPolygon->areControlPointsUsed())
1457         {
1458             // Check if the edge exists
1459             const bool bNextIndexValidWithoutClose(nIndex + 1 < mpPolygon->count());
1460 
1461             if(bNextIndexValidWithoutClose || mpPolygon->isClosed())
1462             {
1463                 const sal_uInt32 nNextIndex(bNextIndexValidWithoutClose ? nIndex + 1 : 0);
1464                 return (!mpPolygon->getPrevControlVector(nNextIndex).equalZero()
1465                     || !mpPolygon->getNextControlVector(nIndex).equalZero());
1466             }
1467             else
1468             {
1469                 // no valid edge -> no bezier segment, even when local next
1470                 // vector may be used
1471                 return false;
1472             }
1473         }
1474         else
1475         {
1476             // no control points -> no bezier segment
1477             return false;
1478         }
1479     }
1480 
getBezierSegment(sal_uInt32 nIndex,B2DCubicBezier & rTarget) const1481     void B2DPolygon::getBezierSegment(sal_uInt32 nIndex, B2DCubicBezier& rTarget) const
1482     {
1483         OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
1484         const bool bNextIndexValidWithoutClose(nIndex + 1 < mpPolygon->count());
1485 
1486         if(bNextIndexValidWithoutClose || mpPolygon->isClosed())
1487         {
1488             const sal_uInt32 nNextIndex(bNextIndexValidWithoutClose ? nIndex + 1 : 0);
1489             rTarget.setStartPoint(mpPolygon->getPoint(nIndex));
1490             rTarget.setEndPoint(mpPolygon->getPoint(nNextIndex));
1491 
1492             if(mpPolygon->areControlPointsUsed())
1493             {
1494                 rTarget.setControlPointA(rTarget.getStartPoint() + mpPolygon->getNextControlVector(nIndex));
1495                 rTarget.setControlPointB(rTarget.getEndPoint() + mpPolygon->getPrevControlVector(nNextIndex));
1496             }
1497             else
1498             {
1499                 // no bezier, reset control poins at rTarget
1500                 rTarget.setControlPointA(rTarget.getStartPoint());
1501                 rTarget.setControlPointB(rTarget.getEndPoint());
1502             }
1503         }
1504         else
1505         {
1506             // no valid edge at all, reset rTarget to current point
1507             const B2DPoint aPoint(mpPolygon->getPoint(nIndex));
1508             rTarget.setStartPoint(aPoint);
1509             rTarget.setEndPoint(aPoint);
1510             rTarget.setControlPointA(aPoint);
1511             rTarget.setControlPointB(aPoint);
1512         }
1513     }
1514 
getDefaultAdaptiveSubdivision() const1515     B2DPolygon B2DPolygon::getDefaultAdaptiveSubdivision() const
1516     {
1517         return mpPolygon->getDefaultAdaptiveSubdivision(*this);
1518     }
1519 
getB2DRange() const1520     B2DRange B2DPolygon::getB2DRange() const
1521     {
1522         return mpPolygon->getB2DRange(*this);
1523     }
1524 
insert(sal_uInt32 nIndex,const B2DPolygon & rPoly,sal_uInt32 nIndex2,sal_uInt32 nCount)1525     void B2DPolygon::insert(sal_uInt32 nIndex, const B2DPolygon& rPoly, sal_uInt32 nIndex2, sal_uInt32 nCount)
1526     {
1527         OSL_ENSURE(nIndex <= mpPolygon->count(), "B2DPolygon Insert outside range (!)");
1528 
1529         if(rPoly.count())
1530         {
1531             if(!nCount)
1532             {
1533                 nCount = rPoly.count();
1534             }
1535 
1536             if(0 == nIndex2 && nCount == rPoly.count())
1537             {
1538                 mpPolygon->insert(nIndex, *rPoly.mpPolygon);
1539             }
1540             else
1541             {
1542                 OSL_ENSURE(nIndex2 + nCount <= rPoly.mpPolygon->count(), "B2DPolygon Insert outside range (!)");
1543                 ImplB2DPolygon aTempPoly(*rPoly.mpPolygon, nIndex2, nCount);
1544                 mpPolygon->insert(nIndex, aTempPoly);
1545             }
1546         }
1547     }
1548 
append(const B2DPolygon & rPoly,sal_uInt32 nIndex,sal_uInt32 nCount)1549     void B2DPolygon::append(const B2DPolygon& rPoly, sal_uInt32 nIndex, sal_uInt32 nCount)
1550     {
1551         if(rPoly.count())
1552         {
1553             if(!nCount)
1554             {
1555                 nCount = rPoly.count();
1556             }
1557 
1558             if(0 == nIndex && nCount == rPoly.count())
1559             {
1560                 mpPolygon->insert(mpPolygon->count(), *rPoly.mpPolygon);
1561             }
1562             else
1563             {
1564                 OSL_ENSURE(nIndex + nCount <= rPoly.mpPolygon->count(), "B2DPolygon Append outside range (!)");
1565                 ImplB2DPolygon aTempPoly(*rPoly.mpPolygon, nIndex, nCount);
1566                 mpPolygon->insert(mpPolygon->count(), aTempPoly);
1567             }
1568         }
1569     }
1570 
remove(sal_uInt32 nIndex,sal_uInt32 nCount)1571     void B2DPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
1572     {
1573         OSL_ENSURE(nIndex + nCount <= mpPolygon->count(), "B2DPolygon Remove outside range (!)");
1574 
1575         if(nCount)
1576         {
1577             mpPolygon->remove(nIndex, nCount);
1578         }
1579     }
1580 
clear()1581     void B2DPolygon::clear()
1582     {
1583         mpPolygon = DefaultPolygon::get();
1584     }
1585 
isClosed() const1586     bool B2DPolygon::isClosed() const
1587     {
1588         return mpPolygon->isClosed();
1589     }
1590 
setClosed(bool bNew)1591     void B2DPolygon::setClosed(bool bNew)
1592     {
1593         if(isClosed() != bNew)
1594         {
1595             mpPolygon->setClosed(bNew);
1596         }
1597     }
1598 
flip()1599     void B2DPolygon::flip()
1600     {
1601         if(count() > 1)
1602         {
1603             mpPolygon->flip();
1604         }
1605     }
1606 
hasDoublePoints() const1607     bool B2DPolygon::hasDoublePoints() const
1608     {
1609         return (mpPolygon->count() > 1 && mpPolygon->hasDoublePoints());
1610     }
1611 
removeDoublePoints()1612     void B2DPolygon::removeDoublePoints()
1613     {
1614         if(hasDoublePoints())
1615         {
1616             mpPolygon->removeDoublePointsAtBeginEnd();
1617             mpPolygon->removeDoublePointsWholeTrack();
1618         }
1619     }
1620 
transform(const B2DHomMatrix & rMatrix)1621     void B2DPolygon::transform(const B2DHomMatrix& rMatrix)
1622     {
1623         if(mpPolygon->count() && !rMatrix.isIdentity())
1624         {
1625             mpPolygon->transform(rMatrix);
1626         }
1627     }
1628 
begin() const1629     const B2DPoint* B2DPolygon::begin() const
1630     {
1631         return mpPolygon->begin();
1632     }
1633 
end() const1634     const B2DPoint* B2DPolygon::end() const
1635     {
1636         return mpPolygon->end();
1637     }
1638 
begin()1639     B2DPoint* B2DPolygon::begin()
1640     {
1641         return mpPolygon->begin();
1642     }
1643 
end()1644     B2DPoint* B2DPolygon::end()
1645     {
1646         return mpPolygon->end();
1647     }
1648 } // end of namespace basegfx
1649 
1650 //////////////////////////////////////////////////////////////////////////////
1651 // eof
1652