1 /// Custom sized 2-dimension Matrices 2 module gfm.math.matrix; 3 4 import std.math, 5 std.typetuple, 6 std.traits, 7 std..string, 8 std.typecons, 9 std.conv; 10 11 import gfm.math.vector, 12 gfm.math.shapes, 13 gfm.math.quaternion; 14 15 /// Generic non-resizeable matrix with R rows and C columns. 16 /// Intended for 3D use (size 3x3 and 4x4). 17 /// Important: <b>Matrices here are in row-major order whereas OpenGL is column-major.</b> 18 /// Params: 19 /// T = type of elements 20 /// R = number of rows 21 /// C = number of columns 22 struct Matrix(T, int R, int C) 23 { 24 public 25 { 26 static assert(R >= 1 && C >= 1); 27 28 alias Vector!(T, C) row_t; 29 alias Vector!(T, R) column_t; 30 31 enum bool isSquare = (R == C); 32 33 // fields definition 34 union 35 { 36 T[C*R] v; // all elements 37 row_t[R] rows; // all rows 38 T[C][R] c; // components 39 } 40 41 @nogc this(U...)(U values) pure nothrow 42 { 43 static if ((U.length == C*R) && allSatisfy!(isTAssignable, U)) 44 { 45 // construct with components 46 foreach(int i, x; values) 47 v[i] = x; 48 } 49 else static if ((U.length == 1) && (isAssignable!(U[0])) && (!is(U[0] : Matrix))) 50 { 51 // construct with assignment 52 opAssign!(U[0])(values[0]); 53 } 54 else static assert(false, "cannot create a matrix from given arguments"); 55 } 56 57 /// Construct a matrix from columns. 58 @nogc static Matrix fromColumns(column_t[] columns) pure nothrow 59 { 60 assert(columns.length == C); 61 Matrix res; 62 for (int i = 0; i < R; ++i) 63 for (int j = 0; j < C; ++j) 64 { 65 res.c[i][j] = columns[j][i]; 66 } 67 return res; 68 } 69 70 /// Construct a matrix from rows. 71 @nogc static Matrix fromRows(row_t[] rows) pure nothrow 72 { 73 assert(rows.length == R); 74 Matrix res; 75 res.rows[] = rows[]; 76 return res; 77 } 78 79 /// Construct matrix with a scalar. 80 @nogc this(U)(T x) pure nothrow 81 { 82 for (int i = 0; i < _N; ++i) 83 v[i] = x; 84 } 85 86 /// Assign with a scalar. 87 @nogc ref Matrix opAssign(U : T)(U x) pure nothrow 88 { 89 for (int i = 0; i < R * C; ++i) 90 v[i] = x; 91 return this; 92 } 93 94 /// Assign with a samey matrice. 95 @nogc ref Matrix opAssign(U : Matrix)(U x) pure nothrow 96 { 97 for (int i = 0; i < R * C; ++i) 98 v[i] = x.v[i]; 99 return this; 100 } 101 102 /// Assign from other small matrices (same size, compatible type). 103 @nogc ref Matrix opAssign(U)(U x) pure nothrow 104 if (isMatrixInstantiation!U 105 && is(U._T : _T) 106 && (!is(U: Matrix)) 107 && (U._R == R) && (U._C == C)) 108 { 109 for (int i = 0; i < R * C; ++i) 110 v[i] = x.v[i]; 111 return this; 112 } 113 114 /// Assign with a static array of size R * C. 115 @nogc ref Matrix opAssign(U)(U x) pure nothrow 116 if ((isStaticArray!U) 117 && is(typeof(x[0]) : T) 118 && (U.length == R * C)) 119 { 120 for (int i = 0; i < R * C; ++i) 121 v[i] = x[i]; 122 return this; 123 } 124 125 /// Assign with a dynamic array of size R * C. 126 @nogc ref Matrix opAssign(U)(U x) pure nothrow 127 if ((isDynamicArray!U) 128 && is(typeof(x[0]) : T)) 129 { 130 assert(x.length == R * C); 131 for (int i = 0; i < R * C; ++i) 132 v[i] = x[i]; 133 return this; 134 } 135 136 /// Return a pointer to content. 137 @nogc inout(T)* ptr() pure inout nothrow @property 138 { 139 return v.ptr; 140 } 141 142 /// Returns a column as a vector 143 /// Returns: column j as a vector. 144 @nogc column_t column(int j) pure const nothrow 145 { 146 column_t res = void; 147 for (int i = 0; i < R; ++i) 148 res.v[i] = c[i][j]; 149 return res; 150 } 151 152 /// Returns a row as a vector 153 /// Returns: row i as a vector. 154 @nogc row_t row(int i) pure const nothrow 155 { 156 return rows[i]; 157 } 158 159 /// Covnerts to pretty string. 160 string toString() const nothrow 161 { 162 try 163 return format("%s", v); 164 catch (Exception e) 165 assert(false); // should not happen since format is right 166 } 167 168 /// Matrix * scalar multiplication. 169 @nogc Matrix opBinary(string op)(T factor) pure const nothrow if (op == "*") 170 { 171 Matrix result = void; 172 173 for (int i = 0; i < R; ++i) 174 { 175 for (int j = 0; j < C; ++j) 176 { 177 result.c[i][j] = c[i][j] * factor; 178 } 179 } 180 return result; 181 } 182 183 /// Matrix * vector multiplication. 184 @nogc column_t opBinary(string op)(row_t x) pure const nothrow if (op == "*") 185 { 186 column_t res = void; 187 for (int i = 0; i < R; ++i) 188 { 189 T sum = 0; 190 for (int j = 0; j < C; ++j) 191 { 192 sum += c[i][j] * x.v[j]; 193 } 194 res.v[i] = sum; 195 } 196 return res; 197 } 198 199 /// Matrix * matrix multiplication. 200 @nogc auto opBinary(string op, U)(U x) pure const nothrow 201 if (isMatrixInstantiation!U && (U._R == C) && (op == "*")) 202 { 203 Matrix!(T, R, U._C) result = void; 204 205 for (int i = 0; i < R; ++i) 206 { 207 for (int j = 0; j < U._C; ++j) 208 { 209 T sum = 0; 210 for (int k = 0; k < C; ++k) 211 sum += c[i][k] * x.c[k][j]; 212 result.c[i][j] = sum; 213 } 214 } 215 return result; 216 } 217 218 /// Matrix add and substraction. 219 @nogc Matrix opBinary(string op, U)(U other) pure const nothrow 220 if (is(U : Matrix) && (op == "+" || op == "-")) 221 { 222 Matrix result = void; 223 224 for (int i = 0; i < R; ++i) 225 { 226 for (int j = 0; j < C; ++j) 227 { 228 mixin("result.c[i][j] = c[i][j] " ~ op ~ " other.c[i][j];"); 229 } 230 } 231 return result; 232 } 233 234 // matrix *= scalar 235 @nogc ref Matrix opOpAssign(string op, U : T)(U x) pure nothrow if (op == "*") 236 { 237 for (int i = 0; i < R * C; ++i) 238 v[i] *= x; 239 return this; 240 } 241 242 /// Assignment operator with another samey matrix. 243 @nogc ref Matrix opOpAssign(string op, U)(U operand) pure nothrow 244 if (is(U : Matrix) && (op == "*" || op == "+" || op == "-")) 245 { 246 mixin("Matrix result = this " ~ op ~ " operand;"); 247 return opAssign!Matrix(result); 248 } 249 250 /// Matrix += <something convertible to a Matrix> 251 /// Matrix -= <something convertible to a Matrix> 252 @nogc ref Matrix opOpAssign(string op, U)(U operand) pure nothrow 253 if ((isConvertible!U) && (op == "*" || op == "+" || op == "-")) 254 { 255 Matrix conv = operand; 256 return opOpAssign!op(conv); 257 } 258 259 /// Cast to other matrix types. 260 /// If the size are different, the resulting matrix is truncated 261 /// and/or filled with identity coefficients. 262 @nogc U opCast(U)() pure const nothrow if (isMatrixInstantiation!U) 263 { 264 U res = U.identity(); 265 enum minR = R < U._R ? R : U._R; 266 enum minC = C < U._C ? C : U._C; 267 for (int i = 0; i < minR; ++i) 268 for (int j = 0; j < minC; ++j) 269 { 270 res.c[i][j] = cast(U._T)(c[i][j]); 271 } 272 return res; 273 } 274 275 @nogc bool opEquals(U)(U other) pure const nothrow if (is(U : Matrix)) 276 { 277 for (int i = 0; i < R * C; ++i) 278 if (v[i] != other.v[i]) 279 return false; 280 return true; 281 } 282 283 @nogc bool opEquals(U)(U other) pure const nothrow 284 if ((isAssignable!U) && (!is(U: Matrix))) 285 { 286 Matrix conv = other; 287 return opEquals(conv); 288 } 289 290 // +matrix, -matrix, ~matrix, !matrix 291 @nogc Matrix opUnary(string op)() pure const nothrow if (op == "+" || op == "-" || op == "~" || op == "!") 292 { 293 Matrix res = void; 294 for (int i = 0; i < N; ++i) 295 mixin("res.v[i] = " ~ op ~ "v[i];"); 296 return res; 297 } 298 299 /// Convert 3x3 rotation matrix to quaternion. 300 /// See_also: 3D Math Primer for Graphics and Game Development. 301 @nogc U opCast(U)() pure const nothrow if (isQuaternionInstantiation!U 302 && is(U._T : _T) 303 && (_R == 3) && (_C == 3)) 304 { 305 T fourXSquaredMinus1 = c[0][0] - c[1][1] - c[2][2]; 306 T fourYSquaredMinus1 = c[1][1] - c[0][0] - c[2][2]; 307 T fourZSquaredMinus1 = c[2][2] - c[0][0] - c[1][1]; 308 T fourWSquaredMinus1 = c[0][0] + c[1][1] + c[2][2]; 309 310 int biggestIndex = 0; 311 T fourBiggestSquaredMinus1 = fourWSquaredMinus1; 312 313 if(fourXSquaredMinus1 > fourBiggestSquaredMinus1) 314 { 315 fourBiggestSquaredMinus1 = fourXSquaredMinus1; 316 biggestIndex = 1; 317 } 318 319 if(fourYSquaredMinus1 > fourBiggestSquaredMinus1) 320 { 321 fourBiggestSquaredMinus1 = fourYSquaredMinus1; 322 biggestIndex = 2; 323 } 324 325 if(fourZSquaredMinus1 > fourBiggestSquaredMinus1) 326 { 327 fourBiggestSquaredMinus1 = fourZSquaredMinus1; 328 biggestIndex = 3; 329 } 330 331 T biggestVal = sqrt(fourBiggestSquaredMinus1 + 1) / 2; 332 T mult = 1 / (biggestVal * 4); 333 334 U quat; 335 switch(biggestIndex) 336 { 337 case 1: 338 quat.w = (c[1][2] - c[2][1]) * mult; 339 quat.x = biggestVal; 340 quat.y = (c[0][1] + c[1][0]) * mult; 341 quat.z = (c[2][0] + c[0][2]) * mult; 342 break; 343 344 case 2: 345 quat.w = (c[2][0] - c[0][2]) * mult; 346 quat.x = (c[0][1] + c[1][0]) * mult; 347 quat.y = biggestVal; 348 quat.z = (c[1][2] + c[2][1]) * mult; 349 break; 350 351 case 3: 352 quat.w = (c[0][1] - c[1][0]) * mult; 353 quat.x = (c[2][0] + c[0][2]) * mult; 354 quat.y = (c[1][2] + c[2][1]) * mult; 355 quat.z = biggestVal; 356 break; 357 358 default: // biggestIndex == 0 359 quat.w = biggestVal; 360 quat.x = (c[1][2] - c[2][1]) * mult; 361 quat.y = (c[2][0] - c[0][2]) * mult; 362 quat.z = (c[0][1] - c[1][0]) * mult; 363 break; 364 } 365 366 return quat; 367 } 368 369 /// Converts a 4x4 rotation matrix to quaternion. 370 @nogc U opCast(U)() pure const nothrow if (isQuaternionInstantiation!U 371 && is(U._T : _T) 372 && (_R == 4) && (_C == 4)) 373 { 374 auto m3 = cast(mat3!T)(this); 375 return cast(U)(m3); 376 } 377 378 static if (isSquare && isFloatingPoint!T && R == 1) 379 { 380 /// Returns an inverted copy of this matrix 381 /// Returns: inverse of matrix. 382 /// Note: Matrix inversion is provided for 1x1, 2x2, 3x3 and 4x4 floating point matrices. 383 @nogc Matrix inverse() pure const nothrow 384 { 385 return Matrix( 1 / c[0][0]); 386 } 387 } 388 389 static if (isSquare && isFloatingPoint!T && R == 2) 390 { 391 /// Returns an inverted copy of this matrix 392 /// Returns: inverse of matrix. 393 /// Note: Matrix inversion is provided for 1x1, 2x2, 3x3 and 4x4 floating point matrices. 394 @nogc Matrix inverse() pure const nothrow 395 { 396 T invDet = 1 / (c[0][0] * c[1][1] - c[0][1] * c[1][0]); 397 return Matrix( c[1][1] * invDet, -c[0][1] * invDet, 398 -c[1][0] * invDet, c[0][0] * invDet); 399 } 400 } 401 402 static if (isSquare && isFloatingPoint!T && R == 3) 403 { 404 /// Returns an inverted copy of this matrix 405 /// Returns: inverse of matrix. 406 /// Note: Matrix inversion is provided for 1x1, 2x2, 3x3 and 4x4 floating point matrices. 407 @nogc Matrix inverse() pure const nothrow 408 { 409 T det = c[0][0] * (c[1][1] * c[2][2] - c[2][1] * c[1][2]) 410 - c[0][1] * (c[1][0] * c[2][2] - c[1][2] * c[2][0]) 411 + c[0][2] * (c[1][0] * c[2][1] - c[1][1] * c[2][0]); 412 T invDet = 1 / det; 413 414 Matrix res = void; 415 res.c[0][0] = (c[1][1] * c[2][2] - c[2][1] * c[1][2]) * invDet; 416 res.c[0][1] = -(c[0][1] * c[2][2] - c[0][2] * c[2][1]) * invDet; 417 res.c[0][2] = (c[0][1] * c[1][2] - c[0][2] * c[1][1]) * invDet; 418 res.c[1][0] = -(c[1][0] * c[2][2] - c[1][2] * c[2][0]) * invDet; 419 res.c[1][1] = (c[0][0] * c[2][2] - c[0][2] * c[2][0]) * invDet; 420 res.c[1][2] = -(c[0][0] * c[1][2] - c[1][0] * c[0][2]) * invDet; 421 res.c[2][0] = (c[1][0] * c[2][1] - c[2][0] * c[1][1]) * invDet; 422 res.c[2][1] = -(c[0][0] * c[2][1] - c[2][0] * c[0][1]) * invDet; 423 res.c[2][2] = (c[0][0] * c[1][1] - c[1][0] * c[0][1]) * invDet; 424 return res; 425 } 426 } 427 428 static if (isSquare && isFloatingPoint!T && R == 4) 429 { 430 /// Returns an inverted copy of this matrix 431 /// Returns: inverse of matrix. 432 /// Note: Matrix inversion is provided for 1x1, 2x2, 3x3 and 4x4 floating point matrices. 433 @nogc Matrix inverse() pure const nothrow 434 { 435 T det2_01_01 = c[0][0] * c[1][1] - c[0][1] * c[1][0]; 436 T det2_01_02 = c[0][0] * c[1][2] - c[0][2] * c[1][0]; 437 T det2_01_03 = c[0][0] * c[1][3] - c[0][3] * c[1][0]; 438 T det2_01_12 = c[0][1] * c[1][2] - c[0][2] * c[1][1]; 439 T det2_01_13 = c[0][1] * c[1][3] - c[0][3] * c[1][1]; 440 T det2_01_23 = c[0][2] * c[1][3] - c[0][3] * c[1][2]; 441 442 T det3_201_012 = c[2][0] * det2_01_12 - c[2][1] * det2_01_02 + c[2][2] * det2_01_01; 443 T det3_201_013 = c[2][0] * det2_01_13 - c[2][1] * det2_01_03 + c[2][3] * det2_01_01; 444 T det3_201_023 = c[2][0] * det2_01_23 - c[2][2] * det2_01_03 + c[2][3] * det2_01_02; 445 T det3_201_123 = c[2][1] * det2_01_23 - c[2][2] * det2_01_13 + c[2][3] * det2_01_12; 446 447 T det = - det3_201_123 * c[3][0] + det3_201_023 * c[3][1] - det3_201_013 * c[3][2] + det3_201_012 * c[3][3]; 448 T invDet = 1 / det; 449 450 T det2_03_01 = c[0][0] * c[3][1] - c[0][1] * c[3][0]; 451 T det2_03_02 = c[0][0] * c[3][2] - c[0][2] * c[3][0]; 452 T det2_03_03 = c[0][0] * c[3][3] - c[0][3] * c[3][0]; 453 T det2_03_12 = c[0][1] * c[3][2] - c[0][2] * c[3][1]; 454 T det2_03_13 = c[0][1] * c[3][3] - c[0][3] * c[3][1]; 455 T det2_03_23 = c[0][2] * c[3][3] - c[0][3] * c[3][2]; 456 T det2_13_01 = c[1][0] * c[3][1] - c[1][1] * c[3][0]; 457 T det2_13_02 = c[1][0] * c[3][2] - c[1][2] * c[3][0]; 458 T det2_13_03 = c[1][0] * c[3][3] - c[1][3] * c[3][0]; 459 T det2_13_12 = c[1][1] * c[3][2] - c[1][2] * c[3][1]; 460 T det2_13_13 = c[1][1] * c[3][3] - c[1][3] * c[3][1]; 461 T det2_13_23 = c[1][2] * c[3][3] - c[1][3] * c[3][2]; 462 463 T det3_203_012 = c[2][0] * det2_03_12 - c[2][1] * det2_03_02 + c[2][2] * det2_03_01; 464 T det3_203_013 = c[2][0] * det2_03_13 - c[2][1] * det2_03_03 + c[2][3] * det2_03_01; 465 T det3_203_023 = c[2][0] * det2_03_23 - c[2][2] * det2_03_03 + c[2][3] * det2_03_02; 466 T det3_203_123 = c[2][1] * det2_03_23 - c[2][2] * det2_03_13 + c[2][3] * det2_03_12; 467 468 T det3_213_012 = c[2][0] * det2_13_12 - c[2][1] * det2_13_02 + c[2][2] * det2_13_01; 469 T det3_213_013 = c[2][0] * det2_13_13 - c[2][1] * det2_13_03 + c[2][3] * det2_13_01; 470 T det3_213_023 = c[2][0] * det2_13_23 - c[2][2] * det2_13_03 + c[2][3] * det2_13_02; 471 T det3_213_123 = c[2][1] * det2_13_23 - c[2][2] * det2_13_13 + c[2][3] * det2_13_12; 472 473 T det3_301_012 = c[3][0] * det2_01_12 - c[3][1] * det2_01_02 + c[3][2] * det2_01_01; 474 T det3_301_013 = c[3][0] * det2_01_13 - c[3][1] * det2_01_03 + c[3][3] * det2_01_01; 475 T det3_301_023 = c[3][0] * det2_01_23 - c[3][2] * det2_01_03 + c[3][3] * det2_01_02; 476 T det3_301_123 = c[3][1] * det2_01_23 - c[3][2] * det2_01_13 + c[3][3] * det2_01_12; 477 478 Matrix res = void; 479 res.c[0][0] = - det3_213_123 * invDet; 480 res.c[1][0] = + det3_213_023 * invDet; 481 res.c[2][0] = - det3_213_013 * invDet; 482 res.c[3][0] = + det3_213_012 * invDet; 483 484 res.c[0][1] = + det3_203_123 * invDet; 485 res.c[1][1] = - det3_203_023 * invDet; 486 res.c[2][1] = + det3_203_013 * invDet; 487 res.c[3][1] = - det3_203_012 * invDet; 488 489 res.c[0][2] = + det3_301_123 * invDet; 490 res.c[1][2] = - det3_301_023 * invDet; 491 res.c[2][2] = + det3_301_013 * invDet; 492 res.c[3][2] = - det3_301_012 * invDet; 493 494 res.c[0][3] = - det3_201_123 * invDet; 495 res.c[1][3] = + det3_201_023 * invDet; 496 res.c[2][3] = - det3_201_013 * invDet; 497 res.c[3][3] = + det3_201_012 * invDet; 498 return res; 499 } 500 } 501 502 /// Returns a transposed copy of this matrix 503 /// Returns: transposed matrice. 504 @nogc Matrix!(T, C, R) transposed() pure const nothrow 505 { 506 Matrix!(T, C, R) res; 507 for (int i = 0; i < C; ++i) 508 for (int j = 0; j < R; ++j) 509 res.c[i][j] = c[j][i]; 510 return res; 511 } 512 513 static if (isSquare && R > 1) 514 { 515 /// Makes a diagonal matrix from a vector. 516 @nogc static Matrix diag(Vector!(T, R) v) pure nothrow 517 { 518 Matrix res = void; 519 for (int i = 0; i < R; ++i) 520 for (int j = 0; j < C; ++j) 521 res.c[i][j] = (i == j) ? v.v[i] : 0; 522 return res; 523 } 524 525 /// In-place translate by (v, 1) 526 @nogc void translate(Vector!(T, R-1) v) pure nothrow 527 { 528 for (int i = 0; i < R; ++i) 529 { 530 T dot = 0; 531 for (int j = 0; j + 1 < C; ++j) 532 dot += v.v[j] * c[i][j]; 533 534 c[i][C-1] += dot; 535 } 536 } 537 538 /// Make a translation matrix. 539 @nogc static Matrix translation(Vector!(T, R-1) v) pure nothrow 540 { 541 Matrix res = identity(); 542 for (int i = 0; i + 1 < R; ++i) 543 res.c[i][C-1] += v.v[i]; 544 return res; 545 } 546 547 /// In-place matrix scaling. 548 void scale(Vector!(T, R-1) v) pure nothrow 549 { 550 for (int i = 0; i < R; ++i) 551 for (int j = 0; j + 1 < C; ++j) 552 c[i][j] *= v.v[j]; 553 } 554 555 /// Make a scaling matrix. 556 @nogc static Matrix scaling(Vector!(T, R-1) v) pure nothrow 557 { 558 Matrix res = identity(); 559 for (int i = 0; i + 1 < R; ++i) 560 res.c[i][i] = v.v[i]; 561 return res; 562 } 563 } 564 565 // rotations are implemented for 3x3 and 4x4 matrices. 566 static if (isSquare && (R == 3 || R == 4) && isFloatingPoint!T) 567 { 568 @nogc public static Matrix rotateAxis(int i, int j)(T angle) pure nothrow 569 { 570 Matrix res = identity(); 571 const T cosa = cos(angle); 572 const T sina = sin(angle); 573 res.c[i][i] = cosa; 574 res.c[i][j] = -sina; 575 res.c[j][i] = sina; 576 res.c[j][j] = cosa; 577 return res; 578 } 579 580 /// Rotate along X axis 581 /// Returns: rotation matrix along axis X 582 alias rotateAxis!(1, 2) rotateX; 583 584 /// Rotate along Y axis 585 /// Returns: rotation matrix along axis Y 586 alias rotateAxis!(2, 0) rotateY; 587 588 /// Rotate along Z axis 589 /// Returns: rotation matrix along axis Z 590 alias rotateAxis!(0, 1) rotateZ; 591 592 /// Similar to the glRotate matrix, however the angle is expressed in radians 593 /// See_also: $(LINK http://www.cs.rutgers.edu/~decarlo/428/gl_man/rotate.html) 594 @nogc static Matrix rotation(T angle, vec3!T axis) pure nothrow 595 { 596 Matrix res = identity(); 597 const T c = cos(angle); 598 const oneMinusC = 1 - c; 599 const T s = sin(angle); 600 axis = axis.normalized(); 601 T x = axis.x, 602 y = axis.y, 603 z = axis.z; 604 T xy = x * y, 605 yz = y * z, 606 xz = x * z; 607 608 res.c[0][0] = x * x * oneMinusC + c; 609 res.c[0][1] = x * y * oneMinusC - z * s; 610 res.c[0][2] = x * z * oneMinusC + y * s; 611 res.c[1][0] = y * x * oneMinusC + z * s; 612 res.c[1][1] = y * y * oneMinusC + c; 613 res.c[1][2] = y * z * oneMinusC - x * s; 614 res.c[2][0] = z * x * oneMinusC - y * s; 615 res.c[2][1] = z * y * oneMinusC + x * s; 616 res.c[2][2] = z * z * oneMinusC + c; 617 return res; 618 } 619 } 620 621 // 4x4 specific transformations for 3D usage 622 static if (isSquare && R == 4 && isFloatingPoint!T) 623 { 624 /// Orthographic projection 625 /// Returns: orthographic projection. 626 @nogc static Matrix orthographic(T left, T right, T bottom, T top, T near, T far) pure nothrow 627 { 628 T dx = right - left, 629 dy = top - bottom, 630 dz = far - near; 631 632 T tx = -(right + left) / dx; 633 T ty = -(top + bottom) / dy; 634 T tz = -(far + near) / dz; 635 636 return Matrix(2 / dx, 0, 0, tx, 637 0, 2 / dy, 0, ty, 638 0, 0, -2 / dz, tz, 639 0, 0, 0, 1); 640 } 641 642 /// Perspective projection 643 /// Returns: perspective projection. 644 @nogc static Matrix perspective(T FOVInRadians, T aspect, T zNear, T zFar) pure nothrow 645 { 646 T f = 1 / tan(FOVInRadians / 2); 647 T d = 1 / (zNear - zFar); 648 649 return Matrix(f / aspect, 0, 0, 0, 650 0, f, 0, 0, 651 0, 0, (zFar + zNear) * d, 2 * d * zFar * zNear, 652 0, 0, -1, 0); 653 } 654 655 /// Look At projection 656 /// Returns: "lookAt" projection. 657 /// Thanks to vuaru for corrections. 658 @nogc static Matrix lookAt(vec3!T eye, vec3!T target, vec3!T up) pure nothrow 659 { 660 vec3!T Z = (eye - target).normalized(); 661 vec3!T X = cross(-up, Z).normalized(); 662 vec3!T Y = cross(Z, -X); 663 664 return Matrix(-X.x, -X.y, -X.z, dot(X, eye), 665 Y.x, Y.y, Y.z, -dot(Y, eye), 666 Z.x, Z.y, Z.z, -dot(Z, eye), 667 0, 0, 0, 1); 668 } 669 670 /// Extract frustum from a 4x4 matrice. 671 @nogc Frustum!T frustum() pure const nothrow 672 { 673 auto left = Plane!T(row(3) + row(0)); 674 auto right = Plane!T(row(3) - row(0)); 675 auto top = Plane!T(row(3) - row(1)); 676 auto bottom = Plane!T(row(3) + row(1)); 677 auto near = Plane!T(row(3) + row(2)); 678 auto far = Plane!T(row(3) - row(2)); 679 return Frustum!T(left, right, top, bottom, near, far); 680 } 681 682 } 683 } 684 685 package 686 { 687 alias T _T; 688 enum _R = R; 689 enum _C = C; 690 } 691 692 private 693 { 694 template isAssignable(T) 695 { 696 enum bool isAssignable = std.traits.isAssignable!(Matrix, T); 697 } 698 699 template isConvertible(T) 700 { 701 enum bool isConvertible = (!is(T : Matrix)) && isAssignable!T; 702 } 703 704 template isTAssignable(U) 705 { 706 enum bool isTAssignable = std.traits.isAssignable!(T, U); 707 } 708 709 template isRowConvertible(U) 710 { 711 enum bool isRowConvertible = is(U : row_t); 712 } 713 714 template isColumnConvertible(U) 715 { 716 enum bool isColumnConvertible = is(U : column_t); 717 } 718 } 719 720 public 721 { 722 /// Construct an identity matrix 723 /// Returns: an identity matrix. 724 /// Note: the identity matrix, while only meaningful for square matrices, 725 /// is also defined for non-square ones. 726 @nogc static Matrix identity() pure nothrow 727 { 728 Matrix res = void; 729 for (int i = 0; i < R; ++i) 730 for (int j = 0; j < C; ++j) 731 res.c[i][j] = (i == j) ? 1 : 0; 732 return res; 733 } 734 735 /// Construct an constant matrix 736 /// Returns: a constant matrice. 737 @nogc static Matrix constant(U)(U x) pure nothrow 738 { 739 Matrix res = void; 740 741 for (int i = 0; i < R * C; ++i) 742 res.v[i] = cast(T)x; 743 return res; 744 } 745 } 746 } 747 748 template isMatrixInstantiation(U) 749 { 750 private static void isMatrix(T, int R, int C)(Matrix!(T, R, C) x) 751 { 752 } 753 754 enum bool isMatrixInstantiation = is(typeof(isMatrix(U.init))); 755 } 756 757 // GLSL is a big inspiration here 758 // we defines types with more or less the same names 759 760 /// 761 template mat2x2(T) { alias Matrix!(T, 2, 2) mat2x2; } 762 /// 763 template mat3x3(T) { alias Matrix!(T, 3, 3) mat3x3; } 764 /// 765 template mat4x4(T) { alias Matrix!(T, 4, 4) mat4x4; } 766 767 // WARNING: in GLSL, first number is _columns_, second is rows 768 // It is the opposite here: first number is rows, second is columns 769 // With this convention mat2x3 * mat3x4 -> mat2x4. 770 771 /// 772 template mat2x3(T) { alias Matrix!(T, 2, 3) mat2x3; } 773 /// 774 template mat2x4(T) { alias Matrix!(T, 2, 4) mat2x4; } 775 /// 776 template mat3x2(T) { alias Matrix!(T, 3, 2) mat3x2; } 777 /// 778 template mat3x4(T) { alias Matrix!(T, 3, 4) mat3x4; } 779 /// 780 template mat4x2(T) { alias Matrix!(T, 4, 2) mat4x2; } 781 /// 782 template mat4x3(T) { alias Matrix!(T, 4, 3) mat4x3; } 783 784 // shorter names for most common matrices 785 alias mat2x2 mat2;/// 786 alias mat3x3 mat3;/// 787 alias mat4x4 mat4;/// 788 789 // Define a lot of type names 790 // Most useful are probably mat4f and mat4d 791 792 alias mat2!float mat2f;/// 793 alias mat2!double mat2d;/// 794 795 alias mat3!float mat3f;/// 796 alias mat3!double mat3d;/// 797 798 alias mat4!float mat4f;/// 799 alias mat4!double mat4d;/// 800 801 alias mat2x2!float mat2x2f;/// 802 alias mat2x2!double mat2x2d;/// 803 804 alias mat3x3!float mat3x3f;/// 805 alias mat3x3!double mat3x3d;/// 806 807 alias mat4x4!float mat4x4f;/// 808 alias mat4x4!double mat4x4d;/// 809 810 unittest 811 { 812 alias mat2i = mat2!int; 813 alias mat2x3f = mat2x3!float; 814 alias mat3x4f = mat3x4!float; 815 alias mat2x4f = mat2x4!float; 816 817 mat2i x = mat2i(0, 1, 818 2, 3); 819 assert(x.c[0][0] == 0 && x.c[0][1] == 1 && x.c[1][0] == 2 && x.c[1][1] == 3); 820 821 vec2i[2] cols = [vec2i(0, 2), vec2i(1, 3)]; 822 mat2i y = mat2i.fromColumns(cols[]); 823 assert(y.c[0][0] == 0 && y.c[0][1] == 1 && y.c[1][0] == 2 && y.c[1][1] == 3); 824 y = mat2i.fromRows(cols[]); 825 assert(y.c[0][0] == 0 && y.c[1][0] == 1 && y.c[0][1] == 2 && y.c[1][1] == 3); 826 y = y.transposed(); 827 828 assert(x == y); 829 x = [0, 1, 2, 3]; 830 assert(x == y); 831 832 mat2i z = x * y; 833 assert(z == mat2i([2, 3, 6, 11])); 834 vec2i vz = z * vec2i(2, -1); 835 assert(vz == vec2i(1, 1)); 836 837 mat2f a = z; 838 mat2d ad = a; 839 ad += a; 840 mat2f w = [4, 5, 6, 7]; 841 z = cast(mat2i)w; 842 assert(w == z); 843 844 { 845 mat2x3f A; 846 mat3x4f B; 847 mat2x4f C = A * B; 848 } 849 850 assert(mat2i.diag(vec2i(1, 2)) == mat2i(1, 0, 851 0, 2)); 852 853 // Construct with a single scalar 854 auto D = mat4f(1.0f); 855 } 856 857 // Issue #206 (matrix *= scalar) not yielding matrix * scalar but matrix * matrix(scalar) 858 unittest 859 { 860 mat4f mvp = mat4f.identity; 861 mvp *= 2; 862 assert(mvp == mat4f(2, 0, 0, 0, 863 0, 2, 0, 0, 864 0, 0, 2, 0, 865 0, 0, 0, 2)); 866 867 mvp = mat4f.identity * 2; 868 assert(mvp == mat4f(2, 0, 0, 0, 869 0, 2, 0, 0, 870 0, 0, 2, 0, 871 0, 0, 0, 2)); 872 873 874 mvp = mat4f(1) * mat4f(1); 875 assert(mvp == mat4f(4, 4, 4, 4, 876 4, 4, 4, 4, 877 4, 4, 4, 4, 878 4, 4, 4, 4)); 879 880 mvp = mat4f(1); 881 mvp *= mat4f(1); 882 assert(mvp == mat4f(4, 4, 4, 4, 883 4, 4, 4, 4, 884 4, 4, 4, 4, 885 4, 4, 4, 4)); 886 }