{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Diagrams.Located
( Located (..)
, at, viewLoc, mapLoc, located, _loc
)
where
import Control.Lens (Lens, Lens')
import Text.Read
import Linear.Affine
import Linear.Vector
import Diagrams.Align
import Diagrams.Core
import Diagrams.Core.Transform
import Diagrams.Parametric
import Data.Serialize (Serialize)
import GHC.Generics (Generic)
data Located a =
Loc { forall a. Located a -> Point (V a) (N a)
loc :: Point (V a) (N a)
, forall a. Located a -> a
unLoc :: a
} deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Located a) x -> Located a
forall a x. Located a -> Rep (Located a) x
$cto :: forall a x. Rep (Located a) x -> Located a
$cfrom :: forall a x. Located a -> Rep (Located a) x
Generic)
instance (Serialize a, Serialize (V a (N a))) => Serialize (Located a)
infix 5 `at`
at :: a -> Point (V a) (N a) -> Located a
at :: forall a. a -> Point (V a) (N a) -> Located a
at a
a Point (V a) (N a)
p = forall a. Point (V a) (N a) -> a -> Located a
Loc Point (V a) (N a)
p a
a
viewLoc :: Located a -> (Point (V a) (N a), a)
viewLoc :: forall a. Located a -> (Point (V a) (N a), a)
viewLoc (Loc Point (V a) (N a)
p a
a) = (Point (V a) (N a)
p,a
a)
mapLoc :: SameSpace a b => (a -> b) -> Located a -> Located b
mapLoc :: forall a b. SameSpace a b => (a -> b) -> Located a -> Located b
mapLoc a -> b
f (Loc Point (V a) (N a)
p a
a) = forall a. Point (V a) (N a) -> a -> Located a
Loc Point (V a) (N a)
p (a -> b
f a
a)
located :: SameSpace a b => Lens (Located a) (Located b) a b
located :: forall a b. SameSpace a b => Lens (Located a) (Located b) a b
located a -> f b
f (Loc Point (V a) (N a)
p a
a) = forall a. Point (V a) (N a) -> a -> Located a
Loc Point (V a) (N a)
p forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
_loc :: Lens' (Located a) (Point (V a) (N a))
_loc :: forall a. Lens' (Located a) (Point (V a) (N a))
_loc Point (V a) (N a) -> f (Point (V a) (N a))
f (Loc Point (V a) (N a)
p a
a) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Point (V a) (N a) -> a -> Located a
Loc a
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Point (V a) (N a) -> f (Point (V a) (N a))
f Point (V a) (N a)
p
deriving instance (Eq (V a (N a)), Eq a ) => Eq (Located a)
deriving instance (Ord (V a (N a)), Ord a ) => Ord (Located a)
instance (Show (V a (N a)), Show a) => Show (Located a) where
showsPrec :: Int -> Located a -> ShowS
showsPrec Int
d (Loc Point (V a) (N a)
p a
a) = Bool -> ShowS -> ShowS
showParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
5) forall a b. (a -> b) -> a -> b
$
forall a. Show a => Int -> a -> ShowS
showsPrec Int
6 a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" `at` " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
6 Point (V a) (N a)
p
instance (Read (V a (N a)), Read a) => Read (Located a) where
readPrec :: ReadPrec (Located a)
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
5 forall a b. (a -> b) -> a -> b
$ do
a
a <- forall a. Read a => ReadPrec a
readPrec
Punc String
"`" <- ReadPrec Lexeme
lexP
Ident String
"at" <- ReadPrec Lexeme
lexP
Punc String
"`" <- ReadPrec Lexeme
lexP
Point (V a) (N a)
p <- forall a. Read a => ReadPrec a
readPrec
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Point (V a) (N a) -> a -> Located a
Loc Point (V a) (N a)
p a
a)
type instance V (Located a) = V a
type instance N (Located a) = N a
instance (Num (N a), Additive (V a)) => HasOrigin (Located a) where
moveOriginTo :: Point (V (Located a)) (N (Located a)) -> Located a -> Located a
moveOriginTo Point (V (Located a)) (N (Located a))
o (Loc Point (V a) (N a)
p a
a) = forall a. Point (V a) (N a) -> a -> Located a
Loc (forall t. HasOrigin t => Point (V t) (N t) -> t -> t
moveOriginTo Point (V (Located a)) (N (Located a))
o Point (V a) (N a)
p) a
a
instance (Additive (V a), Num (N a), Transformable a) => Transformable (Located a) where
transform :: Transformation (V (Located a)) (N (Located a))
-> Located a -> Located a
transform t :: Transformation (V (Located a)) (N (Located a))
t@(Transformation V (Located a) (N (Located a)) :-: V (Located a) (N (Located a))
t1 V (Located a) (N (Located a)) :-: V (Located a) (N (Located a))
t2 V (Located a) (N (Located a))
_) (Loc Point (V a) (N a)
p a
a)
= forall a. Point (V a) (N a) -> a -> Located a
Loc (forall t. Transformable t => Transformation (V t) (N t) -> t -> t
transform Transformation (V (Located a)) (N (Located a))
t Point (V a) (N a)
p) (forall t. Transformable t => Transformation (V t) (N t) -> t -> t
transform (forall (v :: * -> *) n.
(v n :-: v n) -> (v n :-: v n) -> v n -> Transformation v n
Transformation V (Located a) (N (Located a)) :-: V (Located a) (N (Located a))
t1 V (Located a) (N (Located a)) :-: V (Located a) (N (Located a))
t2 forall (f :: * -> *) a. (Additive f, Num a) => f a
zero) a
a)
instance Enveloped a => Enveloped (Located a) where
getEnvelope :: Located a -> Envelope (V (Located a)) (N (Located a))
getEnvelope (Loc Point (V a) (N a)
p a
a) = forall (v :: * -> *) n t.
(InSpace v n t, HasOrigin t) =>
Point v n -> t -> t
moveTo Point (V a) (N a)
p (forall a. Enveloped a => a -> Envelope (V a) (N a)
getEnvelope a
a)
instance Enveloped a => Juxtaposable (Located a) where
juxtapose :: Vn (Located a) -> Located a -> Located a -> Located a
juxtapose = forall a. (Enveloped a, HasOrigin a) => Vn a -> a -> a -> a
juxtaposeDefault
instance (Traced a, Num (N a)) => Traced (Located a) where
getTrace :: Located a -> Trace (V (Located a)) (N (Located a))
getTrace (Loc Point (V a) (N a)
p a
a) = forall (v :: * -> *) n t.
(InSpace v n t, HasOrigin t) =>
Point v n -> t -> t
moveTo Point (V a) (N a)
p (forall a. Traced a => a -> Trace (V a) (N a)
getTrace a
a)
instance Alignable a => Alignable (Located a) where
defaultBoundary :: forall (v :: * -> *) n.
(V (Located a) ~ v, N (Located a) ~ n) =>
v n -> Located a -> Point v n
defaultBoundary v n
v = forall a (v :: * -> *) n.
(Alignable a, V a ~ v, N a ~ n) =>
v n -> a -> Point v n
defaultBoundary v n
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Located a -> a
unLoc
instance Qualifiable a => Qualifiable (Located a) where
a
n .>> :: forall a. IsName a => a -> Located a -> Located a
.>> Loc Point (V a) (N a)
p a
a = forall a. Point (V a) (N a) -> a -> Located a
Loc Point (V a) (N a)
p (a
n forall q a. (Qualifiable q, IsName a) => a -> q -> q
.>> a
a)
type instance Codomain (Located a) = Point (Codomain a)
instance (InSpace v n a, Parametric a, Codomain a ~ v)
=> Parametric (Located a) where
Loc Point (V a) (N a)
x a
a atParam :: Located a -> N (Located a) -> Codomain (Located a) (N (Located a))
`atParam` N (Located a)
p = Point (V a) (N a)
x forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ (a
a forall p. Parametric p => p -> N p -> Codomain p (N p)
`atParam` N (Located a)
p)
instance DomainBounds a => DomainBounds (Located a) where
domainLower :: Located a -> N (Located a)
domainLower (Loc Point (V a) (N a)
_ a
a) = forall p. DomainBounds p => p -> N p
domainLower a
a
domainUpper :: Located a -> N (Located a)
domainUpper (Loc Point (V a) (N a)
_ a
a) = forall p. DomainBounds p => p -> N p
domainUpper a
a
instance (InSpace v n a, EndValues a, Codomain a ~ v) => EndValues (Located a)
instance (InSpace v n a, Fractional n, Parametric a, Sectionable a, Codomain a ~ v)
=> Sectionable (Located a) where
splitAtParam :: Located a -> N (Located a) -> (Located a, Located a)
splitAtParam (Loc Point (V a) (N a)
x a
a) N (Located a)
p = (forall a. Point (V a) (N a) -> a -> Located a
Loc Point (V a) (N a)
x a
a1, forall a. Point (V a) (N a) -> a -> Located a
Loc (Point (V a) (N a)
x forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ (a
a forall p. Parametric p => p -> N p -> Codomain p (N p)
`atParam` N (Located a)
p)) a
a2)
where (a
a1,a
a2) = forall p. Sectionable p => p -> N p -> (p, p)
splitAtParam a
a N (Located a)
p
section :: Located a -> N (Located a) -> N (Located a) -> Located a
section (Loc Point (V a) (N a)
x a
a) N (Located a)
p1 N (Located a)
p2 = forall a. Point (V a) (N a) -> a -> Located a
Loc (Point (V a) (N a)
x forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ (a
a forall p. Parametric p => p -> N p -> Codomain p (N p)
`atParam` N (Located a)
p1)) (forall p. Sectionable p => p -> N p -> N p -> p
section a
a N (Located a)
p1 N (Located a)
p2)
reverseDomain :: Located a -> Located a
reverseDomain (Loc Point (V a) (N a)
x a
a) = forall a. Point (V a) (N a) -> a -> Located a
Loc (Point (V a) (N a)
x forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ Codomain a (N a)
y) (forall p. Sectionable p => p -> p
reverseDomain a
a)
where y :: Codomain a (N a)
y = a
a forall p. Parametric p => p -> N p -> Codomain p (N p)
`atParam` forall p. DomainBounds p => p -> N p
domainUpper a
a
instance (InSpace v n a, Fractional n, HasArcLength a, Codomain a ~ v)
=> HasArcLength (Located a) where
arcLengthBounded :: N (Located a) -> Located a -> Interval (N (Located a))
arcLengthBounded N (Located a)
eps (Loc Point (V a) (N a)
_ a
a) = forall p. HasArcLength p => N p -> p -> Interval (N p)
arcLengthBounded N (Located a)
eps a
a
arcLengthToParam :: N (Located a) -> Located a -> N (Located a) -> N (Located a)
arcLengthToParam N (Located a)
eps (Loc Point (V a) (N a)
_ a
a) = forall p. HasArcLength p => N p -> p -> N p -> N p
arcLengthToParam N (Located a)
eps a
a