Matrix.cpp 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#define LOG_TAG "UIMatrix" 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 { 30 31void Matrix4::loadIdentity() { 32 mMat[0] = 1.0f; 33 mMat[1] = 0.0f; 34 mMat[2] = 0.0f; 35 mMat[3] = 0.0f; 36 37 mMat[4] = 0.0f; 38 mMat[5] = 1.0f; 39 mMat[6] = 0.0f; 40 mMat[7] = 0.0f; 41 42 mMat[8] = 0.0f; 43 mMat[9] = 0.0f; 44 mMat[10] = 1.0f; 45 mMat[11] = 0.0f; 46 47 mMat[12] = 0.0f; 48 mMat[13] = 0.0f; 49 mMat[14] = 0.0f; 50 mMat[15] = 1.0f; 51} 52 53void Matrix4::load(const float* v) { 54 memcpy(mMat, v, sizeof(mMat)); 55} 56 57void Matrix4::load(const Matrix4& v) { 58 memcpy(mMat, v.mMat, sizeof(mMat)); 59} 60 61void Matrix4::load(const SkMatrix& v) { 62 memset(mMat, 0, sizeof(mMat)); 63 64 mMat[0] = v[SkMatrix::kMScaleX]; 65 mMat[4] = v[SkMatrix::kMSkewX]; 66 mMat[12] = v[SkMatrix::kMTransX]; 67 68 mMat[1] = v[SkMatrix::kMSkewY]; 69 mMat[5] = v[SkMatrix::kMScaleY]; 70 mMat[13] = v[SkMatrix::kMTransY]; 71 72 mMat[3] = v[SkMatrix::kMPersp0]; 73 mMat[7] = v[SkMatrix::kMPersp1]; 74 mMat[15] = v[SkMatrix::kMPersp2]; 75 76 mMat[10] = 1.0f; 77} 78 79void Matrix4::copyTo(SkMatrix& v) const { 80 v.reset(); 81 82 v.set(SkMatrix::kMScaleX, mMat[0]); 83 v.set(SkMatrix::kMSkewX, mMat[4]); 84 v.set(SkMatrix::kMTransX, mMat[12]); 85 86 v.set(SkMatrix::kMSkewY, mMat[1]); 87 v.set(SkMatrix::kMScaleY, mMat[5]); 88 v.set(SkMatrix::kMTransY, mMat[13]); 89 90 v.set(SkMatrix::kMPersp0, mMat[3]); 91 v.set(SkMatrix::kMPersp1, mMat[7]); 92 v.set(SkMatrix::kMPersp2, mMat[15]); 93} 94 95void Matrix4::copyTo(float* v) const { 96 memcpy(v, mMat, sizeof(mMat)); 97} 98 99void Matrix4::loadTranslate(float x, float y, float z) { 100 loadIdentity(); 101 mMat[12] = x; 102 mMat[13] = y; 103 mMat[14] = z; 104} 105 106void Matrix4::loadScale(float sx, float sy, float sz) { 107 loadIdentity(); 108 mMat[0] = sx; 109 mMat[5] = sy; 110 mMat[10] = sz; 111} 112 113void Matrix4::loadRotate(float angle, float x, float y, float z) { 114 mMat[3] = 0.0f; 115 mMat[7] = 0.0f; 116 mMat[11] = 0.0f; 117 mMat[12] = 0.0f; 118 mMat[13] = 0.0f; 119 mMat[14] = 0.0f; 120 mMat[15] = 1.0f; 121 122 angle *= float(M_PI / 180.0f); 123 float c = cosf(angle); 124 float s = sinf(angle); 125 126 const float length = sqrtf(x * x + y * y + z * z); 127 const float nc = 1.0f - c; 128 const float xy = x * y; 129 const float yz = y * z; 130 const float zx = z * x; 131 const float xs = x * s; 132 const float ys = y * s; 133 const float zs = z * s; 134 135 mMat[0] = x * x * nc + c; 136 mMat[4] = xy * nc - zs; 137 mMat[8] = zx * nc + ys; 138 mMat[1] = xy * nc + zs; 139 mMat[5] = y * y * nc + c; 140 mMat[9] = yz * nc - xs; 141 mMat[2] = zx * nc - ys; 142 mMat[6] = yz * nc + xs; 143 mMat[10] = z * z * nc + c; 144} 145 146void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) { 147 for (int i = 0 ; i < 4 ; i++) { 148 float x = 0; 149 float y = 0; 150 float z = 0; 151 float w = 0; 152 153 for (int j = 0 ; j < 4 ; j++) { 154 const float e = v.get(i,j); 155 x += u.get(j, 0) * e; 156 y += u.get(j, 1) * e; 157 z += u.get(j, 2) * e; 158 w += u.get(j, 3) * e; 159 } 160 161 set(i, 0, x); 162 set(i, 1, y); 163 set(i, 2, z); 164 set(i, 3, w); 165 } 166} 167 168void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) { 169 loadIdentity(); 170 mMat[0] = 2.0f / (right - left); 171 mMat[5] = 2.0f / (top - bottom); 172 mMat[10] = -2.0f / (far - near); 173 mMat[12] = -(right + left) / (right - left); 174 mMat[13] = -(top + bottom) / (top - bottom); 175 mMat[14] = -(far + near) / (far - near); 176} 177 178void Matrix4::dump() const { 179 LOGD("Matrix4["); 180 LOGD(" %f %f %f %f", mMat[0], mMat[4], mMat[ 8], mMat[12]); 181 LOGD(" %f %f %f %f", mMat[1], mMat[5], mMat[ 9], mMat[13]); 182 LOGD(" %f %f %f %f", mMat[2], mMat[6], mMat[10], mMat[14]); 183 LOGD(" %f %f %f %f", mMat[3], mMat[7], mMat[11], mMat[15]); 184 LOGD("]"); 185} 186 187}; 188