never executed always true always false
1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE PackageImports #-}
3 module Reanimate.Transform
4 ( identity
5 , transformPoint
6 , mkMatrix
7 , toTransformation
8 ) where
9
10 -- XXX: Use Linear.Matrix instead of Data.Matrix to drop the 'matrix' dependency.
11 import Data.List
12 import "matrix" Data.Matrix (Matrix)
13 import qualified "matrix" Data.Matrix as M
14 import Data.Maybe
15 import Graphics.SvgTree
16 import Linear.V2
17
18 type TMatrix = Matrix Coord
19
20 identity :: TMatrix
21 identity = M.identity 3
22
23 fromList :: [Coord] -> TMatrix
24 fromList [a,b,c,d,e,f] = M.fromList 3 3 [a,c,e,b,d,f,0,0,1]
25 fromList _ = error "Reanimate.Transform.fromList: bad input"
26
27 transformPoint :: TMatrix -> RPoint -> RPoint
28 transformPoint m (V2 x y) = V2 (a*x +c*y + e) (b*x + d*y +f)
29 where
30 !a = M.unsafeGet 1 1 m
31 !c = M.unsafeGet 1 2 m
32 !e = M.unsafeGet 1 3 m
33 !b = M.unsafeGet 2 1 m
34 !d = M.unsafeGet 2 2 m
35 !f = M.unsafeGet 2 3 m
36 -- (a:c:e:b:d:f:_) = M.toList m
37
38 mkMatrix :: Maybe [Transformation] -> TMatrix
39 mkMatrix Nothing = identity
40 mkMatrix (Just ts) = foldl' (*) identity (map transformationMatrix ts)
41
42 transformationMatrix :: Transformation -> TMatrix
43 transformationMatrix transformation =
44 case transformation of
45 TransformMatrix a b c d e f -> fromList [a,b,c,d,e,f]
46 Translate x y -> translate x y
47 Scale sx mbSy -> fromList [sx,0,0,fromMaybe sx mbSy,0,0]
48 Rotate a Nothing -> rotate a
49 Rotate a (Just (x,y)) -> translate x y * rotate a * translate (-x) (-y)
50 SkewX a -> fromList [1,0,tan (a*pi/180),1,0,0]
51 SkewY a -> fromList [1,tan (a*pi/180),0,1,0,0]
52 TransformUnknown -> identity
53 where
54 translate x y = fromList [1,0,0,1,x,y]
55 rotate a = fromList [cos r,sin r,-sin r,cos r,0,0]
56 where r = a * pi / 180
57
58 toTransformation :: TMatrix -> Transformation
59 toTransformation m = TransformMatrix a b c d e f
60 where
61 [a,c,e,b,d,f,_,_,_] = M.toList m