Debian Codegraph @

src/Diagrams/Transform/ScaleInv.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE StandaloneDeriving #-}
8 {-# LANGUAGE TemplateHaskell #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# LANGUAGE TypeOperators #-}
11 {-# LANGUAGE UndecidableInstances #-}
12 -----------------------------------------------------------------------------
13 -- |
14 -- Module : Diagrams.TwoD.Transform.ScaleInv
15 -- Copyright : (c) 2012-2013 diagrams-lib team (see LICENSE)
16 -- License : BSD-style (see LICENSE)
17 -- Maintainer : [email protected]
18 --
19 -- Wrapper for creating scale-invariant objects in two dimensions.
20 --
21 -----------------------------------------------------------------------------
22
23 module Diagrams.Transform.ScaleInv
24 ( ScaleInv(..)
25 , scaleInvObj, scaleInvDir, scaleInvLoc
26 , scaleInv, scaleInvPrim)
27 where
28
29 import Control.Lens (makeLenses, view, (^.))
30 import Data.Typeable
31
32 import Diagrams.Angle
33 import Diagrams.Core
34 import Diagrams.TwoD.Transform
35 import Diagrams.TwoD.Types
36
37 import Linear.Affine
38 import Linear.Vector
39
40 -- | The @ScaleInv@ wrapper creates two-dimensional /scale-invariant/
41 -- objects. Intuitively, a scale-invariant object is affected by
42 -- transformations like translations and rotations, but not by scales.
43 --
44 -- However, this is problematic when it comes to /non-uniform/
45 -- scales (/e.g./ @scaleX 2 . scaleY 3@) since they can introduce a
46 -- perceived rotational component. The prototypical example is an
47 -- arrowhead on the end of a path, which should be scale-invariant.
48 -- However, applying a non-uniform scale to the path but not the
49 -- arrowhead would leave the arrowhead pointing in the wrong
50 -- direction.
51 --
52 -- Moreover, for objects whose local origin is not at the local
53 -- origin of the parent diagram, any scale can result in a
54 -- translational component as well.
55 --
56 -- The solution is to also store a point (indicating the location,
57 -- /i.e./ the local origin) and a unit vector (indicating the
58 -- /direction/) along with a scale-invariant object. A
59 -- transformation to be applied is decomposed into rotational and
60 -- translational components as follows:
61 --
62 -- * The transformation is applied to the direction vector, and the
63 -- difference in angle between the original direction vector and its
64 -- image under the transformation determines the rotational
65 -- component. The rotation is applied with respect to the stored
66 -- location, rather than the global origin.
67 --
68 -- * The vector from the location to the image of the location under
69 -- the transformation determines the translational component.
70
71 data ScaleInv t =
72 ScaleInv
73 { _scaleInvObj :: t
74 , _scaleInvDir :: Vn t
75 , _scaleInvLoc :: Point (V t) (N t)
76 }
77 deriving Typeable
78
79 deriving instance (Show t, Show (Vn t)) => Show (ScaleInv t)
80
81 makeLenses ''ScaleInv
82
83 -- | Create a scale-invariant object pointing in the given direction,
84 -- located at the origin.
85 scaleInv :: (V t ~ v, N t ~ n, Additive v, Num n) => t -> v n -> ScaleInv t
86 scaleInv t d = ScaleInv t d origin
87
88 type instance V (ScaleInv t) = V t
89 type instance N (ScaleInv t) = N t
90
91 instance (V t ~ v, N t ~ n, Additive v, Num n, HasOrigin t) => HasOrigin (ScaleInv t) where
92 moveOriginTo p (ScaleInv t v l) = ScaleInv (moveOriginTo p t) v (moveOriginTo p l)
93
94 instance (V t ~ V2, N t ~ n, RealFloat n, Transformable t) => Transformable (ScaleInv t) where
95 transform tr (ScaleInv t v l) = ScaleInv (trans . rot $ t) (rot v) l'
96 where
97 angle = transform tr v ^. _theta
98
99 rot :: (V k ~ V t, N k ~ N t, Transformable k) => k -> k
100 rot = rotateAround l angle
101
102 l' = transform tr l
103
104 trans = translate (l' .-. l)
105
106 {- Proof that the above satisfies the monoid action laws.
107
108 1. transform mempty (ScaleInv t v l)
109 = ScaleInv (trans . rot $ t) (rot v) l'
110 { l' = transform mempty l = l }
111 { trans = translate (l' .-. l)
112 = translate (l .-. l)
113 = translate zeroV
114 = id
115 }
116 { rot = rotateAround l angle
117 = rotateAround l (direction (transform mempty v) - direction v)
118 = rotateAround l (direction v - direction v)
119 = rotateAround l 0
120 = id
121 }
122 = ScaleInv t v l
123
124 2. transform t1 (transform t2 (ScaleInv t v l))
125 = let angle = direction (transform t2 v) - direction v
126 rot = rotateAround l angle
127 l' = transform t2 l
128 trans = translate (l' .-. l)
129 in
130 transform t1 (ScaleInv (trans . rot $ t) (rot v) l')
131
132 = let angle = direction (transform t2 v) - direction v
133 rot = rotateAround l angle
134 l' = transform t2 l
135 trans = translate (l' .-. l)
136 angle2 = direction (transform t1 (rot v)) - direction (rot v)
137 rot2 = rotateAround l' angle2
138 l'2 = transform t1 l'
139 trans2 = translate (l'2 .-. l')
140 in
141 ScaleInv (trans2 . rot2 . trans . rot $ t) (rot2 . rot $ v) l'2
142
143 { l'2 = transform t1 l'
144 = transform t1 (transform t2 l)
145 = transform (t1 <> t2) l
146 }
147 { trans2 = translate (l'2 .-. l')
148 = translate (transform (t1 <> t2) l .-. transform t2 l)
149 = translate (transform t1 l .-. l)
150 }
151 { rot v = rotateAround l angle v
152 = rotate angle `under` translation (origin .-. l) $ v
153 = rotate angle v
154 }
155 { angle2 = direction (transform t1 (rot v)) - direction (rot v)
156 = direction (transform t1 (rotate angle v)) - direction (rotate angle v)
157 = direction (transform t1 (rotate angle v)) - direction v - angle
158 }
159 { rot2 = rotateAround l' angle2
160 = ???
161 }
162
163 -}
164
165 instance (V t ~ V2, N t ~ n, RealFloat n, Renderable t b) => Renderable (ScaleInv t) b where
166 render b = render b . view scaleInvObj
167
168 -- | Create a diagram from a single scale-invariant primitive. The
169 -- vector argument specifies the direction in which the primitive is
170 -- \"pointing\" (for the purpose of keeping it rotated correctly
171 -- under non-uniform scaling). The primitive is assumed to be
172 -- \"located\" at the origin (for the purpose of translating it
173 -- correctly under scaling).
174 --
175 -- Note that the resulting diagram will have an /empty/ envelope,
176 -- trace, and query. The reason is that the envelope, trace, and
177 -- query cannot be cached---applying a transformation would cause
178 -- the cached envelope, etc. to get \"out of sync\" with the
179 -- scale-invariant object. The intention, at any rate, is that
180 -- scale-invariant things will be used only as \"decorations\" (/e.g./
181 -- arrowheads) which should not affect the envelope, trace, and
182 -- query.
183 scaleInvPrim :: (V t ~ V2, N t ~ n, RealFloat n, Typeable t, Renderable t b, Monoid m)
184 => t -> V2 n -> QDiagram b (V t) (N t) m
185 scaleInvPrim t d = mkQD (Prim $ scaleInv t d) mempty mempty mempty mempty