1 // This is an incomplete port of http://glmatrix.net/
2 //
3 // Copyright (c) 2013 Brandon Jones, Colin MacKenzie IV
4 //
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors be held liable for any damages arising from the
7 // use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not claim
14 // that you wrote the original software. If you use this software in a
15 // product, an acknowledgment in the product documentation would be
16 // appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not be
19 // misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22
23 #include <mbgl/util/mat3.hpp>
24
25 #include <cmath>
26
27 namespace mbgl {
28 namespace matrix {
29
identity(mat3 & out)30 void identity(mat3& out) {
31 out[0] = 1.0f;
32 out[1] = 0.0f;
33 out[2] = 0.0f;
34 out[3] = 0.0f;
35 out[4] = 1.0f;
36 out[5] = 0.0f;
37 out[6] = 0.0f;
38 out[7] = 0.0f;
39 out[8] = 1.0f;
40 }
41
translate(mat3 & out,const mat3 & a,double x,double y)42 void translate(mat3& out, const mat3& a, double x, double y) {
43 double a00 = a[0], a01 = a[1], a02 = a[2],
44 a10 = a[3], a11 = a[4], a12 = a[5],
45 a20 = a[6], a21 = a[7], a22 = a[8];
46
47 out[0] = a00;
48 out[1] = a01;
49 out[2] = a02;
50
51 out[3] = a10;
52 out[4] = a11;
53 out[5] = a12;
54
55 out[6] = x * a00 + y * a10 + a20;
56 out[7] = x * a01 + y * a11 + a21;
57 out[8] = x * a02 + y * a12 + a22;
58 }
59
rotate(mat3 & out,const mat3 & a,double rad)60 void rotate(mat3& out, const mat3& a, double rad) {
61 double s = std::sin(rad),
62 c = std::cos(rad),
63 a00 = a[0],
64 a01 = a[1],
65 a02 = a[2],
66 a10 = a[3],
67 a11 = a[4],
68 a12 = a[5],
69 a20 = a[6],
70 a21 = a[7],
71 a22 = a[8];
72
73 out[0] = c * a00 + s * a10;
74 out[1] = c * a01 + s * a11;
75 out[2] = c * a02 + s * a12;
76
77 out[3] = c * a10 - s * a00;
78 out[4] = c * a11 - s * a01;
79 out[5] = c * a12 - s * a02;
80
81 out[6] = a20;
82 out[7] = a21;
83 out[8] = a22;
84 }
85
scale(mat3 & out,const mat3 & a,double x,double y)86 void scale(mat3& out, const mat3& a, double x, double y) {
87 out[0] = x * a[0];
88 out[1] = x * a[1];
89 out[2] = x * a[2];
90 out[3] = y * a[3];
91 out[4] = y * a[4];
92 out[5] = y * a[5];
93 out[6] = a[6];
94 out[7] = a[7];
95 out[8] = a[8];
96 }
97
transformMat3f(vec3f & out,const vec3f & a,const mat3 & m)98 void transformMat3f(vec3f& out, const vec3f& a, const mat3& m) {
99 out[0] = m[0] * a[0] + m[3] * a[1] + m[6] * a[2];
100 out[1] = m[1] * a[0] + m[4] * a[1] + m[7] * a[2];
101 out[2] = m[2] * a[0] + m[5] * a[1] + m[8] * a[2];
102 }
103
104 } // namespace matrix
105 } // namespace mbgl
106