Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basegfx / source / polygon / b3dpolypolygon.cxx
blob7cb977065e0f5143e3961c773ec71049ce89c97a
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 <osl/diagnose.h>
21 #include <basegfx/polygon/b3dpolypolygon.hxx>
22 #include <basegfx/polygon/b3dpolygon.hxx>
23 #include <rtl/instance.hxx>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <basegfx/matrix/b3dhommatrix.hxx>
26 #include <functional>
27 #include <vector>
28 #include <algorithm>
30 class ImplB3DPolyPolygon
32 typedef std::vector< ::basegfx::B3DPolygon > PolygonVector;
34 PolygonVector maPolygons;
36 public:
37 ImplB3DPolyPolygon() : maPolygons()
41 explicit ImplB3DPolyPolygon(const ::basegfx::B3DPolygon& rToBeCopied) :
42 maPolygons(1,rToBeCopied)
46 bool operator==(const ImplB3DPolyPolygon& rPolygonList) const
48 // same polygon count?
49 if(maPolygons.size() != rPolygonList.maPolygons.size())
50 return false;
52 // compare polygon content
53 if(maPolygons != rPolygonList.maPolygons)
54 return false;
56 return true;
59 const ::basegfx::B3DPolygon& getB3DPolygon(sal_uInt32 nIndex) const
61 return maPolygons[nIndex];
64 void setB3DPolygon(sal_uInt32 nIndex, const ::basegfx::B3DPolygon& rPolygon)
66 maPolygons[nIndex] = rPolygon;
69 void insert(sal_uInt32 nIndex, const ::basegfx::B3DPolygon& rPolygon, sal_uInt32 nCount)
71 if(nCount)
73 // add nCount copies of rPolygon
74 PolygonVector::iterator aIndex(maPolygons.begin());
75 if( nIndex )
76 aIndex += nIndex;
77 maPolygons.insert(aIndex, nCount, rPolygon);
81 void insert(sal_uInt32 nIndex, const ::basegfx::B3DPolyPolygon& rPolyPolygon)
83 // add all polygons from rPolyPolygon
84 PolygonVector::iterator aIndex(maPolygons.begin());
85 if( nIndex )
86 aIndex += nIndex;
87 maPolygons.insert(aIndex, rPolyPolygon.begin(), rPolyPolygon.end());
90 void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
92 if(nCount)
94 // remove polygon data
95 PolygonVector::iterator aStart(maPolygons.begin());
96 aStart += nIndex;
97 const PolygonVector::iterator aEnd(aStart + nCount);
99 maPolygons.erase(aStart, aEnd);
103 sal_uInt32 count() const
105 return maPolygons.size();
108 void flip()
110 for (auto& aPolygon : maPolygons)
111 aPolygon.flip();
114 void removeDoublePoints()
116 for (auto& aPolygon : maPolygons)
117 aPolygon.removeDoublePoints();
120 void transform(const ::basegfx::B3DHomMatrix& rMatrix)
122 for (auto& aPolygon : maPolygons)
123 aPolygon.transform(rMatrix);
126 void clearBColors()
128 for (auto& aPolygon : maPolygons)
129 aPolygon.clearBColors();
132 void transformNormals(const ::basegfx::B3DHomMatrix& rMatrix)
134 for (auto& aPolygon : maPolygons)
135 aPolygon.transformNormals(rMatrix);
138 void clearNormals()
140 for (auto& aPolygon : maPolygons)
141 aPolygon.clearNormals();
144 void transformTextureCoordinates(const ::basegfx::B2DHomMatrix& rMatrix)
146 for (auto& aPolygon : maPolygons)
147 aPolygon.transformTextureCoordinates(rMatrix);
150 void clearTextureCoordinates()
152 for (auto& aPolygon : maPolygons)
153 aPolygon.clearTextureCoordinates();
156 const basegfx::B3DPolygon* begin() const
158 if (maPolygons.empty())
159 return nullptr;
160 else
161 return maPolygons.data();
164 const basegfx::B3DPolygon* end() const
166 if (maPolygons.empty())
167 return nullptr;
168 else
169 return maPolygons.data() + maPolygons.size();
172 basegfx::B3DPolygon* begin()
174 if (maPolygons.empty())
175 return nullptr;
176 else
177 return maPolygons.data();
180 basegfx::B3DPolygon* end()
182 if (maPolygons.empty())
183 return nullptr;
184 else
185 return maPolygons.data() + maPolygons.size();
189 namespace basegfx
191 namespace { struct DefaultPolyPolygon : public rtl::Static<B3DPolyPolygon::ImplType,
192 DefaultPolyPolygon> {}; }
194 B3DPolyPolygon::B3DPolyPolygon() :
195 mpPolyPolygon(DefaultPolyPolygon::get())
199 B3DPolyPolygon::B3DPolyPolygon(const B3DPolyPolygon&) = default;
201 B3DPolyPolygon::B3DPolyPolygon(B3DPolyPolygon&&) = default;
203 B3DPolyPolygon::B3DPolyPolygon(const B3DPolygon& rPolygon) :
204 mpPolyPolygon( ImplB3DPolyPolygon(rPolygon) )
208 B3DPolyPolygon::~B3DPolyPolygon() = default;
210 B3DPolyPolygon& B3DPolyPolygon::operator=(const B3DPolyPolygon&) = default;
212 B3DPolyPolygon& B3DPolyPolygon::operator=(B3DPolyPolygon&&) = default;
214 bool B3DPolyPolygon::operator==(const B3DPolyPolygon& rPolyPolygon) const
216 if(mpPolyPolygon.same_object(rPolyPolygon.mpPolyPolygon))
217 return true;
219 return ((*mpPolyPolygon) == (*rPolyPolygon.mpPolyPolygon));
222 bool B3DPolyPolygon::operator!=(const B3DPolyPolygon& rPolyPolygon) const
224 return !(*this == rPolyPolygon);
227 sal_uInt32 B3DPolyPolygon::count() const
229 return mpPolyPolygon->count();
232 B3DPolygon const & B3DPolyPolygon::getB3DPolygon(sal_uInt32 nIndex) const
234 OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B3DPolyPolygon access outside range (!)");
236 return mpPolyPolygon->getB3DPolygon(nIndex);
239 void B3DPolyPolygon::setB3DPolygon(sal_uInt32 nIndex, const B3DPolygon& rPolygon)
241 OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B3DPolyPolygon access outside range (!)");
243 if(getB3DPolygon(nIndex) != rPolygon)
244 mpPolyPolygon->setB3DPolygon(nIndex, rPolygon);
247 bool B3DPolyPolygon::areBColorsUsed() const
249 for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
251 if(mpPolyPolygon->getB3DPolygon(a).areBColorsUsed())
253 return true;
257 return false;
260 void B3DPolyPolygon::clearBColors()
262 if(areBColorsUsed())
263 mpPolyPolygon->clearBColors();
266 void B3DPolyPolygon::transformNormals(const B3DHomMatrix& rMatrix)
268 if(!rMatrix.isIdentity())
269 mpPolyPolygon->transformNormals(rMatrix);
272 bool B3DPolyPolygon::areNormalsUsed() const
274 for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
276 if(mpPolyPolygon->getB3DPolygon(a).areNormalsUsed())
278 return true;
282 return false;
285 void B3DPolyPolygon::clearNormals()
287 if(areNormalsUsed())
288 mpPolyPolygon->clearNormals();
291 void B3DPolyPolygon::transformTextureCoordinates(const B2DHomMatrix& rMatrix)
293 if(!rMatrix.isIdentity())
294 mpPolyPolygon->transformTextureCoordinates(rMatrix);
297 bool B3DPolyPolygon::areTextureCoordinatesUsed() const
299 for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
301 if(mpPolyPolygon->getB3DPolygon(a).areTextureCoordinatesUsed())
303 return true;
307 return false;
310 void B3DPolyPolygon::clearTextureCoordinates()
312 if(areTextureCoordinatesUsed())
313 mpPolyPolygon->clearTextureCoordinates();
316 void B3DPolyPolygon::append(const B3DPolygon& rPolygon, sal_uInt32 nCount)
318 if(nCount)
319 mpPolyPolygon->insert(mpPolyPolygon->count(), rPolygon, nCount);
322 void B3DPolyPolygon::append(const B3DPolyPolygon& rPolyPolygon)
324 if(rPolyPolygon.count())
325 mpPolyPolygon->insert(mpPolyPolygon->count(), rPolyPolygon);
328 void B3DPolyPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
330 OSL_ENSURE(nIndex + nCount <= mpPolyPolygon->count(), "B3DPolyPolygon Remove outside range (!)");
332 if(nCount)
333 mpPolyPolygon->remove(nIndex, nCount);
336 void B3DPolyPolygon::clear()
338 mpPolyPolygon = DefaultPolyPolygon::get();
341 void B3DPolyPolygon::flip()
343 mpPolyPolygon->flip();
346 bool B3DPolyPolygon::hasDoublePoints() const
348 bool bRetval(false);
350 for(sal_uInt32 a(0); !bRetval && a < mpPolyPolygon->count(); a++)
352 if(mpPolyPolygon->getB3DPolygon(a).hasDoublePoints())
354 bRetval = true;
358 return bRetval;
361 void B3DPolyPolygon::removeDoublePoints()
363 if(hasDoublePoints())
364 mpPolyPolygon->removeDoublePoints();
367 void B3DPolyPolygon::transform(const B3DHomMatrix& rMatrix)
369 if(mpPolyPolygon->count() && !rMatrix.isIdentity())
371 mpPolyPolygon->transform(rMatrix);
375 const B3DPolygon* B3DPolyPolygon::begin() const
377 return mpPolyPolygon->begin();
380 const B3DPolygon* B3DPolyPolygon::end() const
382 return mpPolyPolygon->end();
385 B3DPolygon* B3DPolyPolygon::begin()
387 return mpPolyPolygon->begin();
390 B3DPolygon* B3DPolyPolygon::end()
392 return mpPolyPolygon->end();
394 } // end of namespace basegfx
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */