Skip to content

Commit

Permalink
Add Reversing instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
cchalmers committed Mar 9, 2015
1 parent 77a411e commit 6edb069
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Diagrams/Path.hs
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,5 @@ scalePath d p = under (movedFrom (pathCentroid p)) (scale d) p
reversePath :: (Metric v, OrderedField n) => Path v n -> Path v n
reversePath = _Wrapped . mapped %~ reverseLocTrail

instance (Metric v, OrderedField n) => Reversing (Path v n) where
reversing = _Wrapped' . mapped %~ reversing
16 changes: 13 additions & 3 deletions src/Diagrams/Segment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ module Diagrams.Segment

) where

import Control.Lens (Each (..), Rewrapped, Wrapped (..),
iso, makeLenses, op, over)
import Control.Lens hiding (at, transform)
import Data.FingerTree
import Data.Monoid.MList
import Data.Semigroup
Expand Down Expand Up @@ -122,6 +121,10 @@ instance Each (Offset c v n) (Offset c v' n') (v n) (v' n') where
each _ OffsetOpen = pure OffsetOpen
{-# INLINE each #-}

instance (Additive v, Num n) => Reversing (Offset c v n) where
reversing (OffsetClosed off) = OffsetClosed $ negated off
reversing a@OffsetOpen = a

type instance V (Offset c v n) = v
type instance N (Offset c v n) = n

Expand Down Expand Up @@ -172,6 +175,9 @@ instance Each (Segment c v n) (Segment c v' n') (v n) (v' n') where
each f (Cubic v1 v2 offset) = Cubic <$> f v1 <*> f v2 <*> each f offset
{-# INLINE each #-}

instance (Additive v, Num n) => Reversing (Segment Closed v n) where
reversing = reverseSegment

-- | Map over the vectors of each segment.
mapSegmentVectors :: (v n -> v' n') -> Segment c v n -> Segment c v' n'
mapSegmentVectors = over each
Expand Down Expand Up @@ -314,7 +320,7 @@ reverseSegment :: (Num n, Additive v) => Segment Closed v n -> Segment Closed v
reverseSegment (Linear (OffsetClosed v)) = straight (negated v)
reverseSegment (Cubic c1 c2 (OffsetClosed x2)) = bezier3 (c2 ^-^ x2) (c1 ^-^ x2) (negated x2)

instance (Metric v, Floating n, Ord n, Additive v)
instance (Metric v, OrderedField n)
=> HasArcLength (Segment Closed v n) where

arcLengthBounded _ (Linear (OffsetClosed x1)) = I.singleton $ norm x1
Expand Down Expand Up @@ -364,6 +370,10 @@ instance Each (FixedSegment v n) (FixedSegment v' n') (Point v n) (Point v' n')
each f (FCubic p0 p1 p2 p3) = FCubic <$> f p0 <*> f p1 <*> f p2 <*> f p3
{-# INLINE each #-}

instance Reversing (FixedSegment v n) where
reversing (FLinear p0 p1) = FLinear p1 p0
reversing (FCubic p0 p1 p2 p3) = FCubic p3 p2 p1 p0

instance (Additive v, Num n) => Transformable (FixedSegment v n) where
transform t = over each (papply t)

Expand Down
14 changes: 14 additions & 0 deletions src/Diagrams/Trail.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,3 +1247,17 @@ reverseLocLoop :: (Metric v, OrderedField n)
=> Located (Trail' Loop v n) -> Located (Trail' Loop v n)
reverseLocLoop = mapLoc reverseLoop

instance (Metric v, OrderedField n) => Reversing (Trail' l v n) where
reversing t@(Line _) = onLineSegments (reverse . map reversing) t
reversing t@(Loop _ _) = glueLine . reversing . cutLoop $ t

instance (Metric v, OrderedField n) => Reversing (Trail v n) where
reversing (Trail t) = Trail (reversing t)

instance (Metric v, OrderedField n) => Reversing (Located (Trail' l v n)) where
reversing l@(Loc _ Line {}) = reverseLocLine l
reversing l@(Loc _ Loop {}) = reverseLocLoop l

instance (Metric v, OrderedField n) => Reversing (Located (Trail v n)) where
reversing = reverseLocTrail

3 comments on commit 6edb069

@bergey
Copy link
Member

@bergey bergey commented on 6edb069 Mar 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pun on Reversing. I think I like it, but these instances are not "a generalized notion of list reversal extended to other containers" as the docs for Reversing have it.

@cchalmers
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure about it. But don't think people would expect it be onLineSegments reverse. Maybe I could add comments to make this clear. I'm not desperate to have this, the reversed iso just seemed to fit nicely with the rest of the PR.

@bergey
Copy link
Member

@bergey bergey commented on 6edb069 Mar 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that onLineSegments reverse is much less useful, and would surprise users in a bad way. I think these definitions are useful; adding a comment would make them better.

Please sign in to comment.