module Diagrams.TwoD.Deform where

import           Control.Lens

import           Diagrams.Deform

import           Linear.V2
import           Linear.Vector

-- | The parallel projection onto the plane x=0
parallelX0 :: (R1 v, Num n) => Deformation v v n
parallelX0 :: forall (v :: * -> *) n. (R1 v, Num n) => Deformation v v n
parallelX0 = forall (v :: * -> *) (u :: * -> *) n.
(Point v n -> Point u n) -> Deformation v u n
Deformation (forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x forall s t a b. ASetter s t a b -> b -> s -> t
.~ n
0)

-- | The perspective division onto the plane x=1 along lines going
-- through the origin.
perspectiveX1 :: (R1 v, Functor v, Fractional n) => Deformation v v n
perspectiveX1 :: forall (v :: * -> *) n.
(R1 v, Functor v, Fractional n) =>
Deformation v v n
perspectiveX1 = forall (v :: * -> *) (u :: * -> *) n.
(Point v n -> Point u n) -> Deformation v u n
Deformation forall a b. (a -> b) -> a -> b
$ \Point v n
p -> Point v n
p forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ (Point v n
p forall s a. s -> Getting a s a -> a
^. forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x)

-- | The parallel projection onto the plane y=0
parallelY0 :: (R2 v, Num n) => Deformation v v n
parallelY0 :: forall (v :: * -> *) n. (R2 v, Num n) => Deformation v v n
parallelY0 = forall (v :: * -> *) (u :: * -> *) n.
(Point v n -> Point u n) -> Deformation v u n
Deformation (forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y forall s t a b. ASetter s t a b -> b -> s -> t
.~ n
0)

-- | The perspective division onto the plane y=1 along lines going
-- through the origin.
perspectiveY1 :: (R2 v, Functor v, Floating n) => Deformation v v n
perspectiveY1 :: forall (v :: * -> *) n.
(R2 v, Functor v, Floating n) =>
Deformation v v n
perspectiveY1 = forall (v :: * -> *) (u :: * -> *) n.
(Point v n -> Point u n) -> Deformation v u n
Deformation forall a b. (a -> b) -> a -> b
$ \Point v n
p -> Point v n
p forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ (Point v n
p forall s a. s -> Getting a s a -> a
^. forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y)

-- | The viewing transform for a viewer facing along the positive X
-- axis.  X coördinates stay fixed, while Y coördinates are compressed
-- with increasing distance.  @asDeformation (translation unitX) <>
-- parallelX0 <> frustrumX = perspectiveX1@
facingX :: (R1 v, Functor v, Fractional n) => Deformation v v n
facingX :: forall (v :: * -> *) n.
(R1 v, Functor v, Fractional n) =>
Deformation v v n
facingX = forall (v :: * -> *) (u :: * -> *) n.
(Point v n -> Point u n) -> Deformation v u n
Deformation forall a b. (a -> b) -> a -> b
$
  \Point v n
p -> let x :: n
x = Point v n
p forall s a. s -> Getting a s a -> a
^. forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x
        in  Point v n
p forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ n
x forall a b. a -> (a -> b) -> b
& forall (t :: * -> *) a. R1 t => Lens' (t a) a
_x forall s t a b. ASetter s t a b -> b -> s -> t
.~ n
x

facingY :: (R2 v, Functor v, Fractional n) => Deformation v v n
facingY :: forall (v :: * -> *) n.
(R2 v, Functor v, Fractional n) =>
Deformation v v n
facingY = forall (v :: * -> *) (u :: * -> *) n.
(Point v n -> Point u n) -> Deformation v u n
Deformation forall a b. (a -> b) -> a -> b
$
  \Point v n
p -> let y :: n
y = Point v n
p forall s a. s -> Getting a s a -> a
^. forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y
        in  Point v n
p forall (f :: * -> *) a.
(Functor f, Fractional a) =>
f a -> a -> f a
^/ n
y forall a b. a -> (a -> b) -> b
& forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y forall s t a b. ASetter s t a b -> b -> s -> t
.~ n
y