Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/basegfx/matrix/b2dhommatrix.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 <sal/config.h>
23
24
#include <ostream>
25
26
#include <sal/types.h>
27
#include <basegfx/basegfxdllapi.h>
28
#include <array>
29
30
namespace basegfx
31
{
32
    class B2DTuple;
33
34
    class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B2DHomMatrix
35
    {
36
    private:
37
        // Since this is a graphics matrix, the last row is always 0 0 1, so we don't bother to store it.
38
        std::array<std::array<double, 3>, 2> mfValues {
39
            std::array<double, 3>{ 1.0, 0.0, 0.0 },
40
            std::array<double, 3>{ 0.0, 1.0, 0.0 } };
41
42
    public:
43
2.11M
        constexpr B2DHomMatrix() = default;
44
45
        /** constructor to allow setting all needed values for a 3x2 matrix at once. The
46
            parameter f_0x1 e.g. is the same as using set(0, 1, f)
47
         */
48
        constexpr B2DHomMatrix(double f_0x0, double f_0x1, double f_0x2, double f_1x0, double f_1x1, double f_1x2)
49
1.14M
        {
50
1.14M
            mfValues[0][0] = f_0x0;
51
1.14M
            mfValues[0][1] = f_0x1;
52
1.14M
            mfValues[0][2] = f_0x2;
53
1.14M
            mfValues[1][0] = f_1x0;
54
1.14M
            mfValues[1][1] = f_1x1;
55
1.14M
            mfValues[1][2] = f_1x2;
56
1.14M
        }
57
58
        /** Convenience creator for declaration of the matrix that is commonly
59
            used by web standards (SVG, CSS, HTML).
60
61
           Values a,b,c,d,e,f represent the following values in the matrix:
62
              [a,c,e]      [a,c,e]
63
              [b,d,f]  or  [b,d,f]
64
                           [0,0,1]
65
66
        */
67
        static B2DHomMatrix abcdef(double da, double db, double dc, double dd, double de, double df)
68
3.35k
        {
69
3.35k
            return B2DHomMatrix(da, dc, de, db, dd, df);
70
3.35k
        }
71
72
        // Convenience accessor for value at 0,0 position in the matrix
73
6.38k
        double a() const { return get(0,0); }
74
        // Convenience accessor for value at 1,0 position in the matrix
75
5.26k
        double b() const { return get(1,0); }
76
        // Convenience accessor for value at 0,1 position in the matrix
77
6.38k
        double c() const { return get(0,1); }
78
        // Convenience accessor for value at 1,1 position in the matrix
79
5.26k
        double d() const { return get(1,1); }
80
        // Convenience accessor for value at 0,2 position in the matrix
81
0
        double e() const { return get(0,2); }
82
        // Convenience accessor for value at 1,2 position in the matrix
83
0
        double f() const { return get(1,2); }
84
85
        double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
86
28.5M
        {
87
28.5M
            return mfValues[nRow][nColumn];
88
28.5M
        }
89
90
        void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
91
4.27M
        {
92
4.27M
            mfValues[nRow][nColumn] = fValue;
93
4.27M
        }
94
95
        /** allow setting all needed values for a 3x2 matrix in one call. The
96
            parameter f_0x1 e.g. is the same as using set(0, 1, f)
97
         */
98
        void set3x2(double f_0x0, double f_0x1, double f_0x2, double f_1x0, double f_1x1, double f_1x2);
99
100
        // reset to a standard matrix
101
        bool isIdentity() const;
102
        void identity();
103
104
        bool isInvertible() const;
105
        bool invert();
106
107
        void rotate(double fRadiant);
108
109
        void translate(double fX, double fY);
110
        void translate(const B2DTuple& rTuple);
111
112
        void scale(double fX, double fY);
113
        void scale(const B2DTuple& rTuple);
114
115
        // Shearing-Matrices
116
        void shearX(double fSx);
117
        void shearY(double fSy);
118
119
        bool operator==(const B2DHomMatrix& rMat) const;
120
121
        // matrix multiplication from the left to the local
122
        B2DHomMatrix& operator*=(const B2DHomMatrix& rMat);
123
124
        /**
125
         * Help routine to decompose given homogen 3x3 matrix to components. A correction of the
126
         * components is done to avoid inaccuracies.
127
         *
128
         * See basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix() for the opposite, to
129
         * compose a homogen 3x3 matrix from components.
130
         */
131
        bool decompose(B2DTuple& rScale, B2DTuple& rTranslate, double& rRotate, double& rShearX) const;
132
133
    private:
134
        void computeAdjoint(double (&dst)[6]) const;
135
        double computeDeterminant(const double (&dst)[6]) const;
136
        void doMulMatrix(const B2DHomMatrix& rMat);
137
    };
138
139
    inline B2DHomMatrix operator*(const B2DHomMatrix& rMatA, const B2DHomMatrix& rMatB)
140
402k
    {
141
402k
        B2DHomMatrix aMul(rMatB);
142
402k
        aMul *= rMatA;
143
402k
        return aMul;
144
402k
    }
145
146
    template<typename charT, typename traits>
147
    std::basic_ostream<charT, traits> & operator <<(
148
        std::basic_ostream<charT, traits> & stream, B2DHomMatrix const & matrix)
149
0
    {
150
0
        return stream
151
0
            << '[' << matrix.get(0, 0) << ' ' << matrix.get(0, 1) << ' '
152
0
            << matrix.get(0, 2) << "; " << matrix.get(1, 0) << ' '
153
0
            << matrix.get(1, 1) << ' ' << matrix.get(1, 2) << ']';
154
0
    }
155
} // end of namespace basegfx
156
157
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */