Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/drawinglayer/source/primitive3d/sdrextrudelathetools3d.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <drawinglayer/primitive3d/sdrextrudelathetools3d.hxx>
21
22
#include <osl/diagnose.h>
23
#include <basegfx/polygon/b2dpolypolygon.hxx>
24
#include <basegfx/range/b2drange.hxx>
25
#include <basegfx/polygon/b2dpolypolygontools.hxx>
26
#include <basegfx/matrix/b2dhommatrix.hxx>
27
#include <basegfx/point/b3dpoint.hxx>
28
#include <basegfx/polygon/b3dpolygon.hxx>
29
#include <basegfx/polygon/b3dpolygontools.hxx>
30
#include <basegfx/polygon/b3dpolypolygontools.hxx>
31
#include <basegfx/range/b3drange.hxx>
32
#include <basegfx/matrix/b3dhommatrix.hxx>
33
#include <basegfx/polygon/b2dpolygontools.hxx>
34
#include <drawinglayer/geometry/viewinformation3d.hxx>
35
#include <numeric>
36
37
38
// decomposition helpers for extrude/lathe (rotation) objects
39
40
namespace
41
{
42
43
    // common helpers
44
45
    basegfx::B2DPolyPolygon impScalePolyPolygonOnCenter(
46
        const basegfx::B2DPolyPolygon& rSource,
47
        double fScale)
48
0
    {
49
0
        basegfx::B2DPolyPolygon aRetval(rSource);
50
51
0
        if(!basegfx::fTools::equalZero(fScale))
52
0
        {
53
0
            const basegfx::B2DRange aRange(rSource.getB2DRange());
54
0
            const basegfx::B2DPoint aCenter(aRange.getCenter());
55
0
            basegfx::B2DHomMatrix aTrans;
56
57
0
            aTrans.translate(-aCenter.getX(), -aCenter.getY());
58
0
            aTrans.scale(fScale, fScale);
59
0
            aTrans.translate(aCenter.getX(), aCenter.getY());
60
0
            aRetval.transform(aTrans);
61
0
        }
62
63
0
        return aRetval;
64
0
    }
65
66
    void impGetOuterPolyPolygon(
67
        basegfx::B2DPolyPolygon& rPolygon,
68
        basegfx::B2DPolyPolygon& rOuterPolyPolygon,
69
        double fOffset,
70
        bool bCharacterMode)
71
0
    {
72
0
        rOuterPolyPolygon = rPolygon;
73
74
0
        if (fOffset <= 0.0 || basegfx::fTools::equalZero(fOffset))
75
0
            return;
76
77
0
        if(bCharacterMode)
78
0
        {
79
            // grow the outside polygon and scale all polygons to original size. This is done
80
            // to avoid a shrink which potentially would lead to self-intersections, but changes
81
            // the original polygon -> not a precision step, so e.g. not usable for charts
82
0
            const basegfx::B2DRange aRange(rPolygon.getB2DRange());
83
0
            rPolygon = basegfx::utils::growInNormalDirection(rPolygon, fOffset);
84
0
            const basegfx::B2DRange aGrownRange(rPolygon.getB2DRange());
85
0
            const double fScaleX(basegfx::fTools::equalZero(aGrownRange.getWidth()) ? 1.0 : aRange.getWidth() / aGrownRange.getWidth());
86
0
            const double fScaleY(basegfx::fTools::equalZero(aGrownRange.getHeight())? 1.0 : aRange.getHeight() / aGrownRange.getHeight());
87
0
            basegfx::B2DHomMatrix aScaleTrans;
88
89
0
            aScaleTrans.translate(-aGrownRange.getMinX(), -aGrownRange.getMinY());
90
0
            aScaleTrans.scale(fScaleX, fScaleY);
91
0
            aScaleTrans.translate(aRange.getMinX(), aRange.getMinY());
92
0
            rPolygon.transform(aScaleTrans);
93
0
            rOuterPolyPolygon.transform(aScaleTrans);
94
0
        }
95
0
        else
96
0
        {
97
            // use more precision, shrink the outer polygons. Since this may lead to self-intersections,
98
            // some kind of correction should be applied here after that step
99
0
            rOuterPolyPolygon = basegfx::utils::growInNormalDirection(rPolygon, -fOffset);
100
            // basegfx::utils::correctGrowShrinkPolygonPair(rPolygon, rOuterPolyPolygon);
101
0
        }
102
0
    }
103
104
    void impAddInBetweenFill(
105
        basegfx::B3DPolyPolygon& rTarget,
106
        const basegfx::B3DPolyPolygon& rPolA,
107
        const basegfx::B3DPolyPolygon& rPolB,
108
        double fTexVerStart,
109
        double fTexVerStop,
110
        bool bCreateNormals,
111
        bool bCreateTextureCoordinates)
112
0
    {
113
0
        OSL_ENSURE(rPolA.count() == rPolB.count(), "impAddInBetweenFill: unequally sized polygons (!)");
114
0
        const sal_uInt32 nPolygonCount(std::min(rPolA.count(), rPolB.count()));
115
116
0
        for(sal_uInt32 a(0); a < nPolygonCount; a++)
117
0
        {
118
0
            const basegfx::B3DPolygon& aSubA(rPolA.getB3DPolygon(a));
119
0
            const basegfx::B3DPolygon& aSubB(rPolB.getB3DPolygon(a));
120
0
            OSL_ENSURE(aSubA.count() == aSubB.count(), "impAddInBetweenFill: unequally sized polygons (!)");
121
0
            const sal_uInt32 nPointCount(std::min(aSubA.count(), aSubB.count()));
122
123
0
            if(nPointCount)
124
0
            {
125
0
                const sal_uInt32 nEdgeCount(aSubA.isClosed() ? nPointCount : nPointCount - 1);
126
0
                double fTexHorMultiplicatorA(0.0), fTexHorMultiplicatorB(0.0);
127
0
                double fPolygonPosA(0.0), fPolygonPosB(0.0);
128
129
0
                if(bCreateTextureCoordinates)
130
0
                {
131
0
                    const double fPolygonLengthA(basegfx::utils::getLength(aSubA));
132
0
                    if (basegfx::fTools::equalZero(fPolygonLengthA))
133
0
                        fTexHorMultiplicatorA = 1.0;
134
0
                    else
135
0
                    {
136
0
                        assert(fPolygonLengthA != 0 && "help coverity see it's not zero");
137
0
                        fTexHorMultiplicatorA = 1.0 / fPolygonLengthA;
138
0
                    }
139
140
0
                    const double fPolygonLengthB(basegfx::utils::getLength(aSubB));
141
0
                    if (basegfx::fTools::equalZero(fPolygonLengthB))
142
0
                        fTexHorMultiplicatorB = 1.0;
143
0
                    else
144
0
                    {
145
0
                        assert(fPolygonLengthB != 0 && "help coverity see it's not zero");
146
0
                        fTexHorMultiplicatorB = 1.0 / fPolygonLengthB;
147
0
                    }
148
0
                }
149
150
0
                for(sal_uInt32 b(0); b < nEdgeCount; b++)
151
0
                {
152
0
                    const sal_uInt32 nIndexA(b);
153
0
                    const sal_uInt32 nIndexB((b + 1) % nPointCount);
154
155
0
                    const basegfx::B3DPoint aStartA(aSubA.getB3DPoint(nIndexA));
156
0
                    const basegfx::B3DPoint aEndA(aSubA.getB3DPoint(nIndexB));
157
0
                    const basegfx::B3DPoint aStartB(aSubB.getB3DPoint(nIndexA));
158
0
                    const basegfx::B3DPoint aEndB(aSubB.getB3DPoint(nIndexB));
159
160
0
                    basegfx::B3DPolygon aNew;
161
0
                    aNew.setClosed(true);
162
163
0
                    aNew.append(aStartA);
164
0
                    aNew.append(aStartB);
165
0
                    aNew.append(aEndB);
166
0
                    aNew.append(aEndA);
167
168
0
                    if(bCreateNormals)
169
0
                    {
170
0
                        aNew.setNormal(0, aSubA.getNormal(nIndexA));
171
0
                        aNew.setNormal(1, aSubB.getNormal(nIndexA));
172
0
                        aNew.setNormal(2, aSubB.getNormal(nIndexB));
173
0
                        aNew.setNormal(3, aSubA.getNormal(nIndexB));
174
0
                    }
175
176
0
                    if(bCreateTextureCoordinates)
177
0
                    {
178
0
                        const double fRelTexAL(fPolygonPosA * fTexHorMultiplicatorA);
179
0
                        const double fEdgeLengthA(basegfx::B3DVector(aEndA - aStartA).getLength());
180
0
                        fPolygonPosA += fEdgeLengthA;
181
0
                        const double fRelTexAR(fPolygonPosA * fTexHorMultiplicatorA);
182
183
0
                        const double fRelTexBL(fPolygonPosB * fTexHorMultiplicatorB);
184
0
                        const double fEdgeLengthB(basegfx::B3DVector(aEndB - aStartB).getLength());
185
0
                        fPolygonPosB += fEdgeLengthB;
186
0
                        const double fRelTexBR(fPolygonPosB * fTexHorMultiplicatorB);
187
188
0
                        aNew.setTextureCoordinate(0, basegfx::B2DPoint(fRelTexAL, fTexVerStart));
189
0
                        aNew.setTextureCoordinate(1, basegfx::B2DPoint(fRelTexBL, fTexVerStop));
190
0
                        aNew.setTextureCoordinate(2, basegfx::B2DPoint(fRelTexBR, fTexVerStop));
191
0
                        aNew.setTextureCoordinate(3, basegfx::B2DPoint(fRelTexAR, fTexVerStart));
192
0
                    }
193
194
0
                    rTarget.append(aNew);
195
0
                }
196
0
            }
197
0
        }
198
0
    }
199
200
    void impSetNormal(
201
        basegfx::B3DPolyPolygon& rCandidate,
202
        const basegfx::B3DVector& rNormal)
203
0
    {
204
0
        for(sal_uInt32 a(0); a < rCandidate.count(); a++)
205
0
        {
206
0
            basegfx::B3DPolygon aSub(rCandidate.getB3DPolygon(a));
207
208
0
            for(sal_uInt32 b(0); b < aSub.count(); b++)
209
0
            {
210
0
                aSub.setNormal(b, rNormal);
211
0
            }
212
213
0
            rCandidate.setB3DPolygon(a, aSub);
214
0
        }
215
0
    }
216
217
    void impCreateInBetweenNormals(
218
        basegfx::B3DPolyPolygon& rPolA,
219
        basegfx::B3DPolyPolygon& rPolB)
220
0
    {
221
0
        OSL_ENSURE(rPolA.count() == rPolB.count(), "sdrExtrudePrimitive3D: unequally sized polygons (!)");
222
0
        const sal_uInt32 nPolygonCount(std::min(rPolA.count(), rPolB.count()));
223
224
0
        for(sal_uInt32 a(0); a < nPolygonCount; a++)
225
0
        {
226
0
            basegfx::B3DPolygon aSubA(rPolA.getB3DPolygon(a));
227
0
            basegfx::B3DPolygon aSubB(rPolB.getB3DPolygon(a));
228
0
            OSL_ENSURE(aSubA.count() == aSubB.count(), "sdrExtrudePrimitive3D: unequally sized polygons (!)");
229
0
            const sal_uInt32 nPointCount(std::min(aSubA.count(), aSubB.count()));
230
231
0
            if(nPointCount)
232
0
            {
233
0
                basegfx::B3DPoint aPrevA(aSubA.getB3DPoint(nPointCount - 1));
234
0
                basegfx::B3DPoint aCurrA(aSubA.getB3DPoint(0));
235
0
                const bool bClosed(aSubA.isClosed());
236
237
0
                for(sal_uInt32 b(0); b < nPointCount; b++)
238
0
                {
239
0
                    const sal_uInt32 nIndNext((b + 1) % nPointCount);
240
0
                    const basegfx::B3DPoint aNextA(aSubA.getB3DPoint(nIndNext));
241
0
                    const basegfx::B3DPoint aCurrB(aSubB.getB3DPoint(b));
242
243
                    // vector to back
244
0
                    basegfx::B3DVector aDepth(aCurrB - aCurrA);
245
0
                    aDepth.normalize();
246
247
0
                    if(aDepth.equalZero())
248
0
                    {
249
                        // no difference, try to get depth from next point
250
0
                        const basegfx::B3DPoint aNextB(aSubB.getB3DPoint(nIndNext));
251
0
                        aDepth = aNextB - aNextA;
252
0
                        aDepth.normalize();
253
0
                    }
254
255
                    // vector to left (correct for non-closed lines)
256
0
                    const bool bFirstAndNotClosed(!bClosed && 0 == b);
257
0
                    basegfx::B3DVector aLeft(bFirstAndNotClosed ? aCurrA - aNextA : aPrevA - aCurrA);
258
0
                    aLeft.normalize();
259
260
                    // create left normal
261
0
                    const basegfx::B3DVector aNormalLeft(aDepth.getPerpendicular(aLeft));
262
263
                    // smooth horizontal normals
264
0
                    {
265
                        // vector to right (correct for non-closed lines)
266
0
                        const bool bLastAndNotClosed(!bClosed && b + 1 == nPointCount);
267
0
                        basegfx::B3DVector aRight(bLastAndNotClosed ? aCurrA - aPrevA : aNextA - aCurrA);
268
0
                        aRight.normalize();
269
270
                        // create right normal
271
0
                        const basegfx::B3DVector aNormalRight(aRight.getPerpendicular(aDepth));
272
273
                        // create smoothed in-between normal
274
0
                        basegfx::B3DVector aNewNormal(aNormalLeft + aNormalRight);
275
0
                        aNewNormal.normalize();
276
277
                        // set as new normal at polygons
278
0
                        aSubA.setNormal(b, aNewNormal);
279
0
                        aSubB.setNormal(b, aNewNormal);
280
0
                    }
281
282
                    // prepare next step
283
0
                    aPrevA = aCurrA;
284
0
                    aCurrA = aNextA;
285
0
                }
286
287
0
                rPolA.setB3DPolygon(a, aSubA);
288
0
                rPolB.setB3DPolygon(a, aSubB);
289
0
            }
290
0
        }
291
0
    }
292
293
    void impMixNormals(
294
        basegfx::B3DPolyPolygon& rPolA,
295
        const basegfx::B3DPolyPolygon& rPolB,
296
        double fWeightA)
297
0
    {
298
0
        const double fWeightB(1.0 - fWeightA);
299
0
        OSL_ENSURE(rPolA.count() == rPolB.count(), "sdrExtrudePrimitive3D: unequally sized polygons (!)");
300
0
        const sal_uInt32 nPolygonCount(std::min(rPolA.count(), rPolB.count()));
301
302
0
        for(sal_uInt32 a(0); a < nPolygonCount; a++)
303
0
        {
304
0
            basegfx::B3DPolygon aSubA(rPolA.getB3DPolygon(a));
305
0
            const basegfx::B3DPolygon& aSubB(rPolB.getB3DPolygon(a));
306
0
            OSL_ENSURE(aSubA.count() == aSubB.count(), "sdrExtrudePrimitive3D: unequally sized polygons (!)");
307
0
            const sal_uInt32 nPointCount(std::min(aSubA.count(), aSubB.count()));
308
309
0
            for(sal_uInt32 b(0); b < nPointCount; b++)
310
0
            {
311
0
                const basegfx::B3DVector aVA(aSubA.getNormal(b) * fWeightA);
312
0
                const basegfx::B3DVector aVB(aSubB.getNormal(b) * fWeightB);
313
0
                basegfx::B3DVector aVNew(aVA + aVB);
314
0
                aVNew.normalize();
315
0
                aSubA.setNormal(b, aVNew);
316
0
            }
317
318
0
            rPolA.setB3DPolygon(a, aSubA);
319
0
        }
320
0
    }
321
322
    bool impHasCutWith(const basegfx::B2DPolygon& rPoly, const basegfx::B2DPoint& rStart, const basegfx::B2DPoint& rEnd)
323
0
    {
324
        // polygon is closed, one of the points is a member
325
0
        const sal_uInt32 nPointCount(rPoly.count());
326
327
0
        if(!nPointCount)
328
0
            return false;
329
330
0
        basegfx::B2DPoint aCurrent(rPoly.getB2DPoint(0));
331
0
        const basegfx::B2DVector aVector(rEnd - rStart);
332
333
0
        for(sal_uInt32 a(0); a < nPointCount; a++)
334
0
        {
335
0
            const sal_uInt32 nNextIndex((a + 1) % nPointCount);
336
0
            const basegfx::B2DPoint aNext(rPoly.getB2DPoint(nNextIndex));
337
0
            const basegfx::B2DVector aEdgeVector(aNext - aCurrent);
338
339
0
            if(basegfx::utils::findCut(
340
0
                rStart, aVector,
341
0
                aCurrent, aEdgeVector) != CutFlagValue::NONE)
342
0
            {
343
0
                return true;
344
0
            }
345
346
0
            aCurrent = aNext;
347
0
        }
348
349
0
        return false;
350
0
    }
351
} // end of anonymous namespace
352
353
354
namespace drawinglayer::primitive3d
355
{
356
        void createLatheSlices(
357
            Slice3DVector& rSliceVector,
358
            const basegfx::B2DPolyPolygon& rSource,
359
            double fBackScale,
360
            double fDiagonal,
361
            double fRotation,
362
            sal_uInt32 nSteps,
363
            bool bCharacterMode,
364
            bool bCloseFront,
365
            bool bCloseBack)
366
0
        {
367
0
            if(basegfx::fTools::equalZero(fRotation) || 0 == nSteps)
368
0
            {
369
                // no rotation or no steps, just one plane
370
0
                rSliceVector.emplace_back(rSource, basegfx::B3DHomMatrix());
371
0
            }
372
0
            else
373
0
            {
374
0
                const bool bBackScale(!basegfx::fTools::equal(fBackScale, 1.0));
375
0
                const bool bClosedRotation(!bBackScale && basegfx::fTools::equal(fRotation, 2 * M_PI));
376
0
                basegfx::B2DPolyPolygon aFront(rSource);
377
0
                basegfx::B2DPolyPolygon aBack(rSource);
378
0
                basegfx::B3DHomMatrix aTransformBack;
379
0
                basegfx::B2DPolyPolygon aOuterBack;
380
381
0
                if(bClosedRotation)
382
0
                {
383
0
                    bCloseFront = bCloseBack = false;
384
0
                }
385
386
0
                if(bBackScale)
387
0
                {
388
                    // avoid null zoom
389
0
                    if(basegfx::fTools::equalZero(fBackScale))
390
0
                    {
391
0
                        fBackScale = 0.000001;
392
0
                    }
393
394
                    // back is scaled compared to front, create scaled version
395
0
                    aBack = impScalePolyPolygonOnCenter(aBack, fBackScale);
396
0
                }
397
398
0
                if(bCloseFront || bCloseBack)
399
0
                {
400
0
                    const basegfx::B2DRange aBaseRange(aFront.getB2DRange());
401
0
                    const double fOuterLength(aBaseRange.getMaxX() * fRotation);
402
0
                    const double fInnerLength(aBaseRange.getMinX() * fRotation);
403
0
                    const double fAverageLength((fOuterLength + fInnerLength) * 0.5);
404
405
0
                    if(bCloseFront)
406
0
                    {
407
0
                        const double fOffsetLen((fAverageLength / 12.0) * fDiagonal);
408
0
                        basegfx::B2DPolyPolygon aOuterFront;
409
0
                        impGetOuterPolyPolygon(aFront, aOuterFront, fOffsetLen, bCharacterMode);
410
0
                        basegfx::B3DHomMatrix aTransform;
411
0
                        aTransform.translate(0.0, 0.0, fOffsetLen);
412
0
                        rSliceVector.emplace_back(aOuterFront, aTransform, SLICETYPE3D_FRONTCAP);
413
0
                    }
414
415
0
                    if(bCloseBack)
416
0
                    {
417
0
                        const double fOffsetLen((fAverageLength / 12.0) * fDiagonal);
418
0
                        impGetOuterPolyPolygon(aBack, aOuterBack, fOffsetLen, bCharacterMode);
419
0
                        aTransformBack.translate(0.0, 0.0, -fOffsetLen);
420
0
                        aTransformBack.rotate(0.0, fRotation, 0.0);
421
0
                    }
422
0
                }
423
424
                // add start polygon (a = 0)
425
0
                if(!bClosedRotation)
426
0
                {
427
0
                    rSliceVector.emplace_back(aFront, basegfx::B3DHomMatrix());
428
0
                }
429
430
                // create segments (a + 1 .. nSteps)
431
0
                const double fStepSize(1.0 / static_cast<double>(nSteps));
432
433
0
                for(sal_uInt32 a(0); a < nSteps; a++)
434
0
                {
435
0
                    const double fStep(static_cast<double>(a + 1) * fStepSize);
436
0
                    basegfx::B2DPolyPolygon aNewPoly(bBackScale ? basegfx::utils::interpolate(aFront, aBack, fStep) : aFront);
437
0
                    basegfx::B3DHomMatrix aNewMat;
438
0
                    aNewMat.rotate(0.0, fRotation * fStep, 0.0);
439
0
                    rSliceVector.emplace_back(aNewPoly, aNewMat);
440
0
                }
441
442
0
                if(bCloseBack)
443
0
                {
444
0
                    rSliceVector.emplace_back(aOuterBack, aTransformBack, SLICETYPE3D_BACKCAP);
445
0
                }
446
0
            }
447
0
        }
448
449
        void createExtrudeSlices(
450
            Slice3DVector& rSliceVector,
451
            const basegfx::B2DPolyPolygon& rSource,
452
            double fBackScale,
453
            double fDiagonal,
454
            double fDepth,
455
            bool bCharacterMode,
456
            bool bCloseFront,
457
            bool bCloseBack)
458
0
        {
459
0
            if(basegfx::fTools::equalZero(fDepth))
460
0
            {
461
                // no depth, just one plane
462
0
                rSliceVector.emplace_back(rSource, basegfx::B3DHomMatrix());
463
0
            }
464
0
            else
465
0
            {
466
                // there is depth, create Polygons for front,back and their default depth positions
467
0
                basegfx::B2DPolyPolygon aFront(rSource);
468
0
                basegfx::B2DPolyPolygon aBack(rSource);
469
0
                const bool bBackScale(!basegfx::fTools::equal(fBackScale, 1.0));
470
0
                double fZFront(fDepth); // default depth for aFront
471
0
                double fZBack(0.0); // default depth for aBack
472
0
                basegfx::B2DPolyPolygon aOuterBack;
473
474
0
                if(bBackScale)
475
0
                {
476
                    // avoid null zoom
477
0
                    if(basegfx::fTools::equalZero(fBackScale))
478
0
                    {
479
0
                        fBackScale = 0.000001;
480
0
                    }
481
482
                    // aFront is scaled compared to aBack, create scaled version
483
0
                    aFront = impScalePolyPolygonOnCenter(aFront, fBackScale);
484
0
                }
485
486
0
                if(bCloseFront)
487
0
                {
488
0
                    const double fOffset(fDepth * fDiagonal * 0.5);
489
0
                    fZFront = fDepth - fOffset;
490
0
                    basegfx::B2DPolyPolygon aOuterFront;
491
0
                    impGetOuterPolyPolygon(aFront, aOuterFront, fOffset, bCharacterMode);
492
0
                    basegfx::B3DHomMatrix aTransformFront;
493
0
                    aTransformFront.translate(0.0, 0.0, fDepth);
494
0
                    rSliceVector.emplace_back(aOuterFront, aTransformFront, SLICETYPE3D_FRONTCAP);
495
0
                }
496
497
0
                if(bCloseBack)
498
0
                {
499
0
                    const double fOffset(fDepth * fDiagonal * 0.5);
500
0
                    fZBack = fOffset;
501
0
                    impGetOuterPolyPolygon(aBack, aOuterBack, fOffset, bCharacterMode);
502
0
                }
503
504
                // add front and back polygons at evtl. changed depths
505
0
                {
506
0
                    basegfx::B3DHomMatrix aTransformA, aTransformB;
507
508
0
                    aTransformA.translate(0.0, 0.0, fZFront);
509
0
                    rSliceVector.emplace_back(aFront, aTransformA);
510
511
0
                    aTransformB.translate(0.0, 0.0, fZBack);
512
0
                    rSliceVector.emplace_back(aBack, aTransformB);
513
0
                }
514
515
0
                if(bCloseBack)
516
0
                {
517
0
                    rSliceVector.emplace_back(aOuterBack, basegfx::B3DHomMatrix(), SLICETYPE3D_BACKCAP);
518
0
                }
519
0
            }
520
0
        }
521
522
        basegfx::B3DPolyPolygon extractHorizontalLinesFromSlice(const Slice3DVector& rSliceVector, bool bCloseHorLines)
523
0
        {
524
0
            basegfx::B3DPolyPolygon aRetval;
525
0
            const sal_uInt32 nNumSlices(rSliceVector.size());
526
527
0
            if(nNumSlices)
528
0
            {
529
0
                const sal_uInt32 nSlideSubPolygonCount(rSliceVector[0].getB3DPolyPolygon().count());
530
531
0
                for(sal_uInt32 b(0); b < nSlideSubPolygonCount; b++)
532
0
                {
533
0
                    const sal_uInt32 nSubPolygonPointCount(rSliceVector[0].getB3DPolyPolygon().getB3DPolygon(b).count());
534
535
0
                    for(sal_uInt32 c(0); c < nSubPolygonPointCount; c++)
536
0
                    {
537
0
                        basegfx::B3DPolygon aNew;
538
539
0
                        for(sal_uInt32 d(0); d < nNumSlices; d++)
540
0
                        {
541
0
                            const bool bSamePolygonCount(nSlideSubPolygonCount == rSliceVector[d].getB3DPolyPolygon().count());
542
0
                            const bool bSamePointCount(nSubPolygonPointCount == rSliceVector[d].getB3DPolyPolygon().getB3DPolygon(b).count());
543
544
0
                            if(bSamePolygonCount && bSamePointCount)
545
0
                            {
546
0
                                aNew.append(rSliceVector[d].getB3DPolyPolygon().getB3DPolygon(b).getB3DPoint(c));
547
0
                            }
548
0
                            else
549
0
                            {
550
0
                                OSL_ENSURE(bSamePolygonCount, "Slice tools::PolyPolygon with different Polygon count (!)");
551
0
                                OSL_ENSURE(bSamePointCount, "Slice Polygon with different point count (!)");
552
0
                            }
553
0
                        }
554
555
0
                        aNew.setClosed(bCloseHorLines);
556
0
                        aRetval.append(aNew);
557
0
                    }
558
0
                }
559
0
            }
560
561
0
            return aRetval;
562
0
        }
563
564
        basegfx::B3DPolyPolygon  extractVerticalLinesFromSlice(const Slice3DVector& rSliceVector)
565
0
        {
566
0
            basegfx::B3DPolyPolygon aRetval;
567
0
            const sal_uInt32 nNumSlices(rSliceVector.size());
568
569
0
            for(sal_uInt32 a(0); a < nNumSlices; a++)
570
0
            {
571
0
                aRetval.append(rSliceVector[a].getB3DPolyPolygon());
572
0
            }
573
574
0
            return aRetval;
575
0
        }
576
577
        void extractPlanesFromSlice(
578
            std::vector< basegfx::B3DPolyPolygon >& rFill,
579
            const Slice3DVector& rSliceVector,
580
            bool bCreateNormals,
581
            bool bSmoothNormals,
582
            bool bSmoothLids,
583
            bool bClosed,
584
            double fSmoothNormalsMix,
585
            double fSmoothLidsMix,
586
            bool bCreateTextureCoordinates,
587
            const basegfx::B2DHomMatrix& rTexTransform)
588
0
        {
589
0
            const sal_uInt32 nNumSlices(rSliceVector.size());
590
591
0
            if(!nNumSlices)
592
0
                return;
593
594
            // common parameters
595
0
            const sal_uInt32 nLoopCount(bClosed ? nNumSlices : nNumSlices - 1);
596
0
            basegfx::B3DPolyPolygon aEdgeRounding;
597
0
            sal_uInt32 a;
598
599
            // texture parameters
600
0
            double fInvTexHeight(1.0);
601
0
            std::vector<double> aTexHeightArray;
602
0
            basegfx::B3DRange aTexRangeFront;
603
0
            basegfx::B3DRange aTexRangeBack;
604
605
0
            if(bCreateTextureCoordinates)
606
0
            {
607
0
                aTexRangeFront = basegfx::utils::getRange(rSliceVector[0].getB3DPolyPolygon());
608
0
                aTexRangeBack = basegfx::utils::getRange(rSliceVector[nNumSlices - 1].getB3DPolyPolygon());
609
610
0
                if(aTexRangeBack.getDepth() > aTexRangeBack.getWidth())
611
0
                {
612
                    // last polygon is rotated so that depth is bigger than width, exchange X and Z
613
                    // for making applyDefaultTextureCoordinatesParallel use Z instead of X for
614
                    // horizontal texture coordinate
615
0
                    aTexRangeBack = basegfx::B3DRange(
616
0
                        aTexRangeBack.getMinZ(), aTexRangeBack.getMinY(), aTexRangeBack.getMinX(),
617
0
                        aTexRangeBack.getMaxZ(), aTexRangeBack.getMaxY(), aTexRangeBack.getMaxX());
618
0
                }
619
620
0
                basegfx::B3DPoint aCenter(basegfx::utils::getRange(rSliceVector[0].getB3DPolyPolygon()).getCenter());
621
622
0
                for(a = 0; a < nLoopCount; a++)
623
0
                {
624
0
                    const basegfx::B3DPoint aNextCenter(basegfx::utils::getRange(rSliceVector[(a + 1) % nNumSlices].getB3DPolyPolygon()).getCenter());
625
0
                    const double fLength(basegfx::B3DVector(aNextCenter - aCenter).getLength());
626
0
                    aTexHeightArray.push_back(fLength);
627
0
                    aCenter = aNextCenter;
628
0
                }
629
630
0
                const double fTexHeight(std::accumulate(aTexHeightArray.begin(), aTexHeightArray.end(), 0.0));
631
632
0
                if(!basegfx::fTools::equalZero(fTexHeight))
633
0
                {
634
0
                    assert(fTexHeight != 0 && "help coverity see it's not zero");
635
0
                    fInvTexHeight = 1.0 / fTexHeight;
636
0
                }
637
0
            }
638
639
0
            if(nLoopCount)
640
0
            {
641
0
                double fTexHeightPos(0.0);
642
0
                for(a = 0; a < nLoopCount; a++)
643
0
                {
644
0
                    const Slice3D& rSliceA(rSliceVector[a]);
645
0
                    const Slice3D& rSliceB(rSliceVector[(a + 1) % nNumSlices]);
646
0
                    const bool bAcceptPair(SLICETYPE3D_REGULAR == rSliceA.getSliceType() && SLICETYPE3D_REGULAR == rSliceB.getSliceType());
647
0
                    basegfx::B3DPolyPolygon aPolA(rSliceA.getB3DPolyPolygon());
648
0
                    basegfx::B3DPolyPolygon aPolB(rSliceB.getB3DPolyPolygon());
649
650
0
                    if(bAcceptPair)
651
0
                    {
652
0
                        if(bCreateNormals)
653
0
                        {
654
0
                            impCreateInBetweenNormals(aPolB, aPolA);
655
0
                        }
656
657
0
                        {
658
0
                            const sal_uInt32 nIndPrev((a + nNumSlices - 1) % nNumSlices);
659
0
                            const Slice3D& rSlicePrev(rSliceVector[nIndPrev]);
660
0
                            basegfx::B3DPolyPolygon aPrev(rSlicePrev.getB3DPolyPolygon());
661
0
                            basegfx::B3DPolyPolygon aPolAA(rSliceA.getB3DPolyPolygon());
662
663
0
                            if(SLICETYPE3D_FRONTCAP == rSlicePrev.getSliceType())
664
0
                            {
665
0
                                basegfx::B3DPolyPolygon aFront(rSlicePrev.getB3DPolyPolygon());
666
0
                                const bool bHasSlant(aPolAA != aPrev);
667
668
0
                                if(bCreateTextureCoordinates)
669
0
                                {
670
0
                                    aFront = basegfx::utils::applyDefaultTextureCoordinatesParallel(aFront, aTexRangeFront);
671
0
                                }
672
673
0
                                if(bCreateNormals)
674
0
                                {
675
0
                                    basegfx::B3DVector aNormal(0.0, 0.0, -1.0);
676
677
0
                                    if(aFront.count())
678
0
                                    {
679
0
                                        aNormal = -aFront.getB3DPolygon(0).getNormal();
680
0
                                    }
681
682
0
                                    impSetNormal(aFront, aNormal);
683
684
0
                                    if(bHasSlant)
685
0
                                    {
686
0
                                        impCreateInBetweenNormals(aPolAA, aPrev);
687
688
0
                                        if(bSmoothNormals)
689
0
                                        {
690
                                            // smooth and copy
691
0
                                            impMixNormals(aPolA, aPolAA, fSmoothNormalsMix);
692
0
                                            aPolAA = aPolA;
693
0
                                        }
694
0
                                        else
695
0
                                        {
696
                                            // take over from surface
697
0
                                            aPolAA = aPolA;
698
0
                                        }
699
700
0
                                        if(bSmoothLids)
701
0
                                        {
702
                                            // smooth and copy
703
0
                                            impMixNormals(aFront, aPrev, fSmoothLidsMix);
704
0
                                            aPrev = aFront;
705
0
                                        }
706
0
                                        else
707
0
                                        {
708
                                            // take over from front
709
0
                                            aPrev = aFront;
710
0
                                        }
711
0
                                    }
712
0
                                    else
713
0
                                    {
714
0
                                        if(bSmoothNormals)
715
0
                                        {
716
                                            // smooth
717
0
                                            impMixNormals(aPolA, aFront, fSmoothNormalsMix);
718
0
                                        }
719
720
0
                                        if(bSmoothLids)
721
0
                                        {
722
                                            // smooth and copy
723
0
                                            impMixNormals(aFront, aPolA, fSmoothLidsMix);
724
0
                                            aPolA = aFront;
725
0
                                        }
726
0
                                    }
727
0
                                }
728
729
0
                                if(bHasSlant)
730
0
                                {
731
0
                                    double fTexStart{};
732
0
                                    double fTexStop{};
733
0
                                    if(bCreateTextureCoordinates)
734
0
                                    {
735
0
                                        fTexStart = fTexHeightPos * fInvTexHeight;
736
0
                                        fTexStop = (fTexHeightPos - aTexHeightArray[(a + nLoopCount - 1) % nLoopCount]) * fInvTexHeight;
737
0
                                    }
738
739
0
                                    impAddInBetweenFill(aEdgeRounding, aPolAA, aPrev, fTexStart, fTexStop, bCreateNormals, bCreateTextureCoordinates);
740
0
                                }
741
742
0
                                aFront.flip();
743
0
                                rFill.push_back(aFront);
744
0
                            }
745
0
                            else
746
0
                            {
747
0
                                if(bCreateNormals && bSmoothNormals && (nIndPrev != a + 1))
748
0
                                {
749
0
                                    impCreateInBetweenNormals(aPolAA, aPrev);
750
0
                                    impMixNormals(aPolA, aPolAA, 0.5);
751
0
                                }
752
0
                            }
753
0
                        }
754
755
0
                        {
756
0
                            const sal_uInt32 nIndNext((a + 2) % nNumSlices);
757
0
                            const Slice3D& rSliceNext(rSliceVector[nIndNext]);
758
0
                            basegfx::B3DPolyPolygon aNext(rSliceNext.getB3DPolyPolygon());
759
0
                            basegfx::B3DPolyPolygon aPolBB(rSliceB.getB3DPolyPolygon());
760
761
0
                            if(SLICETYPE3D_BACKCAP == rSliceNext.getSliceType())
762
0
                            {
763
0
                                basegfx::B3DPolyPolygon aBack(rSliceNext.getB3DPolyPolygon());
764
0
                                const bool bHasSlant(aPolBB != aNext);
765
766
0
                                if(bCreateTextureCoordinates)
767
0
                                {
768
0
                                    aBack = basegfx::utils::applyDefaultTextureCoordinatesParallel(aBack, aTexRangeBack);
769
0
                                }
770
771
0
                                if(bCreateNormals)
772
0
                                {
773
0
                                    const basegfx::B3DVector aNormal(aBack.count() ? aBack.getB3DPolygon(0).getNormal() : basegfx::B3DVector(0.0, 0.0, 1.0));
774
0
                                    impSetNormal(aBack, aNormal);
775
776
0
                                    if(bHasSlant)
777
0
                                    {
778
0
                                        impCreateInBetweenNormals(aNext, aPolBB);
779
780
0
                                        if(bSmoothNormals)
781
0
                                        {
782
                                            // smooth and copy
783
0
                                            impMixNormals(aPolB, aPolBB, fSmoothNormalsMix);
784
0
                                            aPolBB = aPolB;
785
0
                                        }
786
0
                                        else
787
0
                                        {
788
                                            // take over from surface
789
0
                                            aPolBB = aPolB;
790
0
                                        }
791
792
0
                                        if(bSmoothLids)
793
0
                                        {
794
                                            // smooth and copy
795
0
                                            impMixNormals(aBack, aNext, fSmoothLidsMix);
796
0
                                            aNext = aBack;
797
0
                                        }
798
0
                                        else
799
0
                                        {
800
                                            // take over from back
801
0
                                            aNext = aBack;
802
0
                                        }
803
0
                                    }
804
0
                                    else
805
0
                                    {
806
0
                                        if(bSmoothNormals)
807
0
                                        {
808
                                            // smooth
809
0
                                            impMixNormals(aPolB, aBack, fSmoothNormalsMix);
810
0
                                        }
811
812
0
                                        if(bSmoothLids)
813
0
                                        {
814
                                            // smooth and copy
815
0
                                            impMixNormals(aBack, aPolB, fSmoothLidsMix);
816
0
                                            aPolB = aBack;
817
0
                                        }
818
0
                                    }
819
0
                                }
820
821
0
                                if(bHasSlant)
822
0
                                {
823
0
                                    double fTexStart{};
824
0
                                    double fTexStop{};
825
0
                                    if(bCreateTextureCoordinates)
826
0
                                    {
827
0
                                        fTexStart = (fTexHeightPos + aTexHeightArray[a] + aTexHeightArray[(a + 1) % nLoopCount]) * fInvTexHeight;
828
0
                                        fTexStop = (fTexHeightPos + aTexHeightArray[a]) * fInvTexHeight;
829
0
                                    }
830
831
0
                                    impAddInBetweenFill(aEdgeRounding, aNext, aPolBB, fTexStart, fTexStop, bCreateNormals, bCreateTextureCoordinates);
832
0
                                }
833
834
0
                                rFill.push_back(aBack);
835
0
                            }
836
0
                            else
837
0
                            {
838
0
                                if(bCreateNormals && bSmoothNormals && (nIndNext != a))
839
0
                                {
840
0
                                    impCreateInBetweenNormals(aNext, aPolBB);
841
0
                                    impMixNormals(aPolB, aPolBB, 0.5);
842
0
                                }
843
0
                            }
844
0
                        }
845
846
0
                        double fTexStart{};
847
0
                        double fTexStop{};
848
0
                        if(bCreateTextureCoordinates)
849
0
                        {
850
0
                            fTexStart = (fTexHeightPos + aTexHeightArray[a]) * fInvTexHeight;
851
0
                            fTexStop = fTexHeightPos * fInvTexHeight;
852
0
                        }
853
854
0
                        impAddInBetweenFill(aEdgeRounding, aPolB, aPolA, fTexStart, fTexStop, bCreateNormals, bCreateTextureCoordinates);
855
0
                    }
856
857
0
                    if(bCreateTextureCoordinates)
858
0
                    {
859
0
                        fTexHeightPos += aTexHeightArray[a];
860
0
                    }
861
0
                }
862
0
            }
863
0
            else
864
0
            {
865
                // no loop, but a single slice (1 == nNumSlices), create a filling from the single
866
                // front plane
867
0
                const Slice3D& rSlice(rSliceVector[0]);
868
0
                basegfx::B3DPolyPolygon aFront(rSlice.getB3DPolyPolygon());
869
870
0
                if(bCreateTextureCoordinates)
871
0
                {
872
0
                    aFront = basegfx::utils::applyDefaultTextureCoordinatesParallel(aFront, aTexRangeFront);
873
0
                }
874
875
0
                if(bCreateNormals)
876
0
                {
877
0
                    basegfx::B3DVector aNormal(0.0, 0.0, -1.0);
878
879
0
                    if(aFront.count())
880
0
                    {
881
0
                        aNormal = -aFront.getB3DPolygon(0).getNormal();
882
0
                    }
883
884
0
                    impSetNormal(aFront, aNormal);
885
0
                }
886
887
0
                aFront.flip();
888
0
                rFill.push_back(aFront);
889
0
            }
890
891
0
            if(bCreateTextureCoordinates)
892
0
            {
893
0
                aEdgeRounding.transformTextureCoordinates(rTexTransform);
894
0
            }
895
896
0
            for(a = 0; a < aEdgeRounding.count(); a++)
897
0
            {
898
0
                rFill.emplace_back(aEdgeRounding.getB3DPolygon(a));
899
0
            }
900
0
        }
901
902
        void createReducedOutlines(
903
            const geometry::ViewInformation3D& rViewInformation,
904
            const basegfx::B3DHomMatrix& rObjectTransform,
905
            const basegfx::B3DPolygon& rLoopA,
906
            const basegfx::B3DPolygon& rLoopB,
907
            basegfx::B3DPolyPolygon& rTarget)
908
0
        {
909
0
            const sal_uInt32 nPointCount(rLoopA.count());
910
911
            // with identical polygons there are no outlines
912
0
            if(rLoopA == rLoopB)
913
0
                return;
914
915
0
            if(!(nPointCount && nPointCount == rLoopB.count()))
916
0
                return;
917
918
0
            const basegfx::B3DHomMatrix aObjectTransform(rViewInformation.getObjectToView() * rObjectTransform);
919
0
            const basegfx::B2DPolygon a2DLoopA(basegfx::utils::createB2DPolygonFromB3DPolygon(rLoopA, aObjectTransform));
920
0
            const basegfx::B2DPolygon a2DLoopB(basegfx::utils::createB2DPolygonFromB3DPolygon(rLoopB, aObjectTransform));
921
0
            const basegfx::B2DPoint a2DCenterA(a2DLoopA.getB2DRange().getCenter());
922
0
            const basegfx::B2DPoint a2DCenterB(a2DLoopB.getB2DRange().getCenter());
923
924
            // without detectable Y-Axis there are no outlines
925
0
            if(a2DCenterA.equal(a2DCenterB))
926
0
                return;
927
928
            // search for outmost left and right inter-loop-edges which do not cut the loops
929
0
            const basegfx::B2DPoint aCommonCenter(basegfx::average(a2DCenterA, a2DCenterB));
930
0
            const basegfx::B2DVector aAxisVector(a2DCenterA - a2DCenterB);
931
0
            double fMaxLeft(0.0);
932
0
            double fMaxRight(0.0);
933
0
            sal_uInt32 nIndexLeft(0);
934
0
            sal_uInt32 nIndexRight(0);
935
936
0
            for(sal_uInt32 a(0); a < nPointCount; a++)
937
0
            {
938
0
                const basegfx::B2DPoint aStart(a2DLoopA.getB2DPoint(a));
939
0
                const basegfx::B2DPoint aEnd(a2DLoopB.getB2DPoint(a));
940
0
                const basegfx::B2DPoint aMiddle(basegfx::average(aStart, aEnd));
941
942
0
                if(!basegfx::utils::isInside(a2DLoopA, aMiddle))
943
0
                {
944
0
                    if(!basegfx::utils::isInside(a2DLoopB, aMiddle))
945
0
                    {
946
0
                        if(!impHasCutWith(a2DLoopA, aStart, aEnd))
947
0
                        {
948
0
                            if(!impHasCutWith(a2DLoopB, aStart, aEnd))
949
0
                            {
950
0
                                const basegfx::B2DVector aCandidateVector(aMiddle - aCommonCenter);
951
0
                                const double fCross(aCandidateVector.cross(aAxisVector));
952
0
                                const double fDistance(aCandidateVector.getLength());
953
954
0
                                if(fCross > 0.0)
955
0
                                {
956
0
                                    if(fDistance > fMaxLeft)
957
0
                                    {
958
0
                                        fMaxLeft = fDistance;
959
0
                                        nIndexLeft = a;
960
0
                                    }
961
0
                                }
962
0
                                else if(fCross < 0.0)
963
0
                                {
964
0
                                    if(fDistance > fMaxRight)
965
0
                                    {
966
0
                                        fMaxRight = fDistance;
967
0
                                        nIndexRight = a;
968
0
                                    }
969
0
                                }
970
0
                            }
971
0
                        }
972
0
                    }
973
0
                }
974
0
            }
975
976
0
            if(fMaxLeft != 0.0)
977
0
            {
978
0
                basegfx::B3DPolygon aToBeAdded;
979
0
                aToBeAdded.append(rLoopA.getB3DPoint(nIndexLeft));
980
0
                aToBeAdded.append(rLoopB.getB3DPoint(nIndexLeft));
981
0
                rTarget.append(aToBeAdded);
982
0
            }
983
984
0
            if(fMaxRight != 0.0)
985
0
            {
986
0
                basegfx::B3DPolygon aToBeAdded;
987
0
                aToBeAdded.append(rLoopA.getB3DPoint(nIndexRight));
988
0
                aToBeAdded.append(rLoopB.getB3DPoint(nIndexRight));
989
0
                rTarget.append(aToBeAdded);
990
0
            }
991
0
        }
992
993
} // end of namespace
994
995
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */