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 <sal/log.hxx>
21 #include <osl/diagnose.h>
23 #include <tools/poly.hxx>
24 #include <tools/debug.hxx>
25 #include <tools/stream.hxx>
26 #include <tools/vcompat.hxx>
27 #include <tools/gen.hxx>
28 #include <basegfx/polygon/b2dpolypolygon.hxx>
29 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
33 PolyPolygon::PolyPolygon( sal_uInt16 nInitSize
)
34 : mpImplPolyPolygon( ImplPolyPolygon( nInitSize
) )
38 PolyPolygon::PolyPolygon( const tools::Polygon
& rPoly
)
39 : mpImplPolyPolygon( rPoly
)
43 PolyPolygon::PolyPolygon( const tools::PolyPolygon
& rPolyPoly
)
44 : mpImplPolyPolygon( rPolyPoly
.mpImplPolyPolygon
)
48 PolyPolygon::PolyPolygon( tools::PolyPolygon
&& rPolyPoly
) noexcept
49 : mpImplPolyPolygon( std::move(rPolyPoly
.mpImplPolyPolygon
) )
53 PolyPolygon::~PolyPolygon()
57 void PolyPolygon::Insert( const tools::Polygon
& rPoly
, sal_uInt16 nPos
)
59 assert ( mpImplPolyPolygon
->mvPolyAry
.size() < MAX_POLYGONS
);
61 if ( nPos
> mpImplPolyPolygon
->mvPolyAry
.size() )
62 nPos
= mpImplPolyPolygon
->mvPolyAry
.size();
64 mpImplPolyPolygon
->mvPolyAry
.insert(mpImplPolyPolygon
->mvPolyAry
.begin() + nPos
, rPoly
);
67 void PolyPolygon::Remove( sal_uInt16 nPos
)
69 assert(nPos
< Count() && "PolyPolygon::Remove(): nPos >= nSize");
71 mpImplPolyPolygon
->mvPolyAry
.erase(mpImplPolyPolygon
->mvPolyAry
.begin() + nPos
);
74 void PolyPolygon::Replace( const tools::Polygon
& rPoly
, sal_uInt16 nPos
)
76 assert(nPos
< Count() && "PolyPolygon::Replace(): nPos >= nSize");
78 mpImplPolyPolygon
->mvPolyAry
[nPos
] = rPoly
;
81 const tools::Polygon
& PolyPolygon::GetObject( sal_uInt16 nPos
) const
83 assert(nPos
< Count() && "PolyPolygon::GetObject(): nPos >= nSize");
85 return mpImplPolyPolygon
->mvPolyAry
[nPos
];
88 bool PolyPolygon::IsRect() const
92 bIsRect
= mpImplPolyPolygon
->mvPolyAry
[ 0 ].IsRect();
96 void PolyPolygon::Clear()
98 mpImplPolyPolygon
->mvPolyAry
.clear();
101 void PolyPolygon::Optimize( PolyOptimizeFlags nOptimizeFlags
)
103 if(bool(nOptimizeFlags
) && Count())
105 // #115630# ImplDrawHatch does not work with beziers included in the polypolygon, take care of that
106 bool bIsCurve(false);
108 for(sal_uInt16
a(0); !bIsCurve
&& a
< Count(); a
++)
110 if((*this)[a
].HasFlags())
118 OSL_ENSURE(false, "Optimize does *not* support curves, falling back to AdaptiveSubdivide()...");
119 tools::PolyPolygon aPolyPoly
;
121 AdaptiveSubdivide(aPolyPoly
);
122 aPolyPoly
.Optimize(nOptimizeFlags
);
128 const bool bEdges
= ( nOptimizeFlags
& PolyOptimizeFlags::EDGES
) == PolyOptimizeFlags::EDGES
;
129 sal_uInt16 nPercent
= 0;
133 const tools::Rectangle
aBound( GetBoundRect() );
135 fArea
= ( aBound
.GetWidth() + aBound
.GetHeight() ) * 0.5;
137 nOptimizeFlags
&= ~PolyOptimizeFlags::EDGES
;
141 for( sal_uInt16 i
= 0, nPolyCount
= mpImplPolyPolygon
->mvPolyAry
.size(); i
< nPolyCount
; i
++ )
145 mpImplPolyPolygon
->mvPolyAry
[ i
].Optimize( PolyOptimizeFlags::NO_SAME
);
146 tools::Polygon::ImplReduceEdges( mpImplPolyPolygon
->mvPolyAry
[ i
], fArea
, nPercent
);
149 if( bool(nOptimizeFlags
) )
150 mpImplPolyPolygon
->mvPolyAry
[ i
].Optimize( nOptimizeFlags
);
156 void PolyPolygon::AdaptiveSubdivide( tools::PolyPolygon
& rResult
) const
160 tools::Polygon aPolygon
;
162 for( size_t i
= 0; i
< mpImplPolyPolygon
->mvPolyAry
.size(); i
++ )
164 mpImplPolyPolygon
->mvPolyAry
[ i
].AdaptiveSubdivide( aPolygon
, 1.0 );
165 rResult
.Insert( aPolygon
);
169 tools::PolyPolygon
PolyPolygon::SubdivideBezier( const tools::PolyPolygon
& rPolyPoly
)
171 sal_uInt16 i
, nPolys
= rPolyPoly
.Count();
172 tools::PolyPolygon
aPolyPoly( nPolys
);
173 for( i
=0; i
<nPolys
; ++i
)
174 aPolyPoly
.Insert( tools::Polygon::SubdivideBezier( rPolyPoly
.GetObject(i
) ) );
180 void PolyPolygon::GetIntersection( const tools::PolyPolygon
& rPolyPoly
, tools::PolyPolygon
& rResult
) const
182 ImplDoOperation( rPolyPoly
, rResult
, PolyClipOp::INTERSECT
);
185 void PolyPolygon::GetUnion( const tools::PolyPolygon
& rPolyPoly
, tools::PolyPolygon
& rResult
) const
187 ImplDoOperation( rPolyPoly
, rResult
, PolyClipOp::UNION
);
190 void PolyPolygon::ImplDoOperation( const tools::PolyPolygon
& rPolyPoly
, tools::PolyPolygon
& rResult
, PolyClipOp nOperation
) const
192 // Convert to B2DPolyPolygon, temporarily. It might be
193 // advantageous in the future, to have a tools::PolyPolygon adaptor that
194 // just simulates a B2DPolyPolygon here...
195 basegfx::B2DPolyPolygon
aMergePolyPolygonA( getB2DPolyPolygon() );
196 basegfx::B2DPolyPolygon
aMergePolyPolygonB( rPolyPoly
.getB2DPolyPolygon() );
198 // normalize the two polypolygons before. Force properly oriented
200 aMergePolyPolygonA
= basegfx::utils::prepareForPolygonOperation( aMergePolyPolygonA
);
201 aMergePolyPolygonB
= basegfx::utils::prepareForPolygonOperation( aMergePolyPolygonB
);
205 // All code extracted from svx/source/svdraw/svedtv2.cxx
207 case PolyClipOp::UNION
:
209 // merge A and B (OR)
210 aMergePolyPolygonA
= basegfx::utils::solvePolygonOperationOr(aMergePolyPolygonA
, aMergePolyPolygonB
);
215 case PolyClipOp::INTERSECT
:
217 // cut poly 1 against polys 2..n (AND)
218 aMergePolyPolygonA
= basegfx::utils::solvePolygonOperationAnd(aMergePolyPolygonA
, aMergePolyPolygonB
);
223 rResult
= tools::PolyPolygon( aMergePolyPolygonA
);
226 sal_uInt16
PolyPolygon::Count() const
228 return mpImplPolyPolygon
->mvPolyAry
.size();
231 void PolyPolygon::Move( long nHorzMove
, long nVertMove
)
233 // Required for DrawEngine
234 if( nHorzMove
|| nVertMove
)
237 sal_uInt16 nPolyCount
= mpImplPolyPolygon
->mvPolyAry
.size();
238 for ( sal_uInt16 i
= 0; i
< nPolyCount
; i
++ )
239 mpImplPolyPolygon
->mvPolyAry
[i
].Move( nHorzMove
, nVertMove
);
243 void PolyPolygon::Translate( const Point
& rTrans
)
246 for ( sal_uInt16 i
= 0, nCount
= mpImplPolyPolygon
->mvPolyAry
.size(); i
< nCount
; i
++ )
247 mpImplPolyPolygon
->mvPolyAry
[ i
].Translate( rTrans
);
250 void PolyPolygon::Scale( double fScaleX
, double fScaleY
)
253 for ( sal_uInt16 i
= 0, nCount
= mpImplPolyPolygon
->mvPolyAry
.size(); i
< nCount
; i
++ )
254 mpImplPolyPolygon
->mvPolyAry
[ i
].Scale( fScaleX
, fScaleY
);
257 void PolyPolygon::Rotate( const Point
& rCenter
, sal_uInt16 nAngle10
)
263 const double fAngle
= F_PI1800
* nAngle10
;
264 Rotate( rCenter
, sin( fAngle
), cos( fAngle
) );
268 void PolyPolygon::Rotate( const Point
& rCenter
, double fSin
, double fCos
)
271 for ( sal_uInt16 i
= 0, nCount
= mpImplPolyPolygon
->mvPolyAry
.size(); i
< nCount
; i
++ )
272 mpImplPolyPolygon
->mvPolyAry
[ i
].Rotate( rCenter
, fSin
, fCos
);
275 void PolyPolygon::Clip( const tools::Rectangle
& rRect
)
277 sal_uInt16 nPolyCount
= mpImplPolyPolygon
->mvPolyAry
.size();
283 // Clip every polygon, deleting the empty ones
284 for ( i
= 0; i
< nPolyCount
; i
++ )
285 mpImplPolyPolygon
->mvPolyAry
[i
].Clip( rRect
);
288 if ( GetObject( nPolyCount
-1 ).GetSize() <= 2 )
289 Remove( nPolyCount
-1 );
294 tools::Rectangle
PolyPolygon::GetBoundRect() const
296 long nXMin
=0, nXMax
=0, nYMin
=0, nYMax
=0;
298 sal_uInt16 nPolyCount
= mpImplPolyPolygon
->mvPolyAry
.size();
300 for ( sal_uInt16 n
= 0; n
< nPolyCount
; n
++ )
302 const tools::Polygon
* pPoly
= &mpImplPolyPolygon
->mvPolyAry
[n
];
303 const Point
* pAry
= pPoly
->GetConstPointAry();
304 sal_uInt16 nPointCount
= pPoly
->GetSize();
306 for ( sal_uInt16 i
= 0; i
< nPointCount
; i
++ )
308 const Point
* pPt
= &pAry
[ i
];
312 nXMin
= nXMax
= pPt
->X();
313 nYMin
= nYMax
= pPt
->Y();
318 if ( pPt
->X() < nXMin
)
320 if ( pPt
->X() > nXMax
)
322 if ( pPt
->Y() < nYMin
)
324 if ( pPt
->Y() > nYMax
)
331 return tools::Rectangle( nXMin
, nYMin
, nXMax
, nYMax
);
333 return tools::Rectangle();
336 Polygon
& PolyPolygon::operator[]( sal_uInt16 nPos
)
338 assert(nPos
< Count() && "PolyPolygon::[](): nPos >= nSize");
340 return mpImplPolyPolygon
->mvPolyAry
[nPos
];
343 PolyPolygon
& PolyPolygon::operator=( const tools::PolyPolygon
& rPolyPoly
)
345 mpImplPolyPolygon
= rPolyPoly
.mpImplPolyPolygon
;
349 PolyPolygon
& PolyPolygon::operator=( tools::PolyPolygon
&& rPolyPoly
) noexcept
351 mpImplPolyPolygon
= std::move(rPolyPoly
.mpImplPolyPolygon
);
355 bool PolyPolygon::operator==( const tools::PolyPolygon
& rPolyPoly
) const
357 return rPolyPoly
.mpImplPolyPolygon
== mpImplPolyPolygon
;
360 SvStream
& ReadPolyPolygon( SvStream
& rIStream
, tools::PolyPolygon
& rPolyPoly
)
362 sal_uInt16
nPolyCount(0);
364 // Read number of polygons
365 rIStream
.ReadUInt16( nPolyCount
);
367 const size_t nMinRecordSize
= sizeof(sal_uInt16
);
368 const size_t nMaxRecords
= rIStream
.remainingSize() / nMinRecordSize
;
369 if (nPolyCount
> nMaxRecords
)
371 SAL_WARN("tools", "Parsing error: " << nMaxRecords
<<
372 " max possible entries, but " << nPolyCount
<< " claimed, truncating");
373 nPolyCount
= nMaxRecords
;
378 rPolyPoly
.mpImplPolyPolygon
->mvPolyAry
.resize(nPolyCount
);
380 tools::Polygon aTempPoly
;
381 for ( sal_uInt16 i
= 0; i
< nPolyCount
; i
++ )
383 ReadPolygon( rIStream
, aTempPoly
);
384 rPolyPoly
.mpImplPolyPolygon
->mvPolyAry
[i
] = aTempPoly
;
388 rPolyPoly
= tools::PolyPolygon();
393 SvStream
& WritePolyPolygon( SvStream
& rOStream
, const tools::PolyPolygon
& rPolyPoly
)
395 // Write number of polygons
396 sal_uInt16 nPolyCount
= rPolyPoly
.mpImplPolyPolygon
->mvPolyAry
.size();
397 rOStream
.WriteUInt16( nPolyCount
);
400 for ( sal_uInt16 i
= 0; i
< nPolyCount
; i
++ )
401 WritePolygon( rOStream
, rPolyPoly
.mpImplPolyPolygon
->mvPolyAry
[i
] );
406 void PolyPolygon::Read( SvStream
& rIStream
)
408 VersionCompat
aCompat( rIStream
, StreamMode::READ
);
410 sal_uInt16
nPolyCount(0);
412 // Read number of polygons
413 rIStream
.ReadUInt16( nPolyCount
);
415 const size_t nMinRecordSize
= sizeof(sal_uInt16
);
416 const size_t nMaxRecords
= rIStream
.remainingSize() / nMinRecordSize
;
417 if (nPolyCount
> nMaxRecords
)
419 SAL_WARN("tools", "Parsing error: " << nMaxRecords
<<
420 " max possible entries, but " << nPolyCount
<< " claimed, truncating");
421 nPolyCount
= nMaxRecords
;
426 mpImplPolyPolygon
->mvPolyAry
.clear();
428 for ( sal_uInt16 i
= 0; i
< nPolyCount
; i
++ )
430 tools::Polygon aTempPoly
;
431 aTempPoly
.ImplRead( rIStream
);
432 mpImplPolyPolygon
->mvPolyAry
.emplace_back( aTempPoly
);
436 *this = tools::PolyPolygon();
439 void PolyPolygon::Write( SvStream
& rOStream
) const
441 VersionCompat
aCompat( rOStream
, StreamMode::WRITE
, 1 );
443 // Write number of polygons
444 sal_uInt16 nPolyCount
= mpImplPolyPolygon
->mvPolyAry
.size();
445 rOStream
.WriteUInt16( nPolyCount
);
448 for ( sal_uInt16 i
= 0; i
< nPolyCount
; i
++ )
449 mpImplPolyPolygon
->mvPolyAry
[i
].ImplWrite( rOStream
);
452 // convert to basegfx::B2DPolyPolygon and return
453 basegfx::B2DPolyPolygon
PolyPolygon::getB2DPolyPolygon() const
455 basegfx::B2DPolyPolygon aRetval
;
457 for(size_t a(0); a
< mpImplPolyPolygon
->mvPolyAry
.size(); a
++)
459 tools::Polygon
const & rCandidate
= mpImplPolyPolygon
->mvPolyAry
[a
];
460 aRetval
.append(rCandidate
.getB2DPolygon());
466 // constructor to convert from basegfx::B2DPolyPolygon
467 PolyPolygon::PolyPolygon(const basegfx::B2DPolyPolygon
& rPolyPolygon
)
468 : mpImplPolyPolygon(rPolyPolygon
)
472 } /* namespace tools */
474 ImplPolyPolygon::ImplPolyPolygon(const basegfx::B2DPolyPolygon
& rPolyPolygon
)
476 const sal_uInt16
nCount(sal_uInt16(rPolyPolygon
.count()));
477 DBG_ASSERT(sal_uInt32(nCount
) == rPolyPolygon
.count(),
478 "PolyPolygon::PolyPolygon: Too many sub-polygons in given basegfx::B2DPolyPolygon (!)");
482 mvPolyAry
.resize( nCount
);
484 for(sal_uInt16
a(0); a
< nCount
; a
++)
486 const basegfx::B2DPolygon
& aCandidate(rPolyPolygon
.getB2DPolygon(sal_uInt32(a
)));
487 mvPolyAry
[a
] = tools::Polygon( aCandidate
);
491 mvPolyAry
.reserve(16);
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */