Created
August 31, 2012 22:51
-
-
Save DannyArends/3560459 to your computer and use it in GitHub Desktop.
Code for my second blog post on CTFE in D 2/0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module rotationmatrices; | |
import std.stdio; | |
import cordicalgorithm; | |
struct mat(T = float){ | |
T[][] data; | |
pure mat!T opBinary(string op)(mat!T rhs){ | |
static if (op == "*" || op == "+" || op == "-"){ | |
return mat!T(mmOp!(T, op)(data, rhs.data)); | |
}else static assert(0, "Operator "~op~" not implemented"); | |
} | |
pure bool opEquals(ref const mat!T rhs){ | |
equals_t tmp = true; | |
foreach(i; 0..data.length){ | |
foreach(j; 0..data.length){ | |
if(!tmp) return tmp; | |
tmp = tmp && (data[i][j] == rhs.data[i][j]); | |
} | |
} | |
return tmp; | |
} | |
} | |
pure T[][] mmOp(T = float, string op)(in T[][] A, in T[][] B) | |
in{ | |
assert(isFloatingPoint!T, "T must be a FloatingPoint type"); | |
assert(A.length > 0,"A has no length"); | |
assert(B.length > 0,"B has no length"); | |
foreach(i; 0 .. A.length){ | |
assert(A[i].length == B.length, "A[i].length != B.length"); | |
} | |
} | |
body{ | |
T[][] d; | |
T tmp; | |
foreach(i; 0 .. A.length){ | |
T[] row; | |
foreach(j; 0 .. B[i].length){ | |
tmp = 0.0; | |
foreach(k; 0 .. A[i].length){ | |
tmp += mixin("A[i][k] "~op~" B[k][j]"); | |
} | |
row ~= tmp; | |
} | |
d ~= row; | |
} | |
return d; | |
} | |
pure mat!(T)[3][] gen_rotationmatrices(T = float)(){ | |
mat!(T)[3][] result = new mat!(T)[3][](360); | |
T sini,cosi; | |
debug pragma(msg, "Generating rotation matrices"); | |
foreach(i; 0 .. 360){ | |
sini = sin(i); | |
cosi = cos(i); | |
result[i] = [ | |
mat!(T)([[ 1.0, 0.0, 0.0], [ 0.0, cosi, -sini], [ 0.0, sini, cosi]]), | |
mat!(T)([[cosi, 0.0, sini], [ 0.0, 1.0, 0.0], [-sini, 0.0, cosi]]), | |
mat!(T)([[cosi, -sini, 0.0], [sini, cosi, 0.0], [ 0.0, 0.0, 1.0]])]; | |
} | |
debug pragma(msg, "Done rotation matrices"); | |
return result; | |
} | |
immutable rmatrix = gen_rotationmatrices!double(); | |
enum{YAW = 0, PITCH = 1, ROLL = 2}; | |
pure auto yaw(size_t deg){ deg = degreeloop(deg); return cast(matrix)rmatrix[deg][YAW]; } | |
pure auto pitch(size_t deg){ deg = degreeloop(deg); return cast(matrix)rmatrix[deg][PITCH];} | |
pure auto roll(size_t deg){ deg = degreeloop(deg); return cast(matrix)rmatrix[deg][ROLL]; } | |
pure auto rotationmatrix(size_t y,size_t p,size_t r,){ | |
return yaw(y) * pitch(p) * roll(r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment