|
| 1 | +{-# LANGUAGE ConstraintKinds #-} |
| 2 | +{-# LANGUAGE FlexibleContexts #-} |
| 3 | +{-# LANGUAGE FlexibleInstances #-} |
| 4 | +{-# LANGUAGE GADTs #-} |
| 5 | +{-# LANGUAGE ImpredicativeTypes #-} |
| 6 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 7 | +{-# LANGUAGE RankNTypes #-} |
| 8 | +{-# LANGUAGE TypeFamilies #-} |
| 9 | +{-# LANGUAGE UndecidableInstances #-} |
| 10 | +----------------------------------------------------------------------------- |
| 11 | +-- | |
| 12 | +-- Module : Diagrams.LinearMap |
| 13 | +-- Copyright : (c) 2014 diagrams team (see LICENSE) |
| 14 | +-- License : BSD-style (see LICENSE) |
| 15 | + |
| 16 | +-- |
| 17 | +-- Linear maps. Unlike 'Transformation's these are not restricted to the |
| 18 | +-- same space. In practice these are used for projections in |
| 19 | +-- "Diagrams.ThreeD.Projection". |
| 20 | +-- |
| 21 | +----------------------------------------------------------------------------- |
| 22 | + |
| 23 | +module Diagrams.LinearMap where |
| 24 | + |
| 25 | +import Control.Lens |
| 26 | +import Data.FingerTree as FT |
| 27 | +import Data.Foldable (Foldable) |
| 28 | + |
| 29 | +import Diagrams.Core |
| 30 | +import Diagrams.Core.Transform |
| 31 | +import Diagrams.Located |
| 32 | +import Diagrams.Path |
| 33 | +import Diagrams.Segment |
| 34 | +import Diagrams.Trail hiding (offset) |
| 35 | + |
| 36 | +import Linear.Affine |
| 37 | +import Linear.Metric |
| 38 | +import Linear.Vector |
| 39 | + |
| 40 | + |
| 41 | +-- | Type for holding linear maps. Note that these are not affine transforms so |
| 42 | +-- attemping apply a translation with 'LinearMap' will likely produce incorrect |
| 43 | +-- results. |
| 44 | +newtype LinearMap v u n = LinearMap { lapply :: v n -> u n } |
| 45 | + |
| 46 | +toLinearMap :: Transformation v n -> LinearMap v v n |
| 47 | +toLinearMap (Transformation (m :-: _) _ _) = LinearMap m |
| 48 | + |
| 49 | +-- | Traversal over all the vmap of an object. |
| 50 | +class LinearMappable a b where |
| 51 | + vmap :: (Vn a -> Vn b) -> a -> b |
| 52 | + -- this uses a function instead of LinearMap so we can also use this |
| 53 | + -- class to change number types |
| 54 | + |
| 55 | +-- Note: instances need to be of the form |
| 56 | +-- |
| 57 | +-- r ~ A u m => LinearMappable (A v n) r |
| 58 | +-- |
| 59 | +-- so ghc knows there's only one possible result from calling vmap. |
| 60 | + |
| 61 | +-- | Apply a linear map. |
| 62 | +linmap :: (InSpace v n a, Foldable v, LinearMappable a b, N b ~ n) |
| 63 | + => LinearMap v (V b) n -> a -> b |
| 64 | +linmap = vmap . lapply |
| 65 | + |
| 66 | +instance r ~ Offset c u m => LinearMappable (Offset c v n) r where |
| 67 | + vmap f (OffsetClosed v) = OffsetClosed (f v) |
| 68 | + vmap _ OffsetOpen = OffsetOpen |
| 69 | + {-# INLINE vmap #-} |
| 70 | + |
| 71 | +instance r ~ Segment c u m => LinearMappable (Segment c v n) r where |
| 72 | + vmap f (Linear offset) = Linear (vmap f offset) |
| 73 | + vmap f (Cubic v1 v2 offset) = Cubic (f v1) (f v2) (vmap f offset) |
| 74 | + {-# INLINE vmap #-} |
| 75 | + |
| 76 | +instance (Metric v, Metric u, OrderedField n, OrderedField m, r ~ SegTree u m) |
| 77 | + => LinearMappable (SegTree v n) r where |
| 78 | + vmap f = over _Wrapped (fmap' (vmap f)) |
| 79 | + {-# INLINE vmap #-} |
| 80 | + |
| 81 | +instance (Metric v, Metric u, OrderedField n, OrderedField m, r ~ Trail' l u m) |
| 82 | + => LinearMappable (Trail' l v n) r where |
| 83 | + vmap f (Line st) = Line (vmap f st) |
| 84 | + vmap f (Loop st offset) = Loop (vmap f st) (vmap f offset) |
| 85 | + {-# INLINE vmap #-} |
| 86 | + |
| 87 | +instance (Metric v, Metric u, OrderedField n, OrderedField m, r ~ Trail u m) |
| 88 | + => LinearMappable (Trail v n) r where |
| 89 | + vmap f (Trail (Line st)) = Trail $ Line (vmap f st) |
| 90 | + vmap f (Trail (Loop st offset)) = Trail $ Loop (vmap f st) (vmap f offset) |
| 91 | + {-# INLINE vmap #-} |
| 92 | + |
| 93 | +instance LinearMappable (Point v n) (Point u m) where |
| 94 | + vmap f (P v) = P (f v) |
| 95 | + {-# INLINE vmap #-} |
| 96 | + |
| 97 | +instance r ~ FixedSegment u m => LinearMappable (FixedSegment v n) r where |
| 98 | + vmap f (FLinear p0 p1) = FLinear (vmap f p0) (vmap f p1) |
| 99 | + vmap f (FCubic p0 p1 p2 p3) = FCubic (vmap f p0) (vmap f p1) |
| 100 | + (vmap f p2) (vmap f p3) |
| 101 | + {-# INLINE vmap #-} |
| 102 | + |
| 103 | +instance (LinearMappable a b, r ~ Located b) => LinearMappable (Located a) r where |
| 104 | + vmap f (Loc p a) = Loc (vmap f p) (vmap f a) |
| 105 | + {-# INLINE vmap #-} |
| 106 | + |
| 107 | +instance (Metric v, Metric u, OrderedField n, OrderedField m, r ~ Path u m) |
| 108 | + => LinearMappable (Path v n) r where |
| 109 | + vmap f = _Wrapped . mapped %~ vmap f |
| 110 | + {-# INLINE vmap #-} |
| 111 | + |
| 112 | +-- | Affine linear maps. Unlike Transformation these do not have to be |
| 113 | +-- invertable so we can map between spaces. |
| 114 | +data AffineMap v u n = AffineMap (LinearMap v u n) (u n) |
| 115 | + |
| 116 | +-- | Make an affine map from a linear function and a translation. |
| 117 | +mkAffineMap :: (v n -> u n) -> u n -> AffineMap v u n |
| 118 | +mkAffineMap f = AffineMap (LinearMap f) |
| 119 | + |
| 120 | +toAffineMap :: (HasBasis v, Num n) |
| 121 | + => Transformation v n -> AffineMap v v n |
| 122 | +toAffineMap t = AffineMap (toLinearMap t) (transl t) |
| 123 | + |
| 124 | +class (LinearMappable a b, N a ~ N b) => AffineMappable a b where |
| 125 | + amap :: (Additive (V a), Foldable (V a), Additive (V b), Num (N b)) |
| 126 | + => AffineMap (V a) (V b) (N b) -> a -> b |
| 127 | + amap (AffineMap f _) = linmap f |
| 128 | + {-# INLINE amap #-} |
| 129 | + |
| 130 | +instance r ~ Offset c u n => AffineMappable (Offset c v n) r |
| 131 | +instance r ~ Segment c u n => AffineMappable (Segment c v n) r |
| 132 | +instance (Metric v, Metric u, OrderedField n, r ~ SegTree u n) => AffineMappable (SegTree v n) r |
| 133 | +instance (Metric v, Metric u, OrderedField n, r ~ Trail' l u n) => AffineMappable (Trail' l v n) r |
| 134 | +instance (Metric v, Metric u, OrderedField n, r ~ Trail u n) => AffineMappable (Trail v n) r |
| 135 | + |
| 136 | +instance (Additive v, Foldable v, Num n, r ~ Point u n) => AffineMappable (Point v n) r where |
| 137 | + amap (AffineMap f v) p = linmap f p .+^ v |
| 138 | + {-# INLINE amap #-} |
| 139 | + |
| 140 | +instance r ~ FixedSegment u n => AffineMappable (FixedSegment v n) r where |
| 141 | + amap m (FLinear p0 p1) = FLinear (amap m p0) (amap m p1) |
| 142 | + amap m (FCubic p0 p1 p2 p3) = FCubic (amap m p0) (amap m p1) (amap m p2) (amap m p3) |
| 143 | + {-# INLINE amap #-} |
| 144 | + |
| 145 | +instance (LinearMappable a b, N a ~ N b, r ~ Located b) => AffineMappable (Located a) r where |
| 146 | + amap m@(AffineMap l _) (Loc p x) = Loc (amap m p) (linmap l x) |
| 147 | + {-# INLINE amap #-} |
| 148 | + |
| 149 | +instance (Metric v, Metric u, OrderedField n, r ~ Path u n) |
| 150 | + => AffineMappable (Path v n) r where |
| 151 | + amap m = _Wrapped . mapped %~ amap m |
| 152 | + {-# INLINE amap #-} |
| 153 | + |
0 commit comments