/
Vector3.purs
394 lines (315 loc) · 8.5 KB
/
Vector3.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
-- | - Types
-- | - [Vec](#t:Vec)
-- |
-- | - Constructors
-- | - [vec](#v:vec)
-- | - [oneX](#v:oneX)
-- | - [oneY](#v:oneY)
-- | - [oneZ](#v:oneZ)
-- |
-- | - Destructors
-- | - [unVec](#v:unVec)
-- | - [getX](#v:getX)
-- | - [getY](#v:getY)
-- | - [getZ](#v:getY)
-- |
-- | - Vector Modifiers
-- | - [rotRight](#v:rotRight)
-- | - [rotLeft](#v:rotLeft)
-- |
-- | - Componentwise Operations
-- | - [vdiv](#v:vdiv)
-- | - [vmod](#v:vmod)
-- | - [half](#v:half)
-- | - [twice](#v:twice)
-- |
-- | - Component Modifiers
-- | - [setX](#v:setX)
-- | - [setY](#v:setY)
-- | - [setZ](#v:setZ)
-- | - [modifyX](#v:modifyX)
-- | - [modifyY](#v:modifyY)
-- | - [modifyZ](#v:modifyZ)
-- |
-- | - Lens API
-- | - [_x](#v:_x)
-- | - [_y](#v:_y)
-- | - [_z](#v:_z)
module Data.Vector3
--- Types
( Vec(..)
--- Constructors
, vec
, oneX
, oneY
, oneZ
--- Destructors
, unVec
, getX
, getY
, getZ
--- Vector Modifiers
, rotRight
, rotLeft
--- Componentwise Operations
, vdiv
, (//)
, vmod
, half
, twice
--- Component Modifiers
, setX
, setY
, setZ
, modifyX
, modifyY
, modifyZ
--- Lens API
, _x
, _y
, _z
) where
import Prelude
import Control.Apply (lift2)
import Data.Foldable (class Foldable, foldMapDefaultL)
import Data.Generic.Rep (class Generic)
import Data.Lens (Lens', lens)
import Data.Show.Generic (genericShow)
import Data.Traversable (class Traversable, sequenceDefault)
--------------------------------------------------------------------------------
--- Types
--------------------------------------------------------------------------------
-- | Polymorphic 3D vector
data Vec a
-- | Creates a vector from three components
= Vec a a a
derive instance Generic (Vec a) _
derive instance Eq a => Eq (Vec a)
derive instance Ord a => Ord (Vec a)
derive instance Functor Vec
instance Foldable Vec where
foldr f b (Vec x y z) = f x (f y (f z b))
foldl f b (Vec x y z) = f (f (f b x) y) z
foldMap = foldMapDefaultL
instance Traversable Vec where
traverse f (Vec x y z) = Vec <$> f x <*> f y <*> f z
sequence = sequenceDefault
instance Show a => Show (Vec a) where
show = genericShow
-- | Componentwise `Semiring` implementation
-- | ```
-- | > Vec 2 3 7 * Vec 4 5 2
-- | Vec 8 15 14
-- | ```
instance Semiring a => Semiring (Vec a) where
add = lift2 add
zero = pure zero
mul = lift2 mul
one = pure one
instance Ring a => Ring (Vec a) where
sub = lift2 sub
instance Applicative Vec where
pure x = Vec x x x
-- | Zippy `Apply` implementation
-- | ```
-- | > (<>) <$> Vec "A" "B" "C" <*> Vec "1" "2" "3"
-- | Vec "A1" "B2" "C3"
-- | ```
instance Apply Vec where
apply (Vec f g h) (Vec x y z) = Vec (f x) (g y) (h z)
--------------------------------------------------------------------------------
--- Constructors
--------------------------------------------------------------------------------
-- | Creates a vector from two components
vec :: forall a. a -> a -> a -> Vec a
vec = Vec
-- | Vector with Y value `one` and other values `zero`.
-- |
-- | In analogy to the existing `Semiring` methods `one` and `zero` for `Vec`.
-- |
-- | ```
-- | > oneX + oneY + oneZ == one
-- | true
-- | ```
oneX :: forall a. Semiring a => Vec a
oneX = Vec one zero zero
-- | Vector with Z value `one` and other values `zero`.
-- |
-- | In analogy to the existing `Semiring` methods `one` and `zero` for `Vec`
-- |
-- | ```
-- | > oneX + oneY + oneZ == one
-- | true
-- | ```
oneY :: forall a. Semiring a => Vec a
oneY = Vec zero one one
-- | Vector with X value `one` and other values `zero`.
-- |
-- | In analogy to the existing `Semiring` methods `one` and `zero` for `Vec`
-- |
-- | ```
-- | > oneX + oneY + oneZ == one
-- | true
-- | ```
oneZ :: forall a. Semiring a => Vec a
oneZ = Vec zero zero one
--------------------------------------------------------------------------------
--- Destructors
--------------------------------------------------------------------------------
-- | Pattern match on a vector by providing a reducer function
-- |
-- | ```
-- | > unVec (\x y z -> x <> y <> z) (Vec "1" "2" "3")
-- | "123"
-- | ```
unVec :: forall a z. (a -> a -> a -> z) -> Vec a -> z
unVec f (Vec x y z) = f x y z
-- | Retrieves the X component of a vector
-- |
-- | ```
-- | > getX (Vec 1 2 3)
-- | 1
-- | ```
getX :: forall a. Vec a -> a
getX (Vec x _ _) = x
-- | Retrieves the Y component of a vector
-- |
-- | ```
-- | > getY (Vec 1 2 3)
-- | 2
-- | ```
getY :: forall a. Vec a -> a
getY (Vec _ y _) = y
-- | Retrieves the Z component of a vector
-- |
-- | ```
-- | > getZ (Vec 1 2 3)
-- | 3
-- | ```
getZ :: forall a. Vec a -> a
getZ (Vec _ _ z) = z
--------------------------------------------------------------------------------
--- Vector Modifiers
--------------------------------------------------------------------------------
-- | Rotates the components of the vector to the right
-- |
-- | ```
-- | > rotRight (Vec 1 2 3)
-- | Vec 3 1 2
-- | ```
rotRight :: forall a. Vec a -> Vec a
rotRight (Vec x y z) = Vec z x y
-- | Rotates the components of the vector to the left
-- |
-- | ```
-- | > rotRight (Vec 1 2 3)
-- | Vec 2 3 1
-- | ```
rotLeft :: forall a. Vec a -> Vec a
rotLeft (Vec x y z) = Vec y z x
--------------------------------------------------------------------------------
--- Componentwise Operations
--------------------------------------------------------------------------------
-- | Divides two vectors componentwise.
-- | This exists because there cannot be an `EuclideanRing` instance for `Vec`
-- |
-- | ```
-- | > vdiv (Vec 9 6 4) (Vec 3 2 4)
-- | Vec 3 3 1
-- | ```
vdiv :: forall a. EuclideanRing a => Vec a -> Vec a -> Vec a
vdiv (Vec x1 y1 z1) (Vec x2 y2 z2) = Vec (div x1 x2) (div y1 y2) (div z1 z2)
infixl 7 vdiv as //
-- | Componentwise Modulo operation
-- | This exists because there cannot be an `EuclideanRing` instance for `Vec`
-- |
-- | ```
-- | > mod (Vec 12 120 1200) (Vec 120 100 1000)
-- | Vec 2 20 200
-- | ```
vmod :: forall a. EuclideanRing a => Vec a -> Vec a -> Vec a
vmod (Vec x1 y1 z1) (Vec x2 y2 z2) = Vec (mod x1 x2) (mod y1 y2) (mod z1 z2)
-- | Halves the amount of each component
-- |
-- | ```
-- | > half (Vec 10 100 1000)
-- | Vec 5 50 500
-- | ```
half :: forall a. EuclideanRing a => Vec a -> Vec a
half (Vec x y z) = Vec (x / two) (y / two) (z / two)
-- | Duplicates the amount of each component
-- |
-- | ```
-- | > twice (Vec 10 100 1000)
-- | Vec 20 200 2000
-- | ```
twice :: forall a. EuclideanRing a => Vec a -> Vec a
twice (Vec x y z) = Vec (x * two) (y * two) (z * two)
--------------------------------------------------------------------------------
--- Component Modifiers
--------------------------------------------------------------------------------
-- | Sets the X component of a vector
-- |
-- | ```
-- | > setX "G" (Vec "A" "B" "C")
-- | Vec "G" "B" "C"
-- | ```
setX :: forall a. a -> Vec a -> Vec a
setX x (Vec _ y z) = Vec x y z
-- | Sets the Y component of a vector
-- |
-- | ```
-- | > setY "G" (Vec "A" "B" "C")
-- | Vec "A" "G" "C"
-- | ```
setY :: forall a. a -> Vec a -> Vec a
setY y (Vec x _ z) = Vec x y z
-- | Sets the Z component of a vector
-- |
-- | ```
-- | > setZ "G" (Vec "A" "B" "C")
-- | Vec "A" "B" "G"
-- | ```
setZ :: forall a. a -> Vec a -> Vec a
setZ z (Vec x y _) = Vec x y z
-- | Modifies the X component of a vector
-- |
-- | ```
-- | > modifyX (add 10) (Vec 3 4 2)
-- | Vec 13 4 2
-- | ```
modifyX :: forall a. (a -> a) -> Vec a -> Vec a
modifyX f (Vec x y z) = Vec (f x) y z
-- | Modifies the Y component of a vector
-- |
-- | ```
-- | > modifyY (add 10) (Vec 3 4 2)
-- | Vec 3 14 2
-- | ```
modifyY :: forall a. (a -> a) -> Vec a -> Vec a
modifyY f (Vec x y z) = Vec x (f y) z
-- | Modifies the Z component of a vector
-- |
-- | ```
-- | > modifyZ (add 10) (Vec 3 4 2)
-- | Vec 3 4 20
-- | ```
modifyZ :: forall a. (a -> a) -> Vec a -> Vec a
modifyZ f (Vec x y z) = Vec x y (f z)
--------------------------------------------------------------------------------
--- Lens API
--------------------------------------------------------------------------------
-- | A Lens on the X component of a vector
_x :: forall a. Lens' (Vec a) a
_x = lens getX (flip setX)
-- | A Lens on the Y component of a vector
_y :: forall a. Lens' (Vec a) a
_y = lens getY (flip setY)
-- | A Lens on the Z component of a vector
_z :: forall a. Lens' (Vec a) a
_z = lens getZ (flip setZ)
--------------------------------------------------------------------------------
--- Internal Util
--------------------------------------------------------------------------------
two :: forall a. Semiring a => a
two = one + one