xref: /trunk/main/basegfx/source/polygon/b3dpolypolygontools.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
30*cdf0e10cSrcweir #include <basegfx/polygon/b3dpolypolygontools.hxx>
31*cdf0e10cSrcweir #include <basegfx/range/b3drange.hxx>
32*cdf0e10cSrcweir #include <basegfx/polygon/b3dpolypolygon.hxx>
33*cdf0e10cSrcweir #include <basegfx/polygon/b3dpolygon.hxx>
34*cdf0e10cSrcweir #include <basegfx/polygon/b3dpolygontools.hxx>
35*cdf0e10cSrcweir #include <numeric>
36*cdf0e10cSrcweir #include <basegfx/matrix/b3dhommatrix.hxx>
37*cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
38*cdf0e10cSrcweir #include <osl/mutex.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace basegfx
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir     namespace tools
45*cdf0e10cSrcweir     {
46*cdf0e10cSrcweir         // B3DPolyPolygon tools
47*cdf0e10cSrcweir         B3DRange getRange(const B3DPolyPolygon& rCandidate)
48*cdf0e10cSrcweir         {
49*cdf0e10cSrcweir             B3DRange aRetval;
50*cdf0e10cSrcweir             const sal_uInt32 nPolygonCount(rCandidate.count());
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < nPolygonCount; a++)
53*cdf0e10cSrcweir             {
54*cdf0e10cSrcweir                 B3DPolygon aCandidate = rCandidate.getB3DPolygon(a);
55*cdf0e10cSrcweir                 aRetval.expand(getRange(aCandidate));
56*cdf0e10cSrcweir             }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir             return aRetval;
59*cdf0e10cSrcweir         }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir         void applyLineDashing(const B3DPolyPolygon& rCandidate, const ::std::vector<double>& rDotDashArray, B3DPolyPolygon* pLineTarget, B3DPolyPolygon* pGapTarget, double fFullDashDotLen)
62*cdf0e10cSrcweir         {
63*cdf0e10cSrcweir             if(0.0 == fFullDashDotLen && rDotDashArray.size())
64*cdf0e10cSrcweir             {
65*cdf0e10cSrcweir                 // calculate fFullDashDotLen from rDotDashArray
66*cdf0e10cSrcweir                 fFullDashDotLen = ::std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
67*cdf0e10cSrcweir             }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir             if(rCandidate.count() && fFullDashDotLen > 0.0)
70*cdf0e10cSrcweir             {
71*cdf0e10cSrcweir                 B3DPolyPolygon aLineTarget, aGapTarget;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir                 for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
74*cdf0e10cSrcweir                 {
75*cdf0e10cSrcweir                     const B3DPolygon aCandidate(rCandidate.getB3DPolygon(a));
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir                     applyLineDashing(
78*cdf0e10cSrcweir                         aCandidate,
79*cdf0e10cSrcweir                         rDotDashArray,
80*cdf0e10cSrcweir                         pLineTarget ? &aLineTarget : 0,
81*cdf0e10cSrcweir                         pGapTarget ? &aGapTarget : 0,
82*cdf0e10cSrcweir                         fFullDashDotLen);
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir                     if(pLineTarget)
85*cdf0e10cSrcweir                     {
86*cdf0e10cSrcweir                         pLineTarget->append(aLineTarget);
87*cdf0e10cSrcweir                     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir                     if(pGapTarget)
90*cdf0e10cSrcweir                     {
91*cdf0e10cSrcweir                         pGapTarget->append(aGapTarget);
92*cdf0e10cSrcweir                     }
93*cdf0e10cSrcweir                 }
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         B3DPolyPolygon createUnitCubePolyPolygon()
98*cdf0e10cSrcweir         {
99*cdf0e10cSrcweir             static B3DPolyPolygon aRetval;
100*cdf0e10cSrcweir             ::osl::Mutex m_mutex;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             if(!aRetval.count())
103*cdf0e10cSrcweir             {
104*cdf0e10cSrcweir                 B3DPolygon aTemp;
105*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 0.0, 1.0));
106*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 1.0, 1.0));
107*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 1.0, 1.0));
108*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 0.0, 1.0));
109*cdf0e10cSrcweir                 aTemp.setClosed(true);
110*cdf0e10cSrcweir                 aRetval.append(aTemp);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir                 aTemp.clear();
113*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 0.0, 0.0));
114*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 1.0, 0.0));
115*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 1.0, 0.0));
116*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 0.0, 0.0));
117*cdf0e10cSrcweir                 aTemp.setClosed(true);
118*cdf0e10cSrcweir                 aRetval.append(aTemp);
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir                 aTemp.clear();
121*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 0.0, 0.0));
122*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 0.0, 1.0));
123*cdf0e10cSrcweir                 aRetval.append(aTemp);
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir                 aTemp.clear();
126*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 1.0, 0.0));
127*cdf0e10cSrcweir                 aTemp.append(B3DPoint(0.0, 1.0, 1.0));
128*cdf0e10cSrcweir                 aRetval.append(aTemp);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir                 aTemp.clear();
131*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 1.0, 0.0));
132*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 1.0, 1.0));
133*cdf0e10cSrcweir                 aRetval.append(aTemp);
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir                 aTemp.clear();
136*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 0.0, 0.0));
137*cdf0e10cSrcweir                 aTemp.append(B3DPoint(1.0, 0.0, 1.0));
138*cdf0e10cSrcweir                 aRetval.append(aTemp);
139*cdf0e10cSrcweir             }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir             return aRetval;
142*cdf0e10cSrcweir         }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         B3DPolyPolygon createUnitCubeFillPolyPolygon()
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             static B3DPolyPolygon aRetval;
147*cdf0e10cSrcweir             ::osl::Mutex m_mutex;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             if(!aRetval.count())
150*cdf0e10cSrcweir             {
151*cdf0e10cSrcweir                 B3DPolygon aTemp;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir                 // all points
154*cdf0e10cSrcweir                 const B3DPoint A(0.0, 0.0, 0.0);
155*cdf0e10cSrcweir                 const B3DPoint B(0.0, 1.0, 0.0);
156*cdf0e10cSrcweir                 const B3DPoint C(1.0, 1.0, 0.0);
157*cdf0e10cSrcweir                 const B3DPoint D(1.0, 0.0, 0.0);
158*cdf0e10cSrcweir                 const B3DPoint E(0.0, 0.0, 1.0);
159*cdf0e10cSrcweir                 const B3DPoint F(0.0, 1.0, 1.0);
160*cdf0e10cSrcweir                 const B3DPoint G(1.0, 1.0, 1.0);
161*cdf0e10cSrcweir                 const B3DPoint H(1.0, 0.0, 1.0);
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir                 // create bottom
164*cdf0e10cSrcweir                 aTemp.append(D);
165*cdf0e10cSrcweir                 aTemp.append(A);
166*cdf0e10cSrcweir                 aTemp.append(E);
167*cdf0e10cSrcweir                 aTemp.append(H);
168*cdf0e10cSrcweir                 aTemp.setClosed(true);
169*cdf0e10cSrcweir                 aRetval.append(aTemp);
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir                 // create front
172*cdf0e10cSrcweir                 aTemp.clear();
173*cdf0e10cSrcweir                 aTemp.append(B);
174*cdf0e10cSrcweir                 aTemp.append(A);
175*cdf0e10cSrcweir                 aTemp.append(D);
176*cdf0e10cSrcweir                 aTemp.append(C);
177*cdf0e10cSrcweir                 aTemp.setClosed(true);
178*cdf0e10cSrcweir                 aRetval.append(aTemp);
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir                 // create left
181*cdf0e10cSrcweir                 aTemp.clear();
182*cdf0e10cSrcweir                 aTemp.append(E);
183*cdf0e10cSrcweir                 aTemp.append(A);
184*cdf0e10cSrcweir                 aTemp.append(B);
185*cdf0e10cSrcweir                 aTemp.append(F);
186*cdf0e10cSrcweir                 aTemp.setClosed(true);
187*cdf0e10cSrcweir                 aRetval.append(aTemp);
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir                 // create top
190*cdf0e10cSrcweir                 aTemp.clear();
191*cdf0e10cSrcweir                 aTemp.append(C);
192*cdf0e10cSrcweir                 aTemp.append(G);
193*cdf0e10cSrcweir                 aTemp.append(F);
194*cdf0e10cSrcweir                 aTemp.append(B);
195*cdf0e10cSrcweir                 aTemp.setClosed(true);
196*cdf0e10cSrcweir                 aRetval.append(aTemp);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir                 // create right
199*cdf0e10cSrcweir                 aTemp.clear();
200*cdf0e10cSrcweir                 aTemp.append(H);
201*cdf0e10cSrcweir                 aTemp.append(G);
202*cdf0e10cSrcweir                 aTemp.append(C);
203*cdf0e10cSrcweir                 aTemp.append(D);
204*cdf0e10cSrcweir                 aTemp.setClosed(true);
205*cdf0e10cSrcweir                 aRetval.append(aTemp);
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir                 // create back
208*cdf0e10cSrcweir                 aTemp.clear();
209*cdf0e10cSrcweir                 aTemp.append(F);
210*cdf0e10cSrcweir                 aTemp.append(G);
211*cdf0e10cSrcweir                 aTemp.append(H);
212*cdf0e10cSrcweir                 aTemp.append(E);
213*cdf0e10cSrcweir                 aTemp.setClosed(true);
214*cdf0e10cSrcweir                 aRetval.append(aTemp);
215*cdf0e10cSrcweir             }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir             return aRetval;
218*cdf0e10cSrcweir         }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir         B3DPolyPolygon createCubePolyPolygonFromB3DRange( const B3DRange& rRange)
221*cdf0e10cSrcweir         {
222*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir             if(!rRange.isEmpty())
225*cdf0e10cSrcweir             {
226*cdf0e10cSrcweir                 aRetval = createUnitCubePolyPolygon();
227*cdf0e10cSrcweir                 B3DHomMatrix aTrans;
228*cdf0e10cSrcweir                 aTrans.scale(rRange.getWidth(), rRange.getHeight(), rRange.getDepth());
229*cdf0e10cSrcweir                 aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
230*cdf0e10cSrcweir                 aRetval.transform(aTrans);
231*cdf0e10cSrcweir                 aRetval.removeDoublePoints();
232*cdf0e10cSrcweir             }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             return aRetval;
235*cdf0e10cSrcweir         }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         B3DPolyPolygon createCubeFillPolyPolygonFromB3DRange( const B3DRange& rRange)
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir             if(!rRange.isEmpty())
242*cdf0e10cSrcweir             {
243*cdf0e10cSrcweir                 aRetval = createUnitCubeFillPolyPolygon();
244*cdf0e10cSrcweir                 B3DHomMatrix aTrans;
245*cdf0e10cSrcweir                 aTrans.scale(rRange.getWidth(), rRange.getHeight(), rRange.getDepth());
246*cdf0e10cSrcweir                 aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
247*cdf0e10cSrcweir                 aRetval.transform(aTrans);
248*cdf0e10cSrcweir                 aRetval.removeDoublePoints();
249*cdf0e10cSrcweir             }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir             return aRetval;
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         // helper for getting the 3D Point from given cartesian coordiantes. fVer is defined from
255*cdf0e10cSrcweir         // [F_PI2 .. -F_PI2], fHor from [0.0 .. F_2PI]
256*cdf0e10cSrcweir         inline B3DPoint getPointFromCartesian(double fVer, double fHor)
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             const double fCosHor(cos(fHor));
259*cdf0e10cSrcweir             return B3DPoint(fCosHor * cos(fVer), sin(fHor), fCosHor * -sin(fVer));
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         B3DPolyPolygon createUnitSpherePolyPolygon(
263*cdf0e10cSrcweir             sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
264*cdf0e10cSrcweir             double fVerStart, double fVerStop,
265*cdf0e10cSrcweir             double fHorStart, double fHorStop)
266*cdf0e10cSrcweir         {
267*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
268*cdf0e10cSrcweir             sal_uInt32 a, b;
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir             if(!nHorSeg)
271*cdf0e10cSrcweir             {
272*cdf0e10cSrcweir                 nHorSeg = fround(fabs(fHorStop - fHorStart) / (F_2PI / 24.0));
273*cdf0e10cSrcweir             }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir             if(!nHorSeg)
276*cdf0e10cSrcweir             {
277*cdf0e10cSrcweir                 nHorSeg = 1L;
278*cdf0e10cSrcweir             }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir             if(!nVerSeg)
281*cdf0e10cSrcweir             {
282*cdf0e10cSrcweir                 nVerSeg = fround(fabs(fVerStop - fVerStart) / (F_2PI / 24.0));
283*cdf0e10cSrcweir             }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir             if(!nVerSeg)
286*cdf0e10cSrcweir             {
287*cdf0e10cSrcweir                 nVerSeg = 1L;
288*cdf0e10cSrcweir             }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir             // create constants
291*cdf0e10cSrcweir             const double fVerDiffPerStep((fVerStop - fVerStart) / (double)nVerSeg);
292*cdf0e10cSrcweir             const double fHorDiffPerStep((fHorStop - fHorStart) / (double)nHorSeg);
293*cdf0e10cSrcweir             bool bHorClosed(fTools::equal(fHorStop - fHorStart, F_2PI));
294*cdf0e10cSrcweir             bool bVerFromTop(fTools::equal(fVerStart, F_PI2));
295*cdf0e10cSrcweir             bool bVerToBottom(fTools::equal(fVerStop, -F_PI2));
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir             // create horizontal rings
298*cdf0e10cSrcweir             const sal_uInt32 nLoopVerInit(bVerFromTop ? 1L : 0L);
299*cdf0e10cSrcweir             const sal_uInt32 nLoopVerLimit(bVerToBottom ? nVerSeg : nVerSeg + 1L);
300*cdf0e10cSrcweir             const sal_uInt32 nLoopHorLimit(bHorClosed ? nHorSeg : nHorSeg + 1L);
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir             for(a = nLoopVerInit; a < nLoopVerLimit; a++)
303*cdf0e10cSrcweir             {
304*cdf0e10cSrcweir                 const double fVer(fVerStart + ((double)(a) * fVerDiffPerStep));
305*cdf0e10cSrcweir                 B3DPolygon aNew;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir                 for(b = 0L; b < nLoopHorLimit; b++)
308*cdf0e10cSrcweir                 {
309*cdf0e10cSrcweir                     const double fHor(fHorStart + ((double)(b) * fHorDiffPerStep));
310*cdf0e10cSrcweir                     aNew.append(getPointFromCartesian(fHor, fVer));
311*cdf0e10cSrcweir                 }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir                 aNew.setClosed(bHorClosed);
314*cdf0e10cSrcweir                 aRetval.append(aNew);
315*cdf0e10cSrcweir             }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir             // create vertical half-rings
318*cdf0e10cSrcweir             for(a = 0L; a < nLoopHorLimit; a++)
319*cdf0e10cSrcweir             {
320*cdf0e10cSrcweir                 const double fHor(fHorStart + ((double)(a) * fHorDiffPerStep));
321*cdf0e10cSrcweir                 B3DPolygon aNew;
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir                 if(bVerFromTop)
324*cdf0e10cSrcweir                 {
325*cdf0e10cSrcweir                     aNew.append(B3DPoint(0.0, 1.0, 0.0));
326*cdf0e10cSrcweir                 }
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir                 for(b = nLoopVerInit; b < nLoopVerLimit; b++)
329*cdf0e10cSrcweir                 {
330*cdf0e10cSrcweir                     const double fVer(fVerStart + ((double)(b) * fVerDiffPerStep));
331*cdf0e10cSrcweir                     aNew.append(getPointFromCartesian(fHor, fVer));
332*cdf0e10cSrcweir                 }
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir                 if(bVerToBottom)
335*cdf0e10cSrcweir                 {
336*cdf0e10cSrcweir                     aNew.append(B3DPoint(0.0, -1.0, 0.0));
337*cdf0e10cSrcweir                 }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir                 aRetval.append(aNew);
340*cdf0e10cSrcweir             }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir             return aRetval;
343*cdf0e10cSrcweir         }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir         B3DPolyPolygon createSpherePolyPolygonFromB3DRange( const B3DRange& rRange,
346*cdf0e10cSrcweir             sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
347*cdf0e10cSrcweir             double fVerStart, double fVerStop,
348*cdf0e10cSrcweir             double fHorStart, double fHorStop)
349*cdf0e10cSrcweir         {
350*cdf0e10cSrcweir             B3DPolyPolygon aRetval(createUnitSpherePolyPolygon(nHorSeg, nVerSeg, fVerStart, fVerStop, fHorStart, fHorStop));
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir             if(aRetval.count())
353*cdf0e10cSrcweir             {
354*cdf0e10cSrcweir                 // move and scale whole construct which is now in [-1.0 .. 1.0] in all directions
355*cdf0e10cSrcweir                 B3DHomMatrix aTrans;
356*cdf0e10cSrcweir                 aTrans.translate(1.0, 1.0, 1.0);
357*cdf0e10cSrcweir                 aTrans.scale(rRange.getWidth() / 2.0, rRange.getHeight() / 2.0, rRange.getDepth() / 2.0);
358*cdf0e10cSrcweir                 aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
359*cdf0e10cSrcweir                 aRetval.transform(aTrans);
360*cdf0e10cSrcweir             }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir             return aRetval;
363*cdf0e10cSrcweir         }
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir         B3DPolyPolygon createUnitSphereFillPolyPolygon(
366*cdf0e10cSrcweir             sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
367*cdf0e10cSrcweir             bool bNormals,
368*cdf0e10cSrcweir             double fVerStart, double fVerStop,
369*cdf0e10cSrcweir             double fHorStart, double fHorStop)
370*cdf0e10cSrcweir         {
371*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir             if(!nHorSeg)
374*cdf0e10cSrcweir             {
375*cdf0e10cSrcweir                 nHorSeg = fround(fabs(fHorStop - fHorStart) / (F_2PI / 24.0));
376*cdf0e10cSrcweir             }
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir             if(!nHorSeg)
379*cdf0e10cSrcweir             {
380*cdf0e10cSrcweir                 nHorSeg = 1L;
381*cdf0e10cSrcweir             }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir             if(!nVerSeg)
384*cdf0e10cSrcweir             {
385*cdf0e10cSrcweir                 nVerSeg = fround(fabs(fVerStop - fVerStart) / (F_2PI / 24.0));
386*cdf0e10cSrcweir             }
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir             if(!nVerSeg)
389*cdf0e10cSrcweir             {
390*cdf0e10cSrcweir                 nVerSeg = 1L;
391*cdf0e10cSrcweir             }
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir             // vertical loop
394*cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < nVerSeg; a++)
395*cdf0e10cSrcweir             {
396*cdf0e10cSrcweir                 const double fVer(fVerStart + (((fVerStop - fVerStart) * a) / nVerSeg));
397*cdf0e10cSrcweir                 const double fVer2(fVerStart + (((fVerStop - fVerStart) * (a + 1)) / nVerSeg));
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir                 // horizontal loop
400*cdf0e10cSrcweir                 for(sal_uInt32 b(0L); b < nHorSeg; b++)
401*cdf0e10cSrcweir                 {
402*cdf0e10cSrcweir                     const double fHor(fHorStart + (((fHorStop - fHorStart) * b) / nHorSeg));
403*cdf0e10cSrcweir                     const double fHor2(fHorStart + (((fHorStop - fHorStart) * (b + 1)) / nHorSeg));
404*cdf0e10cSrcweir                     B3DPolygon aNew;
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir                     aNew.append(getPointFromCartesian(fHor, fVer));
407*cdf0e10cSrcweir                     aNew.append(getPointFromCartesian(fHor2, fVer));
408*cdf0e10cSrcweir                     aNew.append(getPointFromCartesian(fHor2, fVer2));
409*cdf0e10cSrcweir                     aNew.append(getPointFromCartesian(fHor, fVer2));
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir                     if(bNormals)
412*cdf0e10cSrcweir                     {
413*cdf0e10cSrcweir                         for(sal_uInt32 c(0L); c < aNew.count(); c++)
414*cdf0e10cSrcweir                         {
415*cdf0e10cSrcweir                             aNew.setNormal(c, ::basegfx::B3DVector(aNew.getB3DPoint(c)));
416*cdf0e10cSrcweir                         }
417*cdf0e10cSrcweir                     }
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir                     aNew.setClosed(true);
420*cdf0e10cSrcweir                     aRetval.append(aNew);
421*cdf0e10cSrcweir                 }
422*cdf0e10cSrcweir             }
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir             return aRetval;
425*cdf0e10cSrcweir         }
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir         B3DPolyPolygon createSphereFillPolyPolygonFromB3DRange( const B3DRange& rRange,
428*cdf0e10cSrcweir             sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
429*cdf0e10cSrcweir             bool bNormals,
430*cdf0e10cSrcweir             double fVerStart, double fVerStop,
431*cdf0e10cSrcweir             double fHorStart, double fHorStop)
432*cdf0e10cSrcweir         {
433*cdf0e10cSrcweir             B3DPolyPolygon aRetval(createUnitSphereFillPolyPolygon(nHorSeg, nVerSeg, bNormals, fVerStart, fVerStop, fHorStart, fHorStop));
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir             if(aRetval.count())
436*cdf0e10cSrcweir             {
437*cdf0e10cSrcweir                 // move and scale whole construct which is now in [-1.0 .. 1.0] in all directions
438*cdf0e10cSrcweir                 B3DHomMatrix aTrans;
439*cdf0e10cSrcweir                 aTrans.translate(1.0, 1.0, 1.0);
440*cdf0e10cSrcweir                 aTrans.scale(rRange.getWidth() / 2.0, rRange.getHeight() / 2.0, rRange.getDepth() / 2.0);
441*cdf0e10cSrcweir                 aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
442*cdf0e10cSrcweir                 aRetval.transform(aTrans);
443*cdf0e10cSrcweir             }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir             return aRetval;
446*cdf0e10cSrcweir         }
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir         B3DPolyPolygon applyDefaultNormalsSphere( const B3DPolyPolygon& rCandidate, const B3DPoint& rCenter)
449*cdf0e10cSrcweir         {
450*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
453*cdf0e10cSrcweir             {
454*cdf0e10cSrcweir                 aRetval.append(applyDefaultNormalsSphere(rCandidate.getB3DPolygon(a), rCenter));
455*cdf0e10cSrcweir             }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir             return aRetval;
458*cdf0e10cSrcweir         }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir         B3DPolyPolygon invertNormals( const B3DPolyPolygon& rCandidate)
461*cdf0e10cSrcweir         {
462*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
465*cdf0e10cSrcweir             {
466*cdf0e10cSrcweir                 aRetval.append(invertNormals(rCandidate.getB3DPolygon(a)));
467*cdf0e10cSrcweir             }
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir             return aRetval;
470*cdf0e10cSrcweir         }
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir         B3DPolyPolygon applyDefaultTextureCoordinatesParallel( const B3DPolyPolygon& rCandidate, const B3DRange& rRange, bool bChangeX, bool bChangeY)
473*cdf0e10cSrcweir         {
474*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
477*cdf0e10cSrcweir             {
478*cdf0e10cSrcweir                 aRetval.append(applyDefaultTextureCoordinatesParallel(rCandidate.getB3DPolygon(a), rRange, bChangeX, bChangeY));
479*cdf0e10cSrcweir             }
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir             return aRetval;
482*cdf0e10cSrcweir         }
483*cdf0e10cSrcweir 
484*cdf0e10cSrcweir         B3DPolyPolygon applyDefaultTextureCoordinatesSphere( const B3DPolyPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX, bool bChangeY)
485*cdf0e10cSrcweir         {
486*cdf0e10cSrcweir             B3DPolyPolygon aRetval;
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir             for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
489*cdf0e10cSrcweir             {
490*cdf0e10cSrcweir                 aRetval.append(applyDefaultTextureCoordinatesSphere(rCandidate.getB3DPolygon(a), rCenter, bChangeX, bChangeY));
491*cdf0e10cSrcweir             }
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir             return aRetval;
494*cdf0e10cSrcweir         }
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir         bool isInside(const B3DPolyPolygon& rCandidate, const B3DPoint& rPoint, bool bWithBorder)
497*cdf0e10cSrcweir         {
498*cdf0e10cSrcweir             const sal_uInt32 nPolygonCount(rCandidate.count());
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir             if(1L == nPolygonCount)
501*cdf0e10cSrcweir             {
502*cdf0e10cSrcweir                 return isInside(rCandidate.getB3DPolygon(0), rPoint, bWithBorder);
503*cdf0e10cSrcweir             }
504*cdf0e10cSrcweir             else
505*cdf0e10cSrcweir             {
506*cdf0e10cSrcweir                 sal_Int32 nInsideCount(0);
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir                 for(sal_uInt32 a(0); a < nPolygonCount; a++)
509*cdf0e10cSrcweir                 {
510*cdf0e10cSrcweir                     const B3DPolygon aPolygon(rCandidate.getB3DPolygon(a));
511*cdf0e10cSrcweir                     const bool bInside(isInside(aPolygon, rPoint, bWithBorder));
512*cdf0e10cSrcweir 
513*cdf0e10cSrcweir                     if(bInside)
514*cdf0e10cSrcweir                     {
515*cdf0e10cSrcweir                         nInsideCount++;
516*cdf0e10cSrcweir                     }
517*cdf0e10cSrcweir                 }
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir                 return (nInsideCount % 2L);
520*cdf0e10cSrcweir             }
521*cdf0e10cSrcweir         }
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////////
524*cdf0e10cSrcweir         // comparators with tolerance for 3D PolyPolygons
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir         bool equal(const B3DPolyPolygon& rCandidateA, const B3DPolyPolygon& rCandidateB, const double& rfSmallValue)
527*cdf0e10cSrcweir         {
528*cdf0e10cSrcweir             const sal_uInt32 nPolygonCount(rCandidateA.count());
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir             if(nPolygonCount != rCandidateB.count())
531*cdf0e10cSrcweir                 return false;
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir             for(sal_uInt32 a(0); a < nPolygonCount; a++)
534*cdf0e10cSrcweir             {
535*cdf0e10cSrcweir                 const B3DPolygon aCandidate(rCandidateA.getB3DPolygon(a));
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir                 if(!equal(aCandidate, rCandidateB.getB3DPolygon(a), rfSmallValue))
538*cdf0e10cSrcweir                     return false;
539*cdf0e10cSrcweir             }
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir             return true;
542*cdf0e10cSrcweir         }
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir         bool equal(const B3DPolyPolygon& rCandidateA, const B3DPolyPolygon& rCandidateB)
545*cdf0e10cSrcweir         {
546*cdf0e10cSrcweir             const double fSmallValue(fTools::getSmallValue());
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir             return equal(rCandidateA, rCandidateB, fSmallValue);
549*cdf0e10cSrcweir         }
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir     } // end of namespace tools
552*cdf0e10cSrcweir } // end of namespace basegfx
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir // eof
557