packages feed

diagrams-lib-1.4.5.1 / src / Diagrams / TwoD / Transform.hs

{-# LANGUAGE ConstraintKinds #-}{-# LANGUAGE FlexibleContexts      #-}{-# LANGUAGE FlexibleInstances     #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE RankNTypes            #-}{-# LANGUAGE TypeFamilies          #-}{-# LANGUAGE TypeOperators         #-}{-# LANGUAGE ViewPatterns          #-}{-# OPTIONS_GHC -fno-warn-unused-imports #-}  -- for Data.Semigroup------------------------------------------------------------------------------- |-- Module      :  Diagrams.TwoD.Transform-- Copyright   :  (c) 2011-2015 diagrams-lib team (see LICENSE)-- License     :  BSD-style (see LICENSE)-- Maintainer  :  diagrams-discuss@googlegroups.com---- Transformations specific to two dimensions, with a few generic-- transformations (uniform scaling, translation) also re-exported for-- convenience.-------------------------------------------------------------------------------module Diagrams.TwoD.Transform       (         T2         -- * Rotation       , rotation, rotate, rotateBy, rotated       , rotationAround, rotateAround       , rotationTo, rotateTo         -- * Scaling       , scalingX, scaleX       , scalingY, scaleY       , scaling, scale       , scaleToX, scaleToY       , scaleUToX, scaleUToY         -- * Translation       , translationX, translateX       , translationY, translateY       , translation, translate         -- * Conformal affine maps       , scalingRotationTo, scaleRotateTo         -- * Reflection       , reflectionX, reflectX       , reflectionY, reflectY       , reflectionXY, reflectXY       , reflectionAbout, reflectAbout         -- * Shears       , shearingX, shearX       , shearingY, shearY       ) whereimport           Diagrams.Angleimport           Diagrams.Coreimport           Diagrams.Core.Transformimport           Diagrams.Directionimport           Diagrams.Transformimport           Diagrams.Transform.Matriximport           Diagrams.TwoD.Typesimport           Diagrams.TwoD.Vectorimport           Control.Lens              hiding (at, transform)import           Data.Semigroupimport           Linear.Affineimport           Linear.Metricimport           Linear.V2import           Linear.Vector-- Rotation -------------------------------------------------- For the definitions of 'rotation' and 'rotate', see Diagrams.Angle.-- | A synonym for 'rotate', interpreting its argument in units of-- turns; it can be more convenient to write @rotateBy (1\/4)@ than-- @'rotate' (1\/4 \@\@ 'turn')@.rotateBy :: (InSpace V2 n t, Transformable t, Floating n) => n -> t -> trotateBy = transform . rotation . review turn-- | Use an 'Angle' to make an 'Iso' between an object--   rotated and unrotated. This us useful for performing actions--   'under' a rotation:---- @-- under (rotated t) f = rotate (negated t) . f . rotate t-- rotated t ## a      = rotate t a-- a ^. rotated t      = rotate (-t) a-- over (rotated t) f  = rotate t . f . rotate (negated t)-- @rotated :: (InSpace V2 n a, Floating n, SameSpace a b, Transformable a, Transformable b)        => Angle n -> Iso a b a brotated a = transformed $ rotation a-- | @rotationAbout p@ is a rotation about the point @p@ (instead of--   around the local origin).rotationAround :: Floating n => P2 n -> Angle n -> T2 nrotationAround p theta =  conjugate (translation (origin .-. p)) (rotation theta)-- | @rotateAbout p@ is like 'rotate', except it rotates around the--   point @p@ instead of around the local origin.rotateAround :: (InSpace V2 n t, Transformable t, Floating n)             => P2 n -> Angle n -> t -> trotateAround p theta = transform (rotationAround p theta)-- | The rotation that aligns the x-axis with the given direction.rotationTo :: OrderedField n => Direction V2 n -> T2 nrotationTo (view _Dir -> V2 x y) = rotation (atan2A' y x)-- | Rotate around the local origin such that the x axis aligns with the--   given direction.rotateTo :: (InSpace V2 n t, OrderedField n, Transformable t) => Direction V2 n -> t -> trotateTo = transform . rotationTo-- Scaling --------------------------------------------------- | Construct a transformation which scales by the given factor in--   the x (horizontal) direction.scalingX :: (Additive v, R1 v, Fractional n) => n -> Transformation v nscalingX c = fromSymmetric $ (_x *~ c) <-> (_x //~ c)-- | Scale a diagram by the given factor in the x (horizontal)--   direction.  To scale uniformly, use 'scale'.scaleX :: (InSpace v n t, R2 v, Fractional n, Transformable t) => n -> t -> tscaleX = transform . scalingX-- | Construct a transformation which scales by the given factor in--   the y (vertical) direction.scalingY :: (Additive v, R2 v, Fractional n) => n -> Transformation v nscalingY c = fromSymmetric $ (_y *~ c) <-> (_y //~ c)-- | Scale a diagram by the given factor in the y (vertical)--   direction.  To scale uniformly, use 'scale'.scaleY :: (InSpace v n t, R2 v, Fractional n, Transformable t)  => n -> t -> tscaleY = transform . scalingY-- | @scaleToX w@ scales a diagram in the x (horizontal) direction by--   whatever factor required to make its width @w@.  @scaleToX@--   should not be applied to diagrams with a width of 0, such as--   'vrule'.scaleToX :: (InSpace v n t, R2 v, Enveloped t, Transformable t) => n -> t -> tscaleToX w d = scaleX (w / diameter unitX d) d-- | @scaleToY h@ scales a diagram in the y (vertical) direction by--   whatever factor required to make its height @h@.  @scaleToY@--   should not be applied to diagrams with a height of 0, such as--   'hrule'.scaleToY :: (InSpace v n t, R2 v, Enveloped t, Transformable t) => n -> t -> tscaleToY h d = scaleY (h / diameter unitY d) d-- | @scaleUToX w@ scales a diagram /uniformly/ by whatever factor--   required to make its width @w@.  @scaleUToX@ should not be--   applied to diagrams with a width of 0, such as 'vrule'.scaleUToX :: (InSpace v n t, R1 v, Enveloped t, Transformable t) => n -> t -> tscaleUToX w d = scale (w / diameter unitX d) d-- | @scaleUToY h@ scales a diagram /uniformly/ by whatever factor--   required to make its height @h@.  @scaleUToY@ should not be applied--   to diagrams with a height of 0, such as 'hrule'.scaleUToY :: (InSpace v n t, R2 v, Enveloped t, Transformable t) => n -> t -> tscaleUToY h d = scale (h / diameter unitY d) d-- Translation ----------------------------------------------- | Construct a transformation which translates by the given distance--   in the x (horizontal) direction.translationX :: (Additive v, R1 v, Num n) => n -> Transformation v ntranslationX x = translation (zero & _x .~ x)-- | Translate a diagram by the given distance in the x (horizontal)--   direction.translateX :: (InSpace v n t, R1 v, Transformable t) => n -> t -> ttranslateX = transform . translationX-- | Construct a transformation which translates by the given distance--   in the y (vertical) direction.translationY :: (Additive v, R2 v, Num n) => n -> Transformation v ntranslationY y = translation (zero & _y .~ y)-- | Translate a diagram by the given distance in the y (vertical)--   direction.translateY :: (InSpace v n t, R2 v, Transformable t)  => n -> t -> ttranslateY = transform . translationY-- Conformal affine maps ------------------------------------- | The angle-preserving linear map that aligns the x-axis unit vector--   with the given vector.  See also 'scaleRotateTo'.scalingRotationTo :: (Floating n) => V2 n -> T2 nscalingRotationTo v = fromMatWithInv (conf v) (conf w) zero  where    w = reflectY (v ^/ quadrance v)    conf (V2 a b) = (V2 (V2 a (-b)) (V2 b a))-- | Rotate and uniformly scale around the local origin such that the--   x-axis aligns with the given vector.  This satisfies the equation---- @-- scaleRotateTo v = rotateTo (dir v) . scale (norm v)-- @---- up to floating point rounding errors, but is more accurate and-- performant since it avoids cancellable uses of trigonometric functions.scaleRotateTo :: (InSpace V2 n t, Transformable t, Floating n)              => V2 n -> t -> tscaleRotateTo = transform . scalingRotationTo-- Reflection ------------------------------------------------ | Construct a transformation which flips a diagram from left to--   right, i.e. sends the point (x,y) to (-x,y).reflectionX :: (Additive v, R1 v, Num n) => Transformation v nreflectionX = fromSymmetric $ (_x *~ (-1)) <-> (_x *~ (-1))-- | Flip a diagram from left to right, i.e. send the point (x,y) to--   (-x,y).reflectX :: (InSpace v n t, R1 v, Transformable t) => t -> treflectX = transform reflectionX-- | Construct a transformation which flips a diagram from top to--   bottom, i.e. sends the point (x,y) to (x,-y).reflectionY :: (Additive v, R2 v, Num n) => Transformation v nreflectionY = fromSymmetric $ (_y *~ (-1)) <-> (_y *~ (-1))-- | Flip a diagram from top to bottom, i.e. send the point (x,y) to--   (x,-y).reflectY :: (InSpace v n t, R2 v, Transformable t) => t -> treflectY = transform reflectionY-- | Construct a transformation which flips the diagram about x=y, i.e.--   sends the point (x,y) to (y,x).reflectionXY :: (Additive v, R2 v, Num n) => Transformation v nreflectionXY = fromSymmetric $ (_xy %~ view _yx) <-> (_xy %~ view _yx)-- | Flips the diagram about x=y, i.e. send the point (x,y) to (y,x).reflectXY :: (InSpace v n t, R2 v, Transformable t) => t -> treflectXY = transform reflectionXY-- | @reflectionAbout p d@ is a reflection in the line determined by--   the point @p@ and direction @d@.reflectionAbout :: OrderedField n => P2 n -> Direction V2 n -> T2 nreflectionAbout p d =  conjugate (rotationTo (reflectY d) <> translation (origin .-. p))            reflectionY-- | @reflectAbout p d@ reflects a diagram in the line determined by--   the point @p@ and direction @d@.reflectAbout :: (InSpace V2 n t, OrderedField n, Transformable t)             => P2 n -> Direction V2 n -> t -> treflectAbout p v = transform (reflectionAbout p v)-- Shears ---------------------------------------------------- auxiliary functions for shearingX/shearingYsh :: (n -> n -> n -> n) -> (n -> n -> n -> n) -> n -> V2 n -> V2 nsh f g k (V2 x y) = V2 (f k x y) (g k x y)sh' :: (n -> n -> n -> n) -> (n -> n -> n -> n) -> n -> V2 n -> V2 nsh' f g k = swap . sh f g k . swapswap :: V2 n -> V2 nswap (V2 x y) = V2 y x{-# INLINE swap #-}-- | @shearingX d@ is the linear transformation which is the identity on--   y coordinates and sends @(0,1)@ to @(d,1)@.shearingX :: Num n => n -> T2 nshearingX d = fromLinear (sh f g d  <-> sh f g (-d))                         (sh' f g d <-> sh' f g (-d))  where    f k x y = x + k*y    g _ _ y = y-- | @shearX d@ performs a shear in the x-direction which sends--   @(0,1)@ to @(d,1)@.shearX :: (InSpace V2 n t, Transformable t) => n -> t -> tshearX = transform . shearingX-- | @shearingY d@ is the linear transformation which is the identity on--   x coordinates and sends @(1,0)@ to @(1,d)@.shearingY :: Num n => n -> T2 nshearingY d = fromLinear (sh f g d  <-> sh f g (-d))                         (sh' f g d <-> sh' f g (-d))        where          f _ x _ = x          g k x y = y + k*x-- | @shearY d@ performs a shear in the y-direction which sends--   @(1,0)@ to @(1,d)@.shearY :: (InSpace V2 n t, Transformable t) => n -> t -> tshearY = transform . shearingY