1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
));
45 for(sal_uInt32
b(0); b
< nCount
; b
++)
49 const B2DPolygon
& aCompare(rCandidate
.getB2DPolygon(b
));
51 if(utils::isInside(aCompare
, aCandidate
, true))
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
);
65 aRetval
.setB2DPolygon(a
, aFlipped
);
72 B2DPolyPolygon
correctOutmostPolygon(const B2DPolyPolygon
& rCandidate
)
74 const sal_uInt32
nCount(rCandidate
.count());
78 for(sal_uInt32
a(0); a
< nCount
; a
++)
80 const B2DPolygon
& aCandidate(rCandidate
.getB2DPolygon(a
));
83 for(sal_uInt32
b(0); b
< nCount
; b
++)
87 const B2DPolygon
& aCompare(rCandidate
.getB2DPolygon(b
));
89 if(utils::isInside(aCompare
, aCandidate
, true))
98 B2DPolyPolygon
aRetval(rCandidate
);
102 // exchange polygon a and polygon 0
103 aRetval
.setB2DPolygon(0, aCandidate
);
104 aRetval
.setB2DPolygon(a
, rCandidate
.getB2DPolygon(0));
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
));
130 aRetval
.append(rPolygon
);
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
));
156 aRetval
.append(rPolygon
);
168 bool isInside(const B2DPolyPolygon
& rCandidate
, const B2DPoint
& rPoint
, bool bWithBorder
)
170 if(rCandidate
.count() == 1)
172 return isInside(rCandidate
.getB2DPolygon(0), rPoint
, bWithBorder
);
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
)
186 for(auto const& rPolygon
: rCandidate
)
188 aRetval
.expand(utils::getRange(rPolygon
));
194 double getSignedArea(const B2DPolyPolygon
& rCandidate
)
198 for(auto const& rPolygon
: rCandidate
)
200 fRetval
+= utils::getSignedArea(rPolygon
);
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))
222 B2DPolyPolygon aLineTarget
;
224 for(auto const& rPolygon
: rCandidate
)
229 pLineTarget
? &aLineTarget
: nullptr,
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
))
253 B3DPolyPolygon
createB3DPolyPolygonFromB2DPolyPolygon(const B2DPolyPolygon
& rCandidate
, double fZCoordinate
)
255 B3DPolyPolygon aRetval
;
257 for(auto const& rPolygon
: rCandidate
)
259 aRetval
.append(createB3DPolygonFromB2DPolygon(rPolygon
, fZCoordinate
));
265 B2DPolyPolygon
createB2DPolyPolygonFromB3DPolyPolygon(const B3DPolyPolygon
& rCandidate
, const B3DHomMatrix
& rMat
)
267 B2DPolyPolygon aRetval
;
269 for(auto const& rPolygon
: rCandidate
)
271 aRetval
.append(createB2DPolygonFromB3DPolygon(rPolygon
, rMat
));
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
;
288 const double fNewDistance(getSmallestDistancePointToPolygon(aCandidate
, rTestPoint
, nNewEdgeIndex
, fNewCut
));
290 if(fRetval
== DBL_MAX
|| fNewDistance
< fRetval
)
292 fRetval
= fNewDistance
;
294 rEdgeIndex
= nNewEdgeIndex
;
297 if(fTools::equal(fRetval
, fZero
))
299 // already found zero distance, cannot get better. Ensure numerical zero value and end loop.
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
));
321 B2DPolyPolygon
expandToCurve(const B2DPolyPolygon
& rCandidate
)
323 B2DPolyPolygon aRetval
;
325 for(auto const& rPolygon
: rCandidate
)
327 aRetval
.append(expandToCurve(rPolygon
));
333 B2DPolyPolygon
growInNormalDirection(const B2DPolyPolygon
& rCandidate
, double fValue
)
337 B2DPolyPolygon aRetval
;
339 for(auto const& rPolygon
: rCandidate
)
341 aRetval
.append(growInNormalDirection(rPolygon
, fValue
));
352 B2DPolyPolygon
reSegmentPolyPolygon(const B2DPolyPolygon
& rCandidate
, sal_uInt32 nSegments
)
354 B2DPolyPolygon aRetval
;
356 for(auto const& rPolygon
: rCandidate
)
358 aRetval
.append(reSegmentPolygon(rPolygon
, nSegments
));
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
));
377 bool isRectangle( const B2DPolyPolygon
& rPoly
)
379 // exclude some cheap cases first
380 if( rPoly
.count() != 1 )
383 return isRectangle( rPoly
.getB2DPolygon(0) );
387 B2DPolyPolygon
simplifyCurveSegments(const B2DPolyPolygon
& rCandidate
)
389 if(rCandidate
.areControlPointsUsed())
391 B2DPolyPolygon aRetval
;
393 for(auto const& rPolygon
: rCandidate
)
395 aRetval
.append(simplifyCurveSegments(rPolygon
));
406 B2DPolyPolygon
snapPointsOfHorizontalOrVerticalEdges(const B2DPolyPolygon
& rCandidate
)
408 B2DPolyPolygon aRetval
;
410 for(auto const& rPolygon
: rCandidate
)
412 aRetval
.append(snapPointsOfHorizontalOrVerticalEdges(rPolygon
));
418 B2DPolyPolygon
createSevenSegmentPolyPolygon(char nNumber
, bool bLitSegments
)
422 const double fTotalSize
=1.0;
423 const double fPosMiddleSegment
=0.6;
424 const double fSegmentEndChopHoriz
=0.08;
425 const double fSegmentEndChopVert
=0.04;
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:
445 static const double corners
[] =
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):
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
[] =
500 else if( nNumber
== 'E' )
504 else if( nNumber
== '.' )
507 aRes
.append(createPolygonFromCircle(B2DPoint(fTotalSize
/2, fTotalSize
),
508 fSegmentEndChopHoriz
));
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
)) )
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
);
537 start
.setX(start
.getX()+fSegmentEndChopHoriz
);
538 end
.setX(end
.getX()-fSegmentEndChopHoriz
);
541 aCurrSegment
.append(start
);
542 aCurrSegment
.append(end
);
544 aRes
.append(aCurrSegment
);
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
));
566 void B2DPolyPolygonToUnoPointSequenceSequence(
567 const B2DPolyPolygon
& rPolyPolygon
,
568 css::drawing::PointSequenceSequence
& rPointSequenceSequenceRetval
)
570 const sal_uInt32
nCount(rPolyPolygon
.count());
574 rPointSequenceSequenceRetval
.realloc(nCount
);
575 css::drawing::PointSequence
* pPointSequence
= rPointSequenceSequenceRetval
.getArray();
577 for(auto const& rPolygon
: rPolyPolygon
)
579 B2DPolygonToUnoPointSequence(rPolygon
, *pPointSequence
);
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());
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
);
615 void B2DPolyPolygonToUnoPolyPolygonBezierCoords(
616 const B2DPolyPolygon
& rPolyPolygon
,
617 css::drawing::PolyPolygonBezierCoords
& rPolyPolygonBezierCoordsRetval
)
619 const sal_uInt32
nCount(rPolyPolygon
.count());
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(
643 rPolyPolygonBezierCoordsRetval
.Coordinates
.realloc(0);
644 rPolyPolygonBezierCoordsRetval
.Flags
.realloc(0);
648 } // end of namespace
650 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */