Calculate Quaternion Inverse [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-01 16:37:06
Mihai Maruseac

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!