blob: 5f5e709d2c99f46822a487eafb55ca811fc473bc [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 *
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 Matrix2f {
29
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080031 * Creates a new identity 2x2 matrix
32 */
Jason Sams25430d02010-02-02 15:26:40 -080033 public Matrix2f() {
34 mMat = new float[4];
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 4
43 * floats long
44 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080045 public Matrix2f(float[] dataArray) {
Stephen Hinesef65bb32011-03-15 14:47:31 -070046 mMat = new float[4];
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080047 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*2 + 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*2 + 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
89 mMat[2] = 0;
90 mMat[3] = 1;
91 }
92
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070093 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080094 * Sets the values of the matrix to those of the parameter
95 *
96 * @param src matrix to load the values from
97 */
Jason Sams25430d02010-02-02 15:26:40 -080098 public void load(Matrix2f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -080099 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -0800100 }
101
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700102 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800103 * Sets current values to be a rotation matrix of given angle
104 *
105 * @param rot rotation angle
106 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700107 public void loadRotate(float rot) {
108 float c, s;
109 rot *= (float)(java.lang.Math.PI / 180.0f);
110 c = (float)java.lang.Math.cos(rot);
111 s = (float)java.lang.Math.sin(rot);
112 mMat[0] = c;
113 mMat[1] = -s;
114 mMat[2] = s;
115 mMat[3] = c;
116 }
117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800119 * Sets current values to be a scale matrix of given dimensions
120 *
121 * @param x scale component x
122 * @param y scale component y
123 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700124 public void loadScale(float x, float y) {
125 loadIdentity();
126 mMat[0] = x;
127 mMat[3] = y;
128 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800129
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700130 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800131 * Sets current values to be the result of multiplying two given
132 * matrices
133 *
134 * @param lhs left hand side matrix
135 * @param rhs right hand side matrix
136 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700137 public void loadMultiply(Matrix2f lhs, Matrix2f rhs) {
138 for (int i=0 ; i<2 ; i++) {
139 float ri0 = 0;
140 float ri1 = 0;
141 for (int j=0 ; j<2 ; j++) {
142 float rhs_ij = rhs.get(i,j);
143 ri0 += lhs.get(j,0) * rhs_ij;
144 ri1 += lhs.get(j,1) * rhs_ij;
145 }
146 set(i,0, ri0);
147 set(i,1, ri1);
148 }
149 }
150
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700151 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800152 * Post-multiplies the current matrix by a given parameter
153 *
154 * @param rhs right hand side to multiply by
155 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700156 public void multiply(Matrix2f rhs) {
157 Matrix2f tmp = new Matrix2f();
158 tmp.loadMultiply(this, rhs);
159 load(tmp);
160 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700161 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800162 * Modifies the current matrix by post-multiplying it with a
163 * rotation matrix of given angle
164 *
165 * @param rot angle of rotation
166 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700167 public void rotate(float rot) {
168 Matrix2f tmp = new Matrix2f();
169 tmp.loadRotate(rot);
170 multiply(tmp);
171 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800173 * Modifies the current matrix by post-multiplying it with a
174 * scale matrix of given dimensions
175 *
176 * @param x scale component x
177 * @param y scale component y
178 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700179 public void scale(float x, float y) {
180 Matrix2f tmp = new Matrix2f();
181 tmp.loadScale(x, y);
182 multiply(tmp);
183 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700184 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800185 * Sets the current matrix to its transpose
186 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700187 public void transpose() {
188 float temp = mMat[1];
189 mMat[1] = mMat[2];
190 mMat[2] = temp;
191 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700192
Jason Sams25430d02010-02-02 15:26:40 -0800193 final float[] mMat;
194}
195
196
197