blob: c9a0ea8ab04bcf723e21ba4da8b564c3c42c5d05 [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/**
Jason Samsa23d4e72011-01-04 18:59:12 -080024 * Class for exposing the rs_matrix2x2 type back to java applications.
Jason Sams25430d02010-02-02 15:26:40 -080025 *
26 **/
27public class Matrix2f {
28
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080029 /**
30 * Creates a new identity 2x2 matrix
31 */
Jason Sams25430d02010-02-02 15:26:40 -080032 public Matrix2f() {
33 mMat = new float[4];
34 loadIdentity();
35 }
36
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080037 /**
38 * Creates a new matrix and sets its values from the given
39 * parameter
40 *
41 * @param dataArray values to set the matrix to, must be 4
42 * floats long
43 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080044 public Matrix2f(float[] dataArray) {
45 mMat = new float[2];
46 System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
47 }
48
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080049 /**
50 * Return a reference to the internal array representing matrix
51 * values. Modifying this array will also change the matrix
52 *
53 * @return internal array representing the matrix
54 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080055 public float[] getArray() {
56 return mMat;
57 }
58
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080059 /**
60 * Returns the value for a given row and column
61 *
62 * @param i row of the value to return
63 * @param j column of the value to return
64 *
65 * @return value in the ith row and jth column
66 */
Jason Sams25430d02010-02-02 15:26:40 -080067 public float get(int i, int j) {
68 return mMat[i*2 + j];
69 }
70
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080071 /**
72 * Sets the value for a given row and column
73 *
74 * @param i row of the value to set
75 * @param j column of the value to set
76 */
Jason Sams25430d02010-02-02 15:26:40 -080077 public void set(int i, int j, float v) {
78 mMat[i*2 + j] = v;
79 }
80
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080081 /**
82 * Sets the matrix values to identity
83 */
Jason Sams25430d02010-02-02 15:26:40 -080084 public void loadIdentity() {
85 mMat[0] = 1;
86 mMat[1] = 0;
87
88 mMat[2] = 0;
89 mMat[3] = 1;
90 }
91
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080092 /**
93 * Sets the values of the matrix to those of the parameter
94 *
95 * @param src matrix to load the values from
96 */
Jason Sams25430d02010-02-02 15:26:40 -080097 public void load(Matrix2f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -080098 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -080099 }
100
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800101 /**
102 * Sets current values to be a rotation matrix of given angle
103 *
104 * @param rot rotation angle
105 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700106 public void loadRotate(float rot) {
107 float c, s;
108 rot *= (float)(java.lang.Math.PI / 180.0f);
109 c = (float)java.lang.Math.cos(rot);
110 s = (float)java.lang.Math.sin(rot);
111 mMat[0] = c;
112 mMat[1] = -s;
113 mMat[2] = s;
114 mMat[3] = c;
115 }
116
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800117 /**
118 * Sets current values to be a scale matrix of given dimensions
119 *
120 * @param x scale component x
121 * @param y scale component y
122 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700123 public void loadScale(float x, float y) {
124 loadIdentity();
125 mMat[0] = x;
126 mMat[3] = y;
127 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800128
129 /**
130 * Sets current values to be the result of multiplying two given
131 * matrices
132 *
133 * @param lhs left hand side matrix
134 * @param rhs right hand side matrix
135 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700136 public void loadMultiply(Matrix2f lhs, Matrix2f rhs) {
137 for (int i=0 ; i<2 ; i++) {
138 float ri0 = 0;
139 float ri1 = 0;
140 for (int j=0 ; j<2 ; j++) {
141 float rhs_ij = rhs.get(i,j);
142 ri0 += lhs.get(j,0) * rhs_ij;
143 ri1 += lhs.get(j,1) * rhs_ij;
144 }
145 set(i,0, ri0);
146 set(i,1, ri1);
147 }
148 }
149
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800150 /**
151 * Post-multiplies the current matrix by a given parameter
152 *
153 * @param rhs right hand side to multiply by
154 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700155 public void multiply(Matrix2f rhs) {
156 Matrix2f tmp = new Matrix2f();
157 tmp.loadMultiply(this, rhs);
158 load(tmp);
159 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800160 /**
161 * Modifies the current matrix by post-multiplying it with a
162 * rotation matrix of given angle
163 *
164 * @param rot angle of rotation
165 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700166 public void rotate(float rot) {
167 Matrix2f tmp = new Matrix2f();
168 tmp.loadRotate(rot);
169 multiply(tmp);
170 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800171 /**
172 * Modifies the current matrix by post-multiplying it with a
173 * scale matrix of given dimensions
174 *
175 * @param x scale component x
176 * @param y scale component y
177 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700178 public void scale(float x, float y) {
179 Matrix2f tmp = new Matrix2f();
180 tmp.loadScale(x, y);
181 multiply(tmp);
182 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800183 /**
184 * Sets the current matrix to its transpose
185 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700186 public void transpose() {
187 float temp = mMat[1];
188 mMat[1] = mMat[2];
189 mMat[2] = temp;
190 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700191
Jason Sams25430d02010-02-02 15:26:40 -0800192 final float[] mMat;
193}
194
195
196