optemizing rotations

Started by
12 comments, last by nerco666 13 years, 9 months ago
I am wondering if I can simplify my 3d rotation. So I can make it smaller in size. My code is below. This is a translation matrix (x, y, z, t)

float t[3];

t[0] = q[0] * (3.14 / 180);
t[1] = q[1] * (3.14 / 180);
t[2] = q[2] * (3.14 / 180);

float m[12];

m[0] = -sin(t[2]) * sin(t[0]) * cos(t[1]) + -cos(t[2]) * -sin(t[1]);
m[1] = -sin(t[2]) * sin(t[0]) * sin(t[1]) + -cos(t[2]) * cos(t[1]);
m[2] = -sin(t[2]) * cos(t[0]);
m[3] = cos(t[2]) * sin(t[0]) * cos(t[1]) + -sin(t[2]) * -sin(t[1]);
m[4] = cos(t[2]) * sin(t[0]) * sin(t[1]) + -sin(t[2]) * cos(t[1]);
m[5] = cos(t[2]) * cos(t[0]);
m[6] = cos(t[0]) * cos(t[1]);
m[7] = cos(t[0]) * sin(t[1]);
m[8] = -sin(q[0]);
m[9] = a[0] - b[0];
m[10] = a[1] - b[1];
m[11] = a[2] - b[2];
Advertisement
Is this some sort of Euler-angle construction? if so, the best thing to do would probably be just to identify and precompute any common terms (you definitely don't need that many individual calls to sin() and cos() - you should only need 6 such calls, at maximum, for an Euler-angle construction.)
Hey, thanks for the reply. The thing is, all the parts of the rotation are different. So, maybe you could give me a small example?

Thanks,

N
Quote:The thing is, all the parts of the rotation are different.
No they're not. Consider these two lines:
m[0] = -sin(t[2]) * sin(t[0]) * cos(t[1]) + -cos(t[2]) * -sin(t[1]);m[1] = -sin(t[2]) * sin(t[0]) * sin(t[1]) + -cos(t[2]) * cos(t[1]);
First of all, you're computing the same sines and cosines (e.g. sin(t[1])) multiple times, so the first thing to do is to pre-compute these and reuse them (which is what I was saying earlier). For example:
float c0 = cos(t[0]);float s0 = sin(t[0]);float c1 = cos(t[1]);float s1 = sin(t[1]);float c2 = cos(t[2]);float s2 = sin(t[2]);m[0] = -s2 * s0 * c1 + -c2 * -s1;m[1] = -s2 * s0 * s1 + -c2 * c1;
Any optimization beyond that is probably unnecessary, but if you really want to, you can then identify and pre-compute common terms, e.g.:
float s0s2 = s0 * s2;m[0] = -s0s2 * c1 + -c2 * -s1;m[1] = -s0s2 * s1 + -c2 * c1;
And so on.
Thanks for the reply man. One more thing, would it be possible to optemize the 3d rotation so its less lines?

Thanks,

N
Quote:One more thing, would it be possible to optemize the 3d rotation so its less lines?
I'm not sure how you would; with that type of construction, one way or another you need to assign a value to each of the relevant matrix elements, which means having a certain number of statements. (You could probably make the function *look* shorter by building individual rotation matrices using other functions and then concatenating the matrices, but you'd actually be doing more work overall.)

Why do you want the function to be shorter?
Quote:Original post by nerco666
Would it be possible to optemize the 3d rotation so its less lines?
Oh, sure. Check it out:
float t[3];t[0] = q[0] * (3.14 / 180);t[1] = q[1] * (3.14 / 180);t[2] = q[2] * (3.14 / 180);float m[12];m[0]=-sin(t[2]) * sin(t[0]) * cos(t[1]) + -cos(t[2]) * -sin(t[1]);m[1]= -sin(t[2]) * sin(t[0]) * sin(t[1]) + -cos(t[2]) * cos(t[1]);m[2] = -sin(t[2]) * cos(t[0]);m[3] = cos(t[2]) * sin(t[0]) * cos(t[1]) + -sin(t[2]) * -sin(t[1]);m[4] = cos(t[2])*sin(t[0]) * sin(t[1]) + -sin(t[2]) * cos(t[1]);m[5] = cos(t[2]) * cos(t[0]);m[6] = cos(t[0]) * cos(t[1]);m[7] = cos(t[0]) * sin(t[1]);m[8] = -sin(q[0]);m[9] = a[0] - b[0];m[10] = a[1] - b[1];m[11] = a[2] - b[2];
Of course, that doesn't make it any faster. Remember, your code isn't executed line-by-line. It's compiled to a machine language representation, and the resultant sequence of instructions will often look very little like your original code. Optimization is a very large topic, and it's difficult to give hard-and-fast rules, but line count is only a very distant measure of how fast your code will be. Making your code perform fewer operations is considerably more important. Even more important than that, though, is picking good algorithms. Chances are you can double the speed of that code, simply by calling it only half as often.
Quote:Original post by Sneftel
[...] Even more important than that, though, is picking good algorithms.


Amen.

In most of the situations where people think they need angles, it turns out they just need linear algebra. The angle-less code is often easier to read, easier to get right and faster to execute.

Thanks for the replies guys. Since I am no math guru, I would like to ask if there is maybe a better way to acomplish 3d rotation. I did some google searching and most of the algorithms were pretty much just rotation matrices..
Rotation matrices are fine. Sometimes quaternions are even better (if you need to interpolate between positions, in particular).

Why are you building the rotation from Euler angles to begin with? Where are those coming from?

This topic is closed to new replies.

Advertisement