| 1 |
{-# LANGUAGE TypeFamilies #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
----------------------------------------------------------------------------- |
| 5 |
-- | |
| 6 |
-- Module : Diagrams.ThreeD.Types |
| 7 |
-- Copyright : (c) 2011 diagrams-lib team (see LICENSE) |
| 8 |
-- License : BSD-style (see LICENSE) |
| 9 |
-- Maintainer : [email protected] |
| 10 |
-- |
| 11 |
-- Basic types for three-dimensional Euclidean space. |
| 12 |
-- |
| 13 |
----------------------------------------------------------------------------- |
| 14 |
|
| 15 |
module Diagrams.ThreeD.Types |
| 16 |
( -- * 3D Euclidean space |
| 17 |
r3, unr3, mkR3 |
| 18 |
, p3, unp3, mkP3 |
| 19 |
, r3Iso, p3Iso, project |
| 20 |
, r3SphericalIso, r3CylindricalIso |
| 21 |
, V3 (..), P3, T3 |
| 22 |
, R1 (..), R2 (..), R3 (..) |
| 23 |
|
| 24 |
) where |
| 25 |
|
| 26 |
import Control.Lens (Iso', iso, _1, _2, _3) |
| 27 |
|
| 28 |
import Diagrams.Angle |
| 29 |
import Diagrams.Core |
| 30 |
import Diagrams.Points |
| 31 |
import Diagrams.TwoD.Types |
| 32 |
|
| 33 |
import Linear.Metric |
| 34 |
import Linear.V3 as V |
| 35 |
|
| 36 |
------------------------------------------------------------ |
| 37 |
-- 3D Euclidean space |
| 38 |
|
| 39 |
-- Basic R3 types |
| 40 |
|
| 41 |
type P3 = Point V3 |
| 42 |
type T3 = Transformation V3 |
| 43 |
|
| 44 |
r3Iso :: Iso' (V3 n) (n, n, n) |
| 45 |
r3Iso = iso unr3 r3 |
| 46 |
|
| 47 |
|
| 48 |
r3 :: (n, n, n) -> V3 n |
| 49 |
r3 (x,y,z) = V3 x y z |
| 50 |
|
| 51 |
|
| 52 |
mkR3 :: n -> n -> n -> V3 n |
| 53 |
mkR3 = V3 |
| 54 |
|
| 55 |
|
| 56 |
unr3 :: V3 n -> (n, n, n) |
| 57 |
unr3 (V3 x y z) = (x,y,z) |
| 58 |
|
| 59 |
|
| 60 |
p3 :: (n, n, n) -> P3 n |
| 61 |
p3 = P . r3 |
| 62 |
|
| 63 |
|
| 64 |
unp3 :: P3 n -> (n, n, n) |
| 65 |
unp3 (P (V3 x y z)) = (x,y,z) |
| 66 |
|
| 67 |
p3Iso :: Iso' (P3 n) (n, n, n) |
| 68 |
p3Iso = iso unp3 p3 |
| 69 |
|
| 70 |
|
| 71 |
mkP3 :: n -> n -> n -> P3 n |
| 72 |
mkP3 x y z = p3 (x, y, z) |
| 73 |
|
| 74 |
type instance V (V3 n) = V3 |
| 75 |
type instance N (V3 n) = n |
| 76 |
|
| 77 |
instance Transformable (V3 n) where |
| 78 |
transform = apply |
| 79 |
|
| 80 |
r3SphericalIso :: RealFloat n => Iso' (V3 n) (n, Angle n, Angle n) |
| 81 |
r3SphericalIso = iso |
| 82 |
(\v@(V3 x y z) -> (norm v, atan2A y x, acosA (z / norm v))) |
| 83 |
(\(r,θ,φ) -> V3 (r * cosA θ * sinA φ) (r * sinA θ * sinA φ) (r * cosA φ)) |
| 84 |
|
| 85 |
r3CylindricalIso :: RealFloat n => Iso' (V3 n) (n, Angle n, n) |
| 86 |
r3CylindricalIso = iso |
| 87 |
(\(V3 x y z) -> (sqrt $ x*x + y*y, atan2A y x, z)) |
| 88 |
(\(r,θ,z) -> V3 (r*cosA θ) (r*sinA θ) z) |
| 89 |
|
| 90 |
instance HasR V3 where |
| 91 |
_r = r3SphericalIso . _1 |
| 92 |
|
| 93 |
instance HasTheta V3 where |
| 94 |
_theta = r3CylindricalIso . _2 |
| 95 |
|
| 96 |
instance HasPhi V3 where |
| 97 |
_phi = r3SphericalIso . _3 |
| 98 |
|