| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
rotateBy :: (InSpace V2 n t, Transformable t, Floating n) => n -> t -> t |
| 87 |
rotateBy = transform . rotation . review turn |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 104 |
|
| 105 |
rotationAround :: Floating n => P2 n -> Angle n -> T2 n |
| 106 |
rotationAround p theta = |
| 107 |
conjugate (translation (origin .-. p)) (rotation theta) |
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 116 |
rotationTo :: OrderedField n => Direction V2 n -> T2 n |
| 117 |
rotationTo (view _Dir -> V2 x y) = rotation (atan2A' y x) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
rotateTo :: (InSpace V2 n t, OrderedField n, Transformable t) => Direction V2 n -> t -> t |
| 122 |
rotateTo = transform . rotationTo |
| 123 |
|
| 124 |
-- Scaling ------------------------------------------------- |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
scalingX :: (Additive v, R1 v, Fractional n) => n -> Transformation v n |
| 129 |
scalingX c = fromSymmetric $ (_x *~ c) <-> (_x //~ c) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
scaleX :: (InSpace v n t, R2 v, Fractional n, Transformable t) => n -> t -> t |
| 134 |
scaleX = transform . scalingX |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
scalingY :: (Additive v, R2 v, Fractional n) => n -> Transformation v n |
| 139 |
scalingY c = fromSymmetric $ (_y *~ c) <-> (_y //~ c) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
scaleY :: (InSpace v n t, R2 v, Fractional n, Transformable t) |
| 144 |
=> n -> t -> t |
| 145 |
scaleY = transform . scalingY |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 176 |
|
| 177 |
translationX :: (Additive v, R1 v, Num n) => n -> Transformation v n |
| 178 |
translationX x = translation (zero & _x .~ x) |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
translateX :: (InSpace v n t, R1 v, Transformable t) => n -> t -> t |
| 183 |
translateX = transform . translationX |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
translationY :: (Additive v, R2 v, Num n) => n -> Transformation v n |
| 188 |
translationY y = translation (zero & _y .~ y) |
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 199 |
|
| 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 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
scaleRotateTo :: (InSpace V2 n t, Transformable t, Floating n) |
| 216 |
=> V2 n -> t -> t |
| 217 |
scaleRotateTo = transform . scalingRotationTo |
| 218 |
|
| 219 |
-- Reflection ---------------------------------------------- |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
reflectionX :: (Additive v, R1 v, Num n) => Transformation v n |
| 224 |
reflectionX = fromSymmetric $ (_x *~ (-1)) <-> (_x *~ (-1)) |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
reflectX :: (InSpace v n t, R1 v, Transformable t) => t -> t |
| 229 |
reflectX = transform reflectionX |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
reflectionY :: (Additive v, R2 v, Num n) => Transformation v n |
| 234 |
reflectionY = fromSymmetric $ (_y *~ (-1)) <-> (_y *~ (-1)) |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
reflectY :: (InSpace v n t, R2 v, Transformable t) => t -> t |
| 239 |
reflectY = transform reflectionY |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
reflectionXY :: (Additive v, R2 v, Num n) => Transformation v n |
| 244 |
reflectionXY = fromSymmetric $ (_xy %~ view _yx) <-> (_xy %~ view _yx) |
| 245 |
|
| 246 |
|
| 247 |
reflectXY :: (InSpace v n t, R2 v, Transformable t) => t -> t |
| 248 |
reflectXY = transform reflectionXY |
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 261 |
|
| 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 |
|
| 280 |
|
| 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 |
|
| 289 |
|
| 290 |
shearX :: (InSpace V2 n t, Transformable t) => n -> t -> t |
| 291 |
shearX = transform . shearingX |
| 292 |
|
| 293 |
|
| 294 |
|
| 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 |
|
| 303 |
|
| 304 |
shearY :: (InSpace V2 n t, Transformable t) => n -> t -> t |
| 305 |
shearY = transform . shearingY |