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 Matrix2f { |
| 28 | |
| 29 | public Matrix2f() { |
| 30 | mMat = new float[4]; |
| 31 | loadIdentity(); |
| 32 | } |
| 33 | |
| 34 | public float get(int i, int j) { |
| 35 | return mMat[i*2 + j]; |
| 36 | } |
| 37 | |
| 38 | public void set(int i, int j, float v) { |
| 39 | mMat[i*2 + j] = v; |
| 40 | } |
| 41 | |
| 42 | public void loadIdentity() { |
| 43 | mMat[0] = 1; |
| 44 | mMat[1] = 0; |
| 45 | |
| 46 | mMat[2] = 0; |
| 47 | mMat[3] = 1; |
| 48 | } |
| 49 | |
| 50 | public void load(Matrix2f src) { |
| 51 | System.arraycopy(mMat, 0, src, 0, 4); |
| 52 | } |
| 53 | |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame^] | 54 | public void loadRotate(float rot) { |
| 55 | float c, s; |
| 56 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 57 | c = (float)java.lang.Math.cos(rot); |
| 58 | s = (float)java.lang.Math.sin(rot); |
| 59 | mMat[0] = c; |
| 60 | mMat[1] = -s; |
| 61 | mMat[2] = s; |
| 62 | mMat[3] = c; |
| 63 | } |
| 64 | |
| 65 | public void loadScale(float x, float y) { |
| 66 | loadIdentity(); |
| 67 | mMat[0] = x; |
| 68 | mMat[3] = y; |
| 69 | } |
| 70 | public void loadMultiply(Matrix2f lhs, Matrix2f rhs) { |
| 71 | for (int i=0 ; i<2 ; i++) { |
| 72 | float ri0 = 0; |
| 73 | float ri1 = 0; |
| 74 | for (int j=0 ; j<2 ; j++) { |
| 75 | float rhs_ij = rhs.get(i,j); |
| 76 | ri0 += lhs.get(j,0) * rhs_ij; |
| 77 | ri1 += lhs.get(j,1) * rhs_ij; |
| 78 | } |
| 79 | set(i,0, ri0); |
| 80 | set(i,1, ri1); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public void multiply(Matrix2f rhs) { |
| 85 | Matrix2f tmp = new Matrix2f(); |
| 86 | tmp.loadMultiply(this, rhs); |
| 87 | load(tmp); |
| 88 | } |
| 89 | public void rotate(float rot) { |
| 90 | Matrix2f tmp = new Matrix2f(); |
| 91 | tmp.loadRotate(rot); |
| 92 | multiply(tmp); |
| 93 | } |
| 94 | public void scale(float x, float y) { |
| 95 | Matrix2f tmp = new Matrix2f(); |
| 96 | tmp.loadScale(x, y); |
| 97 | multiply(tmp); |
| 98 | } |
| 99 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 100 | final float[] mMat; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |