blob: 8c3c330d85ef98c05846769a0fa281595cb8d7d6 [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
19import java.lang.Math;
20import android.util.Log;
21
22
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070023/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070024 * Class for exposing the native RenderScript rs_matrix3x3 type back to the Android system.
Jason Sams25430d02010-02-02 15:26:40 -080025 *
26 **/
27public class Matrix3f {
28
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070029 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080030 * Creates a new identity 3x3 matrix
31 */
Jason Sams25430d02010-02-02 15:26:40 -080032 public Matrix3f() {
33 mMat = new float[9];
34 loadIdentity();
35 }
36
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070037 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080038 * 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 9
42 * floats long
43 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080044 public Matrix3f(float[] dataArray) {
45 mMat = new float[9];
46 System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
47 }
48
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070049 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080050 * 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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070059 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080060 * Returns the value for a given row and column
61 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070062 * @param x column of the value to return
63 * @param y row of the value to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080064 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070065 * @return value in the yth row and xth column
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080066 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070067 public float get(int x, int y) {
68 return mMat[x*3 + y];
Jason Sams25430d02010-02-02 15:26:40 -080069 }
70
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070071 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080072 * Sets the value for a given row and column
73 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070074 * @param x column of the value to set
75 * @param y row of the value to set
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080076 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070077 public void set(int x, int y, float v) {
78 mMat[x*3 + y] = v;
Jason Sams25430d02010-02-02 15:26:40 -080079 }
80
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070081 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080082 * 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 mMat[2] = 0;
88
89 mMat[3] = 0;
90 mMat[4] = 1;
91 mMat[5] = 0;
92
93 mMat[6] = 0;
94 mMat[7] = 0;
95 mMat[8] = 1;
96 }
97
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080099 * Sets the values of the matrix to those of the parameter
100 *
101 * @param src matrix to load the values from
102 */
Jason Sams25430d02010-02-02 15:26:40 -0800103 public void load(Matrix3f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -0800104 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -0800105 }
106
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700107 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800108 * Sets current values to be a rotation matrix of certain angle
109 * about a given axis
110 *
111 * @param rot angle of rotation
112 * @param x rotation axis x
113 * @param y rotation axis y
114 * @param z rotation axis z
115 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700116 public void loadRotate(float rot, float x, float y, float z) {
117 float c, s;
118 rot *= (float)(java.lang.Math.PI / 180.0f);
119 c = (float)java.lang.Math.cos(rot);
120 s = (float)java.lang.Math.sin(rot);
121
122 float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z);
123 if (!(len != 1)) {
124 float recipLen = 1.f / len;
125 x *= recipLen;
126 y *= recipLen;
127 z *= recipLen;
128 }
129 float nc = 1.0f - c;
130 float xy = x * y;
131 float yz = y * z;
132 float zx = z * x;
133 float xs = x * s;
134 float ys = y * s;
135 float zs = z * s;
136 mMat[0] = x*x*nc + c;
137 mMat[3] = xy*nc - zs;
138 mMat[6] = zx*nc + ys;
139 mMat[1] = xy*nc + zs;
140 mMat[4] = y*y*nc + c;
Stephen Hines0ce7cda2013-01-22 16:01:44 -0800141 mMat[7] = yz*nc - xs;
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700142 mMat[2] = zx*nc - ys;
Stephen Hines39837542013-01-22 18:29:41 -0800143 mMat[5] = yz*nc + xs;
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700144 mMat[8] = z*z*nc + c;
145 }
146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700147 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800148 * Makes the upper 2x2 a rotation matrix of the given angle
149 *
150 * @param rot rotation angle
151 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700152 public void loadRotate(float rot) {
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800153 loadIdentity();
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700154 float c, s;
155 rot *= (float)(java.lang.Math.PI / 180.0f);
156 c = (float)java.lang.Math.cos(rot);
157 s = (float)java.lang.Math.sin(rot);
158 mMat[0] = c;
159 mMat[1] = -s;
160 mMat[3] = s;
161 mMat[4] = c;
162 }
163
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700164 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800165 * Makes the upper 2x2 a scale matrix of given dimensions
166 *
167 * @param x scale component x
168 * @param y scale component y
169 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700170 public void loadScale(float x, float y) {
171 loadIdentity();
172 mMat[0] = x;
173 mMat[4] = y;
174 }
175
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700176 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800177 * Sets current values to be a scale matrix of given dimensions
178 *
179 * @param x scale component x
180 * @param y scale component y
181 * @param z scale component z
182 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700183 public void loadScale(float x, float y, float z) {
184 loadIdentity();
185 mMat[0] = x;
186 mMat[4] = y;
187 mMat[8] = z;
188 }
189
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700190 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800191 * Sets current values to be a translation matrix of given
192 * dimensions
193 *
194 * @param x translation component x
195 * @param y translation component y
196 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700197 public void loadTranslate(float x, float y) {
198 loadIdentity();
199 mMat[6] = x;
200 mMat[7] = y;
201 }
202
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700203 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800204 * Sets current values to be the result of multiplying two given
205 * matrices
206 *
207 * @param lhs left hand side matrix
208 * @param rhs right hand side matrix
209 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700210 public void loadMultiply(Matrix3f lhs, Matrix3f rhs) {
211 for (int i=0 ; i<3 ; i++) {
212 float ri0 = 0;
213 float ri1 = 0;
214 float ri2 = 0;
215 for (int j=0 ; j<3 ; j++) {
216 float rhs_ij = rhs.get(i,j);
217 ri0 += lhs.get(j,0) * rhs_ij;
218 ri1 += lhs.get(j,1) * rhs_ij;
219 ri2 += lhs.get(j,2) * rhs_ij;
220 }
221 set(i,0, ri0);
222 set(i,1, ri1);
223 set(i,2, ri2);
224 }
225 }
226
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700227 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800228 * Post-multiplies the current matrix by a given parameter
229 *
230 * @param rhs right hand side to multiply by
231 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700232 public void multiply(Matrix3f rhs) {
233 Matrix3f tmp = new Matrix3f();
234 tmp.loadMultiply(this, rhs);
235 load(tmp);
236 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800237
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700238 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800239 * Modifies the current matrix by post-multiplying it with a
240 * rotation matrix of certain angle about a given axis
241 *
242 * @param rot angle of rotation
243 * @param x rotation axis x
244 * @param y rotation axis y
245 * @param z rotation axis z
246 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700247 public void rotate(float rot, float x, float y, float z) {
248 Matrix3f tmp = new Matrix3f();
249 tmp.loadRotate(rot, x, y, z);
250 multiply(tmp);
251 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800252
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700253 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800254 * Modifies the upper 2x2 of the current matrix by
255 * post-multiplying it with a rotation matrix of given angle
256 *
257 * @param rot angle of rotation
258 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700259 public void rotate(float rot) {
260 Matrix3f tmp = new Matrix3f();
261 tmp.loadRotate(rot);
262 multiply(tmp);
263 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800264
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700265 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800266 * Modifies the upper 2x2 of the current matrix by
267 * post-multiplying it with a scale matrix of given dimensions
268 *
269 * @param x scale component x
270 * @param y scale component y
271 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700272 public void scale(float x, float y) {
273 Matrix3f tmp = new Matrix3f();
274 tmp.loadScale(x, y);
275 multiply(tmp);
276 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700278 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800279 * Modifies the current matrix by post-multiplying it with a
280 * scale matrix of given dimensions
281 *
282 * @param x scale component x
283 * @param y scale component y
284 * @param z scale component z
285 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700286 public void scale(float x, float y, float z) {
287 Matrix3f tmp = new Matrix3f();
288 tmp.loadScale(x, y, z);
289 multiply(tmp);
290 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800291
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700292 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800293 * Modifies the current matrix by post-multiplying it with a
294 * translation matrix of given dimensions
295 *
296 * @param x translation component x
297 * @param y translation component y
298 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700299 public void translate(float x, float y) {
300 Matrix3f tmp = new Matrix3f();
301 tmp.loadTranslate(x, y);
302 multiply(tmp);
303 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800304
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700305 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800306 * Sets the current matrix to its transpose
307 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700308 public void transpose() {
309 for(int i = 0; i < 2; ++i) {
310 for(int j = i + 1; j < 3; ++j) {
311 float temp = mMat[i*3 + j];
312 mMat[i*3 + j] = mMat[j*3 + i];
313 mMat[j*3 + i] = temp;
314 }
315 }
316 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700317
Jason Sams25430d02010-02-02 15:26:40 -0800318 final float[] mMat;
319}
320
321