Files
loop/src/Quaternion.hs
T
2022-08-21 21:23:59 +01:00

48 lines
1.3 KiB
Haskell

{-# LANGUAGE DeriveAnyClass #-}
--{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{- |
WARNING: orphan instances concerning Aeson classes and Linear.Quaternion datatypes have been introduced.
The warnings have been disabled.
-}
module Quaternion (
rotateToZ,
vToQuat,
module Linear.Quaternion,
) where
import Data.Aeson
import Flat
import Geometry.Data
import Geometry.Vector3D
import Linear.Quaternion
import qualified Linear.Quaternion as Q
import LinearHelp ()
instance ToJSON a => ToJSON (Q.Quaternion a) where
toEncoding = genericToEncoding defaultOptions
instance FromJSON a => FromJSON (Q.Quaternion a)
-- apply a rotation as if the z axis moves to the new point.
-- i think this may instead do as if the new point moves to be on z axis
rotateToZ :: Point3 -> Point3 -> Point3
rotateToZ z1
| cprod == V3 0 0 0 = id
| otherwise = Q.rotate $ Q.axisAngle cprod (angleVV3 z1 (V3 0 0 1))
where
cprod = crossProd z1 (V3 0 0 1)
vToQuat :: Point3 -> Point3 -> Q.Quaternion Float
vToQuat a b
| cprod == V3 0 0 0 = Q.axisAngle (V3 0 0 1) 0
| otherwise = Q.axisAngle cprod (angleVV3 a b)
where
cprod = crossProd a b
deriving instance (Flat a => Flat (Quaternion a))