Changeset 3817
 Timestamp:
 Feb 15, 2015, 2:47:05 PM (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/t/math/matrix.cpp
r3813 r3817 64 64 } 65 65 66 lolunit_declare_test( Multiplication)66 lolunit_declare_test(multiplication_4x4) 67 67 { 68 68 mat4 m0(1.f); … … 70 70 mat4 m2 = m0 * m1; 71 71 72 lolunit_assert_equal(m2[0][0], 1.0f); 73 lolunit_assert_equal(m2[1][0], 0.0f); 74 lolunit_assert_equal(m2[2][0], 0.0f); 75 lolunit_assert_equal(m2[3][0], 0.0f); 76 77 lolunit_assert_equal(m2[0][1], 0.0f); 78 lolunit_assert_equal(m2[1][1], 1.0f); 79 lolunit_assert_equal(m2[2][1], 0.0f); 80 lolunit_assert_equal(m2[3][1], 0.0f); 81 82 lolunit_assert_equal(m2[0][2], 0.0f); 83 lolunit_assert_equal(m2[1][2], 0.0f); 84 lolunit_assert_equal(m2[2][2], 1.0f); 85 lolunit_assert_equal(m2[3][2], 0.0f); 86 87 lolunit_assert_equal(m2[0][3], 0.0f); 88 lolunit_assert_equal(m2[1][3], 0.0f); 89 lolunit_assert_equal(m2[2][3], 0.0f); 90 lolunit_assert_equal(m2[3][3], 1.0f); 91 } 92 93 lolunit_declare_test(Inverse2x2) 72 for (int j = 0; j < 4; ++j) 73 for (int i = 0; i < 4; ++i) 74 lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e5); 75 } 76 77 lolunit_declare_test(inverse_2x2) 94 78 { 95 79 mat2 m0 = inv2; 96 80 mat2 m1 = inverse(m0); 97 98 81 mat2 m2 = m0 * m1; 99 82 100 lolunit_assert_equal(m2[0][0], 1.0f); 101 lolunit_assert_equal(m2[1][0], 0.0f); 102 103 lolunit_assert_equal(m2[0][1], 0.0f); 104 lolunit_assert_equal(m2[1][1], 1.0f); 105 } 106 107 lolunit_declare_test(LUDecomposition3x3) 108 { 109 mat3 m0 = inv3; 110 83 for (int j = 0; j < 2; ++j) 84 for (int i = 0; i < 2; ++i) 85 lolunit_assert_doubles_equal(m2[i][j], mat2(1.f)[i][j], 1e5); 86 } 87 88 lolunit_declare_test(lu_decomposition_3x3) 89 { 90 mat3 m(vec3(2, 3, 5), 91 vec3(3, 2, 3), 92 vec3(9, 5, 7)); 111 93 mat3 L, U; 112 113 lu_decomposition(inv3, L, U); 114 115 mat3 result = L * U; 116 94 lu_decomposition(m, L, U); 95 mat3 m2 = L * U; 96 97 for (int j = 0; j < 3; ++j) 117 98 for (int i = 0; i < 3; ++i) 118 99 { 119 for (int j = 0; j < 3; ++j)120 {121 if (i > j) 122 lolunit_assert_equal(L[i][j], 0);123 else if (i < j)124 lolunit_assert_equal(U[i][j], 0);125 else126 lolunit_assert_equal(L[i][j], 1);127 128 lolunit_assert_equal(result[i][j], inv3[i][j]); 129 }100 lolunit_assert(!isnan(U[i][j])); 101 lolunit_assert(!isnan(L[i][j])); 102 103 if (i < j) 104 lolunit_assert_doubles_equal(U[i][j], 0.f, 1e5); 105 if (i == j) 106 lolunit_assert_doubles_equal(L[i][j], 1.f, 1e5); 107 if (j < i) 108 lolunit_assert_doubles_equal(L[i][j], 0.f, 1e5); 109 110 lolunit_assert_doubles_equal(m2[i][j], m[i][j], 1e5); 130 111 } 131 112 } 132 113 133 lolunit_declare_test(LUDecomposition4x4) 134 { 135 mat4 m0 = inv4; 136 114 lolunit_declare_test(lu_decomposition_4x4_full) 115 { 116 mat4 m(vec4( 1, 1, 2, 1), 117 vec4(2, 1, 2, 2), 118 vec4( 4, 2, 5, 4), 119 vec4( 5, 3, 7, 6)); 137 120 mat4 L, U; 138 139 lu_decomposition(inv4, L, U); 140 141 mat4 result = L * U; 142 121 lu_decomposition(m, L, U); 122 mat4 m2 = L * U; 123 124 for (int j = 0; j < 4; ++j) 143 125 for (int i = 0; i < 4; ++i) 144 126 { 145 for (int j = 0; j < 4; ++j)146 {147 if (i > j) 148 lolunit_assert_equal(L[i][j], 0);149 else if (i < j)150 lolunit_assert_equal(U[i][j], 0);151 else152 lolunit_assert_equal(L[i][j], 1);153 154 lolunit_assert_equal(result[i][j], inv4[i][j]); 155 }127 lolunit_assert(!isnan(U[i][j])); 128 lolunit_assert(!isnan(L[i][j])); 129 130 if (i < j) 131 lolunit_assert_doubles_equal(U[i][j], 0.f, 1e5); 132 if (i == j) 133 lolunit_assert_doubles_equal(L[i][j], 1.f, 1e5); 134 if (j < i) 135 lolunit_assert_doubles_equal(L[i][j], 0.f, 1e5); 136 137 lolunit_assert_doubles_equal(m2[i][j], m[i][j], 1e5); 156 138 } 157 139 } 158 140 159 lolunit_declare_test(LInverse3x3) 160 { 161 mat3 m0 = inv3; 141 lolunit_declare_test(lu_decomposition_4x4_sparse) 142 { 143 mat4 m(vec4(1, 0, 0, 0), 144 vec4(0, 0, 1, 0), 145 vec4(0, 1, 0, 0), 146 vec4(0, 0, 1, 1)); 147 mat4 L, U; 148 lu_decomposition(m, L, U); 149 mat4 m2 = L * U; 150 151 for (int j = 0; j < 4; ++j) 152 for (int i = 0; i < 4; ++i) 153 { 154 lolunit_assert(!isnan(U[i][j])); 155 lolunit_assert(!isnan(L[i][j])); 156 157 if (i < j) 158 lolunit_assert_doubles_equal(U[i][j], 0.f, 1e5); 159 if (i == j) 160 lolunit_assert_doubles_equal(L[i][j], 1.f, 1e5); 161 if (j < i) 162 lolunit_assert_doubles_equal(L[i][j], 0.f, 1e5); 163 164 lolunit_assert_doubles_equal(m2[i][j], m[i][j], 1e5); 165 } 166 } 167 168 lolunit_declare_test(l_inverse_3x3) 169 { 170 mat3 m(vec3(2, 3, 5), 171 vec3(3, 2, 3), 172 vec3(9, 5, 7)); 162 173 mat3 L, U; 163 lu_decomposition(inv3, L, U); 164 mat3 l_inv = l_inverse(L); 165 166 mat3 identity = l_inv * L; 167 168 for (int i = 0 ; i < 3 ; ++i) 169 for (int j = 0 ; j < 3 ; ++j) 170 lolunit_assert_doubles_equal(identity[i][j], i == j ? 1 : 0, 1e5); 171 } 172 173 lolunit_declare_test(LInverse4x4) 174 { 175 mat4 m0 = inv4; 174 lu_decomposition(m, L, U); 175 mat3 m1 = l_inverse(L); 176 mat3 m2 = m1 * L; 177 178 for (int j = 0; j < 3; ++j) 179 for (int i = 0; i < 3; ++i) 180 lolunit_assert_doubles_equal(m2[i][j], mat3(1.f)[i][j], 1e5); 181 } 182 183 lolunit_declare_test(l_inverse_4x4) 184 { 185 mat4 m(vec4( 1, 1, 2, 1), 186 vec4(2, 1, 2, 2), 187 vec4( 4, 2, 5, 4), 188 vec4( 5, 3, 7, 6)); 176 189 mat4 L, U; 177 lu_decomposition(inv4, L, U); 178 mat4 l_inv = l_inverse(L); 179 180 mat4 identity = l_inv * L; 181 182 for (int i = 0 ; i < 4 ; ++i) 183 for (int j = 0 ; j < 4 ; ++j) 184 lolunit_assert_doubles_equal(identity[i][j], i == j ? 1 : 0, 1e5); 185 } 186 187 lolunit_declare_test(UInverse3x3) 188 { 189 mat3 m0 = inv3; 190 lu_decomposition(m, L, U); 191 mat4 m1 = l_inverse(L); 192 mat4 m2 = m1 * L; 193 194 for (int j = 0; j < 4; ++j) 195 for (int i = 0; i < 4; ++i) 196 lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e5); 197 } 198 199 lolunit_declare_test(u_inverse_3x3) 200 { 201 mat3 m(vec3(2, 3, 5), 202 vec3(3, 2, 3), 203 vec3(9, 5, 7)); 190 204 mat3 L, U; 191 lu_decomposition(inv3, L, U); 192 mat3 u_inv = u_inverse(U); 193 194 mat3 identity = u_inv * U; 195 196 for (int i = 0 ; i < 3 ; ++i) 197 for (int j = 0 ; j < 3 ; ++j) 198 lolunit_assert_doubles_equal(identity[i][j], i == j ? 1 : 0, 1e5); 199 } 200 201 lolunit_declare_test(UInverse4x4) 202 { 203 mat4 m0 = inv4; 205 lu_decomposition(m, L, U); 206 mat3 m1 = u_inverse(U); 207 mat3 m2 = m1 * U; 208 209 for (int j = 0; j < 3; ++j) 210 for (int i = 0; i < 3; ++i) 211 lolunit_assert_doubles_equal(m2[i][j], mat3(1.f)[i][j], 1e5); 212 } 213 214 lolunit_declare_test(u_inverse_4x4) 215 { 216 mat4 m(vec4( 1, 1, 2, 1), 217 vec4(2, 1, 2, 2), 218 vec4( 4, 2, 5, 4), 219 vec4( 5, 3, 7, 6)); 204 220 mat4 L, U; 205 lu_decomposition(inv4, L, U); 206 mat4 u_inv = u_inverse(U); 207 208 mat4 identity = u_inv * U; 209 210 for (int i = 0 ; i < 4 ; ++i) 211 for (int j = 0 ; j < 4 ; ++j) 212 lolunit_assert_doubles_equal(identity[i][j], i == j ? 1 : 0, 1e5); 213 } 214 215 lolunit_declare_test(Inverse3x3) 216 { 217 mat3 m0 = inv3; 218 mat3 m1 = inverse(m0); 219 220 mat3 m2 = m0 * m1; 221 222 lolunit_assert_doubles_equal(m2[0][0], 1.0f, 1e4); 223 lolunit_assert_doubles_equal(m2[1][0], 0.0f, 1e4); 224 lolunit_assert_doubles_equal(m2[2][0], 0.0f, 1e4); 225 226 lolunit_assert_doubles_equal(m2[0][1], 0.0f, 1e4); 227 lolunit_assert_doubles_equal(m2[1][1], 1.0f, 1e4); 228 lolunit_assert_doubles_equal(m2[2][1], 0.0f, 1e4); 229 230 lolunit_assert_doubles_equal(m2[0][2], 0.0f, 1e4); 231 lolunit_assert_doubles_equal(m2[1][2], 0.0f, 1e4); 232 lolunit_assert_doubles_equal(m2[2][2], 1.0f, 1e4); 233 } 234 235 lolunit_declare_test(inverse_4x4_1) 236 { 237 mat4 m = inv4; 221 lu_decomposition(m, L, U); 222 mat4 m1 = u_inverse(U); 223 mat4 m2 = m1 * U; 224 225 for (int j = 0; j < 4; ++j) 226 for (int i = 0; i < 4; ++i) 227 lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e5); 228 } 229 230 lolunit_declare_test(inverse_3x3) 231 { 232 mat3 m(vec3(2, 3, 5), 233 vec3(3, 2, 3), 234 vec3(9, 5, 7)); 235 mat3 m2 = inverse(m) * m; 236 237 for (int j = 0; j < 3; ++j) 238 for (int i = 0; i < 3; ++i) 239 lolunit_assert_doubles_equal(m2[i][j], mat3(1.f)[i][j], 1e5); 240 } 241 242 lolunit_declare_test(inverse_4x4_full) 243 { 244 mat4 m(vec4( 1, 1, 2, 1), 245 vec4(2, 1, 2, 2), 246 vec4( 4, 2, 5, 4), 247 vec4( 5, 3, 7, 6)); 238 248 mat4 m2 = inverse(m) * m; 239 249 240 250 for (int j = 0; j < 4; ++j) 241 251 for (int i = 0; i < 4; ++i) 242 lolunit_assert_ equal(m2[i][j], mat4(1.f)[i][j]);243 } 244 245 lolunit_declare_test(inverse_4x4_ 2)246 { 247 mat4 m(vec4(1 .f, 0.f, 0.f, 0.f),248 vec4(0 .f, 0.f, 1.f, 0.f),249 vec4(0 .f, 1.f, 0.f, 0.f),250 vec4(0 .f, 0.f, 1.f, 1.f));252 lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e5); 253 } 254 255 lolunit_declare_test(inverse_4x4_sparse) 256 { 257 mat4 m(vec4(1, 0, 0, 0), 258 vec4(0, 0, 1, 0), 259 vec4(0, 1, 0, 0), 260 vec4(0, 0, 1, 1)); 251 261 mat4 m2 = inverse(m) * m; 252 262 253 263 for (int j = 0; j < 4; ++j) 254 264 for (int i = 0; i < 4; ++i) 255 lolunit_assert_ equal(m2[i][j], mat4(1.f)[i][j]);256 } 257 258 lolunit_declare_test( Kronecker)265 lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e5); 266 } 267 268 lolunit_declare_test(kronecker_product) 259 269 { 260 270 int const COLS1 = 2, ROWS1 = 3;
Note: See TracChangeset
for help on using the changeset viewer.