Python: Implemented CombineRotation.

This commit is contained in:
Don Cross
2019-12-15 20:29:41 -05:00
parent 8c590e4449
commit 126cc04d1d
4 changed files with 132 additions and 0 deletions

View File

@@ -510,6 +510,25 @@ Otherwise, returns `Body.Invalid`.
---
<a name="CombineRotation"></a>
### CombineRotation(a, b)
**Creates a rotation based on applying one rotation followed by another.**
Given two rotation matrices, returns a combined rotation matrix that is
equivalent to rotating based on the first matrix, followed by the second.
b : RotationMatrix
The second rotation to apply.
| Type | Parameter | Description |
| --- | --- | --- |
| [`RotationMatrix`](#RotationMatrix) | `a` | The first rotation to apply. |
### Returns: RotationMatrix
The combined rotation matrix.
---
<a name="Ecliptic"></a>
### Ecliptic(equ)

View File

@@ -5105,6 +5105,51 @@ def InverseRotation(rotation):
[rotation.rot[0][2], rotation.rot[1][2], rotation.rot[2][2]]
])
def CombineRotation(a, b):
"""Creates a rotation based on applying one rotation followed by another.
Given two rotation matrices, returns a combined rotation matrix that is
equivalent to rotating based on the first matrix, followed by the second.
Parameters
----------
a : RotationMatrix
The first rotation to apply.
b : RotationMatrix
The second rotation to apply.
Returns
-------
RotationMatrix
The combined rotation matrix.
"""
# Use matrix multiplication: c = b*a.
# We put 'b' on the left and 'a' on the right because,
# just like when you use a matrix M to rotate a vector V,
# you put the M on the left in the product M*V.
# We can think of this as 'b' rotating all the 3 column vectors in 'a'.
return RotationMatrix([
[
b.rot[0][0]*a.rot[0][0] + b.rot[1][0]*a.rot[0][1] + b.rot[2][0]*a.rot[0][2],
b.rot[0][1]*a.rot[0][0] + b.rot[1][1]*a.rot[0][1] + b.rot[2][1]*a.rot[0][2],
b.rot[0][2]*a.rot[0][0] + b.rot[1][2]*a.rot[0][1] + b.rot[2][2]*a.rot[0][2]
],
[
b.rot[0][0]*a.rot[1][0] + b.rot[1][0]*a.rot[1][1] + b.rot[2][0]*a.rot[1][2],
b.rot[0][1]*a.rot[1][0] + b.rot[1][1]*a.rot[1][1] + b.rot[2][1]*a.rot[1][2],
b.rot[0][2]*a.rot[1][0] + b.rot[1][2]*a.rot[1][1] + b.rot[2][2]*a.rot[1][2]
],
[
b.rot[0][0]*a.rot[2][0] + b.rot[1][0]*a.rot[2][1] + b.rot[2][0]*a.rot[2][2],
b.rot[0][1]*a.rot[2][0] + b.rot[1][1]*a.rot[2][1] + b.rot[2][1]*a.rot[2][2],
b.rot[0][2]*a.rot[2][0] + b.rot[1][2]*a.rot[2][1] + b.rot[2][2]*a.rot[2][2]
]
])
def RotateVector(rotation, vector):
"""Applies a rotation to a vector, yielding a rotated vector.