Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/drawinglayer/source/attribute/sdrlightingattribute3d.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 <drawinglayer/attribute/sdrlightingattribute3d.hxx>
21
#include <basegfx/color/bcolor.hxx>
22
#include <basegfx/vector/b3dvector.hxx>
23
#include <drawinglayer/attribute/sdrlightattribute3d.hxx>
24
25
26
namespace drawinglayer::attribute
27
{
28
        class ImpSdrLightingAttribute
29
        {
30
        public:
31
            // 3D light attribute definitions
32
            basegfx::BColor                         maAmbientLight;
33
            std::vector< Sdr3DLightAttribute >    maLightVector;
34
35
            ImpSdrLightingAttribute(
36
                const basegfx::BColor& rAmbientLight,
37
                std::vector< Sdr3DLightAttribute >&& rLightVector)
38
0
            :   maAmbientLight(rAmbientLight),
39
0
                maLightVector(std::move(rLightVector))
40
0
            {
41
0
            }
42
43
            ImpSdrLightingAttribute()
44
1
            {
45
1
            }
46
47
            // data read access
48
0
            const basegfx::BColor& getAmbientLight() const { return maAmbientLight; }
49
0
            const std::vector< Sdr3DLightAttribute >& getLightVector() const { return maLightVector; }
50
51
            bool operator==(const ImpSdrLightingAttribute& rCandidate) const
52
0
            {
53
0
                return (getAmbientLight() == rCandidate.getAmbientLight()
54
0
                    && getLightVector() == rCandidate.getLightVector());
55
0
            }
56
        };
57
58
        namespace
59
        {
60
            SdrLightingAttribute::ImplType& theGlobalDefault()
61
94.1k
            {
62
94.1k
                static SdrLightingAttribute::ImplType SINGLETON;
63
94.1k
                return SINGLETON;
64
94.1k
            }
65
        }
66
67
        SdrLightingAttribute::SdrLightingAttribute(
68
            const basegfx::BColor& rAmbientLight,
69
            std::vector< Sdr3DLightAttribute >&& rLightVector)
70
0
        :   mpSdrLightingAttribute(ImpSdrLightingAttribute(
71
0
                rAmbientLight, std::move(rLightVector)))
72
0
        {
73
0
        }
74
75
        SdrLightingAttribute::SdrLightingAttribute()
76
94.1k
        :   mpSdrLightingAttribute(theGlobalDefault())
77
94.1k
        {
78
94.1k
        }
79
80
0
        SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute&) = default;
81
82
0
        SdrLightingAttribute::SdrLightingAttribute(SdrLightingAttribute&&) = default;
83
84
94.1k
        SdrLightingAttribute::~SdrLightingAttribute() = default;
85
86
87
        bool SdrLightingAttribute::isDefault() const
88
0
        {
89
0
            return mpSdrLightingAttribute.same_object(theGlobalDefault());
90
0
        }
91
92
0
        SdrLightingAttribute& SdrLightingAttribute::operator=(const SdrLightingAttribute&) = default;
93
94
90.9k
        SdrLightingAttribute& SdrLightingAttribute::operator=(SdrLightingAttribute&&) = default;
95
96
        bool SdrLightingAttribute::operator==(const SdrLightingAttribute& rCandidate) const
97
0
        {
98
            // tdf#87509 default attr is always != non-default attr, even with same values
99
0
            if(rCandidate.isDefault() != isDefault())
100
0
                return false;
101
102
0
            return rCandidate.mpSdrLightingAttribute == mpSdrLightingAttribute;
103
0
        }
104
105
        const std::vector< Sdr3DLightAttribute >& SdrLightingAttribute::getLightVector() const
106
0
        {
107
0
            return mpSdrLightingAttribute->getLightVector();
108
0
        }
109
110
        const basegfx::BColor& SdrLightingAttribute::getAmbientLightColor() const
111
0
        {
112
0
            return mpSdrLightingAttribute->maAmbientLight;
113
0
        }
114
115
        // color model solver
116
        basegfx::BColor SdrLightingAttribute::solveColorModel(
117
            const basegfx::B3DVector& rNormalInEyeCoordinates,
118
            const basegfx::BColor& rColor, const basegfx::BColor& rSpecular,
119
            const basegfx::BColor& rEmission, sal_uInt16 nSpecularIntensity) const
120
0
        {
121
            // initialize with emissive color
122
0
            basegfx::BColor aRetval(rEmission);
123
124
            // take care of global ambient light
125
0
            aRetval += mpSdrLightingAttribute->getAmbientLight() * rColor;
126
127
0
            const std::vector<Sdr3DLightAttribute>& rLightVector = mpSdrLightingAttribute->getLightVector();
128
129
            // prepare light access. Is there a light?
130
0
            const sal_uInt32 nLightCount(rLightVector.size());
131
132
0
            if(nLightCount && !rNormalInEyeCoordinates.equalZero())
133
0
            {
134
                // prepare normal
135
0
                basegfx::B3DVector aEyeNormal(rNormalInEyeCoordinates);
136
0
                aEyeNormal.normalize();
137
138
0
                for(sal_uInt32 a(0); a < nLightCount; a++)
139
0
                {
140
0
                    const Sdr3DLightAttribute& rLight(rLightVector[a]);
141
0
                    const double fCosFac(rLight.getDirection().scalar(aEyeNormal));
142
143
0
                    if(fCosFac > 0.0)
144
0
                    {
145
0
                        aRetval += (rLight.getColor() * rColor) * fCosFac;
146
147
0
                        if(rLight.getSpecular())
148
0
                        {
149
                            // expand by (0.0, 0.0, 1.0) in Z
150
0
                            basegfx::B3DVector aSpecularNormal(rLight.getDirection().getX(), rLight.getDirection().getY(), rLight.getDirection().getZ() + 1.0);
151
0
                            aSpecularNormal.normalize();
152
0
                            double fCosFac2(aSpecularNormal.scalar(aEyeNormal));
153
154
0
                            if(fCosFac2 > 0.0)
155
0
                            {
156
0
                                fCosFac2 = pow(fCosFac2, static_cast<double>(nSpecularIntensity));
157
0
                                aRetval += rSpecular * fCosFac2;
158
0
                            }
159
0
                        }
160
0
                    }
161
0
                }
162
0
            }
163
164
            // clamp to color space before usage
165
0
            aRetval.clamp();
166
167
0
            return aRetval;
168
0
        }
169
170
} // end of namespace
171
172
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */