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