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 androidx.renderscript;
18 
19 import java.lang.Math;
20 import android.util.Log;
21 
22 
23 /**
24  * Class for exposing the native RenderScript rs_matrix2x2 type back to the Android system.
25  *
26  **/
27 public class Matrix2f {
28 
29     /**
30     * Creates a new identity 2x2 matrix
31     */
Matrix2f()32     public Matrix2f() {
33         mMat = new float[4];
34         loadIdentity();
35     }
36 
37     /**
38     * 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 4
42     *                  floats long
43     */
Matrix2f(float[] dataArray)44     public Matrix2f(float[] dataArray) {
45         mMat = new float[4];
46         System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
47     }
48 
49     /**
50     * 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     */
getArray()55     public float[] getArray() {
56         return mMat;
57     }
58 
59     /**
60     * Returns the value for a given row and column
61     *
62     * @param x column of the value to return
63     * @param y row of the value to return
64     *
65     * @return value in the yth row and xth column
66     */
get(int x, int y)67     public float get(int x, int y) {
68         return mMat[x*2 + y];
69     }
70 
71     /**
72     * Sets the value for a given row and column
73     *
74     * @param x column of the value to set
75     * @param y row of the value to set
76     */
set(int x, int y, float v)77     public void set(int x, int y, float v) {
78         mMat[x*2 + y] = v;
79     }
80 
81     /**
82     * Sets the matrix values to identity
83     */
loadIdentity()84     public void loadIdentity() {
85         mMat[0] = 1;
86         mMat[1] = 0;
87 
88         mMat[2] = 0;
89         mMat[3] = 1;
90     }
91 
92     /**
93     * Sets the values of the matrix to those of the parameter
94     *
95     * @param src matrix to load the values from
96     */
load(Matrix2f src)97     public void load(Matrix2f src) {
98         System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
99     }
100 
101     /**
102     * Sets current values to be a rotation matrix of given angle
103     *
104     * @param rot rotation angle
105     */
loadRotate(float rot)106     public void loadRotate(float rot) {
107         float c, s;
108         rot *= (float)(java.lang.Math.PI / 180.0f);
109         c = (float)java.lang.Math.cos(rot);
110         s = (float)java.lang.Math.sin(rot);
111         mMat[0] = c;
112         mMat[1] = -s;
113         mMat[2] = s;
114         mMat[3] = c;
115     }
116 
117     /**
118     * Sets current values to be a scale matrix of given dimensions
119     *
120     * @param x scale component x
121     * @param y scale component y
122     */
loadScale(float x, float y)123     public void loadScale(float x, float y) {
124         loadIdentity();
125         mMat[0] = x;
126         mMat[3] = y;
127     }
128 
129     /**
130     * Sets current values to be the result of multiplying two given
131     * matrices
132     *
133     * @param lhs left hand side matrix
134     * @param rhs right hand side matrix
135     */
loadMultiply(Matrix2f lhs, Matrix2f rhs)136     public void loadMultiply(Matrix2f lhs, Matrix2f rhs) {
137         for (int i=0 ; i<2 ; i++) {
138             float ri0 = 0;
139             float ri1 = 0;
140             for (int j=0 ; j<2 ; j++) {
141                 float rhs_ij = rhs.get(i,j);
142                 ri0 += lhs.get(j,0) * rhs_ij;
143                 ri1 += lhs.get(j,1) * rhs_ij;
144             }
145             set(i,0, ri0);
146             set(i,1, ri1);
147         }
148     }
149 
150     /**
151     * Post-multiplies the current matrix by a given parameter
152     *
153     * @param rhs right hand side to multiply by
154     */
multiply(Matrix2f rhs)155     public void multiply(Matrix2f rhs) {
156         Matrix2f tmp = new Matrix2f();
157         tmp.loadMultiply(this, rhs);
158         load(tmp);
159     }
160     /**
161     * Modifies the current matrix by post-multiplying it with a
162     * rotation matrix of given angle
163     *
164     * @param rot angle of rotation
165     */
rotate(float rot)166     public void rotate(float rot) {
167         Matrix2f tmp = new Matrix2f();
168         tmp.loadRotate(rot);
169         multiply(tmp);
170     }
171     /**
172     * Modifies the current matrix by post-multiplying it with a
173     * scale matrix of given dimensions
174     *
175     * @param x scale component x
176     * @param y scale component y
177     */
scale(float x, float y)178     public void scale(float x, float y) {
179         Matrix2f tmp = new Matrix2f();
180         tmp.loadScale(x, y);
181         multiply(tmp);
182     }
183     /**
184     * Sets the current matrix to its transpose
185     */
transpose()186     public void transpose() {
187         float temp = mMat[1];
188         mMat[1] = mMat[2];
189         mMat[2] = temp;
190     }
191 
192     final float[] mMat;
193 }
194 
195 
196 
197