]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Cardinal.hs
Fix all orphan instances.
[spline3.git] / src / Cardinal.hs
1 -- | The Cardinal module contains the Cardinal data type, representing
2 -- a cardinal direction (one of the 26 directions surrounding the
3 -- center of a cube. In addition to those 26 directions, we also
4 -- include the interior point and a number of composite types that
5 -- allow us to perform arithmetic on directions.
6 module Cardinal
7 where
8
9 import Control.Monad (liftM, liftM2)
10 import Prelude hiding (LT)
11 import Test.QuickCheck (Arbitrary(..), oneof)
12
13 data Cardinal = F -- ^ Front
14 | B -- ^ Back
15 | L -- ^ Left
16 | R -- ^ Right
17 | D -- ^ Down
18 | T -- ^ Top
19 | FL -- ^ Front Left
20 | FR -- ^ Front Right
21 | FD -- ^ Front Down
22 | FT -- ^ Front Top
23 | BL -- ^ Back Left
24 | BR -- ^ Back Right
25 | BD -- ^ Back Down
26 | BT -- ^ Back Top
27 | LD -- ^ Left Down
28 | LT -- ^ Left Top
29 | RD -- ^ Right Down
30 | RT -- ^ Right Top
31 | FLD -- ^ Front Left Down
32 | FLT -- ^ Front Left Top
33 | FRD -- ^ Front Right Down
34 | FRT -- ^ Front Right Top
35 | BLD -- ^ Back Left Down
36 | BLT -- ^ Back Left Top
37 | BRD -- ^ Back Right Down
38 | BRT -- ^ Back Right Top
39 | I -- ^ Interior
40 | Scalar Double -- ^ A wrapper around a scalar value.
41 | Sum Cardinal Cardinal -- ^ The sum of two directions.
42 | Difference Cardinal Cardinal
43 -- ^ The difference of two directions, the first minus the second.
44 | Product Cardinal Cardinal -- ^ The product of two directions.
45 | Quotient Cardinal Cardinal
46 -- ^ The quotient of two directions, the first divided by the
47 -- second.
48 deriving (Show, Eq)
49
50
51 -- | By making Cardinal an instance of 'Num', we gain the ability to
52 -- add, subtract, and multiply directions. The results of these
53 -- operations are never actually calculated; the types just keep
54 -- track of which operations were performed in which order.
55 instance Num Cardinal where
56 x + y = Sum x y
57 x - y = Difference x y
58 x * y = Product x y
59 negate = Product (Scalar (-1))
60 abs x = x
61 signum x = x
62 fromInteger x = Scalar (fromIntegral x)
63
64
65 -- | Like the Num instance, the 'Fractional' instance allows us to
66 -- take quotients of directions.
67 instance Fractional Cardinal where
68 x / y = Quotient x y
69 recip = Quotient (Scalar 1)
70 fromRational x = Scalar (fromRational x)
71
72
73
74 instance Arbitrary Cardinal where
75 arbitrary = oneof [f,b,l,r,d,t,fl,fr,fd,ft,bl,br,bd,bt,ld,lt,
76 rd,rt,fld,flt,frd,frt,bld,blt,brd,brt,i,
77 scalar,csum,cdiff,cprod,cquot]
78 where
79 f = return F
80 b = return B
81 l = return L
82 r = return R
83 d = return D
84 t = return T
85 fl = return FL
86 fr = return FR
87 fd = return FD
88 ft = return FT
89 bl = return BL
90 br = return BR
91 bd = return BD
92 bt = return BT
93 ld = return LD
94 lt = return LT
95 rd = return RD
96 rt = return RT
97 fld = return FLD
98 flt = return FLT
99 frd = return FRD
100 frt = return FRT
101 bld = return BLD
102 blt = return BLT
103 brd = return BRD
104 brt = return BRT
105 i = return I
106 scalar = liftM Scalar arbitrary
107 csum = liftM2 Sum arbitrary arbitrary
108 cdiff = liftM2 Difference arbitrary arbitrary
109 cprod = liftM2 Product arbitrary arbitrary
110 cquot = liftM2 Quotient arbitrary arbitrary
111
112
113 -- | Rotate a cardinal direction counter-clockwise about the x-axis.
114 ccwx :: Cardinal -> Cardinal
115 ccwx F = F
116 ccwx B = B
117 ccwx L = T
118 ccwx R = D
119 ccwx D = L
120 ccwx T = R
121 ccwx FL = FT
122 ccwx FR = FD
123 ccwx FD = FL
124 ccwx FT = FR
125 ccwx BL = BT
126 ccwx BR = BD
127 ccwx BD = BL
128 ccwx BT = BR
129 ccwx LD = LT
130 ccwx LT = RT
131 ccwx RD = LD
132 ccwx RT = RD
133 ccwx FLD = FLT
134 ccwx FLT = FRT
135 ccwx FRD = FLD
136 ccwx FRT = FRD
137 ccwx BLD = BLT
138 ccwx BLT = BRT
139 ccwx BRD = BLD
140 ccwx BRT = BRD
141 ccwx I = I
142 ccwx (Scalar s) = (Scalar s)
143 ccwx (Sum c0 c1) = Sum (ccwx c0) (ccwx c1)
144 ccwx (Difference c0 c1) = Difference (ccwx c0) (ccwx c1)
145 ccwx (Product c0 c1) = Product (ccwx c0) (ccwx c1)
146 ccwx (Quotient c0 c1) = Quotient (ccwx c0) (ccwx c1)
147
148 -- | Rotate a cardinal direction clockwise about the x-axis.
149 cwx :: Cardinal -> Cardinal
150 cwx = ccwx . ccwx . ccwx
151
152
153 -- | Rotate a cardinal direction counter-clockwise about the y-axis.
154 ccwy :: Cardinal -> Cardinal
155 ccwy F = D
156 ccwy B = T
157 ccwy L = L
158 ccwy R = R
159 ccwy D = B
160 ccwy T = F
161 ccwy FL = LD
162 ccwy FR = RD
163 ccwy FD = BD
164 ccwy FT = FD
165 ccwy BL = LT
166 ccwy BR = RT
167 ccwy BD = BT
168 ccwy BT = FT
169 ccwy LD = BL
170 ccwy LT = FL
171 ccwy RD = BR
172 ccwy RT = FR
173 ccwy FLD = BLD
174 ccwy FLT = FLD
175 ccwy FRD = BRD
176 ccwy FRT = FRD
177 ccwy BLD = BLT
178 ccwy BLT = FLT
179 ccwy BRD = BRT
180 ccwy BRT = FRT
181 ccwy I = I
182 ccwy (Scalar s) = (Scalar s)
183 ccwy (Sum c0 c1) = Sum (ccwy c0) (ccwy c1)
184 ccwy (Difference c0 c1) = Difference (ccwy c0) (ccwy c1)
185 ccwy (Product c0 c1) = Product (ccwy c0) (ccwy c1)
186 ccwy (Quotient c0 c1) = Quotient (ccwy c0) (ccwy c1)
187
188 -- | Rotate a cardinal direction clockwise about the y-axis.
189 cwy :: Cardinal -> Cardinal
190 cwy = ccwy . ccwy . ccwy
191
192
193 -- | Rotate a cardinal direction counter-clockwise about the z-axis.
194 ccwz :: Cardinal -> Cardinal
195 ccwz F = R
196 ccwz B = L
197 ccwz L = F
198 ccwz R = B
199 ccwz D = D
200 ccwz T = T
201 ccwz FL = FR
202 ccwz FR = BR
203 ccwz FD = RD
204 ccwz FT = RT
205 ccwz BL = FL
206 ccwz BR = BL
207 ccwz BD = LD
208 ccwz BT = LT
209 ccwz LD = FD
210 ccwz LT = FT
211 ccwz RD = BD
212 ccwz RT = BT
213 ccwz FLD = FRD
214 ccwz FLT = FRT
215 ccwz FRD = BRD
216 ccwz FRT = BRT
217 ccwz BLD = FLD
218 ccwz BLT = FLT
219 ccwz BRD = BLD
220 ccwz BRT = BLT
221 ccwz I = I
222 ccwz (Scalar s) = (Scalar s)
223 ccwz (Sum c0 c1) = Sum (ccwz c0) (ccwz c1)
224 ccwz (Difference c0 c1) = Difference (ccwz c0) (ccwz c1)
225 ccwz (Product c0 c1) = Product (ccwz c0) (ccwz c1)
226 ccwz (Quotient c0 c1) = Quotient (ccwz c0) (ccwz c1)
227
228 -- | Rotate a cardinal direction clockwise about the z-axis.
229 cwz :: Cardinal -> Cardinal
230 cwz = ccwz . ccwz . ccwz