Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/drawinglayer/source/primitive3d/sdrdecompositiontools3d.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 <com/sun/star/drawing/TextureKind2.hpp>
21
#include <com/sun/star/drawing/TextureMode.hpp>
22
23
#include <primitive3d/sdrdecompositiontools3d.hxx>
24
#include <drawinglayer/attribute/strokeattribute.hxx>
25
#include <drawinglayer/primitive3d/baseprimitive3d.hxx>
26
#include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
27
#include <basegfx/polygon/b3dpolypolygon.hxx>
28
#include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
29
#include <drawinglayer/attribute/fillgraphicattribute.hxx>
30
#include <drawinglayer/attribute/sdrfillgraphicattribute.hxx>
31
#include <basegfx/polygon/b3dpolypolygontools.hxx>
32
#include <primitive3d/textureprimitive3d.hxx>
33
#include <drawinglayer/primitive3d/modifiedcolorprimitive3d.hxx>
34
#include <primitive3d/hatchtextureprimitive3d.hxx>
35
#include <primitive3d/shadowprimitive3d.hxx>
36
#include <basegfx/range/b2drange.hxx>
37
#include <basegfx/range/b3drange.hxx>
38
#include <drawinglayer/attribute/sdrlineattribute.hxx>
39
#include <drawinglayer/attribute/sdrobjectattribute3d.hxx>
40
#include <drawinglayer/attribute/sdrfillattribute.hxx>
41
#include <drawinglayer/attribute/sdrshadowattribute.hxx>
42
#include <primitive3d/hiddengeometryprimitive3d.hxx>
43
#include <rtl/ref.hxx>
44
45
46
namespace drawinglayer::primitive3d
47
{
48
        basegfx::B3DRange getRangeFrom3DGeometry(std::vector< basegfx::B3DPolyPolygon >& rFill)
49
0
        {
50
0
            basegfx::B3DRange aRetval;
51
52
0
            for(const basegfx::B3DPolyPolygon & a : rFill)
53
0
            {
54
0
                aRetval.expand(basegfx::utils::getRange(a));
55
0
            }
56
57
0
            return aRetval;
58
0
        }
59
60
        void applyNormalsKindSphereTo3DGeometry(std::vector< basegfx::B3DPolyPolygon >& rFill, const basegfx::B3DRange& rRange)
61
0
        {
62
            // create sphere normals
63
0
            const basegfx::B3DPoint aCenter(rRange.getCenter());
64
65
0
            for(basegfx::B3DPolyPolygon & a : rFill)
66
0
            {
67
0
                a = basegfx::utils::applyDefaultNormalsSphere(a, aCenter);
68
0
            }
69
0
        }
70
71
        void applyNormalsKindFlatTo3DGeometry(std::vector< basegfx::B3DPolyPolygon >& rFill)
72
0
        {
73
0
            for(basegfx::B3DPolyPolygon & a : rFill)
74
0
            {
75
0
                a.clearNormals();
76
0
            }
77
0
        }
78
79
        void applyNormalsInvertTo3DGeometry(std::vector< basegfx::B3DPolyPolygon >& rFill)
80
0
        {
81
            // invert normals
82
0
            for(basegfx::B3DPolyPolygon & a : rFill)
83
0
            {
84
0
                a = basegfx::utils::invertNormals(a);
85
0
            }
86
0
        }
87
88
        void applyTextureTo3DGeometry(
89
            css::drawing::TextureProjectionMode eModeX,
90
            css::drawing::TextureProjectionMode eModeY,
91
            std::vector< basegfx::B3DPolyPolygon >& rFill,
92
            const basegfx::B3DRange& rRange,
93
            const basegfx::B2DVector& rTextureSize)
94
0
        {
95
            // handle texture coordinates X
96
0
            const bool bParallelX(css::drawing::TextureProjectionMode_PARALLEL == eModeX);
97
0
            const bool bSphereX(!bParallelX && (css::drawing::TextureProjectionMode_SPHERE == eModeX));
98
99
            // handle texture coordinates Y
100
0
            const bool bParallelY(css::drawing::TextureProjectionMode_PARALLEL == eModeY);
101
0
            const bool bSphereY(!bParallelY && (css::drawing::TextureProjectionMode_SPHERE == eModeY));
102
103
0
            if(bParallelX || bParallelY)
104
0
            {
105
                // apply parallel texture coordinates in X and/or Y
106
0
                for(auto & a: rFill)
107
0
                {
108
0
                    a = basegfx::utils::applyDefaultTextureCoordinatesParallel(a, rRange, bParallelX, bParallelY);
109
0
                }
110
0
            }
111
112
0
            if(bSphereX || bSphereY)
113
0
            {
114
                // apply spherical texture coordinates in X and/or Y
115
0
                const basegfx::B3DPoint aCenter(rRange.getCenter());
116
117
0
                for(auto & a: rFill)
118
0
                {
119
0
                    a = basegfx::utils::applyDefaultTextureCoordinatesSphere(a, aCenter, bSphereX, bSphereY);
120
0
                }
121
0
            }
122
123
            // transform texture coordinates to texture size
124
0
            basegfx::B2DHomMatrix aTexMatrix;
125
0
            aTexMatrix.scale(rTextureSize.getX(), rTextureSize.getY());
126
127
0
            for(auto & a: rFill)
128
0
            {
129
0
                a.transformTextureCoordinates(aTexMatrix);
130
0
            }
131
0
        }
132
133
        Primitive3DContainer create3DPolyPolygonLinePrimitives(
134
            const basegfx::B3DPolyPolygon& rUnitPolyPolygon,
135
            const basegfx::B3DHomMatrix& rObjectTransform,
136
            const attribute::SdrLineAttribute& rLine)
137
0
        {
138
            // prepare fully scaled polyPolygon
139
0
            basegfx::B3DPolyPolygon aScaledPolyPolygon(rUnitPolyPolygon);
140
0
            aScaledPolyPolygon.transform(rObjectTransform);
141
142
            // create line and stroke attribute
143
0
            const attribute::LineAttribute aLineAttribute(rLine.getColor(), rLine.getWidth(), rLine.getJoin(), rLine.getCap());
144
0
            const attribute::StrokeAttribute aStrokeAttribute(std::vector(rLine.getDotDashArray()), rLine.getFullDotDashLen());
145
146
            // create primitives
147
0
            Primitive3DContainer aRetval(aScaledPolyPolygon.count());
148
149
0
            for(sal_uInt32 a(0); a < aScaledPolyPolygon.count(); a++)
150
0
            {
151
0
                aRetval[a] = new PolygonStrokePrimitive3D(aScaledPolyPolygon.getB3DPolygon(a), aLineAttribute, aStrokeAttribute);
152
0
            }
153
154
0
            if(0.0 != rLine.getTransparence())
155
0
            {
156
                // create UnifiedTransparenceTexturePrimitive3D, add created primitives and exchange
157
0
                const Primitive3DReference xRef(new UnifiedTransparenceTexturePrimitive3D(rLine.getTransparence(), aRetval));
158
0
                aRetval = { xRef };
159
0
            }
160
161
0
            return aRetval;
162
0
        }
163
164
        Primitive3DContainer create3DPolyPolygonFillPrimitives(
165
            const std::vector< basegfx::B3DPolyPolygon >& r3DPolyPolygonVector,
166
            const basegfx::B3DHomMatrix& rObjectTransform,
167
            const basegfx::B2DVector& rTextureSize,
168
            const attribute::Sdr3DObjectAttribute& aSdr3DObjectAttribute,
169
            const attribute::SdrFillAttribute& rFill,
170
            const attribute::FillGradientAttribute& rFillGradient)
171
0
        {
172
0
            Primitive3DContainer aRetval;
173
174
0
            if(!r3DPolyPolygonVector.empty())
175
0
            {
176
                // create list of simple fill primitives
177
0
                aRetval.resize(r3DPolyPolygonVector.size());
178
179
0
                for(size_t a(0); a < r3DPolyPolygonVector.size(); a++)
180
0
                {
181
                    // get scaled PolyPolygon
182
0
                    basegfx::B3DPolyPolygon aScaledPolyPolygon(r3DPolyPolygonVector[a]);
183
0
                    aScaledPolyPolygon.transform(rObjectTransform);
184
185
0
                    if(aScaledPolyPolygon.areNormalsUsed())
186
0
                    {
187
0
                        aScaledPolyPolygon.transformNormals(rObjectTransform);
188
0
                    }
189
190
0
                    const Primitive3DReference xRef(new PolyPolygonMaterialPrimitive3D(
191
0
                        std::move(aScaledPolyPolygon),
192
0
                        aSdr3DObjectAttribute.getMaterial(),
193
0
                        aSdr3DObjectAttribute.getDoubleSided()));
194
0
                    aRetval[a] = xRef;
195
0
                }
196
197
                // look for and evtl. build texture sub-group primitive
198
0
                if(!rFill.getGradient().isDefault()
199
0
                    || !rFill.getHatch().isDefault()
200
0
                    || !rFill.getFillGraphic().isDefault())
201
0
                {
202
0
                    bool bModulate(css::drawing::TextureMode_MODULATE == aSdr3DObjectAttribute.getTextureMode());
203
0
                    bool bFilter(aSdr3DObjectAttribute.getTextureFilter());
204
0
                    rtl::Reference<BasePrimitive3D> pNewTexturePrimitive3D;
205
206
0
                    if(!rFill.getGradient().isDefault())
207
0
                    {
208
                        // create gradientTexture3D with sublist, add to local aRetval
209
0
                        pNewTexturePrimitive3D = new GradientTexturePrimitive3D(
210
0
                            rFill.getGradient(),
211
0
                            aRetval,
212
0
                            rTextureSize,
213
0
                            bModulate,
214
0
                            bFilter);
215
0
                    }
216
0
                    else if(!rFill.getHatch().isDefault())
217
0
                    {
218
                        // create hatchTexture3D with sublist, add to local aRetval
219
0
                        pNewTexturePrimitive3D = new HatchTexturePrimitive3D(
220
0
                            rFill.getHatch(),
221
0
                            aRetval,
222
0
                            rTextureSize,
223
0
                            bModulate,
224
0
                            bFilter);
225
0
                    }
226
0
                    else // if(!rFill.getFillGraphic().isDefault())
227
0
                    {
228
                        // create bitmapTexture3D with sublist, add to local aRetval
229
0
                        const basegfx::B2DRange aTexRange(0.0, 0.0, rTextureSize.getX(), rTextureSize.getY());
230
231
0
                        pNewTexturePrimitive3D = new BitmapTexturePrimitive3D(
232
0
                            rFill.getFillGraphic().createFillGraphicAttribute(aTexRange),
233
0
                            aRetval,
234
0
                            rTextureSize,
235
0
                            bModulate,
236
0
                            bFilter);
237
0
                    }
238
239
                    // exchange aRetval content with texture group
240
0
                    aRetval = { Primitive3DReference(pNewTexturePrimitive3D) };
241
242
0
                    if(css::drawing::TextureKind2_LUMINANCE == aSdr3DObjectAttribute.getTextureKind())
243
0
                    {
244
                        // use modified color primitive to force textures to gray
245
0
                        basegfx::BColorModifierSharedPtr aBColorModifier =
246
0
                            std::make_shared<basegfx::BColorModifier_gray>();
247
0
                        Primitive3DReference xRef2(
248
0
                            new ModifiedColorPrimitive3D(
249
0
                                aRetval,
250
0
                                std::move(aBColorModifier)));
251
252
0
                        aRetval = { xRef2 };
253
0
                    }
254
0
                }
255
256
0
                if(0.0 != rFill.getTransparence())
257
0
                {
258
                    // create UnifiedTransparenceTexturePrimitive3D with sublist and exchange
259
0
                    const Primitive3DReference xRef(new UnifiedTransparenceTexturePrimitive3D(rFill.getTransparence(), aRetval));
260
0
                    aRetval = { xRef };
261
0
                }
262
0
                else if(!rFillGradient.isDefault())
263
0
                {
264
                    // create TransparenceTexturePrimitive3D with sublist and exchange
265
0
                    const Primitive3DReference xRef(new TransparenceTexturePrimitive3D(rFillGradient, aRetval, rTextureSize));
266
0
                    aRetval = { xRef };
267
0
                }
268
0
            }
269
270
0
            return aRetval;
271
0
        }
272
273
        Primitive3DContainer createShadowPrimitive3D(
274
            const Primitive3DContainer& rSource,
275
            const attribute::SdrShadowAttribute& rShadow,
276
            bool bShadow3D)
277
0
        {
278
            // create Shadow primitives. Uses already created primitives
279
0
            if(!rSource.empty() && !basegfx::fTools::moreOrEqual(rShadow.getTransparence(), 1.0))
280
0
            {
281
                // prepare new list for shadow geometry
282
0
                basegfx::B2DHomMatrix aShadowOffset;
283
0
                aShadowOffset.set(0, 2, rShadow.getOffset().getX());
284
0
                aShadowOffset.set(1, 2, rShadow.getOffset().getY());
285
286
                // create shadow primitive and add primitives
287
0
                const Primitive3DReference xRef(new ShadowPrimitive3D(aShadowOffset, rShadow.getColor(), rShadow.getTransparence(), bShadow3D, rSource));
288
0
                return { xRef };
289
0
            }
290
0
            else
291
0
            {
292
0
                return Primitive3DContainer();
293
0
            }
294
0
        }
295
296
        Primitive3DContainer createHiddenGeometryPrimitives3D(
297
            const std::vector< basegfx::B3DPolyPolygon >& r3DPolyPolygonVector,
298
            const basegfx::B3DHomMatrix& rObjectTransform,
299
            const basegfx::B2DVector& rTextureSize,
300
            const attribute::Sdr3DObjectAttribute& aSdr3DObjectAttribute)
301
0
        {
302
            // create hidden sub-geometry which can be used for HitTest
303
            // and BoundRect calculations, but will not be visualized
304
0
            const attribute::SdrFillAttribute aSimplifiedFillAttribute(
305
0
                0.0,
306
0
                basegfx::BColor(),
307
0
                attribute::FillGradientAttribute(),
308
0
                attribute::FillHatchAttribute(),
309
0
                attribute::SdrFillGraphicAttribute());
310
311
0
            const Primitive3DReference aHidden(
312
0
                new HiddenGeometryPrimitive3D(
313
0
                    create3DPolyPolygonFillPrimitives(
314
0
                        r3DPolyPolygonVector,
315
0
                        rObjectTransform,
316
0
                        rTextureSize,
317
0
                        aSdr3DObjectAttribute,
318
0
                        aSimplifiedFillAttribute,
319
0
                        attribute::FillGradientAttribute())));
320
321
0
            return { aHidden };
322
0
        }
323
324
} // end of namespace
325
326
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */