{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# 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 :: forall a. Ord a => [a] -> SortedList a
mkSortedList = forall a. [a] -> SortedList a
SortedList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> [a]
sort
getSortedList :: SortedList a -> [a]
getSortedList :: forall a. SortedList a -> [a]
getSortedList (SortedList [a]
as) = [a]
as
onSortedList :: Ord b => ([a] -> [b]) -> SortedList a -> SortedList b
onSortedList :: forall b a. Ord b => ([a] -> [b]) -> SortedList a -> SortedList b
onSortedList [a] -> [b]
f = forall a b. ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList (forall a. Ord a => [a] -> [a]
sort forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [b]
f)
unsafeOnSortedList :: ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList :: forall a b. ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList [a] -> [b]
f (SortedList [a]
as) = forall a. [a] -> SortedList a
SortedList ([a] -> [b]
f [a]
as)
merge :: Ord a => SortedList a -> SortedList a -> SortedList a
merge :: forall a. Ord a => SortedList a -> SortedList a -> SortedList a
merge (SortedList [a]
as) (SortedList [a]
bs) = forall a. [a] -> SortedList a
SortedList (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 forall a. Ord a => a -> a -> Bool
<= a
y
then a
x forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
merge' [a]
xs (a
yforall a. a -> [a] -> [a]
:[a]
ys)
else a
y forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
merge' (a
xforall a. a -> [a] -> [a]
:[a]
xs) [a]
ys
instance Ord a => Semigroup (SortedList a) where
<> :: 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 = forall a. Semigroup a => a -> a -> a
(<>)
mempty :: SortedList a
mempty = forall a. [a] -> SortedList a
SortedList []
newtype Trace v n = Trace { forall (v :: * -> *) n.
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' :: Iso' (Trace v n) (Unwrapped (Trace v n))
_Wrapped' = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall (v :: * -> *) n.
Trace v n -> Point v n -> v n -> SortedList n
appTrace 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 :: forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
mkTrace = 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) = forall s. Wrapped s => (Unwrapped s -> s) -> Iso' s (Unwrapped s)
_Wrapping' forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace 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 forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ 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 = forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped 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 (forall (v :: * -> *) n.
(Additive v, Num n) =>
Transformation v n -> Point v n -> Point v n
papply (forall (v :: * -> *) n.
(Functor v, Num n) =>
Transformation v n -> Transformation v n
inv Transformation (V (Trace v n)) (N (Trace v n))
t) Point v n
p) (forall (v :: * -> *) n. Transformation v n -> v n -> v n
apply (forall (v :: * -> *) n.
(Functor v, Num n) =>
Transformation v n -> Transformation v n
inv 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 = 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 = forall a b. a -> b -> a
const forall a. Monoid a => a
mempty
instance Traced t => Traced (TransInv t) where
getTrace :: TransInv t -> Trace (V (TransInv t)) (N (TransInv t))
getTrace = forall a. Traced a => a -> Trace (V a) (N a)
getTrace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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) = forall a. Traced a => a -> Trace (V a) (N a)
getTrace a
x forall a. Semigroup a => a -> a -> a
<> 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 = forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map 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 = forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a. Traced a => a -> Trace (V a) (N a)
getTrace forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a. Traced a => a -> Trace (V a) (N a)
getTrace forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: 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 = case forall a. SortedList a -> [a]
getSortedList forall a b. (a -> b) -> a -> b
$ forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (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]
_) -> forall a. a -> Maybe a
Just (n
s forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V a n
v)
[] -> 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 :: forall n a.
(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
p V a n
v a
a = (Point (V a) n
p forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 :: 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 = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall n a.
(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
p V a n
v a
a = (Point (V a) n
p forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 :: forall n a. (n ~ N a, Traced a, Num n) => a -> Trace (V a) n
getRayTrace a
a = forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace forall a b. (a -> b) -> a -> b
$ \Point (V a) n
p V a n
v -> forall a b. ([a] -> [b]) -> SortedList a -> SortedList b
unsafeOnSortedList (forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Ord a => a -> a -> Bool
<n
0)) forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) n.
Trace v n -> Point v n -> v n -> SortedList n
appTrace (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 :: 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 = case forall a. SortedList a -> [a]
getSortedList forall a b. (a -> b) -> a -> b
$ forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (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]
_) -> forall a. a -> Maybe a
Just (n
s forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V a n
v)
[] -> 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 :: forall n a.
(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
p V a n
v a
a = (Point (V a) n
p forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 :: 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 =
case forall a. SortedList a -> [a]
getSortedList forall a b. (a -> b) -> a -> b
$ forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op forall (v :: * -> *) n.
(Point v n -> v n -> SortedList n) -> Trace v n
Trace (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
[] -> forall a. Maybe a
Nothing
[n]
xs -> forall a. a -> Maybe a
Just (forall a. [a] -> a
last [n]
xs 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 :: forall n a.
(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
p V a n
v a
a = (Point (V a) n
p forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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