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