Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / polygon / b3dpolypolygon.cxx
blob3fc6d7b13a37168484f72b960ac0b6522a8bbcf5
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 //////////////////////////////////////////////////////////////////////////////
32 class ImplB3DPolyPolygon
34 typedef ::std::vector< ::basegfx::B3DPolygon > PolygonVector;
36 PolygonVector maPolygons;
38 public:
39 ImplB3DPolyPolygon() : maPolygons()
43 explicit ImplB3DPolyPolygon(const ::basegfx::B3DPolygon& rToBeCopied) :
44 maPolygons(1,rToBeCopied)
48 bool operator==(const ImplB3DPolyPolygon& rPolygonList) const
50 // same polygon count?
51 if(maPolygons.size() != rPolygonList.maPolygons.size())
52 return false;
54 // compare polygon content
55 if(maPolygons != rPolygonList.maPolygons)
56 return false;
58 return true;
61 const ::basegfx::B3DPolygon& getB3DPolygon(sal_uInt32 nIndex) const
63 return maPolygons[nIndex];
66 void setB3DPolygon(sal_uInt32 nIndex, const ::basegfx::B3DPolygon& rPolygon)
68 maPolygons[nIndex] = rPolygon;
71 void insert(sal_uInt32 nIndex, const ::basegfx::B3DPolygon& rPolygon, sal_uInt32 nCount)
73 if(nCount)
75 // add nCount copies of rPolygon
76 PolygonVector::iterator aIndex(maPolygons.begin());
77 if( nIndex )
78 aIndex += nIndex;
79 maPolygons.insert(aIndex, nCount, rPolygon);
83 void insert(sal_uInt32 nIndex, const ::basegfx::B3DPolyPolygon& rPolyPolygon)
85 // add all polygons from rPolyPolygon
86 PolygonVector::iterator aIndex(maPolygons.begin());
87 if( nIndex )
88 aIndex += nIndex;
89 maPolygons.insert(aIndex, rPolyPolygon.begin(), rPolyPolygon.end());
92 void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
94 if(nCount)
96 // remove polygon data
97 PolygonVector::iterator aStart(maPolygons.begin());
98 aStart += nIndex;
99 const PolygonVector::iterator aEnd(aStart + nCount);
101 maPolygons.erase(aStart, aEnd);
105 sal_uInt32 count() const
107 return maPolygons.size();
110 void setClosed(bool bNew)
112 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
114 maPolygons[a].setClosed(bNew);
118 void flip()
120 std::for_each( maPolygons.begin(),
121 maPolygons.end(),
122 std::mem_fun_ref( &::basegfx::B3DPolygon::flip ));
125 void removeDoublePoints()
127 std::for_each( maPolygons.begin(),
128 maPolygons.end(),
129 std::mem_fun_ref( &::basegfx::B3DPolygon::removeDoublePoints ));
132 void transform(const ::basegfx::B3DHomMatrix& rMatrix)
134 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
136 maPolygons[a].transform(rMatrix);
140 void clearBColors()
142 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
144 maPolygons[a].clearBColors();
148 void transformNormals(const ::basegfx::B3DHomMatrix& rMatrix)
150 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
152 maPolygons[a].transformNormals(rMatrix);
156 void clearNormals()
158 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
160 maPolygons[a].clearNormals();
164 void transformTextureCoordiantes(const ::basegfx::B2DHomMatrix& rMatrix)
166 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
168 maPolygons[a].transformTextureCoordiantes(rMatrix);
172 void clearTextureCoordinates()
174 for(sal_uInt32 a(0L); a < maPolygons.size(); a++)
176 maPolygons[a].clearTextureCoordinates();
180 const basegfx::B3DPolygon* begin() const
182 if(maPolygons.empty())
183 return 0;
184 else
185 return &maPolygons.front();
188 const basegfx::B3DPolygon* end() const
190 if(maPolygons.empty())
191 return 0;
192 else
193 return (&maPolygons.back())+1;
196 basegfx::B3DPolygon* begin()
198 if(maPolygons.empty())
199 return 0;
200 else
201 return &maPolygons.front();
204 basegfx::B3DPolygon* end()
206 if(maPolygons.empty())
207 return 0;
208 else
209 return &(maPolygons.back())+1;
213 //////////////////////////////////////////////////////////////////////////////
215 namespace basegfx
217 namespace { struct DefaultPolyPolygon : public rtl::Static<B3DPolyPolygon::ImplType,
218 DefaultPolyPolygon> {}; }
220 B3DPolyPolygon::B3DPolyPolygon() :
221 mpPolyPolygon(DefaultPolyPolygon::get())
225 B3DPolyPolygon::B3DPolyPolygon(const B3DPolyPolygon& rPolyPolygon) :
226 mpPolyPolygon(rPolyPolygon.mpPolyPolygon)
230 B3DPolyPolygon::B3DPolyPolygon(const B3DPolygon& rPolygon) :
231 mpPolyPolygon( ImplB3DPolyPolygon(rPolygon) )
235 B3DPolyPolygon::~B3DPolyPolygon()
239 B3DPolyPolygon& B3DPolyPolygon::operator=(const B3DPolyPolygon& rPolyPolygon)
241 mpPolyPolygon = rPolyPolygon.mpPolyPolygon;
242 return *this;
245 bool B3DPolyPolygon::operator==(const B3DPolyPolygon& rPolyPolygon) const
247 if(mpPolyPolygon.same_object(rPolyPolygon.mpPolyPolygon))
248 return true;
250 return ((*mpPolyPolygon) == (*rPolyPolygon.mpPolyPolygon));
253 bool B3DPolyPolygon::operator!=(const B3DPolyPolygon& rPolyPolygon) const
255 return !(*this == rPolyPolygon);
258 sal_uInt32 B3DPolyPolygon::count() const
260 return mpPolyPolygon->count();
263 B3DPolygon B3DPolyPolygon::getB3DPolygon(sal_uInt32 nIndex) const
265 OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B3DPolyPolygon access outside range (!)");
267 return mpPolyPolygon->getB3DPolygon(nIndex);
270 void B3DPolyPolygon::setB3DPolygon(sal_uInt32 nIndex, const B3DPolygon& rPolygon)
272 OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B3DPolyPolygon access outside range (!)");
274 if(getB3DPolygon(nIndex) != rPolygon)
275 mpPolyPolygon->setB3DPolygon(nIndex, rPolygon);
278 bool B3DPolyPolygon::areBColorsUsed() const
280 for(sal_uInt32 a(0L); a < mpPolyPolygon->count(); a++)
282 if((mpPolyPolygon->getB3DPolygon(a)).areBColorsUsed())
284 return true;
288 return false;
291 void B3DPolyPolygon::clearBColors()
293 if(areBColorsUsed())
294 mpPolyPolygon->clearBColors();
297 void B3DPolyPolygon::transformNormals(const B3DHomMatrix& rMatrix)
299 if(!rMatrix.isIdentity())
300 mpPolyPolygon->transformNormals(rMatrix);
303 bool B3DPolyPolygon::areNormalsUsed() const
305 for(sal_uInt32 a(0L); a < mpPolyPolygon->count(); a++)
307 if((mpPolyPolygon->getB3DPolygon(a)).areNormalsUsed())
309 return true;
313 return false;
316 void B3DPolyPolygon::clearNormals()
318 if(areNormalsUsed())
319 mpPolyPolygon->clearNormals();
322 void B3DPolyPolygon::transformTextureCoordiantes(const B2DHomMatrix& rMatrix)
324 if(!rMatrix.isIdentity())
325 mpPolyPolygon->transformTextureCoordiantes(rMatrix);
328 bool B3DPolyPolygon::areTextureCoordinatesUsed() const
330 for(sal_uInt32 a(0L); a < mpPolyPolygon->count(); a++)
332 if((mpPolyPolygon->getB3DPolygon(a)).areTextureCoordinatesUsed())
334 return true;
338 return false;
341 void B3DPolyPolygon::clearTextureCoordinates()
343 if(areTextureCoordinatesUsed())
344 mpPolyPolygon->clearTextureCoordinates();
347 void B3DPolyPolygon::append(const B3DPolygon& rPolygon, sal_uInt32 nCount)
349 if(nCount)
350 mpPolyPolygon->insert(mpPolyPolygon->count(), rPolygon, nCount);
353 void B3DPolyPolygon::append(const B3DPolyPolygon& rPolyPolygon)
355 if(rPolyPolygon.count())
356 mpPolyPolygon->insert(mpPolyPolygon->count(), rPolyPolygon);
359 void B3DPolyPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
361 OSL_ENSURE(nIndex + nCount <= mpPolyPolygon->count(), "B3DPolyPolygon Remove outside range (!)");
363 if(nCount)
364 mpPolyPolygon->remove(nIndex, nCount);
367 void B3DPolyPolygon::clear()
369 mpPolyPolygon = DefaultPolyPolygon::get();
372 void B3DPolyPolygon::flip()
374 mpPolyPolygon->flip();
377 bool B3DPolyPolygon::hasDoublePoints() const
379 bool bRetval(false);
381 for(sal_uInt32 a(0L); !bRetval && a < mpPolyPolygon->count(); a++)
383 if((mpPolyPolygon->getB3DPolygon(a)).hasDoublePoints())
385 bRetval = true;
389 return bRetval;
392 void B3DPolyPolygon::removeDoublePoints()
394 if(hasDoublePoints())
395 mpPolyPolygon->removeDoublePoints();
398 void B3DPolyPolygon::transform(const B3DHomMatrix& rMatrix)
400 if(mpPolyPolygon->count() && !rMatrix.isIdentity())
402 mpPolyPolygon->transform(rMatrix);
406 const B3DPolygon* B3DPolyPolygon::begin() const
408 return mpPolyPolygon->begin();
411 const B3DPolygon* B3DPolyPolygon::end() const
413 return mpPolyPolygon->end();
416 B3DPolygon* B3DPolyPolygon::begin()
418 return mpPolyPolygon->begin();
421 B3DPolygon* B3DPolyPolygon::end()
423 return mpPolyPolygon->end();
425 } // end of namespace basegfx
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */