Aurimas Liutikas | 93554f2 | 2022-04-19 16:51:35 -0700 | [diff] [blame] | 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_matrix2x2 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 Matrix2f { |
| 29 | |
| 30 | /** |
| 31 | * Creates a new identity 2x2 matrix |
| 32 | */ |
| 33 | public Matrix2f() { |
| 34 | mMat = new float[4]; |
| 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 4 |
| 43 | * floats long |
| 44 | */ |
| 45 | public Matrix2f(float[] dataArray) { |
| 46 | mMat = new float[4]; |
| 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 | */ |
| 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 | */ |
| 68 | public float get(int x, int y) { |
| 69 | return mMat[x*2 + 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 | */ |
| 78 | public void set(int x, int y, float v) { |
| 79 | mMat[x*2 + y] = v; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets the matrix values to identity |
| 84 | */ |
| 85 | public void loadIdentity() { |
| 86 | mMat[0] = 1; |
| 87 | mMat[1] = 0; |
| 88 | |
| 89 | mMat[2] = 0; |
| 90 | mMat[3] = 1; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Sets the values of the matrix to those of the parameter |
| 95 | * |
| 96 | * @param src matrix to load the values from |
| 97 | */ |
| 98 | public void load(Matrix2f src) { |
| 99 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets current values to be a rotation matrix of given angle |
| 104 | * |
| 105 | * @param rot rotation angle |
| 106 | */ |
| 107 | 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 | |
| 118 | /** |
| 119 | * 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 | */ |
| 124 | public void loadScale(float x, float y) { |
| 125 | loadIdentity(); |
| 126 | mMat[0] = x; |
| 127 | mMat[3] = y; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * 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 | */ |
| 137 | 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 | |
| 151 | /** |
| 152 | * Post-multiplies the current matrix by a given parameter |
| 153 | * |
| 154 | * @param rhs right hand side to multiply by |
| 155 | */ |
| 156 | public void multiply(Matrix2f rhs) { |
| 157 | Matrix2f tmp = new Matrix2f(); |
| 158 | tmp.loadMultiply(this, rhs); |
| 159 | load(tmp); |
| 160 | } |
| 161 | /** |
| 162 | * Modifies the current matrix by post-multiplying it with a |
| 163 | * rotation matrix of given angle |
| 164 | * |
| 165 | * @param rot angle of rotation |
| 166 | */ |
| 167 | public void rotate(float rot) { |
| 168 | Matrix2f tmp = new Matrix2f(); |
| 169 | tmp.loadRotate(rot); |
| 170 | multiply(tmp); |
| 171 | } |
| 172 | /** |
| 173 | * 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 | */ |
| 179 | public void scale(float x, float y) { |
| 180 | Matrix2f tmp = new Matrix2f(); |
| 181 | tmp.loadScale(x, y); |
| 182 | multiply(tmp); |
| 183 | } |
| 184 | /** |
| 185 | * Sets the current matrix to its transpose |
| 186 | */ |
| 187 | public void transpose() { |
| 188 | float temp = mMat[1]; |
| 189 | mMat[1] = mMat[2]; |
| 190 | mMat[2] = temp; |
| 191 | } |
| 192 | |
| 193 | final float[] mMat; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |