| 1 |
// COMPILABLE_MATH_TEST |
| 2 |
// https://issues.dlang.org/show_bug.cgi?id=15019 |
| 3 |
// dmd -m32 -c all.d |
| 4 |
|
| 5 |
import std.string; |
| 6 |
|
| 7 |
struct Color() |
| 8 |
{ |
| 9 |
static fromHex(char[] s) |
| 10 |
{ |
| 11 |
import std.conv; |
| 12 |
s.to!ubyte; |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
Color!() RGB ; |
| 17 |
|
| 18 |
struct Matrix(T, int R, int C) |
| 19 |
{ |
| 20 |
Vector!(T, C) row_t; |
| 21 |
T[C] v; // all elements |
| 22 |
|
| 23 |
/// Covnerts to pretty string. |
| 24 |
string toString() const |
| 25 |
{ |
| 26 |
try |
| 27 |
return format("%s", v); |
| 28 |
catch (Throwable) |
| 29 |
assert(false); // should not happen since format is right |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// GLSL is a big inspiration here |
| 34 |
// we defines types with more or less the same names |
| 35 |
template mat2x2(T) { Matrix!(T, 2, 2) mat2x2; } |
| 36 |
template mat3x3(T) { Matrix!(T, 3, 3) mat3x3; } |
| 37 |
template mat4x4(T) { Matrix!(T, 4, 4) mat4x4; } |
| 38 |
|
| 39 |
alias mat2x2 mat2; |
| 40 |
alias mat3x3 mat3; // shorter names for most common matrices |
| 41 |
alias mat4x4 mat4; |
| 42 |
|
| 43 |
string definePostfixAliases(string type) |
| 44 |
{ |
| 45 |
return "alias " ~ type ~ "!byte " ~ type ~ "b;\n" |
| 46 |
~ "alias " ~ type ~ "!ubyte " ~ type ~ "ub;\n" |
| 47 |
~ "alias " ~ type ~ "!short " ~ type ~ "s;\n" |
| 48 |
~ "alias " ~ type ~ "!ushort " ~ type ~ "us;\n" |
| 49 |
~ "alias " ~ type ~ "!int " ~ type ~ "i;\n" |
| 50 |
~ "alias " ~ type ~ "!uint " ~ type ~ "ui;\n" |
| 51 |
~ "alias " ~ type ~ "!long " ~ type ~ "l;\n" |
| 52 |
~ "alias " ~ type ~ "!ulong " ~ type ~ "ul;\n" |
| 53 |
~ "alias " ~ type ~ "!float " ~ type ~ "f;\n" |
| 54 |
~ "alias " ~ type ~ "!double " ~ type ~ "d;\n"; |
| 55 |
} |
| 56 |
|
| 57 |
// define a lot of type names |
| 58 |
mixin(definePostfixAliases("mat2")); |
| 59 |
mixin(definePostfixAliases("mat3")); |
| 60 |
mixin(definePostfixAliases("mat4")); |
| 61 |
import std.string; |
| 62 |
|
| 63 |
struct Vector(T, int N) |
| 64 |
{ |
| 65 |
T[N] v; |
| 66 |
|
| 67 |
string toString() |
| 68 |
{ |
| 69 |
try |
| 70 |
return format("%s", v); |
| 71 |
catch (Throwable) |
| 72 |
assert(false); |
| 73 |
} |
| 74 |
} |