| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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 |
|
| 93 |
|
| 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 |
|
| 102 |
|
| 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 |
|
| 111 |
|
| 112 |
rotationAbout |
| 113 |
:: Floating n |
| 114 |
=> Point V3 n |
| 115 |
-> Direction V3 n |
| 116 |
-> Angle n |
| 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 |
|
| 130 |
|
| 131 |
rotateAbout |
| 132 |
:: (InSpace V3 n t, Floating n, Transformable t) |
| 133 |
=> Point V3 n |
| 134 |
-> Direction V3 n |
| 135 |
-> Angle n |
| 136 |
-> t -> t |
| 137 |
rotateAbout p d theta = transform (rotationAbout p d theta) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
. |
| 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 |
|
| 152 |
|
| 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 |
|
| 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 |
|
| 172 |
|
| 173 |
scalingZ :: (Additive v, R3 v, Fractional n) => n -> Transformation v n |
| 174 |
scalingZ c = fromSymmetric $ (_z *~ c) <-> (_z //~ c) |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
scaleZ :: (InSpace v n t, R3 v, Fractional n, Transformable t) => n -> t -> t |
| 179 |
scaleZ = transform . scalingZ |
| 180 |
|
| 181 |
-- Translation ---------------------------------------- |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
translationZ :: (Additive v, R3 v, Num n) => n -> Transformation v n |
| 186 |
translationZ z = translation (zero & _z .~ z) |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
translateZ :: (InSpace v n t, R3 v, Transformable t) => n -> t -> t |
| 191 |
translateZ = transform . translationZ |
| 192 |
|
| 193 |
-- Reflection ---------------------------------------------- |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
reflectionZ :: (Additive v, R3 v, Num n) => Transformation v n |
| 198 |
reflectionZ = fromSymmetric $ (_z *~ (-1)) <-> (_z *~ (-1)) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
reflectZ :: (InSpace v n t, R3 v, Transformable t) => t -> t |
| 203 |
reflectZ = transform reflectionZ |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 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 |
|
| 219 |
|
| 220 |
|
| 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) |