Matrix.cpp revision c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6c
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Matrix"
18
19#include <math.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <utils/Log.h>
24
25#include <SkMatrix.h>
26
27#include "Matrix.h"
28
29namespace android {
30namespace uirenderer {
31
32void Matrix4::loadIdentity() {
33	data[0]  = 1.0f;
34	data[1]  = 0.0f;
35	data[2]  = 0.0f;
36	data[3]  = 0.0f;
37
38	data[4]  = 0.0f;
39	data[5]  = 1.0f;
40	data[6]  = 0.0f;
41	data[7]  = 0.0f;
42
43	data[8]  = 0.0f;
44	data[9]  = 0.0f;
45	data[10] = 1.0f;
46	data[11] = 0.0f;
47
48	data[12] = 0.0f;
49	data[13] = 0.0f;
50	data[14] = 0.0f;
51	data[15] = 1.0f;
52}
53
54void Matrix4::load(const float* v) {
55	memcpy(data, v, sizeof(data));
56}
57
58void Matrix4::load(const Matrix4& v) {
59	memcpy(data, v.data, sizeof(data));
60}
61
62void Matrix4::load(const SkMatrix& v) {
63	memset(data, 0, sizeof(data));
64
65	data[0]  = v[SkMatrix::kMScaleX];
66	data[4]  = v[SkMatrix::kMSkewX];
67	data[12] = v[SkMatrix::kMTransX];
68
69	data[1]  = v[SkMatrix::kMSkewY];
70	data[5]  = v[SkMatrix::kMScaleY];
71	data[13] = v[SkMatrix::kMTransY];
72
73	data[3]  = v[SkMatrix::kMPersp0];
74	data[7]  = v[SkMatrix::kMPersp1];
75	data[15] = v[SkMatrix::kMPersp2];
76
77	data[10] = 1.0f;
78}
79
80void Matrix4::copyTo(SkMatrix& v) const {
81	v.reset();
82
83	v.set(SkMatrix::kMScaleX, data[0]);
84	v.set(SkMatrix::kMSkewX,  data[4]);
85	v.set(SkMatrix::kMTransX, data[12]);
86
87	v.set(SkMatrix::kMSkewY,  data[1]);
88	v.set(SkMatrix::kMScaleY, data[5]);
89	v.set(SkMatrix::kMTransY, data[13]);
90
91	v.set(SkMatrix::kMPersp0, data[3]);
92	v.set(SkMatrix::kMPersp1, data[7]);
93	v.set(SkMatrix::kMPersp2, data[15]);
94}
95
96void Matrix4::copyTo(float* v) const {
97	memcpy(v, data, sizeof(data));
98}
99
100void Matrix4::loadTranslate(float x, float y, float z) {
101	loadIdentity();
102	data[12] = x;
103	data[13] = y;
104	data[14] = z;
105}
106
107void Matrix4::loadScale(float sx, float sy, float sz) {
108	loadIdentity();
109	data[0]  = sx;
110	data[5]  = sy;
111	data[10] = sz;
112}
113
114void Matrix4::loadRotate(float angle, float x, float y, float z) {
115	data[3]  = 0.0f;
116	data[7]  = 0.0f;
117	data[11] = 0.0f;
118	data[12] = 0.0f;
119	data[13] = 0.0f;
120	data[14] = 0.0f;
121	data[15] = 1.0f;
122
123	angle *= float(M_PI / 180.0f);
124	float c = cosf(angle);
125	float s = sinf(angle);
126
127	const float length = sqrtf(x * x + y * y + z * z);
128	const float nc = 1.0f - c;
129	const float xy = x * y;
130	const float yz = y * z;
131	const float zx = z * x;
132	const float xs = x * s;
133	const float ys = y * s;
134	const float zs = z * s;
135
136	data[0]  = x * x * nc +  c;
137	data[4]  =    xy * nc - zs;
138	data[8]  =    zx * nc + ys;
139	data[1]  =    xy * nc + zs;
140	data[5]  = y * y * nc +  c;
141	data[9]  =    yz * nc - xs;
142	data[2]  =    zx * nc - ys;
143	data[6]  =    yz * nc + xs;
144	data[10] = z * z * nc +  c;
145}
146
147void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) {
148    for (int i = 0 ; i < 4 ; i++) {
149        float x = 0;
150        float y = 0;
151        float z = 0;
152        float w = 0;
153
154        for (int j = 0 ; j < 4 ; j++) {
155            const float e = v.get(i, j);
156            x += u.get(j, 0) * e;
157            y += u.get(j, 1) * e;
158            z += u.get(j, 2) * e;
159            w += u.get(j, 3) * e;
160        }
161
162        set(i, 0, x);
163        set(i, 1, y);
164        set(i, 2, z);
165        set(i, 3, w);
166    }
167}
168
169void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
170    loadIdentity();
171    data[0]  = 2.0f / (right - left);
172    data[5]  = 2.0f / (top - bottom);
173    data[10] = -2.0f / (far - near);
174    data[12] = -(right + left) / (right - left);
175    data[13] = -(top + bottom) / (top - bottom);
176    data[14] = -(far + near) / (far - near);
177}
178
179#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c)
180
181void Matrix4::mapRect(Rect& r) const {
182	const float sx = data[0];
183	const float sy = data[5];
184
185	const float tx = data[12];
186	const float ty = data[13];
187
188	MUL_ADD_STORE(r.left, sx, tx);
189	MUL_ADD_STORE(r.right, sx, tx);
190	MUL_ADD_STORE(r.top, sy, ty);
191	MUL_ADD_STORE(r.bottom, sy, ty);
192}
193
194void Matrix4::dump() const {
195	LOGD("Matrix4[");
196	LOGD("  %f %f %f %f", data[0], data[4], data[ 8], data[12]);
197	LOGD("  %f %f %f %f", data[1], data[5], data[ 9], data[13]);
198	LOGD("  %f %f %f %f", data[2], data[6], data[10], data[14]);
199	LOGD("  %f %f %f %f", data[3], data[7], data[11], data[15]);
200	LOGD("]");
201}
202
203}; // namespace uirenderer
204}; // namespace android
205