Debian Codegraph @

src/Diagrams/ThreeD/Transform.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
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.ThreeD.Transform
16 -- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
17 -- License : BSD-style (see LICENSE)
18 -- Maintainer : [email protected]
19 --
20 -- Transformations specific to three dimensions, with a few generic
21 -- transformations (uniform scaling, translation) also re-exported for
22 -- convenience.
23 --
24 -----------------------------------------------------------------------------
25
26 module Diagrams.ThreeD.Transform
27 ( T3
28
29 -- * Rotation
30 , aboutX, aboutY, aboutZ
31 , rotationAbout, rotateAbout
32 , pointAt, pointAt'
33
34 -- * Scaling
35 , scalingX, scalingY, scalingZ
36 , scaleX, scaleY, scaleZ
37 , scaling, scale
38
39 -- * Translation
40 , translationX, translateX
41 , translationY, translateY
42 , translationZ, translateZ
43 , translation, translate
44
45 -- * Reflection
46 , reflectionX, reflectX
47 , reflectionY, reflectY
48 , reflectionZ, reflectZ
49 , reflectionAcross, reflectAcross
50
51 ) where
52
53 import Diagrams.Core
54 import Diagrams.Core.Transform
55
56 import Diagrams.Angle
57 import Diagrams.Direction
58 import Diagrams.Points
59 import Diagrams.ThreeD.Types
60 import Diagrams.Transform
61
62 import Control.Lens (view, (&), (*~), (.~), (//~))
63 import Data.Semigroup
64 import Diagrams.TwoD.Transform
65
66 import Linear.Affine
67 import Linear.Metric
68 import Linear.V3 (cross)
69 import Linear.Vector
70
71 -- | Create a transformation which rotates by the given angle about
72 -- a line parallel the Z axis passing through the local origin.
73 -- A positive angle brings positive x-values towards the positive-y axis.
74 --
75 -- The angle can be expressed using any type which is an
76 -- instance of 'Angle'. For example, @aboutZ (1\/4 \@\@
77 -- 'turn')@, @aboutZ (tau\/4 \@\@ 'rad')@, and @aboutZ (90 \@\@
78 -- 'deg')@ all represent the same transformation, namely, a
79 -- counterclockwise rotation by a right angle. For more general rotations,
80 -- see 'rotationAbout'.
81 --
82 -- Note that writing @aboutZ (1\/4)@, with no type annotation, will
83 -- yield an error since GHC cannot figure out which sort of angle
84 -- you want to use.
85 aboutZ :: Floating n => Angle n -> Transformation V3 n
86 aboutZ (view rad -> a) = fromOrthogonal r where
87 r = rot a <-> rot (-a)
88 rot θ (V3 x y z) = V3 (cos θ * x - sin θ * y)
89 (sin θ * x + cos θ * y)
90 z
91
92 -- | Like 'aboutZ', but rotates about the X axis, bringing positive y-values
93 -- towards the positive z-axis.
94 aboutX :: Floating n => Angle n -> Transformation V3 n
95 aboutX (view rad -> a) = fromOrthogonal r where
96 r = rot a <-> rot (-a)
97 rot θ (V3 x y z) = V3 x
98 (cos θ * y - sin θ * z)
99 (sin θ * y + cos θ * z)
100
101 -- | Like 'aboutZ', but rotates about the Y axis, bringing postive
102 -- x-values towards the negative z-axis.
103 aboutY :: Floating n => Angle n -> Transformation V3 n
104 aboutY (view rad -> a) = fromOrthogonal r where
105 r = rot a <-> rot (-a)
106 rot θ (V3 x y z) = V3 (cos θ * x + sin θ * z)
107 y
108 (-sin θ * x + cos θ * z)
109
110 -- | @rotationAbout p d a@ is a rotation about a line parallel to @d@
111 -- passing through @p@.
112 rotationAbout
113 :: Floating n
114 => Point V3 n -- ^ origin of rotation
115 -> Direction V3 n -- ^ direction of rotation axis
116 -> Angle n -- ^ angle of rotation
117 -> Transformation V3 n
118 rotationAbout (P t) d (view rad -> a)
119 = mconcat [translation (negated t),
120 fromOrthogonal r,
121 translation t] where
122 r = rot a <-> rot (-a)
123 w = fromDirection d
124
125 rot θ v = v ^* cos θ
126 ^+^ cross w v ^* sin θ
127 ^+^ w ^* ((w `dot` v) * (1 - cos θ))
128
129 -- | @rotationAbout p d a@ is a rotation about a line parallel to @d@
130 -- passing through @p@.
131 rotateAbout
132 :: (InSpace V3 n t, Floating n, Transformable t)
133 => Point V3 n -- ^ origin of rotation
134 -> Direction V3 n -- ^ direction of rotation axis
135 -> Angle n -- ^ angle of rotation
136 -> t -> t
137 rotateAbout p d theta = transform (rotationAbout p d theta)
138
139 -- | @pointAt about initial final@ produces a rotation which brings
140 -- the direction @initial@ to point in the direction @final@ by first
141 -- panning around @about@, then tilting about the axis perpendicular
142 -- to @about@ and @final@. In particular, if this can be accomplished
143 -- without tilting, it will be, otherwise if only tilting is
144 -- necessary, no panning will occur. The tilt will always be between
145 -- ± 1/4 turn.
146 pointAt :: (Floating n, Ord n)
147 => Direction V3 n -> Direction V3 n -> Direction V3 n
148 -> Transformation V3 n
149 pointAt a i f = pointAt' (fromDirection a) (fromDirection i) (fromDirection f)
150
151 -- | pointAt' has the same behavior as 'pointAt', but takes vectors
152 -- instead of directions.
153 pointAt' :: (Floating n, Ord n) => V3 n -> V3 n -> V3 n -> Transformation V3 n
154 pointAt' about initial final = pointAtUnit (signorm about) (signorm initial) (signorm final)
155
156 -- | pointAtUnit has the same behavior as @pointAt@, but takes unit vectors.
157 pointAtUnit :: (Floating n, Ord n) => V3 n -> V3 n -> V3 n -> Transformation V3 n
158 pointAtUnit about initial final = tilt <> pan where
159 -- rotating u by (signedAngle rel u v) about rel gives a vector in the direction of v
160 signedAngle rel u v = signum (cross u v `dot` rel) *^ angleBetween u v
161 inPanPlaneF = final ^-^ project about final
162 inPanPlaneI = initial ^-^ project about initial
163 panAngle = signedAngle about inPanPlaneI inPanPlaneF
164 pan = rotationAbout origin (direction about) panAngle
165 tiltAngle = signedAngle tiltAxis (transform pan initial) final
166 tiltAxis = cross final about
167 tilt = rotationAbout origin (direction tiltAxis) tiltAngle
168
169 -- Scaling -------------------------------------------------
170
171 -- | Construct a transformation which scales by the given factor in
172 -- the z direction.
173 scalingZ :: (Additive v, R3 v, Fractional n) => n -> Transformation v n
174 scalingZ c = fromSymmetric $ (_z *~ c) <-> (_z //~ c)
175
176 -- | Scale a diagram by the given factor in the z direction. To scale
177 -- uniformly, use 'scale'.
178 scaleZ :: (InSpace v n t, R3 v, Fractional n, Transformable t) => n -> t -> t
179 scaleZ = transform . scalingZ
180
181 -- Translation ----------------------------------------
182
183 -- | Construct a transformation which translates by the given distance
184 -- in the z direction.
185 translationZ :: (Additive v, R3 v, Num n) => n -> Transformation v n
186 translationZ z = translation (zero & _z .~ z)
187
188 -- | Translate a diagram by the given distance in the y
189 -- direction.
190 translateZ :: (InSpace v n t, R3 v, Transformable t) => n -> t -> t
191 translateZ = transform . translationZ
192
193 -- Reflection ----------------------------------------------
194
195 -- | Construct a transformation which flips a diagram across z=0,
196 -- i.e. sends the point (x,y,z) to (x,y,-z).
197 reflectionZ :: (Additive v, R3 v, Num n) => Transformation v n
198 reflectionZ = fromSymmetric $ (_z *~ (-1)) <-> (_z *~ (-1))
199
200 -- | Flip a diagram across z=0, i.e. send the point (x,y,z) to
201 -- (x,y,-z).
202 reflectZ :: (InSpace v n t, R3 v, Transformable t) => t -> t
203 reflectZ = transform reflectionZ
204
205 -- | @reflectionAcross p v@ is a reflection across the plane through
206 -- the point @p@ and normal to vector @v@. This also works as a 2D
207 -- transform where @v@ is the normal to the line passing through point
208 -- @p@.
209 reflectionAcross :: (Metric v, Fractional n)
210 => Point v n -> v n -> Transformation v n
211 reflectionAcross p v =
212 conjugate (translation (origin .-. p)) reflect
213 where
214 reflect = fromLinear t (linv t)
215 t = f v <-> f (negated v)
216 f u w = w ^-^ 2 *^ project u w
217
218 -- | @reflectAcross p v@ reflects a diagram across the plane though
219 -- the point @p@ and the vector @v@. This also works as a 2D transform
220 -- where @v@ is the normal to the line passing through point @p@.
221 reflectAcross :: (InSpace v n t, Metric v, Fractional n, Transformable t)
222 => Point v n -> v n -> t -> t
223 reflectAcross p v = transform (reflectionAcross p v)