Debian Codegraph @

src/Diagrams/ThreeD/Attributes.hs

raw symbols SCIP
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
7 -----------------------------------------------------------------------------
8 -- |
9 -- Module : Diagrams.ThreeD.Attributes
10 -- Copyright : (c) 2014 diagrams-lib team (see LICENSE)
11 -- License : BSD-style (see LICENSE)
12 -- Maintainer : [email protected]
13 --
14 -- Diagrams may have /attributes/ which affect the way they are
15 -- rendered. This module defines some common attributes relevant in
16 -- 3D; particular backends may also define more backend-specific
17 -- attributes.
18 --
19 -- Every attribute type must have a /semigroup/ structure, that is, an
20 -- associative binary operation for combining two attributes into one.
21 -- Unless otherwise noted, all the attributes defined here use the
22 -- 'Last' structure, that is, combining two attributes simply keeps
23 -- the second one and throws away the first. This means that child
24 -- attributes always override parent attributes.
25 --
26 -----------------------------------------------------------------------------
27
28 module Diagrams.ThreeD.Attributes where
29
30 import Control.Lens
31 import Data.Semigroup
32 import Data.Typeable
33
34 import Data.Colour
35
36 import Diagrams.Core
37
38 -- | @SurfaceColor@ is the inherent pigment of an object, assumed to
39 -- be opaque.
40 newtype SurfaceColor = SurfaceColor (Last (Colour Double))
41 deriving (Typeable, Semigroup, Show)
42
43 instance AttributeClass SurfaceColor
44
45 _SurfaceColor :: Iso' SurfaceColor (Colour Double)
46 _SurfaceColor = iso (\(SurfaceColor (Last c)) -> c) (SurfaceColor . Last)
47
48 -- | Set the surface color.
49 sc :: HasStyle d => Colour Double -> d -> d
50 sc = applyAttr . review _SurfaceColor
51
52 -- | Lens onto the surface colour of a style.
53 _sc :: Lens' (Style v n) (Maybe (Colour Double))
54 _sc = atAttr . mapping _SurfaceColor
55
56 -- | @Diffuse@ is the fraction of incident light reflected diffusely,
57 -- that is, in all directions. The actual light reflected is the
58 -- product of this value, the incident light, and the @SurfaceColor@
59 -- Attribute. For physical reasonableness, @Diffuse@ should have a
60 -- value between 0 and 1; this is not checked.
61 newtype Diffuse = Diffuse (Last Double)
62 deriving (Typeable, Semigroup, Show)
63
64 instance AttributeClass Diffuse
65
66 -- | Isomorphism between 'Diffuse' and 'Double'
67 _Diffuse :: Iso' Diffuse Double
68 _Diffuse = iso (\(Diffuse (Last d)) -> d) (Diffuse . Last)
69
70 -- | Set the diffuse reflectance.
71 diffuse :: HasStyle d => Double -> d -> d
72 diffuse = applyAttr . review _Diffuse
73
74 -- | Lens onto the possible diffuse reflectance in a style.
75 _diffuse :: Lens' (Style v n) (Maybe Double)
76 _diffuse = atAttr . mapping _Diffuse
77
78 -- | @Ambient@ is an ad-hoc representation of indirect lighting. The
79 -- product of @Ambient@ and @SurfaceColor@ is added to the light
80 -- leaving an object due to diffuse and specular terms. @Ambient@ can
81 -- be set per-object, and can be loosely thought of as the product of
82 -- indirect lighting incident on that object and the diffuse
83 -- reflectance.
84 newtype Ambient = Ambient (Last Double)
85 deriving (Typeable, Semigroup, Show)
86
87 instance AttributeClass Ambient
88
89 _Ambient :: Iso' Ambient Double
90 _Ambient = iso (\(Ambient (Last d)) -> d) (Ambient . Last)
91
92 -- | Set the emittance due to ambient light.
93 ambient :: HasStyle d => Double -> d -> d
94 ambient = applyAttr . review _Ambient
95
96 -- | Lens onto the possible ambience in a style.
97 _ambient :: Lens' (Style v n) (Maybe Double)
98 _ambient = atAttr . mapping _Ambient
99
100 -- | A specular highlight has two terms, the intensity, between 0 and
101 -- 1, and the size. The highlight size is assumed to be the exponent
102 -- in a Phong shading model (though Backends are free to use a
103 -- different shading model). In this model, reasonable values are
104 -- between 1 and 50 or so, with higher values for shinier objects.
105 -- Physically, the intensity and the value of @Diffuse@ must add up to
106 -- less than 1; this is not enforced.
107 data Specular = Specular
108 { _specularIntensity :: Double
109 , _specularSize :: Double
110 } deriving Show
111
112 makeLenses ''Specular
113
114 newtype Highlight = Highlight (Last Specular)
115 deriving (Typeable, Semigroup, Show)
116
117 instance AttributeClass Highlight
118
119 _Highlight :: Iso' Highlight Specular
120 _Highlight = iso (\(Highlight (Last s)) -> s) (Highlight . Last)
121
122 -- | Set the specular highlight.
123 highlight :: HasStyle d => Specular -> d -> d
124 highlight = applyAttr . review _Highlight
125
126 -- | Lens onto the possible specular highlight in a style
127 _highlight :: Lens' (Style v n) (Maybe Specular)
128 _highlight = atAttr . mapping _Highlight
129
130 -- | Traversal over the highlight intensity of a style. If the style has
131 -- no 'Specular', setting this will do nothing.
132 highlightIntensity :: Traversal' (Style v n) Double
133 highlightIntensity = _highlight . _Just . specularSize
134
135 -- | Traversal over the highlight size in a style. If the style has no
136 -- 'Specular', setting this will do nothing.
137 highlightSize :: Traversal' (Style v n) Double
138 highlightSize = _highlight . _Just . specularSize