use insert function instead of for loop
[LibreOffice.git] / basegfx / source / polygon / b2dpolypolygon.cxx
blobf7203a46d88e598a1fce3419b8a85f61d7c0f9c0
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/config.h>
22 #include <cassert>
23 #include <utility>
25 #include <basegfx/polygon/b2dpolypolygon.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/utils/systemdependentdata.hxx>
30 namespace basegfx
33 class ImplB2DPolyPolygon
35 basegfx::B2DPolygonVector maPolygons;
36 // we do not want to 'modify' the ImplB2DPolyPolygon,
37 // but add buffered data that is valid for all referencing instances
38 mutable std::unique_ptr<basegfx::SystemDependentDataHolder> mpSystemDependentDataHolder;
40 public:
41 ImplB2DPolyPolygon()
45 explicit ImplB2DPolyPolygon(ImplB2DPolyPolygon&& rSource) noexcept
46 : maPolygons(std::move(rSource.maPolygons))
50 explicit ImplB2DPolyPolygon(const ImplB2DPolyPolygon& rSource)
51 : maPolygons(rSource.maPolygons)
55 explicit ImplB2DPolyPolygon(const basegfx::B2DPolygon& rToBeCopied)
56 : maPolygons(1,rToBeCopied)
60 ImplB2DPolyPolygon& operator=(const ImplB2DPolyPolygon& rSource)
62 if (this != &rSource)
64 maPolygons = rSource.maPolygons;
65 mpSystemDependentDataHolder.reset();
68 return *this;
71 void addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData) const
73 if(!mpSystemDependentDataHolder)
75 mpSystemDependentDataHolder.reset(new basegfx::SystemDependentDataHolder());
78 mpSystemDependentDataHolder->addOrReplaceSystemDependentData(rData);
81 basegfx::SystemDependentData_SharedPtr getSystemDependentData(SDD_Type aType) const
83 if(!mpSystemDependentDataHolder)
85 return basegfx::SystemDependentData_SharedPtr();
88 return mpSystemDependentDataHolder->getSystemDependentData(aType);
91 bool operator==(const ImplB2DPolyPolygon& rPolygonList) const
93 return maPolygons == rPolygonList.maPolygons;
96 const basegfx::B2DPolygon& getB2DPolygon(sal_uInt32 nIndex) const
98 assert(nIndex < maPolygons.size());
99 return maPolygons[nIndex];
102 void setB2DPolygon(sal_uInt32 nIndex, const basegfx::B2DPolygon& rPolygon)
104 assert(nIndex < maPolygons.size());
105 maPolygons[nIndex] = rPolygon;
108 void insert(sal_uInt32 nIndex, const basegfx::B2DPolygon& rPolygon, sal_uInt32 nCount)
110 assert(nCount > 0);
111 assert(nIndex <= maPolygons.size());
112 // add nCount copies of rPolygon
113 maPolygons.insert(maPolygons.begin() + nIndex, nCount, rPolygon);
116 void append(const basegfx::B2DPolygon& rPolygon, sal_uInt32 nCount)
118 insert(maPolygons.size(), rPolygon, nCount);
121 void reserve(sal_uInt32 nCount)
123 maPolygons.reserve(nCount);
126 void insert(sal_uInt32 nIndex, const basegfx::B2DPolyPolygon& rPolyPolygon)
128 assert(nIndex <= maPolygons.size());
129 // add nCount polygons from rPolyPolygon
130 maPolygons.insert(maPolygons.begin() + nIndex, rPolyPolygon.begin(), rPolyPolygon.end());
133 void append(const basegfx::B2DPolyPolygon& rPolyPolygon)
135 insert(maPolygons.size(), rPolyPolygon);
138 void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
140 assert(nCount > 0);
141 assert(nIndex + nCount <= maPolygons.size());
142 // remove polygon data
143 auto aStart(maPolygons.begin() + nIndex);
144 auto aEnd(aStart + nCount);
146 maPolygons.erase(aStart, aEnd);
149 sal_uInt32 count() const
151 return maPolygons.size();
154 void setClosed(bool bNew)
156 for(basegfx::B2DPolygon & rPolygon : maPolygons)
158 rPolygon.setClosed(bNew);
162 void flip()
164 for (auto& aPolygon : maPolygons)
165 aPolygon.flip();
168 void removeDoublePoints()
170 for (auto& aPolygon : maPolygons)
171 aPolygon.removeDoublePoints();
174 void transform(const basegfx::B2DHomMatrix& rMatrix)
176 for (auto& aPolygon : maPolygons)
177 aPolygon.transform(rMatrix);
180 void makeUnique()
182 for (auto& aPolygon : maPolygons)
183 aPolygon.makeUnique();
186 const basegfx::B2DPolygon* begin() const
188 if (maPolygons.empty())
189 return nullptr;
190 else
191 return maPolygons.data();
194 const basegfx::B2DPolygon* end() const
196 if (maPolygons.empty())
197 return nullptr;
198 else
199 return maPolygons.data() + maPolygons.size();
202 basegfx::B2DPolygon* begin()
204 if (maPolygons.empty())
205 return nullptr;
206 else
207 return maPolygons.data();
210 basegfx::B2DPolygon* end()
212 if (maPolygons.empty())
213 return nullptr;
214 else
215 return maPolygons.data() + maPolygons.size();
219 B2DPolyPolygon::B2DPolyPolygon() = default;
221 B2DPolyPolygon::B2DPolyPolygon(const B2DPolyPolygon&) = default;
223 B2DPolyPolygon::B2DPolyPolygon(B2DPolyPolygon&&) = default;
225 B2DPolyPolygon::B2DPolyPolygon(const B2DPolygon& rPolygon) :
226 mpPolyPolygon( ImplB2DPolyPolygon(rPolygon) )
230 B2DPolyPolygon::~B2DPolyPolygon() = default;
232 B2DPolyPolygon& B2DPolyPolygon::operator=(const B2DPolyPolygon&) = default;
234 B2DPolyPolygon& B2DPolyPolygon::operator=(B2DPolyPolygon&&) = default;
236 void B2DPolyPolygon::makeUnique()
238 mpPolyPolygon->makeUnique(); // non-const cow_wrapper::operator-> calls make_unique
241 bool B2DPolyPolygon::operator==(const B2DPolyPolygon& rPolyPolygon) const
243 if(mpPolyPolygon.same_object(rPolyPolygon.mpPolyPolygon))
244 return true;
246 return ((*mpPolyPolygon) == (*rPolyPolygon.mpPolyPolygon));
249 sal_uInt32 B2DPolyPolygon::count() const
251 return mpPolyPolygon->count();
254 B2DPolygon const & B2DPolyPolygon::getB2DPolygon(sal_uInt32 nIndex) const
256 return mpPolyPolygon->getB2DPolygon(nIndex);
259 void B2DPolyPolygon::setB2DPolygon(sal_uInt32 nIndex, const B2DPolygon& rPolygon)
261 if(getB2DPolygon(nIndex) != rPolygon)
262 mpPolyPolygon->setB2DPolygon(nIndex, rPolygon);
265 bool B2DPolyPolygon::areControlPointsUsed() const
267 for(sal_uInt32 a(0); a < count(); a++)
269 if(getB2DPolygon(a).areControlPointsUsed())
271 return true;
275 return false;
278 void B2DPolyPolygon::insert(sal_uInt32 nIndex, const B2DPolygon& rPolygon, sal_uInt32 nCount)
280 if(nCount)
281 mpPolyPolygon->insert(nIndex, rPolygon, nCount);
284 void B2DPolyPolygon::append(const B2DPolygon& rPolygon, sal_uInt32 nCount)
286 if(nCount)
287 mpPolyPolygon->append(rPolygon, nCount);
290 void B2DPolyPolygon::reserve(sal_uInt32 nCount)
292 if(nCount)
293 mpPolyPolygon->reserve(nCount);
296 B2DPolyPolygon B2DPolyPolygon::getDefaultAdaptiveSubdivision() const
298 B2DPolyPolygon aRetval;
299 if (count())
301 ImplB2DPolyPolygon& dest = *aRetval.mpPolyPolygon;
302 dest.reserve(count());
304 for (sal_uInt32 a(0); a < count(); a++)
306 dest.append(getB2DPolygon(a).getDefaultAdaptiveSubdivision(), 1);
310 return aRetval;
313 B2DRange B2DPolyPolygon::getB2DRange() const
315 B2DRange aRetval;
317 for(sal_uInt32 a(0); a < count(); a++)
319 aRetval.expand(getB2DPolygon(a).getB2DRange());
322 return aRetval;
325 void B2DPolyPolygon::insert(sal_uInt32 nIndex, const B2DPolyPolygon& rPolyPolygon)
327 if(rPolyPolygon.count())
328 mpPolyPolygon->insert(nIndex, rPolyPolygon);
331 void B2DPolyPolygon::append(const B2DPolyPolygon& rPolyPolygon)
333 if(rPolyPolygon.count())
334 mpPolyPolygon->append(rPolyPolygon);
337 void B2DPolyPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
339 if(nCount)
340 mpPolyPolygon->remove(nIndex, nCount);
343 void B2DPolyPolygon::clear()
345 *mpPolyPolygon = ImplB2DPolyPolygon();
348 bool B2DPolyPolygon::isClosed() const
350 // PolyPOlygon is closed when all contained Polygons are closed or
351 // no Polygon exists.
352 for(sal_uInt32 a(0); a < count(); a++)
354 if(!getB2DPolygon(a).isClosed())
355 return false;
358 return true;
361 void B2DPolyPolygon::setClosed(bool bNew)
363 if(bNew != isClosed())
364 mpPolyPolygon->setClosed(bNew);
367 void B2DPolyPolygon::flip()
369 if(count())
371 mpPolyPolygon->flip();
375 bool B2DPolyPolygon::hasDoublePoints() const
377 for(sal_uInt32 a(0); a < count(); a++)
379 if(getB2DPolygon(a).hasDoublePoints())
380 return true;
383 return false;
386 void B2DPolyPolygon::removeDoublePoints()
388 if(hasDoublePoints())
389 mpPolyPolygon->removeDoublePoints();
392 void B2DPolyPolygon::transform(const B2DHomMatrix& rMatrix)
394 if(count() && !rMatrix.isIdentity())
396 mpPolyPolygon->transform(rMatrix);
400 const B2DPolygon* B2DPolyPolygon::begin() const
402 return mpPolyPolygon->begin();
405 const B2DPolygon* B2DPolyPolygon::end() const
407 return mpPolyPolygon->end();
410 B2DPolygon* B2DPolyPolygon::begin()
412 return mpPolyPolygon->begin();
415 B2DPolygon* B2DPolyPolygon::end()
417 return mpPolyPolygon->end();
420 void B2DPolyPolygon::addOrReplaceSystemDependentDataInternal(SystemDependentData_SharedPtr& rData) const
422 mpPolyPolygon->addOrReplaceSystemDependentData(rData);
425 SystemDependentData_SharedPtr B2DPolyPolygon::getSystemDependantDataInternal(SDD_Type aType) const
427 return mpPolyPolygon->getSystemDependentData(aType);
430 } // end of namespace basegfx
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */