Debian Codegraph @

src/Diagrams/Transform/Matrix.hs

raw symbols SCIP
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 -- | Build a matrix from a 'Transformation', ignoring the translation.
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 -- | Build a 3D transformation matrix in homogeneous coordinates from
36 -- a 'Transformation V3'.
37 mkMatHomo :: Num n => Transformation V3 n -> M44 n
38 mkMatHomo t = mkTransformationMat (mkMat t) (transl t)
39
40 -- | Make a 2D transformation from a 2x2 transform matrix and a
41 -- translation vector. Does not check if the matrix is not invertible
42 -- (in which case the 'T2' will be invalid).
43 fromMat22 :: Floating n => M22 n -> V2 n -> T2 n
44 fromMat22 m v = fromMatWithInv m (inv22 m) v
45
46 -- | Make a 3D transformation from a 3x3 transform matrix and a
47 -- translation vector. Does not check if the matrix is not invertible
48 -- (in which case the 'T3' will be invalid).
49 fromMat33 :: Floating n => M33 n -> V3 n -> T3 n
50 fromMat33 m v = fromMatWithInv m (inv33 m) v
51
52 -- | Build a transform with a maxtrix along with its inverse.
53 fromMatWithInv :: (Additive v, Distributive v, F.Foldable v, Num n)
54 => v (v n) -- ^ matrix
55 -> v (v n) -- ^ inverse
56 -> v n -- ^ translation
57 -> Transformation v n
58 fromMatWithInv m m_ v =
59 Transformation ((m !*) <-> (m_ !*))
60 ((distribute m !*) <-> (distribute m_ !*))
61 v
62
63 -- | Prism onto a 2D transformation from a 2x2 transform matrix and
64 -- translation vector. Does not check if the matrix is invertible (in
65 -- which case the 'T2' will be invalid).
66 mat22 :: Floating n => Iso' (M22 n, V2 n) (T2 n)
67 mat22 = iso (uncurry fromMat22) (mkMat &&& transl)
68
69 -- | Prism onto a 3D transformation from a 3x3 transform matrix and
70 -- translation vector. Does not check if the matrix is invertible
71 -- (in which case the 'T3' will be invalid).
72 mat33 :: Floating n => Iso' (M33 n, V3 n) (T3 n)
73 mat33 = iso (uncurry fromMat33) (mkMat &&& transl)