Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 1 | /* |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 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 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 19 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 20 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 21 | * Class for exposing the native RenderScript rs_matrix3x3 type back to the Android system. |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 22 | * |
Xusong Wang | 8b4548c | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 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. |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 26 | **/ |
Xusong Wang | 8b4548c | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 27 | @Deprecated |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 28 | public class Matrix3f { |
| 29 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 30 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 31 | * Creates a new identity 3x3 matrix |
| 32 | */ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 33 | public Matrix3f() { |
| 34 | mMat = new float[9]; |
| 35 | loadIdentity(); |
| 36 | } |
| 37 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 38 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 45 | public Matrix3f(float[] dataArray) { |
| 46 | mMat = new float[9]; |
| 47 | System.arraycopy(dataArray, 0, mMat, 0, mMat.length); |
| 48 | } |
| 49 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 50 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 56 | public float[] getArray() { |
| 57 | return mMat; |
| 58 | } |
| 59 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 60 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 61 | * Returns the value for a given row and column |
| 62 | * |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 63 | * @param x column of the value to return |
| 64 | * @param y row of the value to return |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 65 | * |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 66 | * @return value in the yth row and xth column |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 67 | */ |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 68 | public float get(int x, int y) { |
| 69 | return mMat[x*3 + y]; |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 72 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 73 | * Sets the value for a given row and column |
| 74 | * |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 75 | * @param x column of the value to set |
| 76 | * @param y row of the value to set |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 77 | */ |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 78 | public void set(int x, int y, float v) { |
| 79 | mMat[x*3 + y] = v; |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 82 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 83 | * Sets the matrix values to identity |
| 84 | */ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 99 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 100 | * Sets the values of the matrix to those of the parameter |
| 101 | * |
| 102 | * @param src matrix to load the values from |
| 103 | */ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 104 | public void load(Matrix3f src) { |
Alex Sakhartchouk | b3b89f6 | 2010-12-29 08:43:49 -0800 | [diff] [blame] | 105 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 108 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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; |
Stephen Hines | 0ce7cda | 2013-01-22 16:01:44 -0800 | [diff] [blame] | 142 | mMat[7] = yz*nc - xs; |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 143 | mMat[2] = zx*nc - ys; |
Stephen Hines | 3983754 | 2013-01-22 18:29:41 -0800 | [diff] [blame] | 144 | mMat[5] = yz*nc + xs; |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 145 | mMat[8] = z*z*nc + c; |
| 146 | } |
| 147 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 148 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 149 | * Makes the upper 2x2 a rotation matrix of the given angle |
| 150 | * |
| 151 | * @param rot rotation angle |
| 152 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 153 | public void loadRotate(float rot) { |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 154 | loadIdentity(); |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 165 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 171 | public void loadScale(float x, float y) { |
| 172 | loadIdentity(); |
| 173 | mMat[0] = x; |
| 174 | mMat[4] = y; |
| 175 | } |
| 176 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 177 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 191 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 198 | public void loadTranslate(float x, float y) { |
| 199 | loadIdentity(); |
| 200 | mMat[6] = x; |
| 201 | mMat[7] = y; |
| 202 | } |
| 203 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 204 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 228 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 229 | * Post-multiplies the current matrix by a given parameter |
| 230 | * |
| 231 | * @param rhs right hand side to multiply by |
| 232 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 233 | public void multiply(Matrix3f rhs) { |
| 234 | Matrix3f tmp = new Matrix3f(); |
| 235 | tmp.loadMultiply(this, rhs); |
| 236 | load(tmp); |
| 237 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 238 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 239 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 253 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 254 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 260 | public void rotate(float rot) { |
| 261 | Matrix3f tmp = new Matrix3f(); |
| 262 | tmp.loadRotate(rot); |
| 263 | multiply(tmp); |
| 264 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 265 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 266 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 273 | public void scale(float x, float y) { |
| 274 | Matrix3f tmp = new Matrix3f(); |
| 275 | tmp.loadScale(x, y); |
| 276 | multiply(tmp); |
| 277 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 278 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 279 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 292 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 293 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 300 | public void translate(float x, float y) { |
| 301 | Matrix3f tmp = new Matrix3f(); |
| 302 | tmp.loadTranslate(x, y); |
| 303 | multiply(tmp); |
| 304 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 305 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 306 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 307 | * Sets the current matrix to its transpose |
| 308 | */ |
Alex Sakhartchouk | 518f033 | 2010-08-05 10:28:43 -0700 | [diff] [blame] | 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 | } |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 318 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 319 | final float[] mMat; |
| 320 | } |
| 321 | |
| 322 | |