blob: 9a4af777583c4d621149ab409d3246ac50a4ca30 [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_matrix3x3 type back to the Android system.
Jason Sams25430d02010-02-02 15:26:40 -080022 *
23 **/
24public class Matrix3f {
25
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070026 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080027 * Creates a new identity 3x3 matrix
28 */
Jason Sams25430d02010-02-02 15:26:40 -080029 public Matrix3f() {
30 mMat = new float[9];
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 9
39 * floats long
40 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080041 public Matrix3f(float[] dataArray) {
42 mMat = new float[9];
43 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*3 + 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*3 + 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 mMat[2] = 0;
85
86 mMat[3] = 0;
87 mMat[4] = 1;
88 mMat[5] = 0;
89
90 mMat[6] = 0;
91 mMat[7] = 0;
92 mMat[8] = 1;
93 }
94
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080096 * Sets the values of the matrix to those of the parameter
97 *
98 * @param src matrix to load the values from
99 */
Jason Sams25430d02010-02-02 15:26:40 -0800100 public void load(Matrix3f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -0800101 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -0800102 }
103
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700104 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800105 * Sets current values to be a rotation matrix of certain angle
106 * about a given axis
107 *
108 * @param rot angle of rotation
109 * @param x rotation axis x
110 * @param y rotation axis y
111 * @param z rotation axis z
112 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700113 public void loadRotate(float rot, float x, float y, float z) {
114 float c, s;
115 rot *= (float)(java.lang.Math.PI / 180.0f);
116 c = (float)java.lang.Math.cos(rot);
117 s = (float)java.lang.Math.sin(rot);
118
119 float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z);
120 if (!(len != 1)) {
121 float recipLen = 1.f / len;
122 x *= recipLen;
123 y *= recipLen;
124 z *= recipLen;
125 }
126 float nc = 1.0f - c;
127 float xy = x * y;
128 float yz = y * z;
129 float zx = z * x;
130 float xs = x * s;
131 float ys = y * s;
132 float zs = z * s;
133 mMat[0] = x*x*nc + c;
134 mMat[3] = xy*nc - zs;
135 mMat[6] = zx*nc + ys;
136 mMat[1] = xy*nc + zs;
137 mMat[4] = y*y*nc + c;
Stephen Hines0ce7cda2013-01-22 16:01:44 -0800138 mMat[7] = yz*nc - xs;
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700139 mMat[2] = zx*nc - ys;
Stephen Hines39837542013-01-22 18:29:41 -0800140 mMat[5] = yz*nc + xs;
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700141 mMat[8] = z*z*nc + c;
142 }
143
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700144 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800145 * Makes the upper 2x2 a rotation matrix of the given angle
146 *
147 * @param rot rotation angle
148 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700149 public void loadRotate(float rot) {
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800150 loadIdentity();
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700151 float c, s;
152 rot *= (float)(java.lang.Math.PI / 180.0f);
153 c = (float)java.lang.Math.cos(rot);
154 s = (float)java.lang.Math.sin(rot);
155 mMat[0] = c;
156 mMat[1] = -s;
157 mMat[3] = s;
158 mMat[4] = c;
159 }
160
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700161 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800162 * Makes the upper 2x2 a scale matrix of given dimensions
163 *
164 * @param x scale component x
165 * @param y scale component y
166 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700167 public void loadScale(float x, float y) {
168 loadIdentity();
169 mMat[0] = x;
170 mMat[4] = y;
171 }
172
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700173 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800174 * Sets current values to be a scale matrix of given dimensions
175 *
176 * @param x scale component x
177 * @param y scale component y
178 * @param z scale component z
179 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700180 public void loadScale(float x, float y, float z) {
181 loadIdentity();
182 mMat[0] = x;
183 mMat[4] = y;
184 mMat[8] = z;
185 }
186
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700187 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800188 * Sets current values to be a translation matrix of given
189 * dimensions
190 *
191 * @param x translation component x
192 * @param y translation component y
193 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700194 public void loadTranslate(float x, float y) {
195 loadIdentity();
196 mMat[6] = x;
197 mMat[7] = y;
198 }
199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700200 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800201 * Sets current values to be the result of multiplying two given
202 * matrices
203 *
204 * @param lhs left hand side matrix
205 * @param rhs right hand side matrix
206 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700207 public void loadMultiply(Matrix3f lhs, Matrix3f rhs) {
208 for (int i=0 ; i<3 ; i++) {
209 float ri0 = 0;
210 float ri1 = 0;
211 float ri2 = 0;
212 for (int j=0 ; j<3 ; j++) {
213 float rhs_ij = rhs.get(i,j);
214 ri0 += lhs.get(j,0) * rhs_ij;
215 ri1 += lhs.get(j,1) * rhs_ij;
216 ri2 += lhs.get(j,2) * rhs_ij;
217 }
218 set(i,0, ri0);
219 set(i,1, ri1);
220 set(i,2, ri2);
221 }
222 }
223
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700224 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800225 * Post-multiplies the current matrix by a given parameter
226 *
227 * @param rhs right hand side to multiply by
228 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700229 public void multiply(Matrix3f rhs) {
230 Matrix3f tmp = new Matrix3f();
231 tmp.loadMultiply(this, rhs);
232 load(tmp);
233 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800234
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700235 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800236 * Modifies the current matrix by post-multiplying it with a
237 * rotation matrix of certain angle about a given axis
238 *
239 * @param rot angle of rotation
240 * @param x rotation axis x
241 * @param y rotation axis y
242 * @param z rotation axis z
243 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700244 public void rotate(float rot, float x, float y, float z) {
245 Matrix3f tmp = new Matrix3f();
246 tmp.loadRotate(rot, x, y, z);
247 multiply(tmp);
248 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700250 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800251 * Modifies the upper 2x2 of the current matrix by
252 * post-multiplying it with a rotation matrix of given angle
253 *
254 * @param rot angle of rotation
255 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700256 public void rotate(float rot) {
257 Matrix3f tmp = new Matrix3f();
258 tmp.loadRotate(rot);
259 multiply(tmp);
260 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800261
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700262 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800263 * Modifies the upper 2x2 of the current matrix by
264 * post-multiplying it with a scale matrix of given dimensions
265 *
266 * @param x scale component x
267 * @param y scale component y
268 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700269 public void scale(float x, float y) {
270 Matrix3f tmp = new Matrix3f();
271 tmp.loadScale(x, y);
272 multiply(tmp);
273 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800274
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700275 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800276 * Modifies the current matrix by post-multiplying it with a
277 * scale matrix of given dimensions
278 *
279 * @param x scale component x
280 * @param y scale component y
281 * @param z scale component z
282 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700283 public void scale(float x, float y, float z) {
284 Matrix3f tmp = new Matrix3f();
285 tmp.loadScale(x, y, z);
286 multiply(tmp);
287 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800288
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700289 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800290 * Modifies the current matrix by post-multiplying it with a
291 * translation matrix of given dimensions
292 *
293 * @param x translation component x
294 * @param y translation component y
295 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700296 public void translate(float x, float y) {
297 Matrix3f tmp = new Matrix3f();
298 tmp.loadTranslate(x, y);
299 multiply(tmp);
300 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800301
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700302 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800303 * Sets the current matrix to its transpose
304 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700305 public void transpose() {
306 for(int i = 0; i < 2; ++i) {
307 for(int j = i + 1; j < 3; ++j) {
308 float temp = mMat[i*3 + j];
309 mMat[i*3 + j] = mMat[j*3 + i];
310 mMat[j*3 + i] = temp;
311 }
312 }
313 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700314
Jason Sams25430d02010-02-02 15:26:40 -0800315 final float[] mMat;
316}
317
318