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 | |
| 19 | import java.lang.Math; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 23 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 24 | * 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] | 25 | * |
| 26 | **/ |
| 27 | public class Matrix2f { |
| 28 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 29 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 30 | * Creates a new identity 2x2 matrix |
| 31 | */ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 32 | public Matrix2f() { |
| 33 | mMat = new float[4]; |
| 34 | loadIdentity(); |
| 35 | } |
| 36 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 37 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 38 | * Creates a new matrix and sets its values from the given |
| 39 | * parameter |
| 40 | * |
| 41 | * @param dataArray values to set the matrix to, must be 4 |
| 42 | * floats long |
| 43 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 44 | public Matrix2f(float[] dataArray) { |
Stephen Hines | ef65bb3 | 2011-03-15 14:47:31 -0700 | [diff] [blame] | 45 | mMat = new float[4]; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 46 | System.arraycopy(dataArray, 0, mMat, 0, mMat.length); |
| 47 | } |
| 48 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 49 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 50 | * Return a reference to the internal array representing matrix |
| 51 | * values. Modifying this array will also change the matrix |
| 52 | * |
| 53 | * @return internal array representing the matrix |
| 54 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 55 | public float[] getArray() { |
| 56 | return mMat; |
| 57 | } |
| 58 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 59 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 60 | * Returns the value for a given row and column |
| 61 | * |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 62 | * @param x column of the value to return |
| 63 | * @param y row of the value to return |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 64 | * |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 65 | * @return value in the yth row and xth column |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 66 | */ |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 67 | public float get(int x, int y) { |
| 68 | return mMat[x*2 + y]; |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 71 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 72 | * Sets the value for a given row and column |
| 73 | * |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 74 | * @param x column of the value to set |
| 75 | * @param y row of the value to set |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 76 | */ |
Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 77 | public void set(int x, int y, float v) { |
| 78 | mMat[x*2 + y] = v; |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 81 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 82 | * Sets the matrix values to identity |
| 83 | */ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 84 | public void loadIdentity() { |
| 85 | mMat[0] = 1; |
| 86 | mMat[1] = 0; |
| 87 | |
| 88 | mMat[2] = 0; |
| 89 | mMat[3] = 1; |
| 90 | } |
| 91 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 92 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 93 | * Sets the values of the matrix to those of the parameter |
| 94 | * |
| 95 | * @param src matrix to load the values from |
| 96 | */ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 97 | public void load(Matrix2f src) { |
Alex Sakhartchouk | b3b89f6 | 2010-12-29 08:43:49 -0800 | [diff] [blame] | 98 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 101 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 102 | * Sets current values to be a rotation matrix of given angle |
| 103 | * |
| 104 | * @param rot rotation angle |
| 105 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 106 | public void loadRotate(float rot) { |
| 107 | float c, s; |
| 108 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 109 | c = (float)java.lang.Math.cos(rot); |
| 110 | s = (float)java.lang.Math.sin(rot); |
| 111 | mMat[0] = c; |
| 112 | mMat[1] = -s; |
| 113 | mMat[2] = s; |
| 114 | mMat[3] = c; |
| 115 | } |
| 116 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 117 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 118 | * Sets current values to be a scale matrix of given dimensions |
| 119 | * |
| 120 | * @param x scale component x |
| 121 | * @param y scale component y |
| 122 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 123 | public void loadScale(float x, float y) { |
| 124 | loadIdentity(); |
| 125 | mMat[0] = x; |
| 126 | mMat[3] = y; |
| 127 | } |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 128 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 129 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 130 | * Sets current values to be the result of multiplying two given |
| 131 | * matrices |
| 132 | * |
| 133 | * @param lhs left hand side matrix |
| 134 | * @param rhs right hand side matrix |
| 135 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 136 | public void loadMultiply(Matrix2f lhs, Matrix2f rhs) { |
| 137 | for (int i=0 ; i<2 ; i++) { |
| 138 | float ri0 = 0; |
| 139 | float ri1 = 0; |
| 140 | for (int j=0 ; j<2 ; j++) { |
| 141 | float rhs_ij = rhs.get(i,j); |
| 142 | ri0 += lhs.get(j,0) * rhs_ij; |
| 143 | ri1 += lhs.get(j,1) * rhs_ij; |
| 144 | } |
| 145 | set(i,0, ri0); |
| 146 | set(i,1, ri1); |
| 147 | } |
| 148 | } |
| 149 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 150 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 151 | * Post-multiplies the current matrix by a given parameter |
| 152 | * |
| 153 | * @param rhs right hand side to multiply by |
| 154 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 155 | public void multiply(Matrix2f rhs) { |
| 156 | Matrix2f tmp = new Matrix2f(); |
| 157 | tmp.loadMultiply(this, rhs); |
| 158 | load(tmp); |
| 159 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 160 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 161 | * Modifies the current matrix by post-multiplying it with a |
| 162 | * rotation matrix of given angle |
| 163 | * |
| 164 | * @param rot angle of rotation |
| 165 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 166 | public void rotate(float rot) { |
| 167 | Matrix2f tmp = new Matrix2f(); |
| 168 | tmp.loadRotate(rot); |
| 169 | multiply(tmp); |
| 170 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 171 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 172 | * Modifies the current matrix by post-multiplying it with a |
| 173 | * scale matrix of given dimensions |
| 174 | * |
| 175 | * @param x scale component x |
| 176 | * @param y scale component y |
| 177 | */ |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 178 | public void scale(float x, float y) { |
| 179 | Matrix2f tmp = new Matrix2f(); |
| 180 | tmp.loadScale(x, y); |
| 181 | multiply(tmp); |
| 182 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 183 | /** |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 184 | * Sets the current matrix to its transpose |
| 185 | */ |
Alex Sakhartchouk | 518f033 | 2010-08-05 10:28:43 -0700 | [diff] [blame] | 186 | public void transpose() { |
| 187 | float temp = mMat[1]; |
| 188 | mMat[1] = mMat[2]; |
| 189 | mMat[2] = temp; |
| 190 | } |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 191 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 192 | final float[] mMat; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |