• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009-2012 The Android Open Source Project
3  *
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 
17 package android.renderscript;
18 
19 
20 /**
21  * Class for exposing the native RenderScript rs_matrix3x3 type back to the Android system.
22  *
23  * @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.
26  **/
27 @Deprecated
28 public class Matrix3f {
29 
30     /**
31     * Creates a new identity 3x3 matrix
32     */
Matrix3f()33     public Matrix3f() {
34         mMat = new float[9];
35         loadIdentity();
36     }
37 
38     /**
39     * 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     */
Matrix3f(float[] dataArray)45     public Matrix3f(float[] dataArray) {
46         mMat = new float[9];
47         System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
48     }
49 
50     /**
51     * 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     */
getArray()56     public float[] getArray() {
57         return mMat;
58     }
59 
60     /**
61     * Returns the value for a given row and column
62     *
63     * @param x column of the value to return
64     * @param y row of the value to return
65     *
66     * @return value in the yth row and xth column
67     */
get(int x, int y)68     public float get(int x, int y) {
69         return mMat[x*3 + y];
70     }
71 
72     /**
73     * Sets the value for a given row and column
74     *
75     * @param x column of the value to set
76     * @param y row of the value to set
77     */
set(int x, int y, float v)78     public void set(int x, int y, float v) {
79         mMat[x*3 + y] = v;
80     }
81 
82     /**
83     * Sets the matrix values to identity
84     */
loadIdentity()85     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 
99     /**
100     * Sets the values of the matrix to those of the parameter
101     *
102     * @param src matrix to load the values from
103     */
load(Matrix3f src)104     public void load(Matrix3f src) {
105         System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
106     }
107 
108     /**
109     * 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     */
loadRotate(float rot, float x, float y, float z)117     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;
142         mMat[7] =  yz*nc - xs;
143         mMat[2] =  zx*nc - ys;
144         mMat[5] =  yz*nc + xs;
145         mMat[8] = z*z*nc +  c;
146     }
147 
148     /**
149     * Makes the upper 2x2 a rotation matrix of the given angle
150     *
151     * @param rot rotation angle
152     */
loadRotate(float rot)153     public void loadRotate(float rot) {
154         loadIdentity();
155         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 
165     /**
166     * Makes the upper 2x2 a scale matrix of given dimensions
167     *
168     * @param x scale component x
169     * @param y scale component y
170     */
loadScale(float x, float y)171     public void loadScale(float x, float y) {
172         loadIdentity();
173         mMat[0] = x;
174         mMat[4] = y;
175     }
176 
177     /**
178     * 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     */
loadScale(float x, float y, float z)184     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 
191     /**
192     * 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     */
loadTranslate(float x, float y)198     public void loadTranslate(float x, float y) {
199         loadIdentity();
200         mMat[6] = x;
201         mMat[7] = y;
202     }
203 
204     /**
205     * 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     */
loadMultiply(Matrix3f lhs, Matrix3f rhs)211     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 
228     /**
229     * Post-multiplies the current matrix by a given parameter
230     *
231     * @param rhs right hand side to multiply by
232     */
multiply(Matrix3f rhs)233     public void multiply(Matrix3f rhs) {
234         Matrix3f tmp = new Matrix3f();
235         tmp.loadMultiply(this, rhs);
236         load(tmp);
237     }
238 
239     /**
240     * 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     */
rotate(float rot, float x, float y, float z)248     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     }
253 
254     /**
255     * 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     */
rotate(float rot)260     public void rotate(float rot) {
261         Matrix3f tmp = new Matrix3f();
262         tmp.loadRotate(rot);
263         multiply(tmp);
264     }
265 
266     /**
267     * 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     */
scale(float x, float y)273     public void scale(float x, float y) {
274         Matrix3f tmp = new Matrix3f();
275         tmp.loadScale(x, y);
276         multiply(tmp);
277     }
278 
279     /**
280     * 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     */
scale(float x, float y, float z)287     public void scale(float x, float y, float z) {
288         Matrix3f tmp = new Matrix3f();
289         tmp.loadScale(x, y, z);
290         multiply(tmp);
291     }
292 
293     /**
294     * 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     */
translate(float x, float y)300     public void translate(float x, float y) {
301         Matrix3f tmp = new Matrix3f();
302         tmp.loadTranslate(x, y);
303         multiply(tmp);
304     }
305 
306     /**
307     * Sets the current matrix to its transpose
308     */
transpose()309     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     }
318 
319     final float[] mMat;
320 }
321 
322 
323