Debian Codegraph @

src/Diagrams/ThreeD/Projection.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE RankNTypes #-}
6 {-# LANGUAGE TypeFamilies #-}
7
8 -----------------------------------------------------------------------------
9 -- |
10 -- Module : Diagrams.ThreeD.Projection
11 -- Copyright : (c) 2014 diagrams team (see LICENSE)
12 -- License : BSD-style (see LICENSE)
13 -- Maintainer : [email protected]
14 --
15 -- 3D projections are a way of viewing a three-dimensional objects on a
16 -- two-dimensional plane.
17 --
18 -- This module can be used with the functions in "Linear.Projection".
19 --
20 -- Disclaimer: This module should be considered experimental and is
21 -- likely to change.
22 --
23 -----------------------------------------------------------------------------
24
25 module Diagrams.ThreeD.Projection
26 ( -- * Orthographic projections
27
28 -- $orthographic
29 -- ** Parallel projections
30 facingXY
31 , facingXZ
32 , facingYZ
33
34 -- ** axonometric
35 -- $axonometric
36
37 -- *** Isometric projections
38 -- $isometric
39 , isometricApply
40 , isometric
41
42 , lookingAt
43
44 -- ** Affine maps
45 , m44AffineApply
46 , m44AffineMap
47 , m33AffineApply
48 , m33AffineMap
49
50 -- * Perspective projections
51 -- $perspective
52 -- ** Perspective deformations
53 , m44Deformation
54 , module Linear.Projection
55 ) where
56
57 import Control.Lens hiding (transform)
58 import Data.Functor.Rep
59
60 import Diagrams.Core
61 import Diagrams.Deform
62 import Diagrams.Direction
63 import Diagrams.LinearMap
64 import Diagrams.ThreeD.Types (P3)
65 import Diagrams.ThreeD.Vector
66
67 import Linear as L
68 import Linear.Affine
69 import Linear.Projection
70
71 ------------------------------------------------------------------------
72 -- Orthographic projections
73 ------------------------------------------------------------------------
74
75 -- $orthographic
76 -- Orthographic projections are a form of parallel projections where are
77 -- projection lines are orthogonal to the projection plane.
78
79 -- Parallel projections
80
81 -- | Look at the xy-plane with y as the up direction.
82 facingXY :: (Epsilon n, Floating n) => AffineMap V3 V2 n
83 facingXY = lookingAt unitZ origin yDir
84
85 -- | Look at the xz-plane with z as the up direction.
86 facingXZ :: (Epsilon n, Floating n) => AffineMap V3 V2 n
87 facingXZ = lookingAt unitY origin zDir
88
89 -- | Look at the yz-plane with z as the up direction.
90 facingYZ :: (Epsilon n, Floating n) => AffineMap V3 V2 n
91 facingYZ = lookingAt unitX origin zDir
92
93 -- $axonometric
94 -- Axonometric projections are a type of orthographic projection where
95 -- the object is rotated along one or more of its axes relative to the
96 -- plane of projection.
97
98 -- $isometric
99 -- Isometric projections are when the scale along each axis of the
100 -- projection is the same and the angle between any axis is 120
101 -- degrees.
102
103 -- | Apply an isometric projection given the up direction
104 isometricApply :: (InSpace V3 n a, InSpace V2 n b, AffineMappable a b, Floating n, Epsilon n)
105 => Direction V3 n -> a -> b
106 isometricApply up = amap (isometric up)
107
108 -- | Make an isometric affine map with the given up direction.
109 isometric :: (Floating n, Epsilon n) => Direction V3 n -> AffineMap V3 V2 n
110 isometric up = m44AffineMap m
111 where
112 m = lookAt (V3 1 1 1) zero (fromDirection up)
113
114 lookingAt :: (Epsilon n, Floating n)
115 => P3 n -- ^ Eye
116 -> P3 n -- ^ Center
117 -> Direction V3 n -- ^ Up
118 -> AffineMap V3 V2 n
119 lookingAt (P cam) (P center) d = m44AffineMap m
120 where
121 m = lookAt cam center (d^._Dir)
122
123 -- | Apply the affine part of a homogeneous matrix.
124 m44AffineApply :: (InSpace V3 n a, InSpace V2 n b, AffineMappable a b)
125 => M44 n -> a -> b
126 m44AffineApply = amap . m44AffineMap
127
128 -- | Create an 'AffineMap' from a 4x4 homogeneous matrix, ignoring any
129 -- perspective transforms.
130 m44AffineMap :: Num n => M44 n -> AffineMap V3 V2 n
131 m44AffineMap m = AffineMap (LinearMap f) (f v)
132 where
133 f = view _xy . (m' !*)
134 m' = m ^. linearTransform
135 v = m ^. L.translation
136
137 -- | Apply a transformation matrix and translation.
138 m33AffineApply :: (InSpace V3 n a, InSpace V2 n b, AffineMappable a b)
139 => M33 n -> V2 n -> a -> b
140 m33AffineApply m = amap . m33AffineMap m
141
142 -- | Create an 'AffineMap' from a 3x3 transformation matrix and a
143 -- translation vector.
144 m33AffineMap :: Num n => M33 n -> V2 n -> AffineMap V3 V2 n
145 m33AffineMap m = AffineMap (LinearMap f)
146 where
147 f = view _xy . (m !*)
148
149 -- | Extract the linear transform part of a homogeneous matrix.
150 linearTransform :: (Representable u, R3 v, R3 u) => Lens' (u (v n)) (M33 n)
151 linearTransform = column _xyz . _xyz
152
153 ------------------------------------------------------------------------
154 -- Perspective transforms
155 ------------------------------------------------------------------------
156
157 -- For the time being projective transforms use the deformable class.
158 -- Eventually we would like to replace this with a more specialised
159 -- method.
160
161 -- $perspective
162 -- Perspective projections are when closer objects appear bigger.
163
164 -- | Make a deformation from a 4x4 homogeneous matrix.
165 m44Deformation :: Fractional n => M44 n -> Deformation V3 V2 n
166 m44Deformation m =
167 Deformation (P . view _xy . normalizePoint . (m !*) . point . view _Point)
168