LibreOffice Module vcl (master) 1
Matrix3.cxx
Go to the documentation of this file.
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 */
10
11#include <pdf/Matrix3.hxx>
12#include <cmath>
13
14namespace vcl::pdf
15{
17{
18 // initialize to unity
19 f[0] = 1.0;
20 f[1] = 0.0;
21 f[2] = 0.0;
22 f[3] = 1.0;
23 f[4] = 0.0;
24 f[5] = 0.0;
25}
26
27Point Matrix3::transform(const Point& rOrig) const
28{
29 double x = static_cast<double>(rOrig.X()), y = static_cast<double>(rOrig.Y());
30 return Point(x * f[0] + y * f[2] + f[4], x * f[1] + y * f[3] + f[5]);
31}
32
34{
35 double x = rOrig.getX(), y = rOrig.getY();
36 return basegfx::B2DPoint(x * f[0] + y * f[2] + f[4], x * f[1] + y * f[3] + f[5]);
37}
38
39void Matrix3::skew(double alpha, double beta)
40{
41 double fn[6];
42 double tb = tan(beta);
43 fn[0] = f[0] + f[2] * tb;
44 fn[1] = f[1];
45 fn[2] = f[2] + f[3] * tb;
46 fn[3] = f[3];
47 fn[4] = f[4] + f[5] * tb;
48 fn[5] = f[5];
49 if (alpha != 0.0)
50 {
51 double ta = tan(alpha);
52 fn[1] += f[0] * ta;
53 fn[3] += f[2] * ta;
54 fn[5] += f[4] * ta;
55 }
56 set(fn);
57}
58
59void Matrix3::scale(double sx, double sy)
60{
61 double fn[6];
62 fn[0] = sx * f[0];
63 fn[1] = sy * f[1];
64 fn[2] = sx * f[2];
65 fn[3] = sy * f[3];
66 fn[4] = sx * f[4];
67 fn[5] = sy * f[5];
68 set(fn);
69}
70
71void Matrix3::rotate(double angle)
72{
73 double fn[6];
74 double fSin = sin(angle);
75 double fCos = cos(angle);
76 fn[0] = f[0] * fCos - f[1] * fSin;
77 fn[1] = f[0] * fSin + f[1] * fCos;
78 fn[2] = f[2] * fCos - f[3] * fSin;
79 fn[3] = f[2] * fSin + f[3] * fCos;
80 fn[4] = f[4] * fCos - f[5] * fSin;
81 fn[5] = f[4] * fSin + f[5] * fCos;
82 set(fn);
83}
84
85void Matrix3::translate(double tx, double ty)
86{
87 f[4] += tx;
88 f[5] += ty;
89}
90
92{
93 // short circuit trivial cases
94 if (f[1] == f[2] && f[1] == 0.0 && f[0] == f[3] && f[0] == 1.0)
95 {
96 f[4] = -f[4];
97 f[5] = -f[5];
98 return;
99 }
100
101 // check determinant
102 const double fDet = f[0] * f[3] - f[1] * f[2];
103 if (fDet == 0.0)
104 return;
105
106 // invert the matrix
107 double fn[6];
108 fn[0] = +f[3] / fDet;
109 fn[1] = -f[1] / fDet;
110 fn[2] = -f[2] / fDet;
111 fn[3] = +f[0] / fDet;
112
113 // apply inversion to translation
114 fn[4] = -(f[4] * fn[0] + f[5] * fn[2]);
115 fn[5] = -(f[4] * fn[1] + f[5] * fn[3]);
116
117 set(fn);
118}
119
120} // end vcl::pdf
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr tools::Long Y() const
constexpr tools::Long X() const
TYPE getX() const
TYPE getY() const
void translate(double tx, double ty)
Definition: Matrix3.cxx:85
Point transform(const Point &rPoint) const
Definition: Matrix3.cxx:27
double f[6]
Definition: Matrix3.hxx:29
void rotate(double angle)
Definition: Matrix3.cxx:71
void set(const double *pn)
Definition: Matrix3.hxx:31
void scale(double sx, double sy)
Definition: Matrix3.cxx:59
void skew(double alpha, double beta)
Definition: Matrix3.cxx:39
float y
float x
constexpr double alpha[nDetails]