{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MonoLocalBinds  #-}
{-# LANGUAGE TypeFamilies    #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Diagrams.Transform.Matrix
-- Copyright   :  (c) 2014 diagrams team (see LICENSE)
-- License     :  BSD-style (see LICENSE)
-- Maintainer  :  diagrams-discuss@googlegroups.com
--
-- Functions for converting between 'Transformation's and matricies.
--
-----------------------------------------------------------------------------

module Diagrams.Transform.Matrix where

import           Control.Arrow           ((&&&))
import           Control.Lens
import           Data.Distributive
import qualified Data.Foldable           as F
import           Data.Functor.Rep

import           Diagrams.Core.Transform as D
import           Diagrams.ThreeD.Types
import           Diagrams.TwoD.Types

import           Linear.Matrix
import           Linear.Vector

-- | Build a matrix from a 'Transformation', ignoring the translation.
mkMat :: (HasBasis v, Num n) => Transformation v n -> v (v n)
mkMat :: forall (v :: * -> *) n.
(HasBasis v, Num n) =>
Transformation v n -> v (v n)
mkMat Transformation v n
t = forall (g :: * -> *) (f :: * -> *) a.
(Distributive g, Functor f) =>
f (g a) -> g (f a)
distribute forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Representable f => (Rep f -> a) -> f a
tabulate forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) n. Transformation v n -> v n -> v n
apply Transformation v n
t forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a.
(Additive t, Num a) =>
ASetter' (t a) a -> t a
unit forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\E v
x -> forall (t :: * -> *). E t -> forall x. Lens' (t x) x
el E v
x)

-- | Build a 3D transformation matrix in homogeneous coordinates from
--   a 'Transformation V3'.
mkMatHomo :: Num n => Transformation V3 n -> M44 n
mkMatHomo :: forall n. Num n => Transformation V3 n -> M44 n
mkMatHomo Transformation V3 n
t = forall a. Num a => M33 a -> V3 a -> M44 a
mkTransformationMat (forall (v :: * -> *) n.
(HasBasis v, Num n) =>
Transformation v n -> v (v n)
mkMat Transformation V3 n
t) (forall (v :: * -> *) n. Transformation v n -> v n
transl Transformation V3 n
t)

-- | Make a 2D transformation from a 2x2 transform matrix and a
--   translation vector. Does not check if the matrix is not invertible
--   (in which case the 'T2' will be invalid).
fromMat22 :: Floating n => M22 n -> V2 n -> T2 n
fromMat22 :: forall n. Floating n => M22 n -> V2 n -> T2 n
fromMat22 M22 n
m V2 n
v = forall (v :: * -> *) n.
(Additive v, Distributive v, Foldable v, Num n) =>
v (v n) -> v (v n) -> v n -> Transformation v n
fromMatWithInv M22 n
m (forall a. Fractional a => M22 a -> M22 a
inv22 M22 n
m) V2 n
v

-- | Make a 3D transformation from a 3x3 transform matrix and a
--   translation vector. Does not check if the matrix is not invertible
--   (in which case the 'T3' will be invalid).
fromMat33 :: Floating n => M33 n -> V3 n -> T3 n
fromMat33 :: forall n. Floating n => M33 n -> V3 n -> T3 n
fromMat33 M33 n
m V3 n
v = forall (v :: * -> *) n.
(Additive v, Distributive v, Foldable v, Num n) =>
v (v n) -> v (v n) -> v n -> Transformation v n
fromMatWithInv M33 n
m (forall a. Fractional a => M33 a -> M33 a
inv33 M33 n
m) V3 n
v

-- | Build a transform with a maxtrix along with its inverse.
fromMatWithInv :: (Additive v, Distributive v, F.Foldable v, Num n)
  => v (v n) -- ^ matrix
  -> v (v n) -- ^ inverse
  -> v n     -- ^ translation
  -> Transformation v n
fromMatWithInv :: forall (v :: * -> *) n.
(Additive v, Distributive v, Foldable v, Num n) =>
v (v n) -> v (v n) -> v n -> Transformation v n
fromMatWithInv v (v n)
m v (v n)
m_ v n
v =
  forall (v :: * -> *) n.
(v n :-: v n) -> (v n :-: v n) -> v n -> Transformation v n
Transformation ((v (v n)
m forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Foldable r, Additive r, Num a) =>
m (r a) -> r a -> m a
!*)            forall u v. (u -> v) -> (v -> u) -> u :-: v
<-> (v (v n)
m_ forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Foldable r, Additive r, Num a) =>
m (r a) -> r a -> m a
!*))
                 ((forall (g :: * -> *) (f :: * -> *) a.
(Distributive g, Functor f) =>
f (g a) -> g (f a)
distribute v (v n)
m forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Foldable r, Additive r, Num a) =>
m (r a) -> r a -> m a
!*) forall u v. (u -> v) -> (v -> u) -> u :-: v
<-> (forall (g :: * -> *) (f :: * -> *) a.
(Distributive g, Functor f) =>
f (g a) -> g (f a)
distribute v (v n)
m_ forall (m :: * -> *) (r :: * -> *) a.
(Functor m, Foldable r, Additive r, Num a) =>
m (r a) -> r a -> m a
!*))
                 v n
v

-- | Prism onto a 2D transformation from a 2x2 transform matrix and
--   translation vector. Does not check if the matrix is invertible (in
--   which case the 'T2' will be invalid).
mat22 :: Floating n => Iso' (M22 n, V2 n) (T2 n)
mat22 :: forall n. Floating n => Iso' (M22 n, V2 n) (T2 n)
mat22 = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall n. Floating n => M22 n -> V2 n -> T2 n
fromMat22) (forall (v :: * -> *) n.
(HasBasis v, Num n) =>
Transformation v n -> v (v n)
mkMat forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall (v :: * -> *) n. Transformation v n -> v n
transl)

-- | Prism onto a 3D transformation from a 3x3 transform matrix and
--   translation vector. Does not check if the matrix is invertible
--   (in which case the 'T3' will be invalid).
mat33 :: Floating n => Iso' (M33 n, V3 n) (T3 n)
mat33 :: forall n. Floating n => Iso' (M33 n, V3 n) (T3 n)
mat33 = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall n. Floating n => M33 n -> V3 n -> T3 n
fromMat33) (forall (v :: * -> *) n.
(HasBasis v, Num n) =>
Transformation v n -> v (v n)
mkMat forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall (v :: * -> *) n. Transformation v n -> v n
transl)