Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/basegfx/vector/b3dvector.hxx
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
#pragma once
21
22
#include <basegfx/tuple/b3dtuple.hxx>
23
#include <basegfx/basegfxdllapi.h>
24
#include <cassert>
25
26
namespace basegfx
27
{
28
    class B3DHomMatrix;
29
30
    /** Base Point class with three double values
31
32
        This class derives all operators and common handling for
33
        a 3D data class from B3DTuple. All necessary extensions
34
        which are special for 3D Vectors are added here.
35
36
        @see B3DTuple
37
    */
38
    class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B3DVector : public ::basegfx::B3DTuple
39
    {
40
    public:
41
        /** Create a 3D Vector
42
43
            The vector is initialized to (0.0, 0.0, 0.0)
44
        */
45
        B3DVector()
46
139k
        {}
47
48
        /** Create a 3D Vector
49
50
            @param fX
51
            This parameter is used to initialize the X-coordinate
52
            of the 3D Vector.
53
54
            @param fY
55
            This parameter is used to initialize the Y-coordinate
56
            of the 3D Vector.
57
58
            @param fZ
59
            This parameter is used to initialize the Z-coordinate
60
            of the 3D Vector.
61
        */
62
        B3DVector(double fX, double fY, double fZ)
63
164k
        :   B3DTuple(fX, fY, fZ)
64
164k
        {}
65
66
        /** constructor with tuple to allow copy-constructing
67
            from B3DTuple-based classes
68
        */
69
        B3DVector(const ::basegfx::B3DTuple& rTuple)
70
27.8k
        :   B3DTuple(rTuple)
71
27.8k
        {}
72
73
        /** *=operator to allow usage from B3DVector, too
74
        */
75
        B3DVector& operator*=( const B3DVector& rPnt )
76
0
        {
77
0
            mnX *= rPnt.mnX;
78
0
            mnY *= rPnt.mnY;
79
0
            mnZ *= rPnt.mnZ;
80
0
            return *this;
81
0
        }
82
83
        /** *=operator to allow usage from B3DVector, too
84
        */
85
        B3DVector& operator*=(double t)
86
3.00k
        {
87
3.00k
            mnX *= t;
88
3.00k
            mnY *= t;
89
3.00k
            mnZ *= t;
90
3.00k
            return *this;
91
3.00k
        }
92
93
        /** assignment operator to allow assigning the results
94
            of B3DTuple calculations
95
        */
96
        B3DVector& operator=( const ::basegfx::B3DTuple& rVec )
97
61.5k
        {
98
61.5k
            mnX = rVec.getX();
99
61.5k
            mnY = rVec.getY();
100
61.5k
            mnZ = rVec.getZ();
101
61.5k
            return *this;
102
61.5k
        }
103
104
        /** Calculate the length of this 3D Vector
105
106
            @return The Length of the 3D Vector
107
        */
108
        double getLength() const
109
5.56k
        {
110
5.56k
            return std::hypot(mnX, mnY, mnZ);
111
5.56k
        }
112
113
        /** Calculate the length in the XZ-Plane for this 3D Vector
114
115
            @return The XZ-Plane Length of the 3D Vector
116
        */
117
        double getXZLength() const
118
0
        {
119
0
            return std::hypot(mnX, mnZ);
120
0
        }
121
122
        /** Calculate the length in the YZ-Plane for this 3D Vector
123
124
            @return The YZ-Plane Length of the 3D Vector
125
        */
126
        double getYZLength() const
127
5.56k
        {
128
5.56k
            return std::hypot(mnY, mnZ);
129
5.56k
        }
130
131
        /** Set the length of this 3D Vector
132
133
            @param fLen
134
            The to be achieved length of the 3D Vector
135
        */
136
        B3DVector& setLength(double fLen)
137
0
        {
138
0
            double fLenNow(std::hypot(mnX, mnY, mnZ));
139
0
140
0
            if(!::basegfx::fTools::equalZero(fLenNow))
141
0
            {
142
0
                assert(fLenNow != 0 && "help coverity see it's not zero");
143
0
144
0
                const double fOne(1.0);
145
0
146
0
                if(!::basegfx::fTools::equal(fOne, fLenNow))
147
0
                {
148
0
                    fLen /= fLenNow;
149
0
                }
150
0
151
0
                mnX *= fLen;
152
0
                mnY *= fLen;
153
0
                mnZ *= fLen;
154
0
            }
155
0
156
0
            return *this;
157
0
        }
158
159
        /** Normalize this 3D Vector
160
161
            The length of the 3D Vector is set to 1.0
162
        */
163
        B3DVector& normalize();
164
165
        /** get a 3D Vector which is perpendicular to this and a given 3D Vector
166
167
            @attention This only works if this and the given 3D Vector are
168
            both normalized.
169
170
            @param rNormalizedVec
171
            A normalized 3D Vector.
172
173
            @return
174
            A 3D Vector perpendicular to this and the given one
175
        */
176
        B3DVector getPerpendicular(const B3DVector& rNormalizedVec) const;
177
178
        /** Calculate the Scalar product
179
180
            This method calculates the Scalar product between this
181
            and the given 3D Vector.
182
183
            @param rVec
184
            A second 3D Vector.
185
186
            @return
187
            The Scalar Product of two 3D Vectors
188
        */
189
        double scalar(const B3DVector& rVec) const
190
0
        {
191
0
            return ((mnX * rVec.mnX) + (mnY * rVec.mnY) + (mnZ * rVec.mnZ));
192
0
        }
193
194
        /** Transform vector by given transformation matrix.
195
196
            Since this is a vector, translational components of the
197
            matrix are disregarded.
198
        */
199
        B3DVector& operator*=( const B3DHomMatrix& rMat );
200
201
        static const B3DVector& getEmptyVector()
202
0
        {
203
0
            return static_cast<const B3DVector&>( ::basegfx::B3DTuple::getEmptyTuple() );
204
0
        }
205
    };
206
207
    // external operators
208
209
210
    /** Test two vectors which need not to be normalized for parallelism
211
212
        @param rVecA
213
        The first 3D Vector
214
215
        @param rVecB
216
        The second 3D Vector
217
218
        @return
219
        bool if the two values are parallel. Also true if
220
        one of the vectors is empty.
221
    */
222
    BASEGFX_DLLPUBLIC bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB );
223
224
    /** Transform vector by given transformation matrix.
225
226
        Since this is a vector, translational components of the
227
        matrix are disregarded.
228
    */
229
    BASEGFX_DLLPUBLIC B3DVector operator*( const B3DHomMatrix& rMat, const B3DVector& rVec );
230
231
    /** Calculate the Cross Product of two 3D Vectors
232
233
        @param rVecA
234
        A first 3D Vector.
235
236
        @param rVecB
237
        A second 3D Vector.
238
239
        @return
240
        The Cross Product of both 3D Vectors
241
    */
242
    inline B3DVector cross(const B3DVector& rVecA, const B3DVector& rVecB)
243
61.1k
    {
244
61.1k
        B3DVector aVec(
245
61.1k
            rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
246
61.1k
            rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
247
61.1k
            rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
248
61.1k
        return aVec;
249
61.1k
    }
250
} // end of namespace basegfx
251
252
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */