{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module Diagrams.Core.Trace
(
SortedList
, mkSortedList, getSortedList, onSortedList, unsafeOnSortedList
, Trace(Trace)
, appTrace
, mkTrace
, Traced(..)
, traceV, traceP
, maxTraceV, maxTraceP
, getRayTrace
, rayTraceV, rayTraceP
, maxRayTraceV, maxRayTraceP
) where
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Control.Lens
import Data.List (sort)
import qualified Data.Map as M
import Data.Semigroup
import qualified Data.Set as S
import Diagrams.Core.HasOrigin
import Diagrams.Core.Transform
import Diagrams.Core.V
import Linear.Affine
import Linear.Vector
newtype SortedList a = SortedList [a]
mkSortedList :: Ord a => [a] -> SortedList a
mkSortedList :: [a] -> SortedList a
mkSortedList = [a] -> SortedList a
forall a. [a] -> SortedList a
SortedList ([a] -> SortedList a) -> ([a] -> [a]) -> [a] -> SortedList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
forall a. Ord a => [a] -> [a]
sort
getSortedList :: SortedList a -> [a]
getSortedList :: SortedList a -> [a]
getSortedList (SortedList [a]
as) = [a]
as
onSortedList :: Ord b => ([a] -> [b]) -> SortedList a -> SortedList b
onSortedList :: ([a] -> [b]) -> SortedList a -> SortedList b
onSortedList [a] -> [b]
f = ([a] -> [b]) -> SortedList a -> SortedList b
forall a b. ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList ([b] -> [b]
forall a. Ord a => [a] -> [a]
sort ([b] -> [b]) -> ([a] -> [b]) -> [a] -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [b]
f)
unsafeOnSortedList :: ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList :: ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList [a] -> [b]
f (SortedList [a]
as) = [b] -> SortedList b
forall a. [a] -> SortedList a
SortedList ([a] -> [b]
f [a]
as)
merge :: Ord a => SortedList a -> SortedList a -> SortedList a
merge :: SortedList a -> SortedList a -> SortedList a
merge (SortedList [a]
as) (SortedList [a]
bs) = [a] -> SortedList a
forall a. [a] -> SortedList a
SortedList ([a] -> [a] -> [a]
forall a. Ord a => [a] -> [a] -> [a]
merge' [a]
as [a]
bs)
where
merge' :: [a] -> [a] -> [a]
merge' [a]
xs [] = [a]
xs
merge' [] [a]
ys = [a]
ys
merge' (a
x:[a]
xs) (a
y:[a]
ys) =
if a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
y
then a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
merge' [a]
xs (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)
else a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
merge' (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) [a]
ys
instance Ord a => Semigroup (SortedList a) where
<> :: SortedList a -> SortedList a -> SortedList a
(<>) = SortedList a -> SortedList a -> SortedList a
forall a. Ord a => SortedList a -> SortedList a -> SortedList a
merge
instance Ord a => Monoid (SortedList a) where
mappend :: SortedList a -> SortedList a -> SortedList a
mappend = SortedList a -> SortedList a -> SortedList a
forall a. Semigroup a => a -> a -> a
(<>)
mempty :: SortedList a
mempty = [a] -> SortedList a
forall a. [a] -> SortedList a
SortedList []
newtype Trace v n = Trace { Trace v n -> Point v n -> v n -> SortedList n
appTrace :: Point v n -> v n -> SortedList n }
instance Wrapped (Trace v n) where
type Unwrapped (Trace v n) = Point v n -> v n -> SortedList n
_Wrapped' :: p (Unwrapped (Trace v n)) (f (Unwrapped (Trace v n)))
-> p (Trace v n) (f (Trace v n))
_Wrapped' = (Trace v n -> Point v n -> v n -> SortedList n)
-> ((Point v n -> v n -> SortedList n) -> Trace v n)
-> Iso
(Trace v n)
(Trace v n)
(Point v n -> v n -> SortedList n)
(Point v n -> v n -> SortedList n)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Trace v n -> Point v n -> v n -> SortedList n
forall (v :: * -> *) n.
Trace v n -> Point v n -> v n -> SortedList n
appTrace (Point v n -> v n -> SortedList n) -> Trace v n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace
instance Rewrapped (Trace v n) (Trace v' n')
mkTrace :: (Point v n -> v n -> SortedList n) -> Trace v n
mkTrace :: (Point v n -> v n -> SortedList n) -> Trace v n
mkTrace = (Point v n -> v n -> SortedList n) -> Trace v n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace
deriving instance (Ord n) => Semigroup (Trace v n)
deriving instance (Ord n) => Monoid (Trace v n)
type instance V (Trace v n) = v
type instance N (Trace v n) = n
instance (Additive v, Num n) => HasOrigin (Trace v n) where
moveOriginTo :: Point (V (Trace v n)) (N (Trace v n)) -> Trace v n -> Trace v n
moveOriginTo (P V (Trace v n) (N (Trace v n))
u) = (Unwrapped (Trace v n) -> Trace v n)
-> Iso' (Trace v n) (Unwrapped (Trace v n))
forall s. Wrapped s => (Unwrapped s -> s) -> Iso' s (Unwrapped s)
_Wrapping' Unwrapped (Trace v n) -> Trace v n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (((Point v n -> v n -> SortedList n)
-> Identity (Point v n -> v n -> SortedList n))
-> Trace v n -> Identity (Trace v n))
-> ((Point v n -> v n -> SortedList n)
-> Point v n -> v n -> SortedList n)
-> Trace v n
-> Trace v n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \Point v n -> v n -> SortedList n
f Point v n
p -> Point v n -> v n -> SortedList n
f (Point v n
p Point v n -> Diff (Point v) n -> Point v n
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ Diff (Point v) n
V (Trace v n) (N (Trace v n))
u)
instance Show (Trace v n) where
show :: Trace v n -> String
show Trace v n
_ = String
"<trace>"
instance (Additive v, Num n) => Transformable (Trace v n) where
transform :: Transformation (V (Trace v n)) (N (Trace v n))
-> Trace v n -> Trace v n
transform Transformation (V (Trace v n)) (N (Trace v n))
t = ((Point v n -> v n -> SortedList n)
-> Identity (Point v n -> v n -> SortedList n))
-> Trace v n -> Identity (Trace v n)
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (((Point v n -> v n -> SortedList n)
-> Identity (Point v n -> v n -> SortedList n))
-> Trace v n -> Identity (Trace v n))
-> ((Point v n -> v n -> SortedList n)
-> Point v n -> v n -> SortedList n)
-> Trace v n
-> Trace v n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \Point v n -> v n -> SortedList n
f Point v n
p v n
v -> Point v n -> v n -> SortedList n
f (Transformation v n -> Point v n -> Point v n
forall (v :: * -> *) n.
(Additive v, Num n) =>
Transformation v n -> Point v n -> Point v n
papply (Transformation v n -> Transformation v n
forall (v :: * -> *) n.
(Functor v, Num n) =>
Transformation v n -> Transformation v n
inv Transformation v n
Transformation (V (Trace v n)) (N (Trace v n))
t) Point v n
p) (Transformation v n -> v n -> v n
forall (v :: * -> *) n. Transformation v n -> v n -> v n
apply (Transformation v n -> Transformation v n
forall (v :: * -> *) n.
(Functor v, Num n) =>
Transformation v n -> Transformation v n
inv Transformation v n
Transformation (V (Trace v n)) (N (Trace v n))
t) v n
v)
class (Additive (V a), Ord (N a)) => Traced a where
getTrace :: a -> Trace (V a) (N a)
instance (Additive v, Ord n) => Traced (Trace v n) where
getTrace :: Trace v n -> Trace (V (Trace v n)) (N (Trace v n))
getTrace = Trace v n -> Trace (V (Trace v n)) (N (Trace v n))
forall a. a -> a
id
instance (Additive v, Ord n) => Traced (Point v n) where
getTrace :: Point v n -> Trace (V (Point v n)) (N (Point v n))
getTrace = Trace v n -> Point v n -> Trace v n
forall a b. a -> b -> a
const Trace v n
forall a. Monoid a => a
mempty
instance Traced t => Traced (TransInv t) where
getTrace :: TransInv t -> Trace (V (TransInv t)) (N (TransInv t))
getTrace = t -> Trace (V t) (N t)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace (t -> Trace (V t) (N t))
-> (TransInv t -> t) -> TransInv t -> Trace (V t) (N t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (TransInv t) -> TransInv t)
-> TransInv t -> Unwrapped (TransInv t)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (TransInv t) -> TransInv t
forall t. t -> TransInv t
TransInv
instance (Traced a, Traced b, SameSpace a b) => Traced (a,b) where
getTrace :: (a, b) -> Trace (V (a, b)) (N (a, b))
getTrace (a
x,b
y) = a -> Trace (V a) (N a)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace a
x Trace (V b) (N b) -> Trace (V b) (N b) -> Trace (V b) (N b)
forall a. Semigroup a => a -> a -> a
<> b -> Trace (V b) (N b)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace b
y
instance (Traced b) => Traced [b] where
getTrace :: [b] -> Trace (V [b]) (N [b])
getTrace = [Trace (V b) (N b)] -> Trace (V b) (N b)
forall a. Monoid a => [a] -> a
mconcat ([Trace (V b) (N b)] -> Trace (V b) (N b))
-> ([b] -> [Trace (V b) (N b)]) -> [b] -> Trace (V b) (N b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> Trace (V b) (N b)) -> [b] -> [Trace (V b) (N b)]
forall a b. (a -> b) -> [a] -> [b]
map b -> Trace (V b) (N b)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace
instance (Traced b) => Traced (M.Map k b) where
getTrace :: Map k b -> Trace (V (Map k b)) (N (Map k b))
getTrace = [Trace (V b) (N b)] -> Trace (V b) (N b)
forall a. Monoid a => [a] -> a
mconcat ([Trace (V b) (N b)] -> Trace (V b) (N b))
-> (Map k b -> [Trace (V b) (N b)]) -> Map k b -> Trace (V b) (N b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> Trace (V b) (N b)) -> [b] -> [Trace (V b) (N b)]
forall a b. (a -> b) -> [a] -> [b]
map b -> Trace (V b) (N b)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace ([b] -> [Trace (V b) (N b)])
-> (Map k b -> [b]) -> Map k b -> [Trace (V b) (N b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k b -> [b]
forall k a. Map k a -> [a]
M.elems
instance (Traced b) => Traced (S.Set b) where
getTrace :: Set b -> Trace (V (Set b)) (N (Set b))
getTrace = [Trace (V b) (N b)] -> Trace (V b) (N b)
forall a. Monoid a => [a] -> a
mconcat ([Trace (V b) (N b)] -> Trace (V b) (N b))
-> (Set b -> [Trace (V b) (N b)]) -> Set b -> Trace (V b) (N b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> Trace (V b) (N b)) -> [b] -> [Trace (V b) (N b)]
forall a b. (a -> b) -> [a] -> [b]
map b -> Trace (V b) (N b)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace ([b] -> [Trace (V b) (N b)])
-> (Set b -> [b]) -> Set b -> [Trace (V b) (N b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set b -> [b]
forall a. Set a -> [a]
S.elems
traceV :: (n ~ N a, Num n, Traced a) => Point (V a) n -> V a n -> a -> Maybe (V a n)
traceV :: Point (V a) n -> V a n -> a -> Maybe (V a n)
traceV Point (V a) n
p V a n
v a
a = case SortedList n -> [n]
forall a. SortedList a -> [a]
getSortedList (SortedList n -> [n]) -> SortedList n -> [n]
forall a b. (a -> b) -> a -> b
$ (Unwrapped (Trace (V a) n) -> Trace (V a) n)
-> Trace (V a) n -> Point (V a) n -> V a n -> SortedList n
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Trace (V a) n) -> Trace (V a) n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (a -> Trace (V a) (N a)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace a
a) Point (V a) n
p V a n
v of
(n
s:[n]
_) -> V a n -> Maybe (V a n)
forall a. a -> Maybe a
Just (n
s n -> V a n -> V a n
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V a n
v)
[] -> Maybe (V a n)
forall a. Maybe a
Nothing
traceP :: (n ~ N a, Traced a, Num n) => Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
traceP :: Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
traceP Point (V a) n
p V a n
v a
a = (Point (V a) n
p Point (V a) n -> Diff (Point (V a)) n -> Point (V a) n
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) (V a n -> Point (V a) n) -> Maybe (V a n) -> Maybe (Point (V a) n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Point (V a) n -> V a n -> a -> Maybe (V a n)
forall n a.
(n ~ N a, Num n, Traced a) =>
Point (V a) n -> V a n -> a -> Maybe (V a n)
traceV Point (V a) n
p V a n
v a
a
maxTraceV :: (n ~ N a, Num n, Traced a) => Point (V a) n -> V a n -> a -> Maybe (V a n)
maxTraceV :: Point (V a) n -> V a n -> a -> Maybe (V a n)
maxTraceV Point (V a) n
p = Point (V a) n -> V a n -> a -> Maybe (V a n)
forall n a.
(n ~ N a, Num n, Traced a) =>
Point (V a) n -> V a n -> a -> Maybe (V a n)
traceV Point (V a) n
p (V a n -> a -> Maybe (V a n))
-> (V a n -> V a n) -> V a n -> a -> Maybe (V a n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. V a n -> V a n
forall (f :: * -> *) a. (Functor f, Num a) => f a -> f a
negated
maxTraceP :: (n ~ N a, Num n, Traced a) => Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
maxTraceP :: Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
maxTraceP Point (V a) n
p V a n
v a
a = (Point (V a) n
p Point (V a) n -> Diff (Point (V a)) n -> Point (V a) n
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) (V a n -> Point (V a) n) -> Maybe (V a n) -> Maybe (Point (V a) n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Point (V a) n -> V a n -> a -> Maybe (V a n)
forall n a.
(n ~ N a, Num n, Traced a) =>
Point (V a) n -> V a n -> a -> Maybe (V a n)
maxTraceV Point (V a) n
p V a n
v a
a
getRayTrace :: (n ~ N a, Traced a, Num n) => a -> Trace (V a) n
getRayTrace :: a -> Trace (V a) n
getRayTrace a
a = (Point (V a) n -> V a n -> SortedList n) -> Trace (V a) n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace ((Point (V a) n -> V a n -> SortedList n) -> Trace (V a) n)
-> (Point (V a) n -> V a n -> SortedList n) -> Trace (V a) n
forall a b. (a -> b) -> a -> b
$ \Point (V a) n
p V a n
v -> ([n] -> [n]) -> SortedList n -> SortedList n
forall a b. ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList ((n -> Bool) -> [n] -> [n]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (n -> n -> Bool
forall a. Ord a => a -> a -> Bool
<n
0)) (SortedList n -> SortedList n) -> SortedList n -> SortedList n
forall a b. (a -> b) -> a -> b
$ Trace (V a) n -> Point (V a) n -> V a n -> SortedList n
forall (v :: * -> *) n.
Trace v n -> Point v n -> v n -> SortedList n
appTrace (a -> Trace (V a) (N a)
forall a. Traced a => a -> Trace (V a) (N a)
getTrace a
a) Point (V a) n
p V a n
v
rayTraceV :: (n ~ N a, Traced a, Num n)
=> Point (V a) n -> V a n -> a -> Maybe (V a n)
rayTraceV :: Point (V a) n -> V a n -> a -> Maybe (V a n)
rayTraceV Point (V a) n
p V a n
v a
a = case SortedList n -> [n]
forall a. SortedList a -> [a]
getSortedList (SortedList n -> [n]) -> SortedList n -> [n]
forall a b. (a -> b) -> a -> b
$ (Unwrapped (Trace (V a) n) -> Trace (V a) n)
-> Trace (V a) n -> Point (V a) n -> V a n -> SortedList n
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Trace (V a) n) -> Trace (V a) n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (a -> Trace (V a) n
forall n a. (n ~ N a, Traced a, Num n) => a -> Trace (V a) n
getRayTrace a
a) Point (V a) n
p V a n
v of
(n
s:[n]
_) -> V a n -> Maybe (V a n)
forall a. a -> Maybe a
Just (n
s n -> V a n -> V a n
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V a n
v)
[] -> Maybe (V a n)
forall a. Maybe a
Nothing
rayTraceP :: (n ~ N a, Traced a, Num n)
=> Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
rayTraceP :: Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
rayTraceP Point (V a) n
p V a n
v a
a = (Point (V a) n
p Point (V a) n -> Diff (Point (V a)) n -> Point (V a) n
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) (V a n -> Point (V a) n) -> Maybe (V a n) -> Maybe (Point (V a) n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Point (V a) n -> V a n -> a -> Maybe (V a n)
forall n a.
(n ~ N a, Traced a, Num n) =>
Point (V a) n -> V a n -> a -> Maybe (V a n)
rayTraceV Point (V a) n
p V a n
v a
a
maxRayTraceV :: (n ~ N a, Traced a, Num n)
=> Point (V a) n -> V a n -> a -> Maybe (V a n)
maxRayTraceV :: Point (V a) n -> V a n -> a -> Maybe (V a n)
maxRayTraceV Point (V a) n
p V a n
v a
a =
case SortedList n -> [n]
forall a. SortedList a -> [a]
getSortedList (SortedList n -> [n]) -> SortedList n -> [n]
forall a b. (a -> b) -> a -> b
$ (Unwrapped (Trace (V a) n) -> Trace (V a) n)
-> Trace (V a) n -> Point (V a) n -> V a n -> SortedList n
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Trace (V a) n) -> Trace (V a) n
forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (a -> Trace (V a) n
forall n a. (n ~ N a, Traced a, Num n) => a -> Trace (V a) n
getRayTrace a
a) Point (V a) n
p V a n
v of
[] -> Maybe (V a n)
forall a. Maybe a
Nothing
[n]
xs -> V a n -> Maybe (V a n)
forall a. a -> Maybe a
Just ([n] -> n
forall a. [a] -> a
last [n]
xs n -> V a n -> V a n
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V a n
v)
maxRayTraceP :: (n ~ N a, Traced a, Num n)
=> Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
maxRayTraceP :: Point (V a) n -> V a n -> a -> Maybe (Point (V a) n)
maxRayTraceP Point (V a) n
p V a n
v a
a = (Point (V a) n
p Point (V a) n -> Diff (Point (V a)) n -> Point (V a) n
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) (V a n -> Point (V a) n) -> Maybe (V a n) -> Maybe (Point (V a) n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Point (V a) n -> V a n -> a -> Maybe (V a n)
forall n a.
(n ~ N a, Traced a, Num n) =>
Point (V a) n -> V a n -> a -> Maybe (V a n)
maxRayTraceV Point (V a) n
p V a n
v a
a