Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/drawinglayer/source/processor3d/shadow3dextractor.cxx
Line
Count
Source (jump to first uncovered line)
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 <processor3d/shadow3dextractor.hxx>
21
#include <primitive3d/shadowprimitive3d.hxx>
22
#include <drawinglayer/primitive2d/shadowprimitive2d.hxx>
23
#include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
24
#include <drawinglayer/primitive3d/transformprimitive3d.hxx>
25
#include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
26
#include <basegfx/polygon/b2dpolygontools.hxx>
27
#include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
28
#include <basegfx/polygon/b2dpolypolygontools.hxx>
29
#include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx>
30
#include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx>
31
#include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
32
#include <osl/diagnose.h>
33
#include <rtl/ref.hxx>
34
#include <utility>
35
36
37
using namespace com::sun::star;
38
39
40
namespace drawinglayer::processor3d
41
{
42
        // as tooling, the process() implementation takes over API handling and calls this
43
        // virtual render method when the primitive implementation is BasePrimitive3D-based.
44
        void Shadow3DExtractingProcessor::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rCandidate)
45
0
        {
46
            // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
47
0
            switch(rCandidate.getPrimitive3DID())
48
0
            {
49
0
                case PRIMITIVE3D_ID_SHADOWPRIMITIVE3D :
50
0
                {
51
                    // shadow3d object. Call recursive with content and start conversion
52
0
                    const primitive3d::ShadowPrimitive3D& rPrimitive = static_cast< const primitive3d::ShadowPrimitive3D& >(rCandidate);
53
54
                    // set new target
55
0
                    primitive2d::Primitive2DContainer aNewSubList;
56
0
                    primitive2d::Primitive2DContainer* pLastTargetSequence = mpPrimitive2DSequence;
57
0
                    mpPrimitive2DSequence = &aNewSubList;
58
59
                    // activate convert
60
0
                    const bool bLastConvert(mbConvert);
61
0
                    mbConvert = true;
62
63
                    // set projection flag
64
0
                    const bool bLastUseProjection(mbUseProjection);
65
0
                    mbUseProjection = rPrimitive.getShadow3D();
66
67
                    // process content
68
0
                    process(rPrimitive.getChildren());
69
70
                    // restore values
71
0
                    mbUseProjection = bLastUseProjection;
72
0
                    mbConvert = bLastConvert;
73
0
                    mpPrimitive2DSequence = pLastTargetSequence;
74
75
                    // create 2d shadow primitive with result. This also fetches all entries
76
                    // from aNewSubList, so there is no need to delete them
77
0
                    rtl::Reference<primitive2d::BasePrimitive2D> pNew = new primitive2d::ShadowPrimitive2D(
78
0
                        rPrimitive.getShadowTransform(),
79
0
                        rPrimitive.getShadowColor(),
80
0
                        0,  // shadow3d doesn't have rPrimitive.getShadowBlur() yet.
81
0
                        std::move(aNewSubList));
82
83
0
                    if(rPrimitive.getShadowTransparence() > 0.0)
84
0
                    {
85
                        // create simpleTransparencePrimitive, add created primitives
86
0
                        primitive2d::Primitive2DContainer aNewTransPrimitiveVector { pNew };
87
88
0
                        pNew = new primitive2d::UnifiedTransparencePrimitive2D(
89
0
                            std::move(aNewTransPrimitiveVector),
90
0
                            rPrimitive.getShadowTransparence());
91
0
                    }
92
93
0
                    mpPrimitive2DSequence->push_back(pNew);
94
95
0
                    break;
96
0
                }
97
0
                case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
98
0
                {
99
                    // transform group. Remember current transformations
100
0
                    const primitive3d::TransformPrimitive3D& rPrimitive = static_cast< const primitive3d::TransformPrimitive3D& >(rCandidate);
101
0
                    const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
102
103
                    // create new transformation; add new object transform from right side
104
0
                    const geometry::ViewInformation3D aNewViewInformation3D(
105
0
                        aLastViewInformation3D.getObjectTransformation() * rPrimitive.getTransformation(),
106
0
                        aLastViewInformation3D.getOrientation(),
107
0
                        aLastViewInformation3D.getProjection(),
108
0
                        aLastViewInformation3D.getDeviceToView(),
109
0
                        aLastViewInformation3D.getViewTime(),
110
0
                        aLastViewInformation3D.getExtendedInformationSequence());
111
0
                    updateViewInformation(aNewViewInformation3D);
112
113
0
                    if(mbShadowProjectionIsValid)
114
0
                    {
115
                        // update buffered WorldToEye and EyeToView
116
0
                        maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
117
0
                        maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
118
0
                    }
119
120
                    // let break down
121
0
                    process(rPrimitive.getChildren());
122
123
                    // restore transformations
124
0
                    updateViewInformation(aLastViewInformation3D);
125
126
0
                    if(mbShadowProjectionIsValid)
127
0
                    {
128
                        // update buffered WorldToEye and EyeToView
129
0
                        maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
130
0
                        maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
131
0
                    }
132
0
                    break;
133
0
                }
134
0
                case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
135
0
                {
136
                    // PolygonHairlinePrimitive3D
137
0
                    if(mbConvert)
138
0
                    {
139
0
                        const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rCandidate);
140
0
                        basegfx::B2DPolygon a2DHairline;
141
142
0
                        if(mbUseProjection)
143
0
                        {
144
0
                            if(mbShadowProjectionIsValid)
145
0
                            {
146
0
                                a2DHairline = impDoShadowProjection(rPrimitive.getB3DPolygon());
147
0
                            }
148
0
                        }
149
0
                        else
150
0
                        {
151
0
                            a2DHairline = basegfx::utils::createB2DPolygonFromB3DPolygon(rPrimitive.getB3DPolygon(), getViewInformation3D().getObjectToView());
152
0
                        }
153
154
0
                        if(a2DHairline.count())
155
0
                        {
156
0
                            a2DHairline.transform(getObjectTransformation());
157
0
                            mpPrimitive2DSequence->push_back(
158
0
                                new primitive2d::PolygonHairlinePrimitive2D(
159
0
                                    std::move(a2DHairline),
160
0
                                    basegfx::BColor()));
161
0
                        }
162
0
                    }
163
0
                    break;
164
0
                }
165
0
                case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
166
0
                {
167
                    // PolyPolygonMaterialPrimitive3D
168
0
                    if(mbConvert)
169
0
                    {
170
0
                        const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rCandidate);
171
0
                        basegfx::B2DPolyPolygon a2DFill;
172
173
0
                        if(mbUseProjection)
174
0
                        {
175
0
                            if(mbShadowProjectionIsValid)
176
0
                            {
177
0
                                a2DFill = impDoShadowProjection(rPrimitive.getB3DPolyPolygon());
178
0
                            }
179
0
                        }
180
0
                        else
181
0
                        {
182
0
                            a2DFill = basegfx::utils::createB2DPolyPolygonFromB3DPolyPolygon(rPrimitive.getB3DPolyPolygon(), getViewInformation3D().getObjectToView());
183
0
                        }
184
185
0
                        if(a2DFill.count())
186
0
                        {
187
0
                            a2DFill.transform(getObjectTransformation());
188
0
                            mpPrimitive2DSequence->push_back(
189
0
                                new primitive2d::PolyPolygonColorPrimitive2D(
190
0
                                    std::move(a2DFill),
191
0
                                    basegfx::BColor()));
192
0
                        }
193
0
                    }
194
0
                    break;
195
0
                }
