| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE MonoLocalBinds #-} |
| 3 |
{-# LANGUAGE TypeFamilies #-} |
| 4 |
|
| 5 |
----------------------------------------------------------------------------- |
| 6 |
-- | |
| 7 |
-- Module : Diagrams.Transform.Matrix |
| 8 |
-- Copyright : (c) 2014 diagrams team (see LICENSE) |
| 9 |
-- License : BSD-style (see LICENSE) |
| 10 |
-- Maintainer : [email protected] |
| 11 |
-- |
| 12 |
-- Functions for converting between 'Transformation's and matricies. |
| 13 |
-- |
| 14 |
----------------------------------------------------------------------------- |
| 15 |
|
| 16 |
module Diagrams.Transform.Matrix where |
| 17 |
|
| 18 |
import Control.Arrow ((&&&)) |
| 19 |
import Control.Lens |
| 20 |
import Data.Distributive |
| 21 |
import qualified Data.Foldable as F |
| 22 |
import Data.Functor.Rep |
| 23 |
|
| 24 |
import Diagrams.Core.Transform as D |
| 25 |
import Diagrams.ThreeD.Types |
| 26 |
import Diagrams.TwoD.Types |
| 27 |
|
| 28 |
import Linear.Matrix |
| 29 |
import Linear.Vector |
| 30 |
|
| 31 |
|
| 32 |
mkMat :: (HasBasis v, Num n) => Transformation v n -> v (v n) |
| 33 |
mkMat t = distribute . tabulate $ apply t . unit . (\x -> el x) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
mkMatHomo :: Num n => Transformation V3 n -> M44 n |
| 38 |
mkMatHomo t = mkTransformationMat (mkMat t) (transl t) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
fromMat22 :: Floating n => M22 n -> V2 n -> T2 n |
| 44 |
fromMat22 m v = fromMatWithInv m (inv22 m) v |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
fromMat33 :: Floating n => M33 n -> V3 n -> T3 n |
| 50 |
fromMat33 m v = fromMatWithInv m (inv33 m) v |
| 51 |
|
| 52 |
|
| 53 |
fromMatWithInv :: (Additive v, Distributive v, F.Foldable v, Num n) |
| 54 |
=> v (v n) |
| 55 |
-> v (v n) |
| 56 |
-> v n |
| 57 |
-> Transformation v n |
| 58 |
fromMatWithInv m m_ v = |
| 59 |
Transformation ((m !*) <-> (m_ !*)) |
| 60 |
((distribute m !*) <-> (distribute m_ !*)) |
| 61 |
v |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
mat22 :: Floating n => Iso' (M22 n, V2 n) (T2 n) |
| 67 |
mat22 = iso (uncurry fromMat22) (mkMat &&& transl) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
mat33 :: Floating n => Iso' (M33 n, V3 n) (T3 n) |
| 73 |
mat33 = iso (uncurry fromMat33) (mkMat &&& transl) |