blob: 048262dc7ebaf9b15f7b4f71dad558e37ee62382 [file] [log] [blame]
Jason Sams25430d02010-02-02 15:26:40 -08001/*
Stephen Hinesec6f2002012-07-10 16:16:22 -07002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Sams25430d02010-02-02 15:26:40 -08003 *
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
Jason Sams25430d02010-02-02 15:26:40 -080019
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070020/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070021 * Class for exposing the native RenderScript rs_matrix2x2 type back to the Android system.
Jason Sams25430d02010-02-02 15:26:40 -080022 *
23 **/
24public class Matrix2f {
25
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070026 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080027 * Creates a new identity 2x2 matrix
28 */
Jason Sams25430d02010-02-02 15:26:40 -080029 public Matrix2f() {
30 mMat = new float[4];
31 loadIdentity();
32 }
33
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070034 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080035 * Creates a new matrix and sets its values from the given
36 * parameter
37 *
38 * @param dataArray values to set the matrix to, must be 4
39 * floats long
40 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080041 public Matrix2f(float[] dataArray) {
Stephen Hinesef65bb32011-03-15 14:47:31 -070042 mMat = new float[4];
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080043 System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
44 }
45
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070046 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080047 * Return a reference to the internal array representing matrix
48 * values. Modifying this array will also change the matrix
49 *
50 * @return internal array representing the matrix
51 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080052 public float[] getArray() {
53 return mMat;
54 }
55
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070056 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080057 * Returns the value for a given row and column
58 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070059 * @param x column of the value to return
60 * @param y row of the value to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080061 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070062 * @return value in the yth row and xth column
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080063 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070064 public float get(int x, int y) {
65 return mMat[x*2 + y];
Jason Sams25430d02010-02-02 15:26:40 -080066 }
67
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080069 * Sets the value for a given row and column
70 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070071 * @param x column of the value to set
72 * @param y row of the value to set
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080073 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070074 public void set(int x, int y, float v) {
75 mMat[x*2 + y] = v;
Jason Sams25430d02010-02-02 15:26:40 -080076 }
77
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070078 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080079 * Sets the matrix values to identity
80 */
Jason Sams25430d02010-02-02 15:26:40 -080081 public void loadIdentity() {
82 mMat[0] = 1;
83 mMat[1] = 0;
84
85 mMat[2] = 0;
86 mMat[3] = 1;
87 }
88
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070089 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080090 * Sets the values of the matrix to those of the parameter
91 *
92 * @param src matrix to load the values from
93 */
Jason Sams25430d02010-02-02 15:26:40 -080094 public void load(Matrix2f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -080095 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -080096 }
97
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080099 * Sets current values to be a rotation matrix of given angle
100 *
101 * @param rot rotation angle
102 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700103 public void loadRotate(float rot) {
104 float c, s;
105 rot *= (float)(java.lang.Math.PI / 180.0f);
106 c = (float)java.lang.Math.cos(rot);
107 s = (float)java.lang.Math.sin(rot);
108 mMat[0] = c;
109 mMat[1] = -s;
110 mMat[2] = s;
111 mMat[3] = c;
112 }
113
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700114 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800115 * Sets current values to be a scale matrix of given dimensions
116 *
117 * @param x scale component x
118 * @param y scale component y
119 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700120 public void loadScale(float x, float y) {
121 loadIdentity();
122 mMat[0] = x;
123 mMat[3] = y;
124 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800125
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700126 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800127 * Sets current values to be the result of multiplying two given
128 * matrices
129 *
130 * @param lhs left hand side matrix
131 * @param rhs right hand side matrix
132 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700133 public void loadMultiply(Matrix2f lhs, Matrix2f rhs) {
134 for (int i=0 ; i<2 ; i++) {
135 float ri0 = 0;
136 float ri1 = 0;
137 for (int j=0 ; j<2 ; j++) {
138 float rhs_ij = rhs.get(i,j);
139 ri0 += lhs.get(j,0) * rhs_ij;
140 ri1 += lhs.get(j,1) * rhs_ij;
141 }
142 set(i,0, ri0);
143 set(i,1, ri1);
144 }
145 }
146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700147 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800148 * Post-multiplies the current matrix by a given parameter
149 *
150 * @param rhs right hand side to multiply by
151 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700152 public void multiply(Matrix2f rhs) {
153 Matrix2f tmp = new Matrix2f();
154 tmp.loadMultiply(this, rhs);
155 load(tmp);
156 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800158 * Modifies the current matrix by post-multiplying it with a
159 * rotation matrix of given angle
160 *
161 * @param rot angle of rotation
162 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700163 public void rotate(float rot) {
164 Matrix2f tmp = new Matrix2f();
165 tmp.loadRotate(rot);
166 multiply(tmp);
167 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700168 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800169 * Modifies the current matrix by post-multiplying it with a
170 * scale matrix of given dimensions
171 *
172 * @param x scale component x
173 * @param y scale component y
174 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700175 public void scale(float x, float y) {
176 Matrix2f tmp = new Matrix2f();
177 tmp.loadScale(x, y);
178 multiply(tmp);
179 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700180 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800181 * Sets the current matrix to its transpose
182 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700183 public void transpose() {
184 float temp = mMat[1];
185 mMat[1] = mMat[2];
186 mMat[2] = temp;
187 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700188
Jason Sams25430d02010-02-02 15:26:40 -0800189 final float[] mMat;
190}
191
192
193