Matrix.h revision 7ae7ac48aa2b53453c9805075171ecd5bcafd7de
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#ifndef ANDROID_UI_MATRIX_H
18#define ANDROID_UI_MATRIX_H
19
20#include <SkMatrix.h>
21
22#include "Rect.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Classes
29///////////////////////////////////////////////////////////////////////////////
30
31class Matrix4 {
32public:
33    float data[16];
34
35    Matrix4() {
36        loadIdentity();
37    }
38
39    Matrix4(const float* v) {
40        load(v);
41    }
42
43    Matrix4(const Matrix4& v) {
44        load(v);
45    }
46
47    Matrix4(const SkMatrix& v) {
48        load(v);
49    }
50
51    void loadIdentity();
52
53    void load(const float* v);
54    void load(const Matrix4& v);
55    void load(const SkMatrix& v);
56
57    void loadTranslate(float x, float y, float z);
58    void loadScale(float sx, float sy, float sz);
59    void loadRotate(float angle, float x, float y, float z);
60    void loadMultiply(const Matrix4& u, const Matrix4& v);
61
62    void loadOrtho(float left, float right, float bottom, float top, float near, float far);
63
64    void multiply(const Matrix4& v) {
65        Matrix4 u;
66        u.loadMultiply(*this, v);
67        load(u);
68    }
69
70    void translate(float x, float y, float z) {
71        Matrix4 u;
72        u.loadTranslate(x, y, z);
73        multiply(u);
74    }
75
76    void scale(float sx, float sy, float sz) {
77        Matrix4 u;
78        u.loadScale(sx, sy, sz);
79        multiply(u);
80    }
81
82    void rotate(float angle, float x, float y, float z) {
83        Matrix4 u;
84        u.loadRotate(angle, x, y, z);
85        multiply(u);
86    }
87
88    void copyTo(float* v) const;
89    void copyTo(SkMatrix& v) const;
90
91    void mapRect(Rect& r) const;
92
93    void dump() const;
94
95private:
96    inline float get(int i, int j) const {
97        return data[i * 4 + j];
98    }
99
100    inline void set(int i, int j, float v) {
101        data[i * 4 + j] = v;
102    }
103}; // class Matrix4
104
105///////////////////////////////////////////////////////////////////////////////
106// Types
107///////////////////////////////////////////////////////////////////////////////
108
109typedef Matrix4 mat4;
110
111}; // namespace uirenderer
112}; // namespace android
113
114#endif // ANDROID_UI_MATRIX_H
115