196
0
                default :
197
0
                {
198
                    // process recursively
199
0
                    process(rCandidate.get3DDecomposition(getViewInformation3D()));
200
0
                    break;
201
0
                }
202
0
            }
203
0
        }
204
205
        Shadow3DExtractingProcessor::Shadow3DExtractingProcessor(
206
            const geometry::ViewInformation3D& rViewInformation,
207
            basegfx::B2DHomMatrix aObjectTransformation,
208
            const basegfx::B3DVector& rLightNormal,
209
            double fShadowSlant,
210
            const basegfx::B3DRange& rContained3DRange)
211
0
        :   BaseProcessor3D(rViewInformation),
212
0
            mpPrimitive2DSequence(&maPrimitive2DSequence),
213
0
            maObjectTransformation(std::move(aObjectTransformation)),
214
0
            maLightNormal(rLightNormal),
215
0
            mfLightPlaneScalar(0.0),
216
0
            mbShadowProjectionIsValid(false),
217
0
            mbConvert(false),
218
0
            mbUseProjection(false)
219
0
        {
220
            // normalize light normal, get and normalize shadow plane normal and calculate scalar from it
221
0
            maLightNormal.normalize();
222
0
            maShadowPlaneNormal = basegfx::B3DVector(0.0, sin(fShadowSlant), cos(fShadowSlant));
223
0
            maShadowPlaneNormal.normalize();
224
0
            mfLightPlaneScalar = maLightNormal.scalar(maShadowPlaneNormal);
225
226
            // use only when scalar is > 0.0, so the light is in front of the object
227
0
            if(mfLightPlaneScalar <= 0.0 || basegfx::fTools::equalZero(mfLightPlaneScalar))
228
0
                return;
229
230
            // prepare buffered WorldToEye and EyeToView
231
0
            maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
232
0
            maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
233
234
            // calculate range to get front edge around which to rotate the shadow's projection
235
0
            basegfx::B3DRange aContained3DRange(rContained3DRange);
236
0
            aContained3DRange.transform(getWorldToEye());
237
0
            maPlanePoint.setX(maShadowPlaneNormal.getX() < 0.0 ? aContained3DRange.getMinX() : aContained3DRange.getMaxX());
238
0
            maPlanePoint.setY(maShadowPlaneNormal.getY() > 0.0 ? aContained3DRange.getMinY() : aContained3DRange.getMaxY());
239
0
            maPlanePoint.setZ(aContained3DRange.getMinZ() - (aContained3DRange.getDepth() / 8.0));
240
241
            // set flag that shadow projection is prepared and allowed
242
0
            mbShadowProjectionIsValid = true;
243
0
        }
244
245
        Shadow3DExtractingProcessor::~Shadow3DExtractingProcessor()
246
0
        {
247
0
            OSL_ENSURE(maPrimitive2DSequence.empty(),
248
0
                "OOps, someone used Shadow3DExtractingProcessor, but did not fetch the results (!)");
249
0
        }
250
251
        basegfx::B2DPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolygon& rSource)
252
0
        {
253
0
            basegfx::B2DPolygon aRetval;
254
255
0
            for(sal_uInt32 a(0); a < rSource.count(); a++)
256
0
            {
257
                // get point, transform to eye coordinate system
258
0
                basegfx::B3DPoint aCandidate(rSource.getB3DPoint(a));
259
0
                aCandidate *= getWorldToEye();
260
261
                // we are in eye coordinates
262
                // ray is (aCandidate + fCut * maLightNormal)
263
                // plane is (maPlanePoint, maShadowPlaneNormal)
264
                // maLightNormal.scalar(maShadowPlaneNormal) is already in mfLightPlaneScalar and > 0.0
265
                // get cut point of ray with shadow plane
266
0
                const double fCut(basegfx::B3DVector(maPlanePoint - aCandidate).scalar(maShadowPlaneNormal) / mfLightPlaneScalar);
267
0
                aCandidate += maLightNormal * fCut;
268
269
                // transform to view, use 2d coordinates
270
0
                aCandidate *= maEyeToView;
271
0
                aRetval.append(basegfx::B2DPoint(aCandidate.getX(), aCandidate.getY()));
272
0
            }
273
274
            // copy closed flag
275
0
            aRetval.setClosed(rSource.isClosed());
276
277
0
            return aRetval;
278
0
        }
279
280
        basegfx::B2DPolyPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolyPolygon& rSource)
281
0
        {
282
0
            basegfx::B2DPolyPolygon aRetval;
283
284
0
            for(sal_uInt32 a(0); a < rSource.count(); a++)
285
0
            {
286
0
                aRetval.append(impDoShadowProjection(rSource.getB3DPolygon(a)));
287
0
            }
288
289
0
            return aRetval;
290
0
        }
291
292
        const primitive2d::Primitive2DContainer& Shadow3DExtractingProcessor::getPrimitive2DSequence() const
293
0
        {
294
0
            return maPrimitive2DSequence;
295
0
        }
296
297
} // end of namespace
298
299
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */