Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | import java.lang.Math; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | |
| 23 | /** |
Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 24 | * Class for exposing the rs_matrix3x3 type back to java applications. |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 25 | * |
| 26 | **/ |
| 27 | public class Matrix3f { |
| 28 | |
| 29 | public Matrix3f() { |
| 30 | mMat = new float[9]; |
| 31 | loadIdentity(); |
| 32 | } |
| 33 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 34 | public Matrix3f(float[] dataArray) { |
| 35 | mMat = new float[9]; |
| 36 | System.arraycopy(dataArray, 0, mMat, 0, mMat.length); |
| 37 | } |
| 38 | |
| 39 | public float[] getArray() { |
| 40 | return mMat; |
| 41 | } |
| 42 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 43 | public float get(int i, int j) { |
| 44 | return mMat[i*3 + j]; |
| 45 | } |
| 46 | |
| 47 | public void set(int i, int j, float v) { |
| 48 | mMat[i*3 + j] = v; |
| 49 | } |
| 50 | |
| 51 | public void loadIdentity() { |
| 52 | mMat[0] = 1; |
| 53 | mMat[1] = 0; |
| 54 | mMat[2] = 0; |
| 55 | |
| 56 | mMat[3] = 0; |
| 57 | mMat[4] = 1; |
| 58 | mMat[5] = 0; |
| 59 | |
| 60 | mMat[6] = 0; |
| 61 | mMat[7] = 0; |
| 62 | mMat[8] = 1; |
| 63 | } |
| 64 | |
| 65 | public void load(Matrix3f src) { |
Alex Sakhartchouk | b3b89f6 | 2010-12-29 08:43:49 -0800 | [diff] [blame] | 66 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 69 | public void loadRotate(float rot, float x, float y, float z) { |
| 70 | float c, s; |
| 71 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 72 | c = (float)java.lang.Math.cos(rot); |
| 73 | s = (float)java.lang.Math.sin(rot); |
| 74 | |
| 75 | float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); |
| 76 | if (!(len != 1)) { |
| 77 | float recipLen = 1.f / len; |
| 78 | x *= recipLen; |
| 79 | y *= recipLen; |
| 80 | z *= recipLen; |
| 81 | } |
| 82 | float nc = 1.0f - c; |
| 83 | float xy = x * y; |
| 84 | float yz = y * z; |
| 85 | float zx = z * x; |
| 86 | float xs = x * s; |
| 87 | float ys = y * s; |
| 88 | float zs = z * s; |
| 89 | mMat[0] = x*x*nc + c; |
| 90 | mMat[3] = xy*nc - zs; |
| 91 | mMat[6] = zx*nc + ys; |
| 92 | mMat[1] = xy*nc + zs; |
| 93 | mMat[4] = y*y*nc + c; |
| 94 | mMat[9] = yz*nc - xs; |
| 95 | mMat[2] = zx*nc - ys; |
| 96 | mMat[6] = yz*nc + xs; |
| 97 | mMat[8] = z*z*nc + c; |
| 98 | } |
| 99 | |
| 100 | public void loadRotate(float rot) { |
| 101 | float c, s; |
| 102 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 103 | c = (float)java.lang.Math.cos(rot); |
| 104 | s = (float)java.lang.Math.sin(rot); |
| 105 | mMat[0] = c; |
| 106 | mMat[1] = -s; |
| 107 | mMat[3] = s; |
| 108 | mMat[4] = c; |
| 109 | } |
| 110 | |
| 111 | public void loadScale(float x, float y) { |
| 112 | loadIdentity(); |
| 113 | mMat[0] = x; |
| 114 | mMat[4] = y; |
| 115 | } |
| 116 | |
| 117 | public void loadScale(float x, float y, float z) { |
| 118 | loadIdentity(); |
| 119 | mMat[0] = x; |
| 120 | mMat[4] = y; |
| 121 | mMat[8] = z; |
| 122 | } |
| 123 | |
| 124 | public void loadTranslate(float x, float y) { |
| 125 | loadIdentity(); |
| 126 | mMat[6] = x; |
| 127 | mMat[7] = y; |
| 128 | } |
| 129 | |
| 130 | public void loadMultiply(Matrix3f lhs, Matrix3f rhs) { |
| 131 | for (int i=0 ; i<3 ; i++) { |
| 132 | float ri0 = 0; |
| 133 | float ri1 = 0; |
| 134 | float ri2 = 0; |
| 135 | for (int j=0 ; j<3 ; 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 | } |
| 141 | set(i,0, ri0); |
| 142 | set(i,1, ri1); |
| 143 | set(i,2, ri2); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public void multiply(Matrix3f rhs) { |
| 148 | Matrix3f tmp = new Matrix3f(); |
| 149 | tmp.loadMultiply(this, rhs); |
| 150 | load(tmp); |
| 151 | } |
| 152 | public void rotate(float rot, float x, float y, float z) { |
| 153 | Matrix3f tmp = new Matrix3f(); |
| 154 | tmp.loadRotate(rot, x, y, z); |
| 155 | multiply(tmp); |
| 156 | } |
| 157 | public void rotate(float rot) { |
| 158 | Matrix3f tmp = new Matrix3f(); |
| 159 | tmp.loadRotate(rot); |
| 160 | multiply(tmp); |
| 161 | } |
| 162 | public void scale(float x, float y) { |
| 163 | Matrix3f tmp = new Matrix3f(); |
| 164 | tmp.loadScale(x, y); |
| 165 | multiply(tmp); |
| 166 | } |
| 167 | public void scale(float x, float y, float z) { |
| 168 | Matrix3f tmp = new Matrix3f(); |
| 169 | tmp.loadScale(x, y, z); |
| 170 | multiply(tmp); |
| 171 | } |
| 172 | public void translate(float x, float y) { |
| 173 | Matrix3f tmp = new Matrix3f(); |
| 174 | tmp.loadTranslate(x, y); |
| 175 | multiply(tmp); |
| 176 | } |
Alex Sakhartchouk | 518f033 | 2010-08-05 10:28:43 -0700 | [diff] [blame] | 177 | public void transpose() { |
| 178 | for(int i = 0; i < 2; ++i) { |
| 179 | for(int j = i + 1; j < 3; ++j) { |
| 180 | float temp = mMat[i*3 + j]; |
| 181 | mMat[i*3 + j] = mMat[j*3 + i]; |
| 182 | mMat[j*3 + i] = temp; |
| 183 | } |
| 184 | } |
| 185 | } |
Alex Sakhartchouk | cf9a44c | 2010-08-04 10:48:30 -0700 | [diff] [blame] | 186 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 187 | final float[] mMat; |
| 188 | } |
| 189 | |
| 190 | |