blob: b620eaf1415d9990c7d3fbc76efd310791874948 [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 *
Xusong Wang8b4548c2021-01-05 10:09:52 -080023 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
24 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
25 * guide</a> for the proposed alternatives.
Jason Sams25430d02010-02-02 15:26:40 -080026 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080027@Deprecated
Jason Sams25430d02010-02-02 15:26:40 -080028public class Matrix3f {
29
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080031 * Creates a new identity 3x3 matrix
32 */
Jason Sams25430d02010-02-02 15:26:40 -080033 public Matrix3f() {
34 mMat = new float[9];
35 loadIdentity();
36 }
37
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070038 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080039 * Creates a new matrix and sets its values from the given
40 * parameter
41 *
42 * @param dataArray values to set the matrix to, must be 9
43 * floats long
44 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080045 public Matrix3f(float[] dataArray) {
46 mMat = new float[9];
47 System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
48 }
49
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070050 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080051 * Return a reference to the internal array representing matrix
52 * values. Modifying this array will also change the matrix
53 *
54 * @return internal array representing the matrix
55 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080056 public float[] getArray() {
57 return mMat;
58 }
59
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070060 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080061 * Returns the value for a given row and column
62 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070063 * @param x column of the value to return
64 * @param y row of the value to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080065 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070066 * @return value in the yth row and xth column
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080067 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070068 public float get(int x, int y) {
69 return mMat[x*3 + y];
Jason Sams25430d02010-02-02 15:26:40 -080070 }
71
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070072 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080073 * Sets the value for a given row and column
74 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070075 * @param x column of the value to set
76 * @param y row of the value to set
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080077 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070078 public void set(int x, int y, float v) {
79 mMat[x*3 + y] = v;
Jason Sams25430d02010-02-02 15:26:40 -080080 }
81
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070082 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080083 * Sets the matrix values to identity
84 */
Jason Sams25430d02010-02-02 15:26:40 -080085 public void loadIdentity() {
86 mMat[0] = 1;
87 mMat[1] = 0;
88 mMat[2] = 0;
89
90 mMat[3] = 0;
91 mMat[4] = 1;
92 mMat[5] = 0;
93
94 mMat[6] = 0;
95 mMat[7] = 0;
96 mMat[8] = 1;
97 }
98
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070099 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800100 * Sets the values of the matrix to those of the parameter
101 *
102 * @param src matrix to load the values from
103 */
Jason Sams25430d02010-02-02 15:26:40 -0800104 public void load(Matrix3f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -0800105 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -0800106 }
107
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700108 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800109 * Sets current values to be a rotation matrix of certain angle
110 * about a given axis
111 *
112 * @param rot angle of rotation
113 * @param x rotation axis x
114 * @param y rotation axis y
115 * @param z rotation axis z
116 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700117 public void loadRotate(float rot, float x, float y, float z) {
118 float c, s;
119 rot *= (float)(java.lang.Math.PI / 180.0f);
120 c = (float)java.lang.Math.cos(rot);
121 s = (float)java.lang.Math.sin(rot);
122
123 float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z);
124 if (!(len != 1)) {
125 float recipLen = 1.f / len;
126 x *= recipLen;
127 y *= recipLen;
128 z *= recipLen;
129 }
130 float nc = 1.0f - c;
131 float xy = x * y;
132 float yz = y * z;
133 float zx = z * x;
134 float xs = x * s;
135 float ys = y * s;
136 float zs = z * s;
137 mMat[0] = x*x*nc + c;
138 mMat[3] = xy*nc - zs;
139 mMat[6] = zx*nc + ys;
140 mMat[1] = xy*nc + zs;
141 mMat[4] = y*y*nc + c;
Stephen Hines0ce7cda2013-01-22 16:01:44 -0800142 mMat[7] = yz*nc - xs;
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700143 mMat[2] = zx*nc - ys;
Stephen Hines39837542013-01-22 18:29:41 -0800144 mMat[5] = yz*nc + xs;
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700145 mMat[8] = z*z*nc + c;
146 }
147
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700148 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800149 * Makes the upper 2x2 a rotation matrix of the given angle
150 *
151 * @param rot rotation angle
152 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700153 public void loadRotate(float rot) {
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800154 loadIdentity();
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700155 float c, s;
156 rot *= (float)(java.lang.Math.PI / 180.0f);
157 c = (float)java.lang.Math.cos(rot);
158 s = (float)java.lang.Math.sin(rot);
159 mMat[0] = c;
160 mMat[1] = -s;
161 mMat[3] = s;
162 mMat[4] = c;
163 }
164
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700165 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800166 * Makes the upper 2x2 a scale matrix of given dimensions
167 *
168 * @param x scale component x
169 * @param y scale component y
170 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700171 public void loadScale(float x, float y) {
172 loadIdentity();
173 mMat[0] = x;
174 mMat[4] = y;
175 }
176
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700177 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800178 * Sets current values to be a scale matrix of given dimensions
179 *
180 * @param x scale component x
181 * @param y scale component y
182 * @param z scale component z
183 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700184 public void loadScale(float x, float y, float z) {
185 loadIdentity();
186 mMat[0] = x;
187 mMat[4] = y;
188 mMat[8] = z;
189 }
190
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700191 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800192 * Sets current values to be a translation matrix of given
193 * dimensions
194 *
195 * @param x translation component x
196 * @param y translation component y
197 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700198 public void loadTranslate(float x, float y) {
199 loadIdentity();
200 mMat[6] = x;
201 mMat[7] = y;
202 }
203
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700204 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800205 * Sets current values to be the result of multiplying two given
206 * matrices
207 *
208 * @param lhs left hand side matrix
209 * @param rhs right hand side matrix
210 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700211 public void loadMultiply(Matrix3f lhs, Matrix3f rhs) {
212 for (int i=0 ; i<3 ; i++) {
213 float ri0 = 0;
214 float ri1 = 0;
215 float ri2 = 0;
216 for (int j=0 ; j<3 ; j++) {
217 float rhs_ij = rhs.get(i,j);
218 ri0 += lhs.get(j,0) * rhs_ij;
219 ri1 += lhs.get(j,1) * rhs_ij;
220 ri2 += lhs.get(j,2) * rhs_ij;
221 }
222 set(i,0, ri0);
223 set(i,1, ri1);
224 set(i,2, ri2);
225 }
226 }
227
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800229 * Post-multiplies the current matrix by a given parameter
230 *
231 * @param rhs right hand side to multiply by
232 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700233 public void multiply(Matrix3f rhs) {
234 Matrix3f tmp = new Matrix3f();
235 tmp.loadMultiply(this, rhs);
236 load(tmp);
237 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800240 * Modifies the current matrix by post-multiplying it with a
241 * rotation matrix of certain angle about a given axis
242 *
243 * @param rot angle of rotation
244 * @param x rotation axis x
245 * @param y rotation axis y
246 * @param z rotation axis z
247 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700248 public void rotate(float rot, float x, float y, float z) {
249 Matrix3f tmp = new Matrix3f();
250 tmp.loadRotate(rot, x, y, z);
251 multiply(tmp);
252 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800253
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700254 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800255 * Modifies the upper 2x2 of the current matrix by
256 * post-multiplying it with a rotation matrix of given angle
257 *
258 * @param rot angle of rotation
259 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700260 public void rotate(float rot) {
261 Matrix3f tmp = new Matrix3f();
262 tmp.loadRotate(rot);
263 multiply(tmp);
264 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800265
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700266 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800267 * Modifies the upper 2x2 of the current matrix by
268 * post-multiplying it with a scale matrix of given dimensions
269 *
270 * @param x scale component x
271 * @param y scale component y
272 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700273 public void scale(float x, float y) {
274 Matrix3f tmp = new Matrix3f();
275 tmp.loadScale(x, y);
276 multiply(tmp);
277 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800278
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700279 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800280 * Modifies the current matrix by post-multiplying it with a
281 * scale matrix of given dimensions
282 *
283 * @param x scale component x
284 * @param y scale component y
285 * @param z scale component z
286 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700287 public void scale(float x, float y, float z) {
288 Matrix3f tmp = new Matrix3f();
289 tmp.loadScale(x, y, z);
290 multiply(tmp);
291 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800292
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700293 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800294 * Modifies the current matrix by post-multiplying it with a
295 * translation matrix of given dimensions
296 *
297 * @param x translation component x
298 * @param y translation component y
299 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700300 public void translate(float x, float y) {
301 Matrix3f tmp = new Matrix3f();
302 tmp.loadTranslate(x, y);
303 multiply(tmp);
304 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800305
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700306 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800307 * Sets the current matrix to its transpose
308 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700309 public void transpose() {
310 for(int i = 0; i < 2; ++i) {
311 for(int j = i + 1; j < 3; ++j) {
312 float temp = mMat[i*3 + j];
313 mMat[i*3 + j] = mMat[j*3 + i];
314 mMat[j*3 + i] = temp;
315 }
316 }
317 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700318
Jason Sams25430d02010-02-02 15:26:40 -0800319 final float[] mMat;
320}
321
322