Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | package android.renderscript; |
| 18 | |
| 19 | import java.lang.Math; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * @hide |
| 25 | * |
| 26 | **/ |
| 27 | public class Matrix3f { |
| 28 | |
| 29 | public Matrix3f() { |
| 30 | mMat = new float[9]; |
| 31 | loadIdentity(); |
| 32 | } |
| 33 | |
| 34 | public float get(int i, int j) { |
| 35 | return mMat[i*3 + j]; |
| 36 | } |
| 37 | |
| 38 | public void set(int i, int j, float v) { |
| 39 | mMat[i*3 + j] = v; |
| 40 | } |
| 41 | |
| 42 | public void loadIdentity() { |
| 43 | mMat[0] = 1; |
| 44 | mMat[1] = 0; |
| 45 | mMat[2] = 0; |
| 46 | |
| 47 | mMat[3] = 0; |
| 48 | mMat[4] = 1; |
| 49 | mMat[5] = 0; |
| 50 | |
| 51 | mMat[6] = 0; |
| 52 | mMat[7] = 0; |
| 53 | mMat[8] = 1; |
| 54 | } |
| 55 | |
| 56 | public void load(Matrix3f src) { |
| 57 | System.arraycopy(mMat, 0, src, 0, 9); |
| 58 | } |
| 59 | |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame^] | 60 | public void loadRotate(float rot, float x, float y, float z) { |
| 61 | float c, s; |
| 62 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 63 | c = (float)java.lang.Math.cos(rot); |
| 64 | s = (float)java.lang.Math.sin(rot); |
| 65 | |
| 66 | float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); |
| 67 | if (!(len != 1)) { |
| 68 | float recipLen = 1.f / len; |
| 69 | x *= recipLen; |
| 70 | y *= recipLen; |
| 71 | z *= recipLen; |
| 72 | } |
| 73 | float nc = 1.0f - c; |
| 74 | float xy = x * y; |
| 75 | float yz = y * z; |
| 76 | float zx = z * x; |
| 77 | float xs = x * s; |
| 78 | float ys = y * s; |
| 79 | float zs = z * s; |
| 80 | mMat[0] = x*x*nc + c; |
| 81 | mMat[3] = xy*nc - zs; |
| 82 | mMat[6] = zx*nc + ys; |
| 83 | mMat[1] = xy*nc + zs; |
| 84 | mMat[4] = y*y*nc + c; |
| 85 | mMat[9] = yz*nc - xs; |
| 86 | mMat[2] = zx*nc - ys; |
| 87 | mMat[6] = yz*nc + xs; |
| 88 | mMat[8] = z*z*nc + c; |
| 89 | } |
| 90 | |
| 91 | public void loadRotate(float rot) { |
| 92 | float c, s; |
| 93 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 94 | c = (float)java.lang.Math.cos(rot); |
| 95 | s = (float)java.lang.Math.sin(rot); |
| 96 | mMat[0] = c; |
| 97 | mMat[1] = -s; |
| 98 | mMat[3] = s; |
| 99 | mMat[4] = c; |
| 100 | } |
| 101 | |
| 102 | public void loadScale(float x, float y) { |
| 103 | loadIdentity(); |
| 104 | mMat[0] = x; |
| 105 | mMat[4] = y; |
| 106 | } |
| 107 | |
| 108 | public void loadScale(float x, float y, float z) { |
| 109 | loadIdentity(); |
| 110 | mMat[0] = x; |
| 111 | mMat[4] = y; |
| 112 | mMat[8] = z; |
| 113 | } |
| 114 | |
| 115 | public void loadTranslate(float x, float y) { |
| 116 | loadIdentity(); |
| 117 | mMat[6] = x; |
| 118 | mMat[7] = y; |
| 119 | } |
| 120 | |
| 121 | public void loadMultiply(Matrix3f lhs, Matrix3f rhs) { |
| 122 | for (int i=0 ; i<3 ; i++) { |
| 123 | float ri0 = 0; |
| 124 | float ri1 = 0; |
| 125 | float ri2 = 0; |
| 126 | for (int j=0 ; j<3 ; j++) { |
| 127 | float rhs_ij = rhs.get(i,j); |
| 128 | ri0 += lhs.get(j,0) * rhs_ij; |
| 129 | ri1 += lhs.get(j,1) * rhs_ij; |
| 130 | ri2 += lhs.get(j,2) * rhs_ij; |
| 131 | } |
| 132 | set(i,0, ri0); |
| 133 | set(i,1, ri1); |
| 134 | set(i,2, ri2); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public void multiply(Matrix3f rhs) { |
| 139 | Matrix3f tmp = new Matrix3f(); |
| 140 | tmp.loadMultiply(this, rhs); |
| 141 | load(tmp); |
| 142 | } |
| 143 | public void rotate(float rot, float x, float y, float z) { |
| 144 | Matrix3f tmp = new Matrix3f(); |
| 145 | tmp.loadRotate(rot, x, y, z); |
| 146 | multiply(tmp); |
| 147 | } |
| 148 | public void rotate(float rot) { |
| 149 | Matrix3f tmp = new Matrix3f(); |
| 150 | tmp.loadRotate(rot); |
| 151 | multiply(tmp); |
| 152 | } |
| 153 | public void scale(float x, float y) { |
| 154 | Matrix3f tmp = new Matrix3f(); |
| 155 | tmp.loadScale(x, y); |
| 156 | multiply(tmp); |
| 157 | } |
| 158 | public void scale(float x, float y, float z) { |
| 159 | Matrix3f tmp = new Matrix3f(); |
| 160 | tmp.loadScale(x, y, z); |
| 161 | multiply(tmp); |
| 162 | } |
| 163 | public void translate(float x, float y) { |
| 164 | Matrix3f tmp = new Matrix3f(); |
| 165 | tmp.loadTranslate(x, y); |
| 166 | multiply(tmp); |
| 167 | } |
| 168 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 169 | final float[] mMat; |
| 170 | } |
| 171 | |
| 172 | |