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