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_matrix3x3 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 Matrix3f { 32 33 /** 34 * Creates a new identity 3x3 matrix 35 */ Matrix3f()36 public Matrix3f() { 37 mMat = new float[9]; 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 9 46 * floats long 47 */ Matrix3f(float[] dataArray)48 public Matrix3f(float[] dataArray) { 49 mMat = new float[9]; 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*3 + 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*3 + 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 mMat[2] = 0; 92 93 mMat[3] = 0; 94 mMat[4] = 1; 95 mMat[5] = 0; 96 97 mMat[6] = 0; 98 mMat[7] = 0; 99 mMat[8] = 1; 100 } 101 102 /** 103 * Sets the values of the matrix to those of the parameter 104 * 105 * @param src matrix to load the values from 106 */ load(Matrix3f src)107 public void load(Matrix3f src) { 108 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); 109 } 110 111 /** 112 * Sets current values to be a rotation matrix of certain angle 113 * about a given axis 114 * 115 * @param rot angle of rotation 116 * @param x rotation axis x 117 * @param y rotation axis y 118 * @param z rotation axis z 119 */ loadRotate(float rot, float x, float y, float z)120 public void loadRotate(float rot, float x, float y, float z) { 121 float c, s; 122 rot *= (float)(java.lang.Math.PI / 180.0f); 123 c = (float)java.lang.Math.cos(rot); 124 s = (float)java.lang.Math.sin(rot); 125 126 float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); 127 if (!(len != 1)) { 128 float recipLen = 1.f / len; 129 x *= recipLen; 130 y *= recipLen; 131 z *= recipLen; 132 } 133 float nc = 1.0f - c; 134 float xy = x * y; 135 float yz = y * z; 136 float zx = z * x; 137 float xs = x * s; 138 float ys = y * s; 139 float zs = z * s; 140 mMat[0] = x*x*nc + c; 141 mMat[3] = xy*nc - zs; 142 mMat[6] = zx*nc + ys; 143 mMat[1] = xy*nc + zs; 144 mMat[4] = y*y*nc + c; 145 mMat[7] = yz*nc - xs; 146 mMat[2] = zx*nc - ys; 147 mMat[5] = yz*nc + xs; 148 mMat[8] = z*z*nc + c; 149 } 150 151 /** 152 * Makes the upper 2x2 a rotation matrix of the given angle 153 * 154 * @param rot rotation angle 155 */ loadRotate(float rot)156 public void loadRotate(float rot) { 157 loadIdentity(); 158 float c, s; 159 rot *= (float)(java.lang.Math.PI / 180.0f); 160 c = (float)java.lang.Math.cos(rot); 161 s = (float)java.lang.Math.sin(rot); 162 mMat[0] = c; 163 mMat[1] = -s; 164 mMat[3] = s; 165 mMat[4] = c; 166 } 167 168 /** 169 * Makes the upper 2x2 a scale matrix of given dimensions 170 * 171 * @param x scale component x 172 * @param y scale component y 173 */ loadScale(float x, float y)174 public void loadScale(float x, float y) { 175 loadIdentity(); 176 mMat[0] = x; 177 mMat[4] = y; 178 } 179 180 /** 181 * Sets current values to be a scale matrix of given dimensions 182 * 183 * @param x scale component x 184 * @param y scale component y 185 * @param z scale component z 186 */ loadScale(float x, float y, float z)187 public void loadScale(float x, float y, float z) { 188 loadIdentity(); 189 mMat[0] = x; 190 mMat[4] = y; 191 mMat[8] = z; 192 } 193 194 /** 195 * Sets current values to be a translation matrix of given 196 * dimensions 197 * 198 * @param x translation component x 199 * @param y translation component y 200 */ loadTranslate(float x, float y)201 public void loadTranslate(float x, float y) { 202 loadIdentity(); 203 mMat[6] = x; 204 mMat[7] = y; 205 } 206 207 /** 208 * Sets current values to be the result of multiplying two given 209 * matrices 210 * 211 * @param lhs left hand side matrix 212 * @param rhs right hand side matrix 213 */ loadMultiply(Matrix3f lhs, Matrix3f rhs)214 public void loadMultiply(Matrix3f lhs, Matrix3f rhs) { 215 for (int i=0 ; i<3 ; i++) { 216 float ri0 = 0; 217 float ri1 = 0; 218 float ri2 = 0; 219 for (int j=0 ; j<3 ; j++) { 220 float rhs_ij = rhs.get(i,j); 221 ri0 += lhs.get(j,0) * rhs_ij; 222 ri1 += lhs.get(j,1) * rhs_ij; 223 ri2 += lhs.get(j,2) * rhs_ij; 224 } 225 set(i,0, ri0); 226 set(i,1, ri1); 227 set(i,2, ri2); 228 } 229 } 230 231 /** 232 * Post-multiplies the current matrix by a given parameter 233 * 234 * @param rhs right hand side to multiply by 235 */ multiply(Matrix3f rhs)236 public void multiply(Matrix3f rhs) { 237 Matrix3f tmp = new Matrix3f(); 238 tmp.loadMultiply(this, rhs); 239 load(tmp); 240 } 241 242 /** 243 * Modifies the current matrix by post-multiplying it with a 244 * rotation matrix of certain angle about a given axis 245 * 246 * @param rot angle of rotation 247 * @param x rotation axis x 248 * @param y rotation axis y 249 * @param z rotation axis z 250 */ rotate(float rot, float x, float y, float z)251 public void rotate(float rot, float x, float y, float z) { 252 Matrix3f tmp = new Matrix3f(); 253 tmp.loadRotate(rot, x, y, z); 254 multiply(tmp); 255 } 256 257 /** 258 * Modifies the upper 2x2 of the current matrix by 259 * post-multiplying it with a rotation matrix of given angle 260 * 261 * @param rot angle of rotation 262 */ rotate(float rot)263 public void rotate(float rot) { 264 Matrix3f tmp = new Matrix3f(); 265 tmp.loadRotate(rot); 266 multiply(tmp); 267 } 268 269 /** 270 * Modifies the upper 2x2 of the current matrix by 271 * post-multiplying it with a scale matrix of given dimensions 272 * 273 * @param x scale component x 274 * @param y scale component y 275 */ scale(float x, float y)276 public void scale(float x, float y) { 277 Matrix3f tmp = new Matrix3f(); 278 tmp.loadScale(x, y); 279 multiply(tmp); 280 } 281 282 /** 283 * Modifies the current matrix by post-multiplying it with a 284 * scale matrix of given dimensions 285 * 286 * @param x scale component x 287 * @param y scale component y 288 * @param z scale component z 289 */ scale(float x, float y, float z)290 public void scale(float x, float y, float z) { 291 Matrix3f tmp = new Matrix3f(); 292 tmp.loadScale(x, y, z); 293 multiply(tmp); 294 } 295 296 /** 297 * Modifies the current matrix by post-multiplying it with a 298 * translation matrix of given dimensions 299 * 300 * @param x translation component x 301 * @param y translation component y 302 */ translate(float x, float y)303 public void translate(float x, float y) { 304 Matrix3f tmp = new Matrix3f(); 305 tmp.loadTranslate(x, y); 306 multiply(tmp); 307 } 308 309 /** 310 * Sets the current matrix to its transpose 311 */ transpose()312 public void transpose() { 313 for(int i = 0; i < 2; ++i) { 314 for(int j = i + 1; j < 3; ++j) { 315 float temp = mMat[i*3 + j]; 316 mMat[i*3 + j] = mMat[j*3 + i]; 317 mMat[j*3 + i] = temp; 318 } 319 } 320 } 321 322 final float[] mMat; 323 } 324 325 326