blob: 99d23db860b6ade83166048a129753559adb9c9f [file] [log] [blame]
Jason Sams25430d02010-02-02 15:26:40 -08001/*
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
17package android.renderscript;
18
19import java.lang.Math;
20import android.util.Log;
21
22
23/**
24 * @hide
25 *
26 **/
27public 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 Sakhartchoukcf9a44c2010-08-04 10:48:30 -070054 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 }
Alex Sakhartchouk518f0332010-08-05 10:28:43 -070099 public void transpose() {
100 float temp = mMat[1];
101 mMat[1] = mMat[2];
102 mMat[2] = temp;
103 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700104
Jason Sams25430d02010-02-02 15:26:40 -0800105 final float[] mMat;
106}
107
108
109