blob: ea97509bb7cad94deeffd84d728ff4c4150980f0 [file] [log] [blame]
Jason Sams0826a6f2009-06-15 19:04:56 -07001/*
2 * Copyright (C) 2009 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
17package android.renderscript;
18
19import java.lang.Math;
20import android.util.Log;
21
22
Jason Samse29d4712009-07-23 15:19:03 -070023/**
24 * @hide
25 *
26 **/
Jason Sams25430d02010-02-02 15:26:40 -080027public class Matrix4f {
Jason Sams0826a6f2009-06-15 19:04:56 -070028
Jason Sams25430d02010-02-02 15:26:40 -080029 public Matrix4f() {
Jason Sams0826a6f2009-06-15 19:04:56 -070030 mMat = new float[16];
31 loadIdentity();
32 }
33
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080034 public Matrix4f(float[] dataArray) {
35 mMat = new float[16];
36 System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
37 }
38
39 public float[] getArray() {
40 return mMat;
41 }
42
Jason Sams0826a6f2009-06-15 19:04:56 -070043 public float get(int i, int j) {
44 return mMat[i*4 + j];
45 }
46
47 public void set(int i, int j, float v) {
48 mMat[i*4 + j] = v;
49 }
50
51 public void loadIdentity() {
52 mMat[0] = 1;
53 mMat[1] = 0;
54 mMat[2] = 0;
55 mMat[3] = 0;
56
57 mMat[4] = 0;
58 mMat[5] = 1;
59 mMat[6] = 0;
60 mMat[7] = 0;
Jason Sams25430d02010-02-02 15:26:40 -080061
Jason Sams0826a6f2009-06-15 19:04:56 -070062 mMat[8] = 0;
63 mMat[9] = 0;
64 mMat[10] = 1;
65 mMat[11] = 0;
66
67 mMat[12] = 0;
68 mMat[13] = 0;
69 mMat[14] = 0;
70 mMat[15] = 1;
71 }
72
Jason Sams25430d02010-02-02 15:26:40 -080073 public void load(Matrix4f src) {
74 System.arraycopy(mMat, 0, src, 0, 16);
Jason Sams0826a6f2009-06-15 19:04:56 -070075 }
76
77 public void loadRotate(float rot, float x, float y, float z) {
78 float c, s;
79 mMat[3] = 0;
80 mMat[7] = 0;
81 mMat[11]= 0;
82 mMat[12]= 0;
83 mMat[13]= 0;
84 mMat[14]= 0;
85 mMat[15]= 1;
86 rot *= (float)(java.lang.Math.PI / 180.0f);
87 c = (float)java.lang.Math.cos(rot);
88 s = (float)java.lang.Math.sin(rot);
Jason Sams25430d02010-02-02 15:26:40 -080089
Jason Sams0826a6f2009-06-15 19:04:56 -070090 float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z);
91 if (!(len != 1)) {
92 float recipLen = 1.f / len;
93 x *= recipLen;
94 y *= recipLen;
95 z *= recipLen;
96 }
97 float nc = 1.0f - c;
98 float xy = x * y;
99 float yz = y * z;
100 float zx = z * x;
101 float xs = x * s;
102 float ys = y * s;
Jason Sams25430d02010-02-02 15:26:40 -0800103 float zs = z * s;
Jason Sams0826a6f2009-06-15 19:04:56 -0700104 mMat[ 0] = x*x*nc + c;
105 mMat[ 4] = xy*nc - zs;
106 mMat[ 8] = zx*nc + ys;
107 mMat[ 1] = xy*nc + zs;
108 mMat[ 5] = y*y*nc + c;
109 mMat[ 9] = yz*nc - xs;
110 mMat[ 2] = zx*nc - ys;
111 mMat[ 6] = yz*nc + xs;
112 mMat[10] = z*z*nc + c;
113 }
114
115 public void loadScale(float x, float y, float z) {
116 loadIdentity();
117 mMat[0] = x;
118 mMat[5] = y;
119 mMat[10] = z;
120 }
Jason Sams25430d02010-02-02 15:26:40 -0800121
Jason Sams0826a6f2009-06-15 19:04:56 -0700122 public void loadTranslate(float x, float y, float z) {
123 loadIdentity();
124 mMat[12] = x;
125 mMat[13] = y;
126 mMat[14] = z;
127 }
128
Jason Sams25430d02010-02-02 15:26:40 -0800129 public void loadMultiply(Matrix4f lhs, Matrix4f rhs) {
Jason Sams0826a6f2009-06-15 19:04:56 -0700130 for (int i=0 ; i<4 ; i++) {
131 float ri0 = 0;
132 float ri1 = 0;
133 float ri2 = 0;
134 float ri3 = 0;
135 for (int j=0 ; j<4 ; j++) {
136 float rhs_ij = rhs.get(i,j);
137 ri0 += lhs.get(j,0) * rhs_ij;
138 ri1 += lhs.get(j,1) * rhs_ij;
139 ri2 += lhs.get(j,2) * rhs_ij;
140 ri3 += lhs.get(j,3) * rhs_ij;
141 }
142 set(i,0, ri0);
143 set(i,1, ri1);
144 set(i,2, ri2);
145 set(i,3, ri3);
146 }
147 }
148
149 public void loadOrtho(float l, float r, float b, float t, float n, float f) {
150 loadIdentity();
151 mMat[0] = 2 / (r - l);
152 mMat[5] = 2 / (t - b);
153 mMat[10]= -2 / (f - n);
154 mMat[12]= -(r + l) / (r - l);
Jason Samsb37c0a52009-06-16 17:49:58 -0700155 mMat[13]= -(t + b) / (t - b);
156 mMat[14]= -(f + n) / (f - n);
Jason Sams0826a6f2009-06-15 19:04:56 -0700157 }
158
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800159 public void loadOrthoWindow(int w, int h) {
160 loadOrtho(0,w, h,0, -1,1);
161 }
162
Jason Sams0826a6f2009-06-15 19:04:56 -0700163 public void loadFrustum(float l, float r, float b, float t, float n, float f) {
164 loadIdentity();
165 mMat[0] = 2 * n / (r - l);
166 mMat[5] = 2 * n / (t - b);
167 mMat[8] = (r + l) / (r - l);
168 mMat[9] = (t + b) / (t - b);
169 mMat[10]= -(f + n) / (f - n);
170 mMat[11]= -1;
171 mMat[14]= -2*f*n / (f - n);
172 mMat[15]= 0;
173 }
174
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800175 public void loadPerspective(float fovy, float aspect, float near, float far) {
176 float top = near * (float)Math.tan((float) (fovy * Math.PI / 360.0f));
177 float bottom = -top;
178 float left = bottom * aspect;
179 float right = top * aspect;
180 loadFrustum(left, right, bottom, top, near, far);
181 }
182
Jason Sams25430d02010-02-02 15:26:40 -0800183 public void multiply(Matrix4f rhs) {
184 Matrix4f tmp = new Matrix4f();
Jason Sams0826a6f2009-06-15 19:04:56 -0700185 tmp.loadMultiply(this, rhs);
186 load(tmp);
187 }
188 public void rotate(float rot, float x, float y, float z) {
Jason Sams25430d02010-02-02 15:26:40 -0800189 Matrix4f tmp = new Matrix4f();
Jason Sams0826a6f2009-06-15 19:04:56 -0700190 tmp.loadRotate(rot, x, y, z);
191 multiply(tmp);
192 }
193 public void scale(float x, float y, float z) {
Jason Sams25430d02010-02-02 15:26:40 -0800194 Matrix4f tmp = new Matrix4f();
Jason Sams0826a6f2009-06-15 19:04:56 -0700195 tmp.loadScale(x, y, z);
196 multiply(tmp);
197 }
198 public void translate(float x, float y, float z) {
Jason Sams25430d02010-02-02 15:26:40 -0800199 Matrix4f tmp = new Matrix4f();
Jason Sams0826a6f2009-06-15 19:04:56 -0700200 tmp.loadTranslate(x, y, z);
201 multiply(tmp);
202 }
Alex Sakhartchoukfacd6fc2010-08-10 17:34:39 -0700203 private float computeCofactor(int i, int j) {
204 int c0 = (i+1) % 4;
205 int c1 = (i+2) % 4;
206 int c2 = (i+3) % 4;
207 int r0 = (j+1) % 4;
208 int r1 = (j+2) % 4;
209 int r2 = (j+3) % 4;
210
211 float minor = (mMat[c0 + 4*r0] * (mMat[c1 + 4*r1] * mMat[c2 + 4*r2] -
212 mMat[c1 + 4*r2] * mMat[c2 + 4*r1]))
213 - (mMat[c0 + 4*r1] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r2] -
214 mMat[c1 + 4*r2] * mMat[c2 + 4*r0]))
215 + (mMat[c0 + 4*r2] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r1] -
216 mMat[c1 + 4*r1] * mMat[c2 + 4*r0]));
217
218 float cofactor = ((i+j) & 1) != 0 ? -minor : minor;
219 return cofactor;
220 }
221
222 public boolean inverse() {
223
224 Matrix4f result = new Matrix4f();
225
226 for (int i = 0; i < 4; ++i) {
227 for (int j = 0; j < 4; ++j) {
228 result.mMat[4*i + j] = computeCofactor(i, j);
229 }
230 }
231
232 // Dot product of 0th column of source and 0th row of result
233 float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[1] +
234 mMat[8]*result.mMat[2] + mMat[12]*result.mMat[3];
235
236 if (Math.abs(det) < 1e-6) {
237 return false;
238 }
239
240 det = 1.0f / det;
241 for (int i = 0; i < 16; ++i) {
242 mMat[i] = result.mMat[i] * det;
243 }
244
245 return true;
246 }
247
248 public boolean inverseTranspose() {
249
250 Matrix4f result = new Matrix4f();
251
252 for (int i = 0; i < 4; ++i) {
253 for (int j = 0; j < 4; ++j) {
254 result.mMat[4*j + i] = computeCofactor(i, j);
255 }
256 }
257
258 float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[4] +
259 mMat[8]*result.mMat[8] + mMat[12]*result.mMat[12];
260
261 if (Math.abs(det) < 1e-6) {
262 return false;
263 }
264
265 det = 1.0f / det;
266 for (int i = 0; i < 16; ++i) {
267 mMat[i] = result.mMat[i] * det;
268 }
269
270 return true;
271 }
272
Alex Sakhartchouk518f0332010-08-05 10:28:43 -0700273 public void transpose() {
274 for(int i = 0; i < 3; ++i) {
275 for(int j = i + 1; j < 4; ++j) {
276 float temp = mMat[i*4 + j];
277 mMat[i*4 + j] = mMat[j*4 + i];
278 mMat[j*4 + i] = temp;
279 }
280 }
281 }
Jason Sams0826a6f2009-06-15 19:04:56 -0700282
Jason Sams25430d02010-02-02 15:26:40 -0800283 final float[] mMat;
Jason Sams0826a6f2009-06-15 19:04:56 -0700284}
285
286
287
288
289