tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / basegfx / source / polygon / b2dpolypolygontools.cxx
blobcccd168bde009b7e3b9d445f116241c6dcebca9b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basegfx/polygon/b2dpolypolygontools.hxx>
21 #include <osl/diagnose.h>
22 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
23 #include <basegfx/polygon/b2dpolypolygon.hxx>
24 #include <basegfx/polygon/b2dpolygon.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/numeric/ftools.hxx>
27 #include <rtl/math.hxx>
29 #include <algorithm>
30 #include <numeric>
32 namespace basegfx::utils
34 B2DPolyPolygon correctOrientations(const B2DPolyPolygon& rCandidate)
36 B2DPolyPolygon aRetval(rCandidate);
37 const sal_uInt32 nCount(aRetval.count());
39 for(sal_uInt32 a(0); a < nCount; a++)
41 const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
42 const B2VectorOrientation aOrientation(utils::getOrientation(aCandidate));
43 sal_uInt32 nDepth(0);
45 for(sal_uInt32 b(0); b < nCount; b++)
47 if(b != a)
49 const B2DPolygon& aCompare(rCandidate.getB2DPolygon(b));
51 if(utils::isInside(aCompare, aCandidate, true))
53 nDepth++;
58 const bool bShallBeHole((nDepth & 0x00000001) == 1);
59 const bool bIsHole(aOrientation == B2VectorOrientation::Negative);
61 if(bShallBeHole != bIsHole && aOrientation != B2VectorOrientation::Neutral)
63 B2DPolygon aFlipped(aCandidate);
64 aFlipped.flip();
65 aRetval.setB2DPolygon(a, aFlipped);
69 return aRetval;
72 B2DPolyPolygon correctOutmostPolygon(const B2DPolyPolygon& rCandidate)
74 const sal_uInt32 nCount(rCandidate.count());
76 if(nCount > 1)
78 for(sal_uInt32 a(0); a < nCount; a++)
80 const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
81 sal_uInt32 nDepth(0);
83 for(sal_uInt32 b(0); b < nCount; b++)
85 if(b != a)
87 const B2DPolygon& aCompare(rCandidate.getB2DPolygon(b));
89 if(utils::isInside(aCompare, aCandidate, true))
91 nDepth++;
96 if(!nDepth)
98 B2DPolyPolygon aRetval(rCandidate);
100 if(a != 0)
102 // exchange polygon a and polygon 0
103 aRetval.setB2DPolygon(0, aCandidate);
104 aRetval.setB2DPolygon(a, rCandidate.getB2DPolygon(0));
107 // exit
108 return aRetval;
113 return rCandidate;
116 B2DPolyPolygon adaptiveSubdivideByDistance(const B2DPolyPolygon& rCandidate, double fDistanceBound, int nRecurseLimit)
118 if(rCandidate.areControlPointsUsed())
120 B2DPolyPolygon aRetval;
122 for(auto const& rPolygon : rCandidate)
124 if(rPolygon.areControlPointsUsed())
126 aRetval.append(utils::adaptiveSubdivideByDistance(rPolygon, fDistanceBound, nRecurseLimit));
128 else
130 aRetval.append(rPolygon);
134 return aRetval;
136 else
138 return rCandidate;
142 B2DPolyPolygon adaptiveSubdivideByAngle(const B2DPolyPolygon& rCandidate, double fAngleBound)
144 if(rCandidate.areControlPointsUsed())
146 B2DPolyPolygon aRetval;
148 for(auto const& rPolygon : rCandidate)
150 if(rPolygon.areControlPointsUsed())
152 aRetval.append(utils::adaptiveSubdivideByAngle(rPolygon, fAngleBound));
154 else
156 aRetval.append(rPolygon);
160 return aRetval;
162 else
164 return rCandidate;
168 bool isInside(const B2DPolyPolygon& rCandidate, const B2DPoint& rPoint, bool bWithBorder)
170 if(rCandidate.count() == 1)
172 return isInside(rCandidate.getB2DPolygon(0), rPoint, bWithBorder);
174 else
176 sal_Int32 nInsideCount = std::count_if(rCandidate.begin(), rCandidate.end(), [rPoint, bWithBorder](B2DPolygon polygon){ return isInside(polygon, rPoint, bWithBorder); });
178 return (nInsideCount % 2);
182 B2DRange getRange(const B2DPolyPolygon& rCandidate)
184 B2DRange aRetval;
186 for(auto const& rPolygon : rCandidate)
188 aRetval.expand(utils::getRange(rPolygon));
191 return aRetval;
194 double getSignedArea(const B2DPolyPolygon& rCandidate)
196 double fRetval(0.0);
198 for(auto const& rPolygon : rCandidate)
200 fRetval += utils::getSignedArea(rPolygon);
203 return fRetval;
206 double getArea(const B2DPolyPolygon& rCandidate)
208 return fabs(getSignedArea(rCandidate));
211 void applyLineDashing(const B2DPolyPolygon& rCandidate, const std::vector<double>& rDotDashArray, B2DPolyPolygon* pLineTarget, double fFullDashDotLen)
213 if(fFullDashDotLen == 0.0 && !rDotDashArray.empty())
215 // calculate fFullDashDotLen from rDotDashArray
216 fFullDashDotLen = std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
219 if(!(rCandidate.count() && fFullDashDotLen > 0.0))
220 return;
222 B2DPolyPolygon aLineTarget;
224 for(auto const& rPolygon : rCandidate)
226 applyLineDashing(
227 rPolygon,
228 rDotDashArray,
229 pLineTarget ? &aLineTarget : nullptr,
230 nullptr,
231 fFullDashDotLen);
233 if(pLineTarget)
235 pLineTarget->append(aLineTarget);
240 bool isInEpsilonRange(const B2DPolyPolygon& rCandidate, const B2DPoint& rTestPosition, double fDistance)
242 for(auto const& rPolygon : rCandidate)
244 if(isInEpsilonRange(rPolygon, rTestPosition, fDistance))
246 return true;
250 return false;
253 B3DPolyPolygon createB3DPolyPolygonFromB2DPolyPolygon(const B2DPolyPolygon& rCandidate, double fZCoordinate)
255 B3DPolyPolygon aRetval;
257 for(auto const& rPolygon : rCandidate)
259 aRetval.append(createB3DPolygonFromB2DPolygon(rPolygon, fZCoordinate));
262 return aRetval;
265 B2DPolyPolygon createB2DPolyPolygonFromB3DPolyPolygon(const B3DPolyPolygon& rCandidate, const B3DHomMatrix& rMat)
267 B2DPolyPolygon aRetval;
269 for(auto const& rPolygon : rCandidate)
271 aRetval.append(createB2DPolygonFromB3DPolygon(rPolygon, rMat));
274 return aRetval;
277 double getSmallestDistancePointToPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPoint& rTestPoint, sal_uInt32& rPolygonIndex, sal_uInt32& rEdgeIndex, double& rCut)
279 double fRetval(DBL_MAX);
280 const double fZero(0.0);
281 const sal_uInt32 nPolygonCount(rCandidate.count());
283 for(sal_uInt32 a(0); a < nPolygonCount; a++)
285 const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
286 sal_uInt32 nNewEdgeIndex;
287 double fNewCut(0.0);
288 const double fNewDistance(getSmallestDistancePointToPolygon(aCandidate, rTestPoint, nNewEdgeIndex, fNewCut));
290 if(fRetval == DBL_MAX || fNewDistance < fRetval)
292 fRetval = fNewDistance;
293 rPolygonIndex = a;
294 rEdgeIndex = nNewEdgeIndex;
295 rCut = fNewCut;
297 if(fTools::equal(fRetval, fZero))
299 // already found zero distance, cannot get better. Ensure numerical zero value and end loop.
300 fRetval = 0.0;
301 break;
306 return fRetval;
309 B2DPolyPolygon distort(const B2DPolyPolygon& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight)
311 B2DPolyPolygon aRetval;
313 for(auto const& rPolygon : rCandidate)
315 aRetval.append(distort(rPolygon, rOriginal, rTopLeft, rTopRight, rBottomLeft, rBottomRight));
318 return aRetval;
321 B2DPolyPolygon expandToCurve(const B2DPolyPolygon& rCandidate)
323 B2DPolyPolygon aRetval;
325 for(auto const& rPolygon : rCandidate)
327 aRetval.append(expandToCurve(rPolygon));
330 return aRetval;
333 B2DPolyPolygon growInNormalDirection(const B2DPolyPolygon& rCandidate, double fValue)
335 if(fValue != 0.0)
337 B2DPolyPolygon aRetval;
339 for(auto const& rPolygon : rCandidate)
341 aRetval.append(growInNormalDirection(rPolygon, fValue));
344 return aRetval;
346 else
348 return rCandidate;
352 B2DPolyPolygon reSegmentPolyPolygon(const B2DPolyPolygon& rCandidate, sal_uInt32 nSegments)
354 B2DPolyPolygon aRetval;
356 for(auto const& rPolygon : rCandidate)
358 aRetval.append(reSegmentPolygon(rPolygon, nSegments));
361 return aRetval;
364 B2DPolyPolygon interpolate(const B2DPolyPolygon& rOld1, const B2DPolyPolygon& rOld2, double t)
366 OSL_ENSURE(rOld1.count() == rOld2.count(), "B2DPolyPolygon interpolate: Different geometry (!)");
367 B2DPolyPolygon aRetval;
369 for(sal_uInt32 a(0); a < rOld1.count(); a++)
371 aRetval.append(interpolate(rOld1.getB2DPolygon(a), rOld2.getB2DPolygon(a), t));
374 return aRetval;
377 bool isRectangle( const B2DPolyPolygon& rPoly )
379 // exclude some cheap cases first
380 if( rPoly.count() != 1 )
381 return false;
383 return isRectangle( rPoly.getB2DPolygon(0) );
386 // #i76891#
387 B2DPolyPolygon simplifyCurveSegments(const B2DPolyPolygon& rCandidate)
389 if(rCandidate.areControlPointsUsed())
391 B2DPolyPolygon aRetval;
393 for(auto const& rPolygon : rCandidate)
395 aRetval.append(simplifyCurveSegments(rPolygon));
398 return aRetval;
400 else
402 return rCandidate;
406 B2DPolyPolygon snapPointsOfHorizontalOrVerticalEdges(const B2DPolyPolygon& rCandidate)
408 B2DPolyPolygon aRetval;
410 for(auto const& rPolygon : rCandidate)
412 aRetval.append(snapPointsOfHorizontalOrVerticalEdges(rPolygon));
415 return aRetval;
418 B2DPolyPolygon createSevenSegmentPolyPolygon(char nNumber, bool bLitSegments)
420 // config here
421 // {
422 const double fTotalSize=1.0;
423 const double fPosMiddleSegment=0.6;
424 const double fSegmentEndChopHoriz=0.08;
425 const double fSegmentEndChopVert =0.04;
426 // }
427 // config here
429 const double fLeft=0.0;
430 const double fRight=fTotalSize;
431 const double fTop=0.0;
432 const double fMiddle=fPosMiddleSegment;
433 const double fBottom=fTotalSize;
435 // from 0 to 5: pair of segment corner coordinates
437 // segment corner indices are these:
439 // 0 - 1
440 // | |
441 // 2 - 3
442 // | |
443 // 4 - 5
445 static const double corners[] =
447 fLeft, fTop,
448 fRight, fTop,
449 fLeft, fMiddle,
450 fRight, fMiddle,
451 fLeft, fBottom,
452 fRight, fBottom
455 // from 0 to 9: which segments are 'lit' for this number?
457 // array denotes graph edges to traverse, with -1 means
458 // stop (the vertices are the corner indices from above):
459 // 0
460 // -
461 // 1 | | 2
462 // - 3
463 // 4 | | 5
464 // -
465 // 6
467 static const int numbers[] =
469 1, 1, 1, 0, 1, 1, 1, // 0
470 0, 0, 1, 0, 0, 1, 0, // 1
471 1, 0, 1, 1, 1, 0, 1, // 2
472 1, 0, 1, 1, 0, 1, 1, // 3
473 0, 1, 1, 1, 0, 1, 0, // 4
474 1, 1, 0, 1, 0, 1, 1, // 5
475 1, 1, 0, 1, 1, 1, 1, // 6
476 1, 0, 1, 0, 0, 1, 0, // 1
477 1, 1, 1, 1, 1, 1, 1, // 8
478 1, 1, 1, 1, 0, 1, 1, // 9
479 0, 0, 0, 1, 0, 0, 0, // '-'
480 1, 1, 0, 1, 1, 0, 1, // 'E'
483 // maps segment index to two corner ids:
484 static const int index2corner[] =
486 0, 2, // 0
487 0, 4, // 1
488 2, 6, // 2
489 4, 6, // 3
490 4, 8, // 4
491 6, 10, // 5
492 8, 10, // 6
495 B2DPolyPolygon aRes;
496 if( nNumber == '-' )
498 nNumber = 10;
500 else if( nNumber == 'E' )
502 nNumber = 11;
504 else if( nNumber == '.' )
506 if( bLitSegments )
507 aRes.append(createPolygonFromCircle(B2DPoint(fTotalSize/2, fTotalSize),
508 fSegmentEndChopHoriz));
509 return aRes;
511 else
513 nNumber=std::clamp<sal_uInt32>(nNumber,'0','9') - '0';
516 B2DPolygon aCurrSegment;
517 const size_t sliceSize=std::size(numbers)/12;
518 const int* pCurrSegment=numbers + nNumber*sliceSize;
519 for( size_t i=0; i<sliceSize; i++, pCurrSegment++)
521 if( !(*pCurrSegment ^ int(bLitSegments)) )
523 const size_t j=2*i;
524 aCurrSegment.clear();
525 B2DPoint start(corners[index2corner[j]],
526 corners[index2corner[j]+1] );
527 B2DPoint end (corners[index2corner[j+1]],
528 corners[index2corner[j+1]+1]);
530 if( rtl::math::approxEqual(start.getX(), end.getX()) )
532 start.setY(start.getY()+fSegmentEndChopVert);
533 end.setY(end.getY()-fSegmentEndChopVert);
535 else
537 start.setX(start.getX()+fSegmentEndChopHoriz);
538 end.setX(end.getX()-fSegmentEndChopHoriz);
541 aCurrSegment.append(start);
542 aCurrSegment.append(end);
544 aRes.append(aCurrSegment);
547 return aRes;
550 // converters for css::drawing::PointSequence
552 B2DPolyPolygon UnoPointSequenceSequenceToB2DPolyPolygon(
553 const css::drawing::PointSequenceSequence& rPointSequenceSequenceSource)
555 B2DPolyPolygon aRetval;
556 aRetval.reserve(rPointSequenceSequenceSource.getLength());
558 for (auto& rPointSequence : rPointSequenceSequenceSource)
560 aRetval.append(UnoPointSequenceToB2DPolygon(rPointSequence));
563 return aRetval;
566 void B2DPolyPolygonToUnoPointSequenceSequence(
567 const B2DPolyPolygon& rPolyPolygon,
568 css::drawing::PointSequenceSequence& rPointSequenceSequenceRetval)
570 const sal_uInt32 nCount(rPolyPolygon.count());
572 if(nCount)
574 rPointSequenceSequenceRetval.realloc(nCount);
575 css::drawing::PointSequence* pPointSequence = rPointSequenceSequenceRetval.getArray();
577 for(auto const& rPolygon : rPolyPolygon)
579 B2DPolygonToUnoPointSequence(rPolygon, *pPointSequence);
580 pPointSequence++;
583 else
585 rPointSequenceSequenceRetval.realloc(0);
589 // converters for css::drawing::PolyPolygonBezierCoords (curved polygons)
591 B2DPolyPolygon UnoPolyPolygonBezierCoordsToB2DPolyPolygon(
592 const css::drawing::PolyPolygonBezierCoords& rPolyPolygonBezierCoordsSource)
594 B2DPolyPolygon aRetval;
595 const sal_Int32 nSequenceCount(rPolyPolygonBezierCoordsSource.Coordinates.getLength());
597 if(nSequenceCount)
599 OSL_ENSURE(nSequenceCount == rPolyPolygonBezierCoordsSource.Flags.getLength(),
600 "UnoPolyPolygonBezierCoordsToB2DPolyPolygon: unequal number of Points and Flags (!)");
602 for(sal_Int32 a(0); a < nSequenceCount; a++)
604 const B2DPolygon aNewPolygon(UnoPolygonBezierCoordsToB2DPolygon(
605 rPolyPolygonBezierCoordsSource.Coordinates[a],
606 rPolyPolygonBezierCoordsSource.Flags[a]));
608 aRetval.append(aNewPolygon);
612 return aRetval;
615 void B2DPolyPolygonToUnoPolyPolygonBezierCoords(
616 const B2DPolyPolygon& rPolyPolygon,
617 css::drawing::PolyPolygonBezierCoords& rPolyPolygonBezierCoordsRetval)
619 const sal_uInt32 nCount(rPolyPolygon.count());
621 if(nCount)
623 // prepare return value memory
624 rPolyPolygonBezierCoordsRetval.Coordinates.realloc(static_cast<sal_Int32>(nCount));
625 rPolyPolygonBezierCoordsRetval.Flags.realloc(static_cast<sal_Int32>(nCount));
627 // get pointers to arrays
628 css::drawing::PointSequence* pPointSequence = rPolyPolygonBezierCoordsRetval.Coordinates.getArray();
629 css::drawing::FlagSequence* pFlagSequence = rPolyPolygonBezierCoordsRetval.Flags.getArray();
631 for(auto const& rSource : rPolyPolygon)
633 B2DPolygonToUnoPolygonBezierCoords(
634 rSource,
635 *pPointSequence,
636 *pFlagSequence);
637 pPointSequence++;
638 pFlagSequence++;
641 else
643 rPolyPolygonBezierCoordsRetval.Coordinates.realloc(0);
644 rPolyPolygonBezierCoordsRetval.Flags.realloc(0);
648 } // end of namespace
650 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */