QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsmatrix4x4.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmatrix4x4.cpp
3 --------------------------------------
4 Date : July 2023
5 Copyright : (C) 2023 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsmatrix4x4.h"
17
18// the implementation is partially based on Qt's QMatrix4x4 (simplified)
19
20
21QgsMatrix4x4::QgsMatrix4x4( double m11, double m12, double m13, double m14,
22 double m21, double m22, double m23, double m24,
23 double m31, double m32, double m33, double m34,
24 double m41, double m42, double m43, double m44 )
25{
26 m[0][0] = m11; m[0][1] = m21; m[0][2] = m31; m[0][3] = m41;
27 m[1][0] = m12; m[1][1] = m22; m[1][2] = m32; m[1][3] = m42;
28 m[2][0] = m13; m[2][1] = m23; m[2][2] = m33; m[2][3] = m43;
29 m[3][0] = m14; m[3][1] = m24; m[3][2] = m34; m[3][3] = m44;
30}
31
33{
34 m[3][0] += m[0][0] * vector.x() + m[1][0] * vector.y() + m[2][0] * vector.z();
35 m[3][1] += m[0][1] * vector.x() + m[1][1] * vector.y() + m[2][1] * vector.z();
36 m[3][2] += m[0][2] * vector.x() + m[1][2] * vector.y() + m[2][2] * vector.z();
37 m[3][3] += m[0][3] * vector.x() + m[1][3] * vector.y() + m[2][3] * vector.z();
38}
39
40QList< double > QgsMatrix4x4::dataList() const
41{
42 QList< double > res;
43 res.reserve( 9 );
44 for ( int i = 0; i < 16; ++i )
45 {
46 res.append( m[i / 4][i % 4] );
47 }
48 return res;
49}
50
51QgsVector3D operator*( const QgsMatrix4x4 &matrix, const QgsVector3D &vector )
52{
53 double x, y, z, w;
54
55 x = vector.x() * matrix.m[0][0] +
56 vector.y() * matrix.m[1][0] +
57 vector.z() * matrix.m[2][0] +
58 matrix.m[3][0];
59 y = vector.x() * matrix.m[0][1] +
60 vector.y() * matrix.m[1][1] +
61 vector.z() * matrix.m[2][1] +
62 matrix.m[3][1];
63 z = vector.x() * matrix.m[0][2] +
64 vector.y() * matrix.m[1][2] +
65 vector.z() * matrix.m[2][2] +
66 matrix.m[3][2];
67 w = vector.x() * matrix.m[0][3] +
68 vector.y() * matrix.m[1][3] +
69 vector.z() * matrix.m[2][3] +
70 matrix.m[3][3];
71 if ( w == 1.0f )
72 return QgsVector3D( x, y, z );
73 else
74 return QgsVector3D( x / w, y / w, z / w );
75}
76
78{
79 if ( m[0][0] != 1.0 || m[0][1] != 0.0 || m[0][2] != 0.0 )
80 return false;
81 if ( m[0][3] != 0.0 || m[1][0] != 0.0 || m[1][1] != 1.0 )
82 return false;
83 if ( m[1][2] != 0.0 || m[1][3] != 0.0 || m[2][0] != 0.0 )
84 return false;
85 if ( m[2][1] != 0.0 || m[2][2] != 1.0 || m[2][3] != 0.0 )
86 return false;
87 if ( m[3][0] != 0.0 || m[3][1] != 0.0 || m[3][2] != 0.0 )
88 return false;
89 return ( m[3][3] == 1.0 );
90}
91
93{
94 m[0][0] = 1.0;
95 m[0][1] = 0.0;
96 m[0][2] = 0.0;
97 m[0][3] = 0.0;
98 m[1][0] = 0.0;
99 m[1][1] = 1.0;
100 m[1][2] = 0.0;
101 m[1][3] = 0.0;
102 m[2][0] = 0.0;
103 m[2][1] = 0.0;
104 m[2][2] = 1.0;
105 m[2][3] = 0.0;
106 m[3][0] = 0.0;
107 m[3][1] = 0.0;
108 m[3][2] = 0.0;
109 m[3][3] = 1.0;
110}
111
112
114{
116 m.m[0][0] = m1.m[0][0] * m2.m[0][0]
117 + m1.m[1][0] * m2.m[0][1]
118 + m1.m[2][0] * m2.m[0][2]
119 + m1.m[3][0] * m2.m[0][3];
120 m.m[0][1] = m1.m[0][1] * m2.m[0][0]
121 + m1.m[1][1] * m2.m[0][1]
122 + m1.m[2][1] * m2.m[0][2]
123 + m1.m[3][1] * m2.m[0][3];
124 m.m[0][2] = m1.m[0][2] * m2.m[0][0]
125 + m1.m[1][2] * m2.m[0][1]
126 + m1.m[2][2] * m2.m[0][2]
127 + m1.m[3][2] * m2.m[0][3];
128 m.m[0][3] = m1.m[0][3] * m2.m[0][0]
129 + m1.m[1][3] * m2.m[0][1]
130 + m1.m[2][3] * m2.m[0][2]
131 + m1.m[3][3] * m2.m[0][3];
132
133 m.m[1][0] = m1.m[0][0] * m2.m[1][0]
134 + m1.m[1][0] * m2.m[1][1]
135 + m1.m[2][0] * m2.m[1][2]
136 + m1.m[3][0] * m2.m[1][3];
137 m.m[1][1] = m1.m[0][1] * m2.m[1][0]
138 + m1.m[1][1] * m2.m[1][1]
139 + m1.m[2][1] * m2.m[1][2]
140 + m1.m[3][1] * m2.m[1][3];
141 m.m[1][2] = m1.m[0][2] * m2.m[1][0]
142 + m1.m[1][2] * m2.m[1][1]
143 + m1.m[2][2] * m2.m[1][2]
144 + m1.m[3][2] * m2.m[1][3];
145 m.m[1][3] = m1.m[0][3] * m2.m[1][0]
146 + m1.m[1][3] * m2.m[1][1]
147 + m1.m[2][3] * m2.m[1][2]
148 + m1.m[3][3] * m2.m[1][3];
149
150 m.m[2][0] = m1.m[0][0] * m2.m[2][0]
151 + m1.m[1][0] * m2.m[2][1]
152 + m1.m[2][0] * m2.m[2][2]
153 + m1.m[3][0] * m2.m[2][3];
154 m.m[2][1] = m1.m[0][1] * m2.m[2][0]
155 + m1.m[1][1] * m2.m[2][1]
156 + m1.m[2][1] * m2.m[2][2]
157 + m1.m[3][1] * m2.m[2][3];
158 m.m[2][2] = m1.m[0][2] * m2.m[2][0]
159 + m1.m[1][2] * m2.m[2][1]
160 + m1.m[2][2] * m2.m[2][2]
161 + m1.m[3][2] * m2.m[2][3];
162 m.m[2][3] = m1.m[0][3] * m2.m[2][0]
163 + m1.m[1][3] * m2.m[2][1]
164 + m1.m[2][3] * m2.m[2][2]
165 + m1.m[3][3] * m2.m[2][3];
166
167 m.m[3][0] = m1.m[0][0] * m2.m[3][0]
168 + m1.m[1][0] * m2.m[3][1]
169 + m1.m[2][0] * m2.m[3][2]
170 + m1.m[3][0] * m2.m[3][3];
171 m.m[3][1] = m1.m[0][1] * m2.m[3][0]
172 + m1.m[1][1] * m2.m[3][1]
173 + m1.m[2][1] * m2.m[3][2]
174 + m1.m[3][1] * m2.m[3][3];
175 m.m[3][2] = m1.m[0][2] * m2.m[3][0]
176 + m1.m[1][2] * m2.m[3][1]
177 + m1.m[2][2] * m2.m[3][2]
178 + m1.m[3][2] * m2.m[3][3];
179 m.m[3][3] = m1.m[0][3] * m2.m[3][0]
180 + m1.m[1][3] * m2.m[3][1]
181 + m1.m[2][3] * m2.m[3][2]
182 + m1.m[3][3] * m2.m[3][3];
183 return m;
184}
A simple 4x4 matrix implementation useful for transformation in 3D space.
QgsMatrix4x4()
Initializes identity matrix.
void setToIdentity()
Sets matrix to be identity matrix.
bool isIdentity() const
Returns whether this matrix is an identity matrix.
void translate(const QgsVector3D &vector)
Multiplies this matrix by another that translates coordinates by the components of a vector.
QList< double > dataList() const
Returns matrix data (in column-major order)
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:52
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
QgsVector3D operator*(const QgsMatrix4x4 &matrix, const QgsVector3D &vector)
Matrix-matrix multiplication (useful to concatenate transforms)