开发者

Calculate Quaternion Inverse [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Hi 开发者_如何学Pythoni'm trying to figure out how to calculate the inverse of a quaternion. A code example would be awesome.

Cheers


See Wikipedia article for the entire Quaternion math.

Don't know what language you want to use but I'll try to give some hints in Haskell.

data Quaternion = Q Double Double Double Double deriving (Show, Eq)

First, you need to implement multiplication and addition of quaternions.

instance Num Quaternion where
 (+) = q_plus
 (*) = q_mult
 --....

q_plus (Q a b c d) (Q a' b' c' d') = Q (a + a') (b + b') (c + c') (d + d')
q_mult (Q a b c d) (Q a' b' c' d') = Q a'' b'' c'' d''
  where
    a'' = a * a' - b * b' - c * c' - d * d'
    b'' = a * b' + b * a' + c * d' - d * c'
    c'' = a * c' - b * d' + c * a' + d * b'
    d'' = a * d' + b * c' - c * b' + d * a'

Multiplication with scalar should be done via a conversion:

scalar_to_q a = Q a 0 0 0

Define

i = Q 0 1 0 0
j = Q 0 0 1 0
k = Q 0 0 0 1

Then implement the conjugate and modulus:

q_conjugate q = (scalar_to_q (negate .5)) * (q + i * q * i + j * q * j + k * q * k)
q_modulus q = sqrt $ q * (q_conjugate q)

Now, the inverse:

q_inverse q = (q_conjugate q) * (scalar_to_q (m * m))
  where
    m = q_modulus q

Hope it's useful.

PS: the instance definition above will simplify things a little if completed successfully. I let you fill in the gaps.


Look at the Matrix and Quaternion FAQ. There are some code samples as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