Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/basegfx/vector/b2dvector.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/b2dtuple.hxx>
23
#include <basegfx/vector/b2ivector.hxx>
24
#include <basegfx/basegfxdllapi.h>
25
26
namespace basegfx
27
{
28
    class B2DHomMatrix;
29
    enum class B2VectorContinuity;
30
    enum class B2VectorOrientation;
31
32
    /** Base Point class with two double values
33
34
        This class derives all operators and common handling for
35
        a 2D data class from B2DTuple. All necessary extensions
36
        which are special for 2D Vectors are added here.
37
38
        @see B2DTuple
39
    */
40
    class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B2DVector : public ::basegfx::B2DTuple
41
    {
42
    public:
43
        /** Create a 2D Vector
44
45
            The vector is initialized to (0.0, 0.0)
46
        */
47
        B2DVector()
48
20.0M
        {}
49
50
        /** Create a 2D Vector
51
52
            @param fX
53
            This parameter is used to initialize the X-coordinate
54
            of the 2D Vector.
55
56
            @param fY
57
            This parameter is used to initialize the Y-coordinate
58
            of the 2D Vector.
59
        */
60
        B2DVector(double fX, double fY)
61
10.6M
        :   B2DTuple(fX, fY)
62
10.6M
        {}
63
64
        /** Create a copy of a 2D Vector
65
66
            @param rVec
67
            The 2D Vector which will be copied.
68
        */
69
        explicit B2DVector(const ::basegfx::B2IVector& rVec)
70
        :   B2DTuple(rVec)
71
0
        {}
72
73
        /** constructor with tuple to allow copy-constructing
74
            from B2DTuple-based classes
75
        */
76
        B2DVector(Tuple2D<double> const& rTuple)
77
1.18G
            :   B2DTuple(rTuple)
78
1.18G
        {}
79
80
        /** *=operator to allow usage from B2DVector, too
81
        */
82
        B2DVector& operator*=( const B2DVector& rPnt )
83
0
        {
84
0
            mnX *= rPnt.mnX;
85
0
            mnY *= rPnt.mnY;
86
0
            return *this;
87
0
        }
88
89
        /** *=operator to allow usage from B2DVector, too
90
        */
91
        B2DVector& operator*=(double t)
92
0
        {
93
0
            mnX *= t;
94
0
            mnY *= t;
95
0
            return *this;
96
0
        }
97
98
        /** assignment operator to allow assigning the results
99
            of B2DTuple calculations
100
        */
101
        B2DVector& operator=(Tuple2D<double> const& rVector)
102
490k
        {
103
490k
            mnX = rVector.getX();
104
490k
            mnY = rVector.getY();
105
490k
            return *this;
106
490k
        }
107
108
        /** Calculate the length of this 2D Vector
109
110
            @return The Length of the 2D Vector
111
        */
112
        double getLength() const;
113
114
        /** Set the length of this 2D Vector
115
116
            @param fLen
117
            The to be achieved length of the 2D Vector
118
        */
119
        B2DVector& setLength(double fLen);
120
121
        /** Normalize this 2D Vector
122
123
            The length of the 2D Vector is set to 1.0
124
        */
125
        B2DVector& normalize();
126
127
        /** Calculate the Scalar with another 2D Vector
128
129
            @param rVec
130
            The second 2D Vector
131
132
            @return
133
            The Scalar value of the two involved 2D Vectors
134
        */
135
294k
        double scalar( const B2DVector& rVec ) const { return((mnX * rVec.mnX) + (mnY * rVec.mnY)); }
136
137
        /** Calculate the length of the cross product with another 2D Vector
138
139
            In 2D, returning an actual vector does not make much
140
            sense here. The magnitude, although, can be readily
141
            used for tasks such as angle calculations, since for
142
            the returned value, the following equation holds:
143
            retVal = getLength(this)*getLength(rVec)*sin(theta),
144
            with theta being the angle between the two vectors.
145
146
            @param rVec
147
            The second 2D Vector
148
149
            @return
150
            The length of the cross product of the two involved 2D Vectors
151
        */
152
18.3M
        double cross( const B2DVector& rVec ) const { return(mnX * rVec.getY() - mnY * rVec.getX()); }
153
154
        /** Calculate the Angle with another 2D Vector
155
156
            @param rVec
157
            The second 2D Vector
158
159
            @return
160
            The Angle value of the two involved 2D Vectors ranging from -pi to +pi
161
        */
162
        double angle( const B2DVector& rVec ) const;
163
164
        /** Transform vector by given transformation matrix.
165
166
            Since this is a vector, translational components of the
167
            matrix are disregarded.
168
        */
169
        B2DVector& operator*=( const B2DHomMatrix& rMat );
170
171
        static const B2DVector& getEmptyVector();
172
    };
173
174
    // external operators
175
176
177
    /** Calculate the orientation to another 2D Vector
178
179
        @param rVecA
180
        The first 2D Vector
181
182
        @param rVecB
183
        The second 2D Vector
184
185
        @return
186
        The mathematical Orientation of the two involved 2D Vectors
187
    */
188
    BASEGFX_DLLPUBLIC B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB );
189
190
    /** Calculate a perpendicular 2D Vector to the given one
191
192
        @param rVec
193
        The source 2D Vector
194
195
        @attention This only works if the given 2D Vector is normalized.
196
197
        @return
198
        A 2D Vector perpendicular to the one given in parameter rVec
199
    */
200
    BASEGFX_DLLPUBLIC B2DVector getPerpendicular( const B2DVector& rNormalizedVec );
201
202
    /** Calculate a perpendicular 2D Vector to the given one,
203
        normalize the given one as preparation
204
205
        @param rVec
206
        The source 2D Vector
207
208
        @return
209
        A normalized 2D Vector perpendicular to the one given in parameter rVec
210
    */
211
    BASEGFX_DLLPUBLIC B2DVector getNormalizedPerpendicular( const B2DVector& rVec );
212
213
    /** Test two vectors which need not to be normalized for parallelism
214
215
        @param rVecA
216
        The first 2D Vector
217
218
        @param rVecB
219
        The second 2D Vector
220
221
        @return
222
        bool if the two values are parallel. Also true if
223
        one of the vectors is empty.
224
    */
225
    BASEGFX_DLLPUBLIC bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB );
226
227
    /** Transform vector by given transformation matrix.
228
229
        Since this is a vector, translational components of the
230
        matrix are disregarded.
231
    */
232
    BASEGFX_DLLPUBLIC B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec );
233
234
    /** Test continuity between given vectors.
235
236
        The two given vectors are assumed to describe control points on a
237
        common point. Calculate if there is a continuity between them.
238
    */
239
    BASEGFX_DLLPUBLIC B2VectorContinuity getContinuity( const B2DVector& rBackVector, const B2DVector& rForwardVector );
240
241
} // end of namespace basegfx
242
243
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */