Skip to content

Commit

Permalink
Merge pull request #215 from diagrams/linear
Browse files Browse the repository at this point in the history
Migrate from `vector-space` package to `linear`

Make types more polymorphic.
  • Loading branch information
bergey committed Oct 10, 2014
2 parents 67d49fd + d92afe2 commit 5c64686
Show file tree
Hide file tree
Showing 82 changed files with 3,039 additions and 3,323 deletions.
2 changes: 1 addition & 1 deletion Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import Distribution.Simple
import Distribution.Simple
main = defaultMain
64 changes: 34 additions & 30 deletions diagrams-lib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,57 @@ Source-repository head

Library
Exposed-modules: Diagrams.Prelude,
Diagrams.Prelude.ThreeD,
Diagrams.Align,
Diagrams.Angle,
Diagrams.Combinators,
Diagrams.Coordinates,
Diagrams.Attributes,
Diagrams.Attributes.Compile,
Diagrams.Points,
Diagrams.BoundingBox,
Diagrams.Combinators,
Diagrams.Coordinates,
Diagrams.CubicSpline,
Diagrams.CubicSpline.Internal,
Diagrams.Deform
Diagrams.Direction,
Diagrams.Envelope,
Diagrams.Located,
Diagrams.Names,
Diagrams.Parametric,
Diagrams.Parametric.Adjust,
Diagrams.Segment,
Diagrams.Trail,
Diagrams.TrailLike,
Diagrams.Path,
Diagrams.CubicSpline,
Diagrams.CubicSpline.Internal,
Diagrams.Direction,
Diagrams.Points,
Diagrams.Query,
Diagrams.Segment,
Diagrams.Solve,
Diagrams.Tangent,
Diagrams.Transform,
Diagrams.Deform
Diagrams.BoundingBox,
Diagrams.Names,
Diagrams.Envelope,
Diagrams.Trace,
Diagrams.Query,
Diagrams.Trail,
Diagrams.TrailLike,
Diagrams.Transform,
Diagrams.TwoD,
Diagrams.TwoD.Types,
Diagrams.TwoD.Adjust,
Diagrams.TwoD.Align,
Diagrams.TwoD.Arc,
Diagrams.TwoD.Arrow,
Diagrams.TwoD.Arrowheads,
Diagrams.TwoD.Attributes,
Diagrams.TwoD.Combinators,
Diagrams.TwoD.Curvature,
Diagrams.TwoD.Deform,
Diagrams.TwoD.Transform,
Diagrams.TwoD.Transform.ScaleInv,
Diagrams.TwoD.Ellipse,
Diagrams.TwoD.Arc,
Diagrams.TwoD.Segment,
Diagrams.TwoD.Curvature,
Diagrams.TwoD.Image,
Diagrams.TwoD.Model,
Diagrams.TwoD.Offset,
Diagrams.TwoD.Path,
Diagrams.TwoD.Polygons,
Diagrams.TwoD.Segment,
Diagrams.TwoD.Shapes,
Diagrams.TwoD.Vector,
Diagrams.TwoD.Size,
Diagrams.TwoD.Model,
Diagrams.TwoD.Text,
Diagrams.TwoD.Image,
Diagrams.TwoD.Adjust,
Diagrams.TwoD.Transform,
Diagrams.TwoD.Transform.ScaleInv,
Diagrams.TwoD.Types,
Diagrams.TwoD.Vector,
Diagrams.ThreeD,
Diagrams.ThreeD.Align,
Diagrams.ThreeD.Attributes,
Diagrams.ThreeD.Camera,
Expand All @@ -86,7 +86,6 @@ Library
Diagrams.ThreeD.Transform,
Diagrams.ThreeD.Types,
Diagrams.ThreeD.Vector,
Diagrams.ThreeD,
Diagrams.Animation,
Diagrams.Animation.Active,
Diagrams.Util,
Expand All @@ -99,9 +98,6 @@ Library
dual-tree >= 0.2 && < 0.3,
diagrams-core >= 1.2 && < 1.3,
active >= 0.1 && < 0.2,
vector-space >= 0.7.7 && < 0.9,
vector-space-points >= 0.1.2 && < 0.3,
MemoTrie >= 0.6 && < 0.7,
colour >= 2.3.2 && < 2.4,
data-default-class < 0.1,
fingertree >= 0.1 && < 0.2,
Expand All @@ -112,6 +108,9 @@ Library
filepath,
JuicyPixels >= 3.1.5 && < 3.2,
hashable >= 1.1 && < 1.3,
linear >= 1.10 && < 1.11,
adjunctions >= 4.0 && < 5.0,
distributive >=0.2.2 && < 1.0,
process >= 1.1 && < 1.3,
fsnotify >= 0.1 && < 0.2,
directory >= 1.2 && < 1.3,
Expand All @@ -122,3 +121,8 @@ Library
Hs-source-dirs: src
ghc-options: -Wall
default-language: Haskell2010
other-extensions: BangPatterns, CPP, DefaultSignatures, DeriveDataTypeable,
DeriveFunctor, DeriveGeneric, EmptyDataDecls, ExistentialQuantification,
GADTs, GeneralizedNewtypeDeriving, NoMonomorphismRestriction, Rank2Types,
RankNTypes, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell,
TypeOperators, TypeSynonymInstances, UndecidableInstances, ViewPatterns
6 changes: 3 additions & 3 deletions misc/DKSolve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
-- See http://en.wikipedia.org/wiki/Durand–Kerner_method


import Data.Complex
import Data.List (inits, tails)
import Data.Complex
import Data.List (inits, tails)

eps :: Double
eps = 1e-14
Expand Down Expand Up @@ -48,4 +48,4 @@ type C = Complex Double
fixedPt :: Double -> ([C] -> [C]) -> [C] -> [C]
fixedPt eps f as | all ((<eps) . realPart . abs) $ zipWith (-) as as' = as
| otherwise = fixedPt eps f as'
where as' = f as
where as' = f as
29 changes: 29 additions & 0 deletions misc/stylish-haskell.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# stylish-haskell configuration file
# ==================================

steps:
# Import cleanup
- imports:
# global produced the smallest diff
align: global

# Language pragmas
- language_pragmas:
style: vertical
remove_redundant: true

# Align the types in record declarations
- records: {}

# Remove trailing whitespace
- trailing_whitespace: {}

# unused steps - UnicodeSyntax, tabs to spaces

# Wrap to 100 columns, because I feel like it
columns: 100

# No language extensions are enabled by default.
# language_extensions:
# - TemplateHaskell
# - QuasiQuotes
115 changes: 58 additions & 57 deletions src/Diagrams/Align.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ module Diagrams.Align
) where

import Diagrams.Core
import Diagrams.Util (applyAll)
import Diagrams.Util (applyAll)

import Data.AffineSpace (alerp, (.-.))
import Data.VectorSpace
import Data.Maybe (fromMaybe)
import Data.Ord (comparing)
import Data.Maybe (fromMaybe)
import Data.Ord (comparing)

import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Foldable as F
import qualified Data.Foldable as F
import qualified Data.Map as M
import qualified Data.Set as S

import Linear.Affine
import Linear.Metric
import Linear.Vector

-- | Class of things which can be aligned.
class Alignable a where
Expand All @@ -56,113 +58,112 @@ class Alignable a where
-- edge of the boundary in the direction of the negation of @v@.
-- Other values of @d@ interpolate linearly (so for example, @d =
-- 0@ centers the origin along the direction of @v@).
alignBy' :: ( HasOrigin a, AdditiveGroup (V a), Num (Scalar (V a))
, Fractional (Scalar (V a)))
=> (V a -> a -> Point (V a)) -> V a -> Scalar (V a) -> a -> a
alignBy' :: (V a ~ v, N a ~ n, HasOrigin a, Additive v, Fractional n)
=> (v n -> a -> Point v n) -> v n -> n -> a -> a
alignBy' = alignBy'Default

defaultBoundary :: V a -> a -> Point (V a)
defaultBoundary :: (V a ~ v, N a ~ n) => v n -> a -> Point v n

alignBy :: (HasOrigin a, Num (Scalar (V a)), Fractional (Scalar (V a)))
=> V a -> Scalar (V a) -> a -> a
alignBy :: (V a ~ v, N a ~ n, Additive v, HasOrigin a, Fractional n)
=> v n -> n -> a -> a
alignBy = alignBy' defaultBoundary

-- | Default implementation of 'alignBy' for types with 'HasOrigin'
-- and 'AdditiveGroup' instances.
alignBy'Default :: ( HasOrigin a, AdditiveGroup (V a), Num (Scalar (V a))
, Fractional (Scalar (V a)))
=> (V a -> a -> Point (V a)) -> V a -> Scalar (V a) -> a -> a
alignBy'Default boundary v d a = moveOriginTo (alerp (boundary (negateV v) a)
(boundary v a)
((d + 1) / 2)) a
alignBy'Default :: (V a ~ v, N a ~ n, HasOrigin a, Additive v, Fractional n)
=> (v n -> a -> Point v n) -> v n -> n -> a -> a
alignBy'Default boundary v d a = moveOriginTo (lerp ((d + 1) / 2)
(boundary v a)
(boundary (negated v) a)
) a
{-# ANN alignBy'Default ("HLint: ignore Use camelCase" :: String) #-}


-- | Some standard functions which can be used as the `boundary` argument to
-- `alignBy'`.
envelopeBoundary :: Enveloped a => V a -> a -> Point (V a)
envelopeBoundary :: (V a ~ v, N a ~ n, Enveloped a) => v n -> a -> Point v n
envelopeBoundary = envelopeP

traceBoundary :: Traced a => V a -> a -> Point (V a)
traceBoundary :: (V a ~ v, N a ~ n, Num n, Traced a) => v n -> a -> Point v n
traceBoundary v a = fromMaybe origin (maxTraceP origin v a)

combineBoundaries
:: (F.Foldable f, InnerSpace (V a), Ord (Scalar (V a)))
=> (V a -> a -> Point (V a)) -> (V a -> f a -> Point (V a))
:: (V a ~ v, N a ~ n, F.Foldable f, Metric v, Ord n, Num n)
=> (v n -> a -> Point v n) -> v n -> f a -> Point v n
combineBoundaries b v fa
= b v $ F.maximumBy (comparing (magnitudeSq . (.-. origin) . b v)) fa
= b v $ F.maximumBy (comparing (quadrance . (.-. origin) . b v)) fa

instance (InnerSpace v, OrderedField (Scalar v)) => Alignable (Envelope v) where
instance (Metric v, OrderedField n) => Alignable (Envelope v n) where
defaultBoundary = envelopeBoundary

instance (InnerSpace v, OrderedField (Scalar v)) => Alignable (Trace v) where
instance (Metric v, OrderedField n) => Alignable (Trace v n) where
defaultBoundary = traceBoundary

instance (InnerSpace (V b), Ord (Scalar (V b)), Alignable b)
=> Alignable [b] where
instance (V b ~ v, N b ~ n, Metric v, OrderedField n, Alignable b) => Alignable [b] where
defaultBoundary = combineBoundaries defaultBoundary

instance (InnerSpace (V b), Ord (Scalar (V b)), Alignable b)
=> Alignable (S.Set b) where
instance (V b ~ v, N b ~ n, Metric v, OrderedField n, Alignable b)
=> Alignable (S.Set b) where
defaultBoundary = combineBoundaries defaultBoundary

instance (InnerSpace (V b), Ord (Scalar (V b)), Alignable b)
=> Alignable (M.Map k b) where
instance (V b ~ v, N b ~ n, Metric v, OrderedField n, Alignable b)
=> Alignable (M.Map k b) where
defaultBoundary = combineBoundaries defaultBoundary

instance ( HasLinearMap v, InnerSpace v, OrderedField (Scalar v)
, Monoid' m
) => Alignable (QDiagram b v m) where
instance (HasLinearMap v, Metric v, OrderedField n, Monoid' m)
=> Alignable (QDiagram b v n m) where
defaultBoundary = envelopeBoundary

-- | Although the 'alignBy' method for the @(b -> a)@ instance is
-- sensible, there is no good implementation for
-- 'defaultBoundary'. Instead, we provide a total method, but one that
-- is not sensible. This should not present a serious problem as long
-- as your use of 'Alignable' happens through 'alignBy'.
instance (HasOrigin a, Alignable a) => Alignable (b -> a) where
alignBy v d f b = alignBy v d (f b)
instance (V a ~ v, N a ~ n, Additive v, Num n, HasOrigin a, Alignable a) => Alignable (b -> a) where
alignBy v d f b = alignBy v d (f b)
defaultBoundary _ _ = origin

-- | @align v@ aligns an enveloped object along the edge in the
-- direction of @v@. That is, it moves the local origin in the
-- direction of @v@ until it is on the edge of the envelope. (Note
-- that if the local origin is outside the envelope to begin with,
-- it may have to move \"backwards\".)
align :: ( Alignable a, HasOrigin a, Num (Scalar (V a))
, Fractional (Scalar (V a))) => V a -> a -> a
align :: (V a ~ v, N a ~ n, Additive v, Alignable a, HasOrigin a, Fractional n) => v n -> a -> a
align v = alignBy v 1

-- | Version of @alignBy@ specialized to use @traceBoundary@
snugBy :: (Alignable a, Traced a, HasOrigin a, Num (Scalar (V a)), Fractional (Scalar (V a)))
=> V a -> Scalar (V a) -> a -> a
snugBy :: (V a ~ v, N a ~ n, Alignable a, Traced a, HasOrigin a, Fractional n)
=> v n -> n -> a -> a
snugBy = alignBy' traceBoundary

-- | Like align but uses trace.
snug :: (Fractional (Scalar (V a)), Alignable a, Traced a, HasOrigin a)
=> V a -> a -> a
snug v = snugBy v 1
snug :: (V a ~ v, N a ~ n, Fractional n, Alignable a, Traced a, HasOrigin a)
=> v n -> a -> a
snug v = snugBy v 1

-- | @centerV v@ centers an enveloped object along the direction of
-- @v@.
centerV :: ( Alignable a, HasOrigin a, Num (Scalar (V a))
, Fractional (Scalar (V a))) => V a -> a -> a
centerV :: (V a ~ v, N a ~ n, Additive v, Alignable a, HasOrigin a, Fractional n) => v n -> a -> a
centerV v = alignBy v 0

-- | @center@ centers an enveloped object along all of its basis vectors.
center :: ( HasLinearMap (V a), Alignable a, HasOrigin a, Num (Scalar (V a)),
Fractional (Scalar (V a))) => a -> a
center d = applyAll fs d
center :: (V a ~ v, N a ~ n, HasLinearMap v, Alignable a, HasOrigin a, Fractional n) => a -> a
center = applyAll fs
where
fs = map centerV basis

-- | Like @centerV@ using trace.
snugCenterV
:: (Fractional (Scalar (V a)), Alignable a, Traced a, HasOrigin a)
=> V a -> a -> a
snugCenterV v = (alignBy' traceBoundary) v 0
:: (V a ~ v, N a ~ n, Fractional n, Alignable a, Traced a, HasOrigin a)
=> v n -> a -> a
snugCenterV v = alignBy' traceBoundary v 0

-- | Like @center@ using trace.
snugCenter :: ( HasLinearMap (V a), Alignable a, HasOrigin a, Num (Scalar (V a)),
Fractional (Scalar (V a)), Traced a) => a -> a
snugCenter d = applyAll fs d
snugCenter :: (V a ~ v, N a ~ n, HasLinearMap v, Alignable a, HasOrigin a, Fractional n, Traced a)
=> a -> a
snugCenter = applyAll fs
where
fs = map snugCenterV basis
fs = map snugCenterV basis

{-# ANN module ("HLint: ignore Use camelCase" :: String) #-}

Loading

0 comments on commit 5c64686

Please sign in to comment.