Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] Annotation of /src/common/sml3d-type-util.sml
ViewVC logotype

Annotation of /src/common/sml3d-type-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 71 - (view) (download)

1 : jhr 19 (* sml3d-type-util.sml
2 : jhr 1 *
3 :     * COPYRIGHT (c) 2006 John Reppy (http://www.cs.uchicago.edu/~jhr)
4 :     * All rights reserved.
5 : jhr 16 *
6 :     * Utility functions on the various vector types.
7 : jhr 1 *)
8 :    
9 : jhr 20 structure SML3dTypeUtil : SML3D_TYPE_UTIL =
10 : jhr 1 struct
11 :    
12 : jhr 20 open SML3dTypes
13 : jhr 1
14 :     (* pack tuples as records *)
15 :     fun packv2 (x, y) = {x=x, y=y}
16 :     fun packv3 (x, y, z) = {x=x, y=y, z=z}
17 :     fun packv4 (x, y, z, w) = {x=x, y=y, z=z, w=w}
18 :    
19 :     fun packt2 (s, t) = {s=s, t=t}
20 :     fun packt3 (s, t, r) = {s=s, t=t, r=r}
21 :     fun packt4 (s, t, r, q) = {s=s, t=t, r=r, q=q}
22 :    
23 :     fun packc3 (r, g, b) = {r=r, g=g, b=b}
24 :     fun packc4 (r, g, b, a) = {r=r, g=g, b=b, a=a}
25 :    
26 : jhr 71 (* add/strip an alpha channel *)
27 :     fun addAlpha ({r, g, b}, a) = {r=r, g=g, b=b, a=a}
28 :     fun stripAlpha {r, g, b, a} = {r=r, g=g, b=b}
29 :    
30 : jhr 1 (* unpack records as tuples *)
31 :     fun unpackv2 {x, y} = (x, y)
32 :     fun unpackv3 {x, y, z} = (x, y, z)
33 :     fun unpackv4 {x, y, z, w} = (x, y, z, w)
34 :    
35 :     fun unpackt2 {s, t} = (s, t)
36 :     fun unpackt3 {s, t, r} = (s, t, r)
37 :     fun unpackt4 {s, t, r, q} = (s, t, r, q)
38 :    
39 :     fun unpackc3 {r, g, b} = (r, g, b)
40 :     fun unpackc4 {r, g, b, a} = (r, g, b, a)
41 :    
42 : jhr 16 (* iterators *)
43 : jhr 20 fun mapv2 f {x, y} = {x = f x, y = f y}
44 :     fun mapv3 f {x, y, z} = {x = f x, y = f y, z = f z}
45 : jhr 16 fun mapv4 f {x, y, z, w} = {x = f x, y = f y, z = f z, w = f w}
46 :    
47 :     fun mapt2 f {s, t} = {s = f s, t = f t}
48 :     fun mapt3 f {s, t, r} = {s = f s, t = f t, r = f r}
49 :     fun mapt4 f {s, t, r, q} = {s = f s, t = f t, r = f r, q = f q}
50 :    
51 :     fun mapc3 f {r, g, b} = {r = f r, g = f g, b = f b}
52 :     fun mapc4 f {r, g, b, a} = {r = f r, g = f g, b = f b, a = f a}
53 :    
54 : jhr 20 fun appv2 (f : 'a -> unit) {x, y} = (f x; f y)
55 :     fun appv3 (f : 'a -> unit) {x, y, z} = (f x; f y; f z)
56 :     fun appv4 (f : 'a -> unit) {x, y, z, w} = (f x; f y; f z; f w)
57 : jhr 16
58 : jhr 20 fun appt2 (f : 'a -> unit) {s, t} = (f s; f t)
59 :     fun appt3 (f : 'a -> unit) {s, t, r} = (f s; f t; f r)
60 :     fun appt4 (f : 'a -> unit) {s, t, r, q} = (f s; f t; f r; f q)
61 : jhr 16
62 : jhr 20 fun appc3 (f : 'a -> unit) {r, g, b} = (f r; f g; f b)
63 :     fun appc4 (f : 'a -> unit) {r, g, b, a} = (f r; f g; f b; f a)
64 : jhr 16
65 : jhr 20 (* string representations *)
66 :     fun fmtv2 fmt {x, y} = String.concat[
67 :     "{x=", fmt x, ", y=", fmt y, "}"
68 :     ]
69 :     fun fmtv3 fmt {x, y, z} = String.concat[
70 :     "{x=", fmt x, ", y=", fmt y, ", z=", fmt z, "}"
71 :     ]
72 :     fun fmtv4 fmt {x, y, z, w} = String.concat[
73 :     "{x=", fmt x, ", y=", fmt y, ", z=", fmt z, ", w=", fmt w, "}"
74 :     ]
75 :    
76 :     fun fmtt2 fmt {s, t} = String.concat[
77 :     "{s=", fmt s, ", t=", fmt t, "}"
78 :     ]
79 :     fun fmtt3 fmt {s, t, r} = String.concat[
80 :     "{s=", fmt s, ", t=", fmt t, ", r=", fmt r, "}"
81 :     ]
82 :     fun fmtt4 fmt {s, t, r, q} = String.concat[
83 :     "{s=", fmt s, ", t=", fmt t, ", r=", fmt r, ", q=", fmt q, "}"
84 :     ]
85 :    
86 :     fun fmtc3 fmt {r,g,b} = String.concat[
87 :     "{r=", fmt r, ", g=", fmt g, ", b=", fmt b, "}"
88 :     ]
89 :     fun fmtc4 fmt {r, g, b, a} = String.concat[
90 :     "{r=", fmt r, ", g=", fmt g, ", b=", fmt b, ", a=", fmt a, "}"
91 :     ]
92 :    
93 :     fun fmt2 fmt (a, b) = String.concat["(", fmt a, ", ", fmt b, ")"]
94 :     fun fmt3 fmt (a, b, c) = String.concat[
95 :     "(", fmt a, ", ", fmt b, ", ", fmt c, ")"
96 :     ]
97 :     fun fmt4 fmt (a, b, c, d) = String.concat[
98 :     "(", fmt a, ", ", fmt b, ", ", fmt c, ", ", fmt d, ")"
99 :     ]
100 :    
101 : jhr 1 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0