Matrix.h revision 079ba2c85b15e882629b8d188f5fbdb42f7f8eea
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 loadInverse(const Matrix4& v);
58
59    void loadTranslate(float x, float y, float z);
60    void loadScale(float sx, float sy, float sz);
61    void loadRotate(float angle, float x, float y, float z);
62    void loadMultiply(const Matrix4& u, const Matrix4& v);
63
64    void loadOrtho(float left, float right, float bottom, float top, float near, float far);
65
66    void multiply(const Matrix4& v) {
67        Matrix4 u;
68        u.loadMultiply(*this, v);
69        load(u);
70    }
71
72    void translate(float x, float y, float z) {
73        Matrix4 u;
74        u.loadTranslate(x, y, z);
75        multiply(u);
76    }
77
78    void scale(float sx, float sy, float sz) {
79        Matrix4 u;
80        u.loadScale(sx, sy, sz);
81        multiply(u);
82    }
83
84    void rotate(float angle, float x, float y, float z) {
85        Matrix4 u;
86        u.loadRotate(angle, x, y, z);
87        multiply(u);
88    }
89
90    void copyTo(float* v) const;
91    void copyTo(SkMatrix& v) const;
92
93    /**
94     * Does not apply rotations!
95     */
96    void mapRect(Rect& r) const;
97
98    float getTranslateX();
99    float getTranslateY();
100
101    void dump() const;
102
103private:
104    inline float get(int i, int j) const {
105        return data[i * 4 + j];
106    }
107
108    inline void set(int i, int j, float v) {
109        data[i * 4 + j] = v;
110    }
111}; // class Matrix4
112
113///////////////////////////////////////////////////////////////////////////////
114// Types
115///////////////////////////////////////////////////////////////////////////////
116
117typedef Matrix4 mat4;
118
119}; // namespace uirenderer
120}; // namespace android
121
122#endif // ANDROID_UI_MATRIX_H
123