| 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_matrix2x2 type back to the Android system. | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 22 | * | 
| Xusong Wang | 1f8dc65 | 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 | 1f8dc65 | 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 Matrix2f { | 
|  | 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 2x2 matrix | 
|  | 32 | */ | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 33 | public Matrix2f() { | 
|  | 34 | mMat = new float[4]; | 
|  | 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 4 | 
|  | 43 | *                  floats long | 
|  | 44 | */ | 
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 45 | public Matrix2f(float[] dataArray) { | 
| Stephen Hines | ef65bb3 | 2011-03-15 14:47:31 -0700 | [diff] [blame] | 46 | mMat = new float[4]; | 
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 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*2 + 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*2 + 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 |  | 
|  | 89 | mMat[2] = 0; | 
|  | 90 | mMat[3] = 1; | 
|  | 91 | } | 
|  | 92 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 93 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 94 | * Sets the values of the matrix to those of the parameter | 
|  | 95 | * | 
|  | 96 | * @param src matrix to load the values from | 
|  | 97 | */ | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 98 | public void load(Matrix2f src) { | 
| Alex Sakhartchouk | b3b89f6 | 2010-12-29 08:43:49 -0800 | [diff] [blame] | 99 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 102 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 103 | * Sets current values to be a rotation matrix of given angle | 
|  | 104 | * | 
|  | 105 | * @param rot rotation angle | 
|  | 106 | */ | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 118 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 124 | public void loadScale(float x, float y) { | 
|  | 125 | loadIdentity(); | 
|  | 126 | mMat[0] = x; | 
|  | 127 | mMat[3] = y; | 
|  | 128 | } | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 129 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 130 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 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 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 151 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 152 | * Post-multiplies the current matrix by a given parameter | 
|  | 153 | * | 
|  | 154 | * @param rhs right hand side to multiply by | 
|  | 155 | */ | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 156 | public void multiply(Matrix2f rhs) { | 
|  | 157 | Matrix2f tmp = new Matrix2f(); | 
|  | 158 | tmp.loadMultiply(this, rhs); | 
|  | 159 | load(tmp); | 
|  | 160 | } | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 161 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 167 | public void rotate(float rot) { | 
|  | 168 | Matrix2f tmp = new Matrix2f(); | 
|  | 169 | tmp.loadRotate(rot); | 
|  | 170 | multiply(tmp); | 
|  | 171 | } | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 172 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 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 | */ | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 179 | public void scale(float x, float y) { | 
|  | 180 | Matrix2f tmp = new Matrix2f(); | 
|  | 181 | tmp.loadScale(x, y); | 
|  | 182 | multiply(tmp); | 
|  | 183 | } | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 184 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 185 | * Sets the current matrix to its transpose | 
|  | 186 | */ | 
| Alex Sakhartchouk | 518f033 | 2010-08-05 10:28:43 -0700 | [diff] [blame] | 187 | public void transpose() { | 
|  | 188 | float temp = mMat[1]; | 
|  | 189 | mMat[1] = mMat[2]; | 
|  | 190 | mMat[2] = temp; | 
|  | 191 | } | 
| Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 192 |  | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 193 | final float[] mMat; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 |  | 
|  | 197 |  |