Efficient way to calculate a 3x3 rotation matrix from the rotation defined by two 3D Vectors

Both the methods you've posted can be optimised.

Method 1

Instead of using acos to find the angle between the two vectors, a better thing to do is to avoid finding the angle at all. How? The axis-angle formula by Rodrigues requires only sin θ, cos θ and 1 - cos θ, so finding the actual angle is redundant.

We know that v1 and v2 are unit vectors; v1 · v2 = |v1| |v2| cos θ since |v1| = |v2| = 1, v1 · v2 directly gives us cos θ, finding 1 - cos θ isn't costly. v1 × v2 = |v1| |v2| sin θ n = sin θ n, where n is a unit vector perpendicular to v1 and v2, finding |v1 × v2| the magnitude of the cross product would directly give sin θ.

Now that we've sin θ and cos θ, we can directly form the rotation matrix by using Rodrigues forumla; here's a simple implementation (though page claims to use Quaternion math, it's the Axis-Angle to Matrix conversion formula).

Method 2

After you've constructed two orthonormal frames as matrices, you can avoid the second transpose you do. Here's the proof.

Say A and B be the two matrices, since you want to rotate from A to B we need some matrix X which when multiplied with A will give B:

XA = B

X = BA⁻¹

This is all you need; when you pre-multiply X to A you'd get B. However, what you find is Y

Y = AB⁻¹

YB = A

Then you transpose Y to get Y⁻¹ i.e.

Y⁻¹YB = Y⁻¹A

B = Y⁻¹A

Instead of doing two inverses (transpose here), you can just do the above method which involves only one transpose.

I'd still say that without benchmarking the methods in their optimized forms, we cannot say method 2 is faster than method 1. So I'd really urge you to benchmark between the two methods (with some non-trivial load) and then draw a conclusion.

Tags:

Math

Matrix