1 /* 2 * Copyright (C) 2011 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.cts; 18 import android.renderscript.Matrix2f; 19 20 public class Matrix2fTest extends RSBaseCompute { 21 22 final float delta = 0.00001f; 23 final float[] setData = { 24 1.0f, 2.0f, 25 3.0f, 4.0f 26 }; 27 28 void checkIdentity(Matrix2f m, float delta) { 29 for (int i = 0; i < 2; i ++) { 30 for (int j = 0; j < 2; j ++) { 31 if (i == j) { 32 assertEquals(1.0f, m.getArray()[i*2 + j], delta); 33 } else { 34 assertEquals(0.0f, m.getArray()[i*2 + j], delta); 35 } 36 } 37 } 38 } 39 40 String getString(float[] array) { 41 StringBuilder builder = new StringBuilder(); 42 for (int i = 0; i < 2; i ++) { 43 builder.append("["); 44 for (int j = 0; j < 2; j ++) { 45 builder.append(array[i*2 + j]); 46 builder.append(" "); 47 } 48 builder.append("]"); 49 } 50 return builder.toString(); 51 } 52 53 void checkData(Matrix2f m, float[] data, float delta) { 54 for (int i = 0; i < data.length; i ++) { 55 assertEquals(data[i], m.getArray()[i], delta); 56 } 57 } 58 59 void checkData(Matrix2f m, float[] data) { 60 String s1 = getString(m.getArray()); 61 String s2 = getString(data); 62 assertEquals(s2, s1); 63 } 64 65 public void testCreation() { 66 Matrix2f m = new Matrix2f(); 67 assertTrue(m.getArray() != null); 68 checkIdentity(m, 0.0f); 69 70 m = new Matrix2f(setData); 71 checkData(m, setData); 72 } 73 74 public void testGet() { 75 76 Matrix2f m = new Matrix2f(setData); 77 78 for (int i = 0; i < 2; i ++) { 79 for (int j = 0; j < 2; j ++) { 80 assertEquals(setData[i*2 + j], m.get(i, j), 0.0f); 81 } 82 } 83 } 84 85 public void testSet() { 86 Matrix2f m = new Matrix2f(); 87 for (int i = 0; i < 2; i ++) { 88 for (int j = 0; j < 2; j ++) { 89 float valToSet = setData[i*2 + j]; 90 m.set(i, j, valToSet); 91 assertEquals(valToSet, m.get(i, j), 0.0f); 92 } 93 } 94 } 95 96 public void testLoadIdentity() { 97 Matrix2f m = new Matrix2f(setData); 98 m.loadIdentity(); 99 checkIdentity(m, 0.0f); 100 } 101 102 public void testLoad() { 103 Matrix2f m1 = new Matrix2f(); 104 Matrix2f m2 = new Matrix2f(setData); 105 106 m1.load(m2); 107 checkData(m1, setData); 108 } 109 110 public void testLoadScale() { 111 float[] expectedData = { 112 2.0f, 0.0f, 113 0.0f, 3.0f 114 }; 115 116 Matrix2f m = new Matrix2f(setData); 117 m.loadScale(2.0f, 3.0f); 118 checkData(m, expectedData); 119 } 120 121 public void testMultiply() { 122 float[] lhsData = { 123 1.0f, 1.0f, 124 1.0f, 1.0f 125 }; 126 127 float[] rhsData = { 128 2.0f, 2.0f, 129 3.0f, 3.0f 130 }; 131 132 float[] expected = { 133 2.0f*2.0f, 2.0f*2.0f, 134 2.0f*3.0f, 2.0f*3.0f 135 }; 136 137 // Due to column major nature of OpenGL, 138 // left hand side and right hand side 139 // are reversed. Premultiplying by row major 140 // and post multiplying by column major 141 // are the same. So lhs and rhs get reversed here. 142 Matrix2f lhs = new Matrix2f(lhsData); 143 Matrix2f rhs = new Matrix2f(rhsData); 144 Matrix2f loadMul = new Matrix2f(); 145 146 loadMul.loadMultiply(lhs, rhs); 147 checkData(loadMul, expected); 148 149 lhs.multiply(rhs); 150 checkData(lhs, expected); 151 } 152 153 public void testScale() { 154 float[] expectedData = new float[setData.length]; 155 System.arraycopy(setData, 0, expectedData, 0, setData.length); 156 float scaleX = 2.0f; 157 float scaleY = 3.0f; 158 expectedData[0] *= scaleX; 159 expectedData[1] *= scaleX; 160 expectedData[2] *= scaleY; 161 expectedData[3] *= scaleY; 162 163 Matrix2f m = new Matrix2f(setData); 164 m.scale(scaleX, scaleY); 165 checkData(m, expectedData); 166 } 167 168 public void testTranspose() { 169 float[] expectedData = new float[setData.length]; 170 System.arraycopy(setData, 0, expectedData, 0, setData.length); 171 float temp = expectedData[1]; 172 expectedData[1] = expectedData[2]; 173 expectedData[2] = temp; 174 175 Matrix2f m = new Matrix2f(setData); 176 m.transpose(); 177 178 checkData(m, expectedData); 179 } 180 181 public void testRotateLoadRotate() { 182 183 float theta = 30.0f * (float)(java.lang.Math.PI / 180.0f); 184 float cosTheta = (float)Math.cos((float)theta); 185 float sinTheta = (float)Math.sin((float)theta); 186 float[] rotate = new float[4]; 187 rotate[0] = cosTheta; 188 rotate[1] = -sinTheta; 189 rotate[2] = sinTheta; 190 rotate[3] = cosTheta; 191 192 Matrix2f m = new Matrix2f(); 193 m.loadRotate(30.0f); 194 checkData(m, rotate); 195 196 m = new Matrix2f(); 197 m.rotate(30.0f); 198 checkData(m, rotate); 199 200 float[] sourceData = { 201 2.0f, 3.0f, 202 4.0f, 5.0f 203 }; 204 float[] rotated = new float[4]; 205 rotated[0] = rotate[0] * sourceData[0] + rotate[1] * sourceData[2]; 206 rotated[1] = rotate[0] * sourceData[1] + rotate[1] * sourceData[3]; 207 rotated[2] = rotate[2] * sourceData[0] + rotate[3] * sourceData[2]; 208 rotated[3] = rotate[2] * sourceData[1] + rotate[3] * sourceData[3]; 209 m = new Matrix2f(sourceData); 210 m.rotate(30.0f); 211 212 checkData(m, rotated); 213 } 214 } 215