Bump version to 6.4-15
[LibreOffice.git] / tools / source / generic / poly2.cxx
blobd37ba809f2fb87446f377bd362c031ffecf565f3
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 <sal/log.hxx>
21 #include <osl/diagnose.h>
22 #include <poly.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>
31 namespace tools {
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
90 bool bIsRect = false;
91 if ( Count() == 1 )
92 bIsRect = mpImplPolyPolygon->mvPolyAry[ 0 ].IsRect();
93 return bIsRect;
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())
112 bIsCurve = true;
116 if(bIsCurve)
118 OSL_ENSURE(false, "Optimize does *not* support curves, falling back to AdaptiveSubdivide()...");
119 tools::PolyPolygon aPolyPoly;
121 AdaptiveSubdivide(aPolyPoly);
122 aPolyPoly.Optimize(nOptimizeFlags);
123 *this = aPolyPoly;
125 else
127 double fArea;
128 const bool bEdges = ( nOptimizeFlags & PolyOptimizeFlags::EDGES ) == PolyOptimizeFlags::EDGES;
129 sal_uInt16 nPercent = 0;
131 if( bEdges )
133 const tools::Rectangle aBound( GetBoundRect() );
135 fArea = ( aBound.GetWidth() + aBound.GetHeight() ) * 0.5;
136 nPercent = 50;
137 nOptimizeFlags &= ~PolyOptimizeFlags::EDGES;
140 // Optimize polygons
141 for( sal_uInt16 i = 0, nPolyCount = mpImplPolyPolygon->mvPolyAry.size(); i < nPolyCount; i++ )
143 if( bEdges )
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
158 rResult.Clear();
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) ) );
176 return aPolyPoly;
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
199 // polygons.
200 aMergePolyPolygonA = basegfx::utils::prepareForPolygonOperation( aMergePolyPolygonA );
201 aMergePolyPolygonB = basegfx::utils::prepareForPolygonOperation( aMergePolyPolygonB );
203 switch( nOperation )
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);
211 break;
214 default:
215 case PolyClipOp::INTERSECT:
217 // cut poly 1 against polys 2..n (AND)
218 aMergePolyPolygonA = basegfx::utils::solvePolygonOperationAnd(aMergePolyPolygonA, aMergePolyPolygonB);
219 break;
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 )
236 // move points
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 )
245 // move points
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 )
252 // Move points
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 )
259 nAngle10 %= 3600;
261 if( 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 )
270 // move points
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();
278 sal_uInt16 i;
280 if ( !nPolyCount )
281 return;
283 // Clip every polygon, deleting the empty ones
284 for ( i = 0; i < nPolyCount; i++ )
285 mpImplPolyPolygon->mvPolyAry[i].Clip( rRect );
286 while ( nPolyCount )
288 if ( GetObject( nPolyCount-1 ).GetSize() <= 2 )
289 Remove( nPolyCount-1 );
290 nPolyCount--;
294 tools::Rectangle PolyPolygon::GetBoundRect() const
296 long nXMin=0, nXMax=0, nYMin=0, nYMax=0;
297 bool bFirst = true;
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 ];
310 if ( bFirst )
312 nXMin = nXMax = pPt->X();
313 nYMin = nYMax = pPt->Y();
314 bFirst = false;
316 else
318 if ( pPt->X() < nXMin )
319 nXMin = pPt->X();
320 if ( pPt->X() > nXMax )
321 nXMax = pPt->X();
322 if ( pPt->Y() < nYMin )
323 nYMin = pPt->Y();
324 if ( pPt->Y() > nYMax )
325 nYMax = pPt->Y();
330 if ( !bFirst )
331 return tools::Rectangle( nXMin, nYMin, nXMax, nYMax );
332 else
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;
346 return *this;
349 PolyPolygon& PolyPolygon::operator=( tools::PolyPolygon&& rPolyPoly ) noexcept
351 mpImplPolyPolygon = std::move(rPolyPoly.mpImplPolyPolygon);
352 return *this;
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;
376 if( nPolyCount )
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;
387 else
388 rPolyPoly = tools::PolyPolygon();
390 return rIStream;
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 );
399 // output polygons
400 for ( sal_uInt16 i = 0; i < nPolyCount; i++ )
401 WritePolygon( rOStream, rPolyPoly.mpImplPolyPolygon->mvPolyAry[i] );
403 return rOStream;
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;
424 if( nPolyCount )
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 );
435 else
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 );
447 // Output polygons
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());
463 return aRetval;
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 (!)");
480 if ( nCount )
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 );
490 else
491 mvPolyAry.reserve(16);
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */