-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port to linear #212
Closed
cchalmers
wants to merge
26
commits into
diagrams:generalize-double
from
cchalmers:generalize-double
Closed
Port to linear #212
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
2a40713
Some progress.
cchalmers 4b8469a
More progress.
cchalmers 48cbd1d
Bounding box.
cchalmers fe4892a
Bounding box.
cchalmers a70c9cf
Merge branch 'generalize-double' of https://github.com/cchalmers/diag…
cchalmers 552c822
Starting on 2D and 3D modules.
cchalmers a267069
Change VN to Vn.
cchalmers 71d5ada
Almost done.
cchalmers 9961036
Some Bergey sugestions. (and combinators)
cchalmers 3a5ff43
Builds without vector-space.
cchalmers e1894c6
Update for new lenses.
cchalmers f237e03
Coordinate instances for linear types.
cchalmers 79c9a2e
Remove unused types.
cchalmers cf195d0
Use linear's classes for _x, _y, _z.
cchalmers c989031
Uncomment default pragma.
cchalmers 26582ff
Bring back prelude.
cchalmers 15751d7
lerp has arguments reversed in linear.
cchalmers 507309a
Added (poor) Traced instances for Bounding box.
cchalmers 46a5a9e
General cleanup.
cchalmers 317c7ec
Merge Prelude.ThreeD with Prelude.
cchalmers d4a5fbd
Added Prelude back.
cchalmers 5d68ad7
Added Polar, Cylindrical and Spherical coordinates.
cchalmers 7e5ca2b
Use stylish-haskell config.
cchalmers e24baef
Fixed scaling measures.
cchalmers a11a0f3
Fix bug with showOrigin.
cchalmers 8457055
Postpone new polar coordinate and general cleanup.
cchalmers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.Maybe (fromMaybe) | ||
import Data.Ord (comparing) | ||
import Data.VectorSpace | ||
import Data.Maybe (fromMaybe) | ||
import Data.Ord (comparing) | ||
|
||
import qualified Data.Foldable as F | ||
import qualified Data.Map as M | ||
import qualified Data.Set as S | ||
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 | ||
|
@@ -56,113 +58,111 @@ 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' :: (Vn a ~ v 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 :: Vn a ~ v 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 :: (Vn a ~ v 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 :: (Vn a ~ v 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 | ||
|
||
|
||
-- | Some standard functions which can be used as the `boundary` argument to | ||
-- `alignBy'`. | ||
envelopeBoundary :: Enveloped a => V a -> a -> Point (V a) | ||
envelopeBoundary :: (Vn a ~ v n, Enveloped a) => v n -> a -> Point v n | ||
envelopeBoundary = envelopeP | ||
|
||
traceBoundary :: Traced a => V a -> a -> Point (V a) | ||
traceBoundary :: (Vn a ~ v 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)) | ||
:: (Vn a ~ v 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 (Vn b ~ v 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 (Vn b ~ v 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 (Vn b ~ v 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 (Vn a ~ v 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 :: (Vn a ~ v 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 :: (Vn a ~ v 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 :: (Vn a ~ v 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 :: (Vn a ~ v 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 :: (Vn a ~ v 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 | ||
:: (Vn a ~ v 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 :: (Vn a ~ v n, HasLinearMap v, Alignable a, HasOrigin a, Fractional n, Traced a) | ||
=> a -> a | ||
snugCenter = applyAll fs | ||
where | ||
fs = map snugCenterV basis | ||
|
||
{-# ANN module ("HLint: ignore Use camelCase" :: String) #-} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To stop HLint complaining about |
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two arguments seem switched ---does
lerp
work dually toalerp
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes:
alerp a b t = lerp t b a
. This should be mentioned in the notes. Spent a while debugging to find this was the culprit (it's not even mentioned inlinear
).