Debian Codegraph @

src/Diagrams/TwoD/Transform.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE RankNTypes #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE TypeOperators #-}
8 {-# LANGUAGE ViewPatterns #-}
9
10 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
11 -- for Data.Semigroup
12
13 -----------------------------------------------------------------------------
14 -- |
15 -- Module : Diagrams.TwoD.Transform
16 -- Copyright : (c) 2011-2015 diagrams-lib team (see LICENSE)
17 -- License : BSD-style (see LICENSE)
18 -- Maintainer : [email protected]
19 --
20 -- Transformations specific to two dimensions, with a few generic
21 -- transformations (uniform scaling, translation) also re-exported for
22 -- convenience.
23 --
24 -----------------------------------------------------------------------------
25
26 module Diagrams.TwoD.Transform
27 (
28 T2
29 -- * Rotation
30 , rotation, rotate, rotateBy, rotated
31 , rotationAround, rotateAround
32 , rotationTo, rotateTo
33
34 -- * Scaling
35 , scalingX, scaleX
36 , scalingY, scaleY
37 , scaling, scale
38
39 , scaleToX, scaleToY
40 , scaleUToX, scaleUToY
41
42 -- * Translation
43 , translationX, translateX
44 , translationY, translateY
45 , translation, translate
46
47 -- * Conformal affine maps
48 , scalingRotationTo, scaleRotateTo
49
50 -- * Reflection
51 , reflectionX, reflectX
52 , reflectionY, reflectY
53 , reflectionXY, reflectXY
54 , reflectionAbout, reflectAbout
55
56 -- * Shears
57 , shearingX, shearX
58 , shearingY, shearY
59
60 ) where
61
62 import Diagrams.Angle
63 import Diagrams.Core
64 import Diagrams.Core.Transform
65 import Diagrams.Direction
66 import Diagrams.Transform
67 import Diagrams.Transform.Matrix
68 import Diagrams.TwoD.Types
69 import Diagrams.TwoD.Vector
70
71 import Control.Lens hiding (at, transform)
72 import Data.Semigroup
73
74 import Linear.Affine
75 import Linear.Metric
76 import Linear.V2
77 import Linear.Vector
78
79 -- Rotation ------------------------------------------------
80
81 -- For the definitions of 'rotation' and 'rotate', see Diagrams.Angle.
82
83 -- | A synonym for 'rotate', interpreting its argument in units of
84 -- turns; it can be more convenient to write @rotateBy (1\/4)@ than
85 -- @'rotate' (1\/4 \@\@ 'turn')@.
86 rotateBy :: (InSpace V2 n t, Transformable t, Floating n) => n -> t -> t
87 rotateBy = transform . rotation . review turn
88
89 -- | Use an 'Angle' to make an 'Iso' between an object
90 -- rotated and unrotated. This us useful for performing actions
91 -- 'under' a rotation:
92 --
93 -- @
94 -- under (rotated t) f = rotate (negated t) . f . rotate t
95 -- rotated t ## a = rotate t a
96 -- a ^. rotated t = rotate (-t) a
97 -- over (rotated t) f = rotate t . f . rotate (negated t)
98 -- @
99 rotated :: (InSpace V2 n a, Floating n, SameSpace a b, Transformable a, Transformable b)
100 => Angle n -> Iso a b a b
101 rotated a = transformed $ rotation a
102
103 -- | @rotationAbout p@ is a rotation about the point @p@ (instead of
104 -- around the local origin).
105 rotationAround :: Floating n => P2 n -> Angle n -> T2 n
106 rotationAround p theta =
107 conjugate (translation (origin .-. p)) (rotation theta)
108
109 -- | @rotateAbout p@ is like 'rotate', except it rotates around the
110 -- point @p@ instead of around the local origin.
111 rotateAround :: (InSpace V2 n t, Transformable t, Floating n)
112 => P2 n -> Angle n -> t -> t
113 rotateAround p theta = transform (rotationAround p theta)
114
115 -- | The rotation that aligns the x-axis with the given direction.
116 rotationTo :: OrderedField n => Direction V2 n -> T2 n
117 rotationTo (view _Dir -> V2 x y) = rotation (atan2A' y x)
118
119 -- | Rotate around the local origin such that the x axis aligns with the
120 -- given direction.
121 rotateTo :: (InSpace V2 n t, OrderedField n, Transformable t) => Direction V2 n -> t -> t
122 rotateTo = transform . rotationTo
123
124 -- Scaling -------------------------------------------------
125
126 -- | Construct a transformation which scales by the given factor in
127 -- the x (horizontal) direction.
128 scalingX :: (Additive v, R1 v, Fractional n) => n -> Transformation v n
129 scalingX c = fromSymmetric $ (_x *~ c) <-> (_x //~ c)
130
131 -- | Scale a diagram by the given factor in the x (horizontal)
132 -- direction. To scale uniformly, use 'scale'.
133 scaleX :: (InSpace v n t, R2 v, Fractional n, Transformable t) => n -> t -> t
134 scaleX = transform . scalingX
135
136 -- | Construct a transformation which scales by the given factor in
137 -- the y (vertical) direction.
138 scalingY :: (Additive v, R2 v, Fractional n) => n -> Transformation v n
139 scalingY c = fromSymmetric $ (_y *~ c) <-> (_y //~ c)
140
141 -- | Scale a diagram by the given factor in the y (vertical)
142 -- direction. To scale uniformly, use 'scale'.
143 scaleY :: (InSpace v n t, R2 v, Fractional n, Transformable t)
144 => n -> t -> t
145 scaleY = transform . scalingY
146
147 -- | @scaleToX w@ scales a diagram in the x (horizontal) direction by
148 -- whatever factor required to make its width @w@. @scaleToX@
149 -- should not be applied to diagrams with a width of 0, such as
150 -- 'vrule'.
151 scaleToX :: (InSpace v n t, R2 v, Enveloped t, Transformable t) => n -> t -> t
152 scaleToX w d = scaleX (w / diameter unitX d) d
153
154 -- | @scaleToY h@ scales a diagram in the y (vertical) direction by
155 -- whatever factor required to make its height @h@. @scaleToY@
156 -- should not be applied to diagrams with a height of 0, such as
157 -- 'hrule'.
158 scaleToY :: (InSpace v n t, R2 v, Enveloped t, Transformable t) => n -> t -> t
159 scaleToY h d = scaleY (h / diameter unitY d) d
160
161 -- | @scaleUToX w@ scales a diagram /uniformly/ by whatever factor
162 -- required to make its width @w@. @scaleUToX@ should not be
163 -- applied to diagrams with a width of 0, such as 'vrule'.
164 scaleUToX :: (InSpace v n t, R1 v, Enveloped t, Transformable t) => n -> t -> t
165 scaleUToX w d = scale (w / diameter unitX d) d
166
167 -- | @scaleUToY h@ scales a diagram /uniformly/ by whatever factor
168 -- required to make its height @h@. @scaleUToY@ should not be applied
169 -- to diagrams with a height of 0, such as 'hrule'.
170 scaleUToY :: (InSpace v n t, R2 v, Enveloped t, Transformable t) => n -> t -> t
171 scaleUToY h d = scale (h / diameter unitY d) d
172
173 -- Translation ---------------------------------------------
174
175 -- | Construct a transformation which translates by the given distance
176 -- in the x (horizontal) direction.
177 translationX :: (Additive v, R1 v, Num n) => n -> Transformation v n
178 translationX x = translation (zero & _x .~ x)
179
180 -- | Translate a diagram by the given distance in the x (horizontal)
181 -- direction.
182 translateX :: (InSpace v n t, R1 v, Transformable t) => n -> t -> t
183 translateX = transform . translationX
184
185 -- | Construct a transformation which translates by the given distance
186 -- in the y (vertical) direction.
187 translationY :: (Additive v, R2 v, Num n) => n -> Transformation v n
188 translationY y = translation (zero & _y .~ y)
189
190 -- | Translate a diagram by the given distance in the y (vertical)
191 -- direction.
192 translateY :: (InSpace v n t, R2 v, Transformable t)
193 => n -> t -> t
194 translateY = transform . translationY
195
196 -- Conformal affine maps -----------------------------------
197
198 -- | The angle-preserving linear map that aligns the x-axis unit vector
199 -- with the given vector. See also 'scaleRotateTo'.
200 scalingRotationTo :: (Floating n) => V2 n -> T2 n
201 scalingRotationTo v = fromMatWithInv (conf v) (conf w) zero
202 where
203 w = reflectY (v ^/ quadrance v)
204 conf (V2 a b) = (V2 (V2 a (-b)) (V2 b a))
205
206 -- | Rotate and uniformly scale around the local origin such that the
207 -- x-axis aligns with the given vector. This satisfies the equation
208 --
209 -- @
210 -- scaleRotateTo v = rotateTo (dir v) . scale (norm v)
211 -- @
212 --
213 -- up to floating point rounding errors, but is more accurate and
214 -- performant since it avoids cancellable uses of trigonometric functions.
215 scaleRotateTo :: (InSpace V2 n t, Transformable t, Floating n)
216 => V2 n -> t -> t
217 scaleRotateTo = transform . scalingRotationTo
218
219 -- Reflection ----------------------------------------------
220
221 -- | Construct a transformation which flips a diagram from left to
222 -- right, i.e. sends the point (x,y) to (-x,y).
223 reflectionX :: (Additive v, R1 v, Num n) => Transformation v n
224 reflectionX = fromSymmetric $ (_x *~ (-1)) <-> (_x *~ (-1))
225
226 -- | Flip a diagram from left to right, i.e. send the point (x,y) to
227 -- (-x,y).
228 reflectX :: (InSpace v n t, R1 v, Transformable t) => t -> t
229 reflectX = transform reflectionX
230
231 -- | Construct a transformation which flips a diagram from top to
232 -- bottom, i.e. sends the point (x,y) to (x,-y).
233 reflectionY :: (Additive v, R2 v, Num n) => Transformation v n
234 reflectionY = fromSymmetric $ (_y *~ (-1)) <-> (_y *~ (-1))
235
236 -- | Flip a diagram from top to bottom, i.e. send the point (x,y) to
237 -- (x,-y).
238 reflectY :: (InSpace v n t, R2 v, Transformable t) => t -> t
239 reflectY = transform reflectionY
240
241 -- | Construct a transformation which flips the diagram about x=y, i.e.
242 -- sends the point (x,y) to (y,x).
243 reflectionXY :: (Additive v, R2 v, Num n) => Transformation v n
244 reflectionXY = fromSymmetric $ (_xy %~ view _yx) <-> (_xy %~ view _yx)
245
246 -- | Flips the diagram about x=y, i.e. send the point (x,y) to (y,x).
247 reflectXY :: (InSpace v n t, R2 v, Transformable t) => t -> t
248 reflectXY = transform reflectionXY
249
250 -- | @reflectionAbout p d@ is a reflection in the line determined by
251 -- the point @p@ and direction @d@.
252 reflectionAbout :: OrderedField n => P2 n -> Direction V2 n -> T2 n
253 reflectionAbout p d =
254 conjugate (rotationTo (reflectY d) <> translation (origin .-. p))
255 reflectionY
256
257
258
259
260 -- | @reflectAbout p d@ reflects a diagram in the line determined by
261 -- the point @p@ and direction @d@.
262 reflectAbout :: (InSpace V2 n t, OrderedField n, Transformable t)
263 => P2 n -> Direction V2 n -> t -> t
264 reflectAbout p v = transform (reflectionAbout p v)
265
266 -- Shears --------------------------------------------------
267
268 -- auxiliary functions for shearingX/shearingY
269 sh :: (n -> n -> n -> n) -> (n -> n -> n -> n) -> n -> V2 n -> V2 n
270 sh f g k (V2 x y) = V2 (f k x y) (g k x y)
271
272 sh' :: (n -> n -> n -> n) -> (n -> n -> n -> n) -> n -> V2 n -> V2 n
273 sh' f g k = swap . sh f g k . swap
274
275 swap :: V2 n -> V2 n
276 swap (V2 x y) = V2 y x
277 {-# INLINE swap #-}
278
279 -- | @shearingX d@ is the linear transformation which is the identity on
280 -- y coordinates and sends @(0,1)@ to @(d,1)@.
281 shearingX :: Num n => n -> T2 n
282 shearingX d = fromLinear (sh f g d <-> sh f g (-d))
283 (sh' f g d <-> sh' f g (-d))
284 where
285 f k x y = x + k*y
286 g _ _ y = y
287
288 -- | @shearX d@ performs a shear in the x-direction which sends
289 -- @(0,1)@ to @(d,1)@.
290 shearX :: (InSpace V2 n t, Transformable t) => n -> t -> t
291 shearX = transform . shearingX
292
293 -- | @shearingY d@ is the linear transformation which is the identity on
294 -- x coordinates and sends @(1,0)@ to @(1,d)@.
295 shearingY :: Num n => n -> T2 n
296 shearingY d = fromLinear (sh f g d <-> sh f g (-d))
297 (sh' f g d <-> sh' f g (-d))
298 where
299 f _ x _ = x
300 g k x y = y + k*x
301
302 -- | @shearY d@ performs a shear in the y-direction which sends
303 -- @(1,0)@ to @(1,d)@.
304 shearY :: (InSpace V2 n t, Transformable t) => n -> t -> t
305 shearY = transform . shearingY