Debian Codegraph @

src/Diagrams/ThreeD/Camera.hs

raw symbols SCIP
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE UndecidableInstances #-}
8
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Diagrams.ThreeD.Camera
12 -- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
13 -- License : BSD-style (see LICENSE)
14 -- Maintainer : [email protected]
15 --
16 -- Types to specify viewpoint for 3D rendering.
17 --
18 -----------------------------------------------------------------------------
19
20 module Diagrams.ThreeD.Camera
21 ( Camera -- do not export constructor
22 -- These are safe to construct manually
23 , PerspectiveLens(..), OrthoLens(..)
24 , horizontalFieldOfView, verticalFieldOfView
25 , orthoWidth, orthoHeight
26 , camLoc, camForward, camUp, camRight, camLens
27 , facing_ZCamera, mm50Camera
28 , mm50, mm50Wide, mm50Narrow
29 , aspect, camAspect
30 )
31 where
32
33 import Control.Lens (makeLenses)
34 import Data.Monoid
35 import Data.Typeable
36
37 import Diagrams.Angle
38 import Diagrams.Core
39 import Diagrams.Direction
40 import Diagrams.ThreeD.Vector
41
42 import Linear.V3
43
44 -- Parameterize Camera on the lens type, so that Backends can express which
45 -- lenses they handle.
46 data Camera l n = Camera
47 { camLoc :: Point V3 n
48 , forward :: V3 n
49 , up :: V3 n
50 , lens :: l n
51 }
52 deriving Typeable
53
54 type instance V (Camera l n) = V3
55 type instance N (Camera l n) = n
56
57 class Typeable l => CameraLens l where
58 -- | The natural aspect ratio of the projection.
59 aspect :: Floating n => l n -> n
60
61 -- | A perspective projection
62 data PerspectiveLens n = PerspectiveLens
63 { _horizontalFieldOfView :: Angle n -- ^ Horizontal field of view.
64 , _verticalFieldOfView :: Angle n -- ^ Vertical field of view.
65 }
66 deriving Typeable
67
68 makeLenses ''PerspectiveLens
69
70 type instance V (PerspectiveLens n) = V3
71 type instance N (PerspectiveLens n) = n
72
73 instance CameraLens PerspectiveLens where
74 aspect (PerspectiveLens h v) = angleRatio h v
75
76 -- | An orthographic projection
77 data OrthoLens n = OrthoLens
78 { _orthoWidth :: n -- ^ Width
79 , _orthoHeight :: n -- ^ Height
80 }
81 deriving Typeable
82
83 makeLenses ''OrthoLens
84
85 type instance V (OrthoLens n) = V3
86 type instance N (OrthoLens n) = n
87
88 instance CameraLens OrthoLens where
89 aspect (OrthoLens h v) = h / v
90
91 instance Num n => Transformable (Camera l n) where
92 transform t (Camera p f u l) =
93 Camera (transform t p)
94 (transform t f)
95 (transform t u)
96 l
97
98 instance Num n => Renderable (Camera l n) NullBackend where
99 render _ _ = mempty
100
101 -- | A camera at the origin facing along the negative Z axis, with its
102 -- up-axis coincident with the positive Y axis. The field of view is
103 -- chosen to match a 50mm camera on 35mm film. Note that Cameras take
104 -- up no space in the Diagram.
105 mm50Camera :: (Typeable n, Floating n, Ord n, Renderable (Camera PerspectiveLens n) b)
106 => QDiagram b V3 n Any
107 mm50Camera = facing_ZCamera mm50
108
109 -- | 'facing_ZCamera l' is a camera at the origin facing along the
110 -- negative Z axis, with its up-axis coincident with the positive Y
111 -- axis, with the projection defined by l.
112 facing_ZCamera :: (Floating n, Ord n, Typeable n, CameraLens l, Renderable (Camera l n) b) =>
113 l n -> QDiagram b V3 n Any
114 facing_ZCamera l = mkQD (Prim $ Camera origin unit_Z unitY l)
115 mempty mempty mempty (Query . const . Any $ False)
116 {-# ANN facing_ZCamera ("HLint: ignore Use camelCase" :: String) #-}
117
118 mm50, mm50Wide, mm50Narrow :: Floating n => PerspectiveLens n
119
120 -- | mm50 has the field of view of a 50mm lens on standard 35mm film,
121 -- hence an aspect ratio of 3:2.
122 mm50 = PerspectiveLens (40.5 @@ deg) (27 @@ deg)
123
124 -- | mm50blWide has the same vertical field of view as mm50, but an
125 -- aspect ratio of 1.6, suitable for wide screen computer monitors.
126 mm50Wide = PerspectiveLens (43.2 @@ deg) (27 @@ deg)
127
128 -- | mm50Narrow has the same vertical field of view as mm50, but an
129 -- aspect ratio of 4:3, for VGA and similar computer resolutions.
130 mm50Narrow = PerspectiveLens (36 @@ deg) (27 @@ deg)
131
132 camForward :: Camera l n -> Direction V3 n
133 camForward = direction . forward
134
135 camUp :: Camera l n -> Direction V3 n
136 camUp = direction . up
137
138 camRight :: Fractional n => Camera l n -> Direction V3 n
139 camRight c = direction right where
140 right = cross (forward c) (up c)
141
142 camLens :: Camera l n -> l n
143 camLens = lens
144
145 camAspect :: (Floating n, CameraLens l) => Camera l n -> n
146 camAspect = aspect . camLens