blob: d3621fab8d7438c5402f591f5dbf8a377d883530 [file] [log] [blame]
Jason Sams25430d02010-02-02 15:26:40 -08001/*
Stephen Hinesec6f2002012-07-10 16:16:22 -07002 * Copyright (C) 2009-2012 The Android Open Source Project
Jason Sams25430d02010-02-02 15:26:40 -08003 *
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
17package android.renderscript;
18
19import java.lang.Math;
20import android.util.Log;
21
22
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070023/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070024 * Class for exposing the native RenderScript rs_matrix2x2 type back to the Android system.
Jason Sams25430d02010-02-02 15:26:40 -080025 *
26 **/
27public class Matrix2f {
28
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070029 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080030 * Creates a new identity 2x2 matrix
31 */
Jason Sams25430d02010-02-02 15:26:40 -080032 public Matrix2f() {
33 mMat = new float[4];
34 loadIdentity();
35 }
36
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070037 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080038 * 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 Sakhartchouke27cdee2010-12-17 11:41:08 -080044 public Matrix2f(float[] dataArray) {
Stephen Hinesef65bb32011-03-15 14:47:31 -070045 mMat = new float[4];
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080046 System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
47 }
48
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070049 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080050 * 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 Sakhartchouke27cdee2010-12-17 11:41:08 -080055 public float[] getArray() {
56 return mMat;
57 }
58
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070059 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080060 * Returns the value for a given row and column
61 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070062 * @param x column of the value to return
63 * @param y row of the value to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080064 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070065 * @return value in the yth row and xth column
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080066 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070067 public float get(int x, int y) {
68 return mMat[x*2 + y];
Jason Sams25430d02010-02-02 15:26:40 -080069 }
70
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070071 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080072 * Sets the value for a given row and column
73 *
Stephen Hinesec6f2002012-07-10 16:16:22 -070074 * @param x column of the value to set
75 * @param y row of the value to set
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080076 */
Stephen Hinesec6f2002012-07-10 16:16:22 -070077 public void set(int x, int y, float v) {
78 mMat[x*2 + y] = v;
Jason Sams25430d02010-02-02 15:26:40 -080079 }
80
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070081 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080082 * Sets the matrix values to identity
83 */
Jason Sams25430d02010-02-02 15:26:40 -080084 public void loadIdentity() {
85 mMat[0] = 1;
86 mMat[1] = 0;
87
88 mMat[2] = 0;
89 mMat[3] = 1;
90 }
91
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070092 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080093 * Sets the values of the matrix to those of the parameter
94 *
95 * @param src matrix to load the values from
96 */
Jason Sams25430d02010-02-02 15:26:40 -080097 public void load(Matrix2f src) {
Alex Sakhartchoukb3b89f62010-12-29 08:43:49 -080098 System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
Jason Sams25430d02010-02-02 15:26:40 -080099 }
100
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700101 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800102 * Sets current values to be a rotation matrix of given angle
103 *
104 * @param rot rotation angle
105 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700106 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 Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800118 * 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 Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700123 public void loadScale(float x, float y) {
124 loadIdentity();
125 mMat[0] = x;
126 mMat[3] = y;
127 }
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800128
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700129 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800130 * 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 Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700136 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 Hines9c9ad3f8c22012-05-07 15:34:29 -0700150 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800151 * Post-multiplies the current matrix by a given parameter
152 *
153 * @param rhs right hand side to multiply by
154 */
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700155 public void multiply(Matrix2f rhs) {
156 Matrix2f tmp = new Matrix2f();
157 tmp.loadMultiply(this, rhs);
158 load(tmp);
159 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700160 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800161 * 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 Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700166 public void rotate(float rot) {
167 Matrix2f tmp = new Matrix2f();
168 tmp.loadRotate(rot);
169 multiply(tmp);
170 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700171 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800172 * 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 Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700178 public void scale(float x, float y) {
179 Matrix2f tmp = new Matrix2f();
180 tmp.loadScale(x, y);
181 multiply(tmp);
182 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700183 /**
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800184 * Sets the current matrix to its transpose
185 */
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700186 public void transpose() {
187 float temp = mMat[1];
188 mMat[1] = mMat[2];
189 mMat[2] = temp;
190 }
Alex Sakhartchoukcf9a44c2010-08-04 10:48:30 -0700191
Jason Sams25430d02010-02-02 15:26:40 -0800192 final float[] mMat;
193}
194
195
196