Math Lib: avoid temp array for rotate_m4

No need to have temp array storage, avoid 2x loops.
This commit is contained in:
Campbell Barton 2016-11-25 21:00:32 +11:00
parent e1e49fd1a8
commit bcd0d8584f

@ -1634,39 +1634,33 @@ void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
*/ */
void rotate_m4(float mat[4][4], const char axis, const float angle) void rotate_m4(float mat[4][4], const char axis, const float angle)
{ {
int col; const float angle_cos = cosf(angle);
float temp[4] = {0.0f, 0.0f, 0.0f, 0.0f}; const float angle_sin = sinf(angle);
float cosine, sine;
assert(axis >= 'X' && axis <= 'Z'); assert(axis >= 'X' && axis <= 'Z');
cosine = cosf(angle);
sine = sinf(angle);
switch (axis) { switch (axis) {
case 'X': case 'X':
for (col = 0; col < 4; col++) for (int col = 0; col < 4; col++) {
temp[col] = cosine * mat[1][col] + sine * mat[2][col]; float temp = angle_cos * mat[1][col] + angle_sin * mat[2][col];
for (col = 0; col < 4; col++) { mat[2][col] = -angle_sin * mat[1][col] + angle_cos * mat[2][col];
mat[2][col] = -sine * mat[1][col] + cosine * mat[2][col]; mat[1][col] = temp;
mat[1][col] = temp[col];
} }
break; break;
case 'Y': case 'Y':
for (col = 0; col < 4; col++) for (int col = 0; col < 4; col++) {
temp[col] = cosine * mat[0][col] - sine * mat[2][col]; float temp = angle_cos * mat[0][col] - angle_sin * mat[2][col];
for (col = 0; col < 4; col++) { mat[2][col] = angle_sin * mat[0][col] + angle_cos * mat[2][col];
mat[2][col] = sine * mat[0][col] + cosine * mat[2][col]; mat[0][col] = temp;
mat[0][col] = temp[col];
} }
break; break;
case 'Z': case 'Z':
for (col = 0; col < 4; col++) for (int col = 0; col < 4; col++) {
temp[col] = cosine * mat[0][col] + sine * mat[1][col]; float temp = angle_cos * mat[0][col] + angle_sin * mat[1][col];
for (col = 0; col < 4; col++) { mat[1][col] = -angle_sin * mat[0][col] + angle_cos * mat[1][col];
mat[1][col] = -sine * mat[0][col] + cosine * mat[1][col]; mat[0][col] = temp;
mat[0][col] = temp[col];
} }
break; break;
} }