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