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

SCM Repository

[sml3d] Annotation of /trunk/sml3d/src/particles/compiler/translate.sml
ViewVC logotype

Annotation of /trunk/sml3d/src/particles/compiler/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1074 - (view) (download)

1 : pavelk 746 (* translate.sml
2 :     *
3 :     * COPYRIGHT (c) 2009 John Reppy (http://cs.uchicago.edu/~jhr)
4 :     * All rights reserved.
5 :     *
6 :     * Translate a particle system to the IR.
7 :     *)
8 :    
9 :     structure Translate : sig
10 :    
11 : jhr 1050 val compile : Particles.program -> PSysIR.program
12 : pavelk 746
13 :     end = struct
14 :    
15 :     open SML3dTypeUtil
16 :    
17 :     structure P = ParticlesImp
18 :     structure PSV = P.PSV
19 :     structure IR = PSysIR
20 : pavelk 770
21 :     fun printErr s = TextIO.output(TextIO.stdErr, s ^ "\n")
22 : pavelk 746
23 :     datatype particle_state = PS of {
24 : jhr 750 pos : IR.var, (* vec3 *)
25 :     vel : IR.var, (* vec3 *)
26 :     size : IR.var, (* float *)
27 : pavelk 915 ttl : IR.var, (* float *)
28 : pavelk 863 color : IR.var, (* vec3 (NOTE: should be vector4) *)
29 : pavelk 1027 rot : IR.var, (* float *)
30 : pavelk 900 pos2 : IR.var, (* vec3 *)
31 : pavelk 863 dummy : IR.var
32 : pavelk 746 }
33 :    
34 :     (* special PSV global variables *)
35 :     val epsilon = PSV.constf(0.00001)
36 :    
37 :     (* constants *)
38 :     val pi = 3.14159265358979
39 :    
40 :     (* dummy placeholder *)
41 :     fun dummy (state, k) =
42 : pavelk 747 IR.mkPRIM(
43 : pavelk 746 IR.newLocal(
44 :     "temp",
45 :     IR.T_BOOL,
46 :     (IR.COPY, [IR.newConst("c", IR.C_BOOL false)])
47 :     ),
48 : pavelk 747 IR.COPY,
49 :     [IR.newConst("c", IR.C_BOOL false)],
50 : pavelk 746 k state
51 :     )
52 :    
53 : pavelk 870
54 :     fun retState s = let
55 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = s
56 : pavelk 870 in
57 : pavelk 905 IR.mkRETURN (
58 : pavelk 1027 [pos, vel, size, ttl, color, rot, pos2, dummy],
59 :     [IR.POS, IR.VEL, IR.SZ, IR.TTL, IR.COLOR, IR.ROT, IR.POS2, IR.DUMMY]
60 : pavelk 905 )
61 : pavelk 870 end
62 :    
63 : pavelk 746 (* translation environment *)
64 :     datatype env = TE of (IR.block list ref * IR.var PSV.Map.map)
65 :    
66 : pavelk 770 fun psvToIRVar (TE(_, env), x as PSV.V{name, id, ...}) = (case PSV.Map.find(env, x)
67 : pavelk 746 of SOME x' => x'
68 : pavelk 770 | NONE => raise Fail (String.concat["unknown variable ", name, " with ID ", Int.toString id])
69 : pavelk 746 (* end case *))
70 :    
71 :     fun insert (TE(blks, env), x, x') = TE(blks, PSV.Map.insert (env, x, x'))
72 :    
73 :     (* create a block that implements the given continuation *)
74 :     fun newBlock (TE(blks, _), k : particle_state -> IR.stmt) = let
75 :     val pos = IR.newParam ("ps_pos", IR.T_VEC)
76 :     val vel = IR.newParam ("ps_vel", IR.T_VEC)
77 :     val size = IR.newParam ("ps_size", IR.T_FLOAT)
78 : pavelk 915 val ttl = IR.newParam ("ps_ttl", IR.T_FLOAT)
79 : pavelk 746 val color = IR.newParam ("ps_color", IR.T_VEC)
80 : pavelk 1027 val rot = IR.newParam("ps_rot", IR.T_FLOAT)
81 : pavelk 864 val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)
82 : pavelk 900 val pos2 = IR.newParam ("ps_pos2", IR.T_VEC)
83 : pavelk 1027 val state = PS{pos=pos, vel=vel, size=size, ttl=ttl, color=color, rot=rot, pos2=pos2, dummy=dummy}
84 :     val blk = IR.newBlock ([pos, vel, size, ttl, color, rot, pos2, dummy], k state)
85 : pavelk 746 in
86 :     blks := blk :: !blks;
87 :     blk
88 :     end
89 :    
90 :     fun newBlockWithArgs (TE(blks, _), args, k : particle_state -> IR.stmt) = let
91 :     val pos = IR.newParam ("ps_pos", IR.T_VEC)
92 :     val vel = IR.newParam ("ps_vel", IR.T_VEC)
93 :     val size = IR.newParam ("ps_size", IR.T_FLOAT)
94 : pavelk 915 val ttl = IR.newParam ("ps_ttl", IR.T_FLOAT)
95 : pavelk 746 val color = IR.newParam ("ps_color", IR.T_VEC)
96 : pavelk 1027 val rot = IR.newParam("ps_rot", IR.T_FLOAT)
97 : pavelk 864 val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)
98 : pavelk 900 val pos2 = IR.newParam ("ps_pos2", IR.T_VEC)
99 : pavelk 1027 val state = PS{pos=pos, vel=vel, size=size, ttl=ttl, color=color, rot=rot, pos2=pos2, dummy=dummy}
100 :     val blk = IR.newBlock ([pos, vel, size, ttl, color, rot, pos2, dummy] @ args, k state)
101 : pavelk 746 in
102 :     blks := blk :: !blks;
103 :     blk
104 :     end
105 :    
106 : pavelk 1027 fun goto (PS{pos, vel, size, ttl, color, rot, pos2, dummy}, blk) =
107 :     IR.mkGOTO(blk, [pos, vel, size, ttl, color, rot, pos2, dummy])
108 : pavelk 746
109 : pavelk 1027 fun gotoWithArgs(PS{pos, vel, size, ttl, color, rot, pos2, dummy}, args, blk) =
110 :     IR.mkGOTO(blk, [pos, vel, size, ttl, color, rot, pos2, dummy] @ args)
111 : pavelk 746
112 :     fun letPRIM (x, ty, p, args, body) = let
113 :     val x' = IR.newLocal(x, ty, (p, args))
114 :     in
115 :     IR.mkPRIM(x', p, args, body x')
116 :     end
117 :    
118 :     (* prim bound to state variable (S_LOCAL for now) *)
119 :     fun letSPRIM(x, ty, p, args, body) = let
120 : pavelk 862 val x' = IR.new(x, IR.S_LOCAL(ref (p, args)), ty)
121 : pavelk 746 in
122 :     IR.mkPRIM(x', p, args, body x')
123 :     end
124 :    
125 :     (* Not sure if this should be made into a primitive or not, but
126 :     * basically this creates the XOR'd value of var1 and var2 and
127 :     * stores it in result.
128 :     *)
129 :     fun mkXOR (result, var1, var2, stmt : IR.var -> IR.stmt) =
130 :     letPRIM("testOR", IR.T_BOOL, IR.OR, [var1, var2], fn testOR =>
131 :     letPRIM("testAND", IR.T_BOOL, IR.AND, [var1, var2], fn testAND =>
132 :     letPRIM("testNAND", IR.T_BOOL, IR.NOT, [testAND], fn testNAND =>
133 :     letPRIM(result, IR.T_BOOL, IR.AND, [testOR, testNAND], stmt))))
134 :    
135 : pavelk 1017 fun genFloatVar (fltVar, env, domain : Float.float P.domain, dist, stmt : IR.var -> IR.stmt) = let
136 :     fun genRandVal(var, stmt : IR.var -> IR.stmt) = (case dist
137 :     of P.DIST_UNIFORM =>
138 :     letPRIM(var, IR.T_FLOAT, IR.RAND, [], stmt)
139 :    
140 :     (* The PDF here is f(x) = 2x when 0 < x <= 1, so the CDF is going
141 :     * to be the integral of f from 0 -> y => y^2. Hence, whenever we
142 :     * generate a random number, in order to get the random value according
143 :     * to this probability distribution, we just square it.
144 :     *)
145 :     | P.DIST_INC_LIN =>
146 :     letPRIM("randVal", IR.T_FLOAT, IR.RAND, [], fn randVal =>
147 :     letPRIM(var, IR.T_FLOAT, IR.MULT, [randVal, randVal], stmt))
148 :    
149 :     (* The PDF here is f(x) = -2x + 2 when 0 <= x < 1, so the CDF is going
150 :     * to be the integral of f from 0 -> y => -(y^2) + 2y. Hence, whenever we
151 :     * generate a random number, in order to get the random value according
152 :     * to this probability distribution, we just square it.
153 :     *)
154 :     | P.DIST_DEC_LIN =>
155 :     letPRIM("randVal", IR.T_FLOAT, IR.RAND, [], fn randVal =>
156 :     letPRIM("randSq", IR.T_FLOAT, IR.MULT, [randVal, randVal], fn randSq =>
157 :     letPRIM("termOne", IR.T_FLOAT, IR.MULT, [randSq, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn termOne =>
158 :     letPRIM("termTwo", IR.T_FLOAT, IR.MULT, [randVal, IR.newConst("negOne", IR.C_FLOAT 2.0)], fn termTwo =>
159 :     letPRIM(var, IR.T_FLOAT, IR.ADD, [termOne, termTwo], stmt)
160 :     ))))
161 :    
162 :     | _ => raise Fail "Unable to create random float for specified distribution."
163 :     (* end case *))
164 :     in
165 :     (case domain
166 :     of P.D_POINT(pt) =>
167 :     (* Our options here are pretty limited... *)
168 :     letPRIM (fltVar, IR.T_FLOAT, IR.COPY, [psvToIRVar(env, pt)], stmt)
169 :    
170 :     | P.D_BOX{max, min} =>
171 :     genRandVal("randf", fn rand =>
172 :     letPRIM("boxDiff", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, max), psvToIRVar(env, max)], fn diff =>
173 :     letPRIM("scale", IR.T_FLOAT, IR.MULT, [diff, rand], fn scale =>
174 :     letPRIM( fltVar, IR.T_FLOAT, IR.ADD, [psvToIRVar(env, max), scale], stmt )
175 :     )))
176 :     | _ => raise Fail "Cannot generate float in specified domain."
177 :     (* end case *))
178 :     end
179 :    
180 : pavelk 746 (* Generates a random vector within the given domain and puts it in vecVar *)
181 : pavelk 1017 fun genVecVar (vecVar, env, domain : Vec3f.vec3 P.domain, stmt : IR.var -> IR.stmt) = (case domain
182 : pavelk 746 of P.D_POINT(pt) =>
183 :     (* Our options here are pretty limited... *)
184 :     letPRIM (vecVar, IR.T_VEC, IR.COPY, [psvToIRVar(env, pt)], stmt)
185 :    
186 :     | P.D_LINE({pt1, pt2}) =>
187 :     (* Lerp between the points. *)
188 :     letPRIM ("randVal", IR.T_FLOAT, IR.RAND, [], fn randVal =>
189 :     letPRIM ("randInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), randVal], fn randInv =>
190 :     letPRIM ("pt1s", IR.T_VEC, IR.SCALE, [randVal, psvToIRVar(env, pt1)], fn pt1ScaleVec =>
191 :     letPRIM ("pt2s", IR.T_VEC, IR.SCALE, [randInv, psvToIRVar(env, pt2)], fn pt2ScaleVec =>
192 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [pt1ScaleVec, pt2ScaleVec], stmt)))))
193 :    
194 : pavelk 873 | P.D_BOX{max, min} =>
195 :     (* Extract the componentwise vector variables *)
196 :     letPRIM("minX", IR.T_FLOAT, IR.EXTRACT_X, [psvToIRVar(env, min)], fn minX =>
197 :     letPRIM("maxX", IR.T_FLOAT, IR.EXTRACT_X, [psvToIRVar(env, max)], fn maxX =>
198 :     letPRIM("minY", IR.T_FLOAT, IR.EXTRACT_Y, [psvToIRVar(env, min)], fn minY =>
199 :     letPRIM("maxY", IR.T_FLOAT, IR.EXTRACT_Y, [psvToIRVar(env, max)], fn maxY =>
200 :     letPRIM("minZ", IR.T_FLOAT, IR.EXTRACT_Z, [psvToIRVar(env, min)], fn minZ =>
201 :     letPRIM("maxZ", IR.T_FLOAT, IR.EXTRACT_Z, [psvToIRVar(env, max)], fn maxZ =>
202 :    
203 :     (* Find the distance in each component *)
204 :     letPRIM("distX", IR.T_FLOAT, IR.SUB, [maxX, minX], fn distX =>
205 :     letPRIM("distY", IR.T_FLOAT, IR.SUB, [maxY, minY], fn distY =>
206 :     letPRIM("distZ", IR.T_FLOAT, IR.SUB, [maxZ, minZ], fn distZ =>
207 :    
208 :     (* Get three random numbers for each of the components *)
209 :     letPRIM("randX", IR.T_FLOAT, IR.RAND, [], fn randX =>
210 :     letPRIM("randY", IR.T_FLOAT, IR.RAND, [], fn randY =>
211 :     letPRIM("randZ", IR.T_FLOAT, IR.RAND, [], fn randZ =>
212 :    
213 :     (* Scale the distances by these random numbers *)
214 :     letPRIM("scaledX", IR.T_FLOAT, IR.MULT, [randX, distX], fn scaledX =>
215 :     letPRIM("scaledY", IR.T_FLOAT, IR.MULT, [randY, distY], fn scaledY =>
216 :     letPRIM("scaledZ", IR.T_FLOAT, IR.MULT, [randZ, distZ], fn scaledZ =>
217 :    
218 :     (* Add them to the minimum vec in order to create a new vec inside
219 :     * of the box.
220 :     *)
221 :     letPRIM("newX", IR.T_FLOAT, IR.ADD, [minX, scaledX], fn newX =>
222 :     letPRIM("newY", IR.T_FLOAT, IR.ADD, [minY, scaledY], fn newY =>
223 :     letPRIM("newZ", IR.T_FLOAT, IR.ADD, [minZ, scaledZ], fn newZ =>
224 :    
225 :     (* Gen the vector *)
226 :     letPRIM(vecVar, IR.T_VEC, IR.GEN_VEC, [newX, newY, newZ], stmt
227 :    
228 :     )))))))))))))))))))
229 :    
230 : pavelk 746
231 :     | P.D_TRIANGLE{pt1, pt2, pt3} =>
232 :     letPRIM ("pt1ToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn pt1ToPt2 =>
233 :     letPRIM ("pt1ToPt3", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt3), psvToIRVar(env, pt1)], fn pt1ToPt3 =>
234 :     letPRIM ("randOne", IR.T_FLOAT, IR.RAND, [], fn rand1 =>
235 :     letPRIM ("randTwo", IR.T_FLOAT, IR.RAND, [], fn rand2 =>
236 :     letPRIM ("randTwoInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), rand2], fn rand2Inv =>
237 :     letPRIM ("scaleOne", IR.T_VEC, IR.SCALE, [rand1, pt1ToPt2], fn scale1 =>
238 :     letPRIM ("nextScale1", IR.T_VEC, IR.SCALE, [rand2Inv, scale1], fn nextScale1 =>
239 :     letPRIM ("scaleTwo", IR.T_VEC, IR.SCALE, [rand2, pt1ToPt3], fn scale2 =>
240 :     letPRIM ("tempAdd", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, pt1), nextScale1], fn tempAdd =>
241 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [tempAdd, scale2], stmt))))))))))
242 :    
243 :     | P.D_CYLINDER {pt1, pt2, irad, orad} => let
244 :     val normVar = PSV.new("local_ht", PSV.T_VEC3F)
245 :     in
246 :     letPRIM("rand", IR.T_FLOAT, IR.RAND, [], fn ourRand =>
247 :     letPRIM("n", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn normVec =>
248 :     letPRIM("ht", IR.T_FLOAT, IR.LEN, [normVec], fn height =>
249 :     letPRIM("htInv", IR.T_FLOAT, IR.DIV, [IR.newConst("one", IR.C_FLOAT 1.0), height], fn htInv =>
250 :     letPRIM("n", IR.T_VEC, IR.SCALE, [htInv, normVec], fn norm =>
251 :     (* Generate a point in the lower disc. *)
252 :     genVecVar("ptInDisc", insert(env, normVar, norm), P.D_DISC{pt = pt1, normal = normVar, irad = irad, orad = orad}, fn ptInDisc =>
253 :     (* Now add this point to a random scaling of the normVec. *)
254 :     letPRIM("s", IR.T_FLOAT, IR.MULT, [height, ourRand], fn scale =>
255 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [scale, normVec], fn scaledNormVec =>
256 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [ptInDisc, scaledNormVec], stmt)))))))))
257 :     end
258 :    
259 :     | P.D_DISC {pt, normal, irad, orad} =>
260 :     (* Get a random angle... *)
261 :     letPRIM ("r", IR.T_FLOAT, IR.RAND, [], fn randForAng =>
262 :     letPRIM ("t", IR.T_FLOAT, IR.MULT, [randForAng, IR.newConst("fullCir", IR.C_FLOAT (2.0 * pi))], fn randAng =>
263 :     (* Get a random radius *)
264 :     letPRIM ("e0", IR.T_FLOAT, IR.RAND, [], fn newRand =>
265 :     letPRIM ("e0sq", IR.T_FLOAT, IR.MULT, [newRand, newRand], fn randRadSq =>
266 :     letPRIM ("radDiff", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, orad), psvToIRVar(env, irad)], fn radDiff =>
267 :     letPRIM ("newRadDist", IR.T_FLOAT, IR.MULT, [randRadSq, radDiff], fn newRadDist =>
268 :     letPRIM ("newRad", IR.T_FLOAT, IR.ADD, [psvToIRVar(env, irad), newRadDist], fn newRad =>
269 :     (* Find a vector in the plane of the disc, and then
270 :     * translate it to the center.
271 :     *)
272 :     letPRIM ("ntoc", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), psvToIRVar(env, normal)], fn normToCen =>
273 :     letPRIM ("v", IR.T_VEC, IR.CROSS, [psvToIRVar(env, pt), normToCen], fn vecInDisc =>
274 :     letPRIM ("vidn", IR.T_VEC, IR.NORM, [vecInDisc], fn vecInDiscNorm =>
275 :     letPRIM ("p", IR.T_VEC, IR.CROSS, [vecInDiscNorm, psvToIRVar(env, normal)], fn ptInDisc =>
276 :     letPRIM ("pidn", IR.T_VEC, IR.NORM, [ptInDisc], fn ptInDiscNorm =>
277 :     (* Figure out x and y values for our new radius and angle *)
278 :     letPRIM ("rx", IR.T_FLOAT, IR.COS, [randAng], fn radX =>
279 :     letPRIM ("ar1", IR.T_FLOAT, IR.MULT, [newRad, radX], fn amtVecOne =>
280 :     letPRIM ("rv1", IR.T_VEC, IR.SCALE, [amtVecOne, vecInDiscNorm], fn resVecOne =>
281 :     letPRIM ("ry", IR.T_FLOAT, IR.SIN, [randAng], fn radY =>
282 :     letPRIM ("ar2", IR.T_FLOAT, IR.MULT, [newRad, radY], fn amtVecTwo =>
283 :     letPRIM ("rv2", IR.T_VEC, IR.SCALE, [amtVecTwo, ptInDiscNorm], fn resVecTwo =>
284 :     letPRIM ("res", IR.T_VEC, IR.ADD_VEC, [resVecOne, resVecTwo], fn result =>
285 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [result, psvToIRVar(env, pt)], stmt))))))))))))))))))))
286 :    
287 :     | P.D_CONE{pt1, pt2, irad, orad} => let
288 :     val normVar = PSV.new("local_ht", PSV.T_VEC3F)
289 :     in
290 :     letPRIM("eh", IR.T_FLOAT, IR.RAND, [], fn ourRand =>
291 :     letPRIM("nv", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn normVec =>
292 :     letPRIM("n", IR.T_VEC, IR.NORM, [normVec], fn norm =>
293 :     genVecVar("ptInDisc", insert(env, normVar, norm), P.D_DISC{pt = pt1, normal = normVar, irad = irad, orad = orad}, fn ptInDisc =>
294 :     letPRIM("gptt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), ptInDisc], fn genPtToTip =>
295 :     letPRIM("gpttlen", IR.T_FLOAT, IR.LEN, [genPtToTip], fn genPtToTipLen =>
296 :     letPRIM("s", IR.T_FLOAT, IR.MULT, [genPtToTipLen, ourRand], fn scale =>
297 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [scale, genPtToTip], fn scaledNormVec =>
298 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [ptInDisc, scaledNormVec], stmt)))))))))
299 :     end
300 : pavelk 1074
301 :     | P.D_SPHERE{center, irad, orad} =>
302 :    
303 :     (* generate two random angles... *)
304 :     letPRIM("r1", IR.T_FLOAT, IR.RAND, [], fn randForAngOne =>
305 :     letPRIM("t1", IR.T_FLOAT, IR.MULT, [randForAngOne, IR.newConst("fullCit", IR.C_FLOAT (2.0 * pi))], fn randAngOne =>
306 :     letPRIM("r2", IR.T_FLOAT, IR.RAND, [], fn randForAngTwo =>
307 :     letPRIM("t2", IR.T_FLOAT, IR.MULT, [randForAngTwo, IR.newConst("fullCit", IR.C_FLOAT (2.0 * pi))], fn randAngTwo =>
308 :    
309 :     (* Generate vector in the sphere ... *)
310 :     (* If my math is correct this should be
311 :     * <(cos t1)(cos t2), (sin t1)(cos t2), sin t2>
312 :     * This is different from wikipedia's article on spherical coordinates
313 :     * because of a phase shift, but for the generation of random numbers,
314 :     * it's irrelevant.
315 :     *)
316 :     letPRIM("cost1", IR.T_FLOAT, IR.COS, [randAngOne], fn cost1 =>
317 :     letPRIM("cost2", IR.T_FLOAT, IR.COS, [randAngTwo], fn cost2 =>
318 :     letPRIM("sint1", IR.T_FLOAT, IR.SIN, [randAngOne], fn sint1 =>
319 :     letPRIM("sint2", IR.T_FLOAT, IR.SIN, [randAngTwo], fn sint2 =>
320 :    
321 :     letPRIM("xVal", IR.T_FLOAT, IR.MULT, [cost1, cost2], fn xVal =>
322 :     letPRIM("yVal", IR.T_FLOAT, IR.MULT, [sint1, cost2], fn yVal =>
323 :     (* zval is just sint2 *)
324 :    
325 :     letPRIM("xVec", IR.T_VEC, IR.SCALE, [xVal, IR.newConst("xDir", IR.C_VEC {x=1.0, y=0.0, z=0.0})], fn xVec =>
326 :     letPRIM("yVec", IR.T_VEC, IR.SCALE, [yVal, IR.newConst("yDir", IR.C_VEC {x=0.0, y=1.0, z=0.0})], fn yVec =>
327 :     letPRIM("zVec", IR.T_VEC, IR.SCALE, [sint2, IR.newConst("zDir", IR.C_VEC {x=0.0, y=0.0, z=1.0})], fn zVec =>
328 :    
329 :     letPRIM("addedVecs", IR.T_VEC, IR.ADD_VEC, [xVec, yVec], fn addedVecs =>
330 :     letPRIM("notNormVec", IR.T_VEC, IR.ADD_VEC, [addedVecs, zVec], fn nnVec =>
331 :     letPRIM("vec", IR.T_VEC, IR.NORM, [nnVec], fn vec =>
332 :    
333 :     (* Generate a random radius... *)
334 :     letPRIM("ratio", IR.T_FLOAT, IR.DIV, [psvToIRVar(env, irad), psvToIRVar(env, orad)], fn ratio =>
335 :     letPRIM("invRatio", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), ratio], fn invRatio =>
336 :     letPRIM("randVar", IR.T_FLOAT, IR.RAND, [], fn rand =>
337 :     letPRIM("randScale", IR.T_FLOAT, IR.MULT, [rand, invRatio], fn randScale =>
338 :     letPRIM("randVal", IR.T_FLOAT, IR.ADD, [randScale, ratio], fn randVal =>
339 :     letPRIM("randValSq", IR.T_FLOAT, IR.MULT, [randVal, randVal], fn randValSq =>
340 :     letPRIM("radDiff", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, orad), psvToIRVar(env, irad)], fn radDiff =>
341 :     letPRIM("randRadVal", IR.T_FLOAT, IR.MULT, [radDiff, randValSq], fn randRadVal =>
342 :     letPRIM("rad", IR.T_FLOAT, IR.ADD, [psvToIRVar(env, irad), randRadVal], fn rad =>
343 :    
344 :     (* Normalize the vector and scale it by the radius. *)
345 :     letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [rad, vec], fn sVec =>
346 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [sVec, psvToIRVar(env, center)], stmt)
347 :     ))))))))))
348 :     ))))))))))))
349 :     ))))
350 : pavelk 746
351 :     | _ => raise Fail "Cannot generate point in specified domain."
352 :     (* end case *))
353 :     (*
354 :     | generate (Dplane{pt, n}) = Vec3f.unpack pt
355 :     | generate (Drectangle{pt, u, v}) = Vec3f.unpack pt
356 :     | generate (Dsphere{c, orad, irad}) = Vec3f.unpack c
357 :     | generate (Dblob{c, stddev}) = Vec3f.unpack c
358 :     *)
359 :    
360 :    
361 :     (* This function takes an IR boolean, its environment, a particle state, domain,
362 :     * and continuation.
363 :     *
364 :     * We set the boolean to whether or not the current particle given by the particle
365 :     * state is within the domain, and then pass the continuation on.
366 :     *)
367 : pavelk 770 fun mkWithinVar (boolVar, env, var, d, stmt : IR.var -> IR.stmt) = let
368 :     val pos = var
369 : pavelk 746 in
370 :     case d
371 :     of P.D_POINT(pt) =>
372 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn subVec =>
373 :     letPRIM("vecLen", IR.T_FLOAT, IR.LEN, [subVec], fn vecLen =>
374 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), vecLen], stmt)))
375 :    
376 :     (* Take the vectors going from our position to pt1, and pt2. Then
377 :     * after we normalize them, if their dot product is equal to -1, then
378 :     * they are pointing in opposite directions meaning that the position
379 :     * is inbetween pt1 and pt2 as desired.
380 :     *)
381 :     | P.D_LINE{pt1, pt2} =>
382 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn posToPt1 =>
383 :     letPRIM("posToPt1Norm", IR.T_VEC, IR.NORM, [posToPt1], fn posToPt1Norm =>
384 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), pos], fn posToPt2 =>
385 :     letPRIM("posToPt2Norm", IR.T_VEC, IR.NORM, [posToPt2], fn posToPt2Norm =>
386 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt2, posToPt1], fn dotProd =>
387 :     letPRIM("testMe", IR.T_FLOAT, IR.SUB, [dotProd, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn testVal =>
388 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), testVal], stmt)))))))
389 :    
390 :     (* Just see whether or not the dot product between the normal
391 :     * and the vector from a point on the plane to our position is
392 :     * greater than zero. Essentially, we're "within" a plane if we're
393 :     * behind it (with respect to the normal)
394 :     *)
395 :     | P.D_PLANE{pt, normal} =>
396 : pavelk 905 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
397 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
398 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))
399 :    
400 :     (* Similar to checking to see whether or not we're within a plane,
401 :     * here all we have to do is see how far we are from the center
402 :     * of the disc (pt), and then see whther or not we're perpendicular to
403 :     * the normal, and that our distance is greater than irad but less than
404 :     * orad.
405 :     *)
406 :     | P.D_DISC{pt, normal, orad, irad} =>
407 : pavelk 907 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
408 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
409 :     letPRIM("inDisc", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.01), dotProd], fn inDisc =>
410 : pavelk 987
411 :     letPRIM("parPosToP", IR.T_VEC, IR.SCALE, [dotProd, psvToIRVar(env, normal)], fn posToPtParallelToNormal =>
412 :     letPRIM("perpPosToP", IR.T_VEC, IR.SUB_VEC, [posToPt, posToPtParallelToNormal], fn posToPtPerpToNormal =>
413 :     letPRIM("inDiscLen", IR.T_FLOAT, IR.LEN, [posToPtPerpToNormal], fn posToPtLen =>
414 :    
415 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToPtLen], fn inOradGt =>
416 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), posToPtLen], fn inOradEq =>
417 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
418 :    
419 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [posToPtLen, psvToIRVar(env, irad)], fn inIradGt =>
420 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [posToPtLen, psvToIRVar(env, irad)], fn inIradEq =>
421 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
422 :    
423 : pavelk 746 letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
424 : pavelk 987
425 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inDisc, inBothRad], stmt))))))))))))))
426 :    
427 : pavelk 746 (* Simply see whether or not the distance from the center is within the
428 :     * specified bounds.
429 :     *)
430 :     | P.D_SPHERE{center, orad, irad} =>
431 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, center), pos], fn posToC =>
432 :     letPRIM("posToPtLen", IR.T_VEC, IR.LEN, [posToC], fn posToCLen =>
433 :     letPRIM("inOrad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToCLen], fn inOrad =>
434 :     letPRIM("inIrad", IR.T_BOOL, IR.GT, [posToCLen, psvToIRVar(env, irad)], fn inIrad =>
435 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inIrad, inOrad], stmt)))))
436 : pavelk 1060
437 :     | P.D_CYLINDER {pt1, pt2, irad, orad} =>
438 :    
439 :     (* !FIXME! Right now, we see whether or not the point is within the two planes defined
440 :     * by the endpoints of the cylinder, and then testing to see whether or not the smallest
441 :     * distance to the line segment falls within the radii. It might be faster to find the
442 :     * closest point to the line defined by the endpoints and then see whether or not the point
443 :     * is within the segment.
444 :     *)
445 :    
446 :     (* Is it in one plane *)
447 :     letPRIM("plane1Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn plane1Norm =>
448 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt1)], fn posToPt1 =>
449 :     letPRIM("dot1", IR.T_FLOAT, IR.DOT, [posToPt1, plane1Norm], fn dot1Prod =>
450 :     letPRIM("inPlane1", IR.T_BOOL, IR.GT, [dot1Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane1=>
451 :    
452 :     (* Is it in another plane *)
453 :     letPRIM("plane2Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), psvToIRVar(env, pt2)], fn plane2Norm =>
454 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt2)], fn posToPt2 =>
455 :     letPRIM("dot2", IR.T_FLOAT, IR.DOT, [posToPt2, plane2Norm], fn dot2Prod =>
456 :     letPRIM("inPlane2", IR.T_BOOL, IR.GT, [dot2Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane2=>
457 :    
458 :     (* Is it in both planes? *)
459 :     letPRIM("inPlanes", IR.T_BOOL, IR.AND, [inPlane1, inPlane2], fn inPlanes =>
460 :    
461 :     (* Find distance from segment *)
462 :     letPRIM("a", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn a =>
463 :     letPRIM("b", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn b =>
464 :     letPRIM("alen", IR.T_FLOAT, IR.LEN, [a], fn alen =>
465 :     letPRIM("axb", IR.T_VEC, IR.CROSS, [a, b], fn axb =>
466 :     letPRIM("axblen", IR.T_FLOAT, IR.LEN, [axb], fn axblen =>
467 :     letPRIM("dist", IR.T_FLOAT, IR.DIV, [axblen, alen], fn dist =>
468 :    
469 :     (* Is distance in both radii? *)
470 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), dist], fn inOradGt =>
471 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), dist], fn inOradEq =>
472 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
473 :    
474 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [dist, psvToIRVar(env, irad)], fn inIradGt =>
475 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [dist, psvToIRVar(env, irad)], fn inIradEq =>
476 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
477 :    
478 :     letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
479 :    
480 :     (* It's in the cylinder (tube) if it's within both radii and in both planes... *)
481 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inPlanes, inBothRad], stmt)
482 :     ))))))))))))))))))))))
483 : pavelk 746 (*
484 :     | P.D_TRIANGLE {pt1: vec3f var, pt2: vec3f var, pt3: vec3f var}
485 :     | P.D_PLANE {pt: vec3f var, normal: vec3f var}
486 :     | P.D_RECT {pt: vec3f var, htvec: vec3f var, wdvec: vec3f var}
487 :     | P.D_BOX {min: vec3f var, max: vec3f var}
488 :     | P.D_SPHERE {center: vec3f var, irad: vec3f var, orad: vec3f var}
489 :     | P.D_CONE {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
490 :     | P.D_BLOB {center: vec3f var, stddev: float var}
491 :     | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
492 :     *)
493 :     | _ => raise Fail "Cannot determine within-ness for specified domain."
494 :     (* end case *)
495 :     end (*end let *)
496 :    
497 :    
498 :     (* generate code to produce a random particle state from a domain *)
499 : pavelk 1074 fun newParticle (posDomain, (szDom : Float.float P.domain, szDist), velDomain, colDomain, env, k : particle_state -> IR.stmt) =
500 : pavelk 746 (* genVecVar (vecVar, env, domain, stmt) *)
501 :     genVecVar("ps_pos", env, posDomain, fn newPos =>
502 :     genVecVar("ps_vel", env, velDomain, fn newVel =>
503 :     genVecVar("ps_col", env, colDomain, fn newCol =>
504 : pavelk 1074 genFloatVar("ps_size", env, szDom, szDist, fn newSize =>
505 : pavelk 915 letSPRIM ("ps_ttl", IR.T_FLOAT, IR.COPY, [IR.newConst("fbool", IR.C_FLOAT 10000.0)], fn newIsDead =>
506 : pavelk 873 k(PS{pos = newPos,
507 :     vel = newVel,
508 :     size = newSize,
509 : pavelk 915 ttl = newIsDead,
510 : pavelk 900 color = newCol,
511 : pavelk 1027 rot = IR.newConst("ps_rot", IR.C_FLOAT 0.0),
512 : pavelk 905 pos2 = IR.newConst("ps_pos2", IR.C_VEC {x=0.0, y=0.0, z=0.0}),
513 :     dummy = IR.newConst("ps_dummy", IR.C_FLOAT 0.01)})
514 : pavelk 1074 )))))
515 : pavelk 746
516 :     (* Find the normal at the given position of the particle for the specified
517 :     * domain. Note, that the particle doesn't necessarily need to be on the
518 :     * domain, but if it's not then the behavior is undefined.
519 :     *)
520 :     fun normAtPoint(retNorm, d, env, state, k : IR.var -> particle_state -> IR.stmt) = let
521 :     val newNorm = IR.newParam("n", IR.T_VEC)
522 :     val nextBlk = newBlockWithArgs(env, [newNorm], k(newNorm))
523 : pavelk 770 val PS{pos, ...} = state
524 : pavelk 746 in
525 :     (case d
526 :     of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
527 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
528 :     | P.D_DISC{pt, normal, irad, orad} =>
529 : pavelk 770 mkWithinVar("inP", env, pos, d, fn inPlane =>
530 : pavelk 746 IR.mkIF(inPlane,
531 :     (* then *)
532 :     letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
533 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk)),
534 :     (* else *)
535 :     letPRIM(retNorm,
536 :     IR.T_VEC,
537 :     IR.SCALE,
538 :     [IR.newConst("negOne", IR.C_FLOAT ~1.0), psvToIRVar(env, normal)],
539 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
540 :     )
541 :     )
542 :    
543 :     | P.D_SPHERE{center, irad, orad} => let
544 : pavelk 870 val PS{pos, ...} = state
545 : pavelk 746 in
546 :     letPRIM("sv", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, center)], fn subVec =>
547 :     letPRIM(retNorm, IR.T_VEC, IR.NORM, [subVec], fn newNormVar => k newNormVar state
548 :     ))
549 :     end
550 :    
551 :     | _ => raise Fail("Cannot find normal to point of specified domain.")
552 :     (* end case *))
553 :     end
554 : pavelk 769
555 : pavelk 770 fun trEmitter(emit, env, state, k : particle_state -> IR.stmt) = let
556 : pavelk 866
557 : pavelk 915 val PS{ttl, ...} = state
558 : pavelk 1074 val P.EMIT{range, szDomain, posDomain, velDomain, colDomain, ...} = emit
559 :     val (rDom, rDist) = range
560 : pavelk 770 val blk = newBlock (env, k)
561 :     in
562 : pavelk 918 letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>
563 : pavelk 770 IR.mkIF(isDead,
564 :     (* then *)
565 : pavelk 1074 genFloatVar("t1", env, rDom, rDist, fn t1 =>
566 : pavelk 903 letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
567 : pavelk 770 letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
568 :     letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
569 :     letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
570 :     IR.mkIF(t3,
571 :     (* then *)
572 : pavelk 1074 newParticle (posDomain, szDomain, velDomain, colDomain, env,
573 : pavelk 866 fn state' => retState state'),
574 : pavelk 770 (* else *)
575 :     IR.DISCARD)))))),
576 :     (* else *)
577 : pavelk 915 retState state))
578 : pavelk 770 end
579 :    
580 : pavelk 769 fun trPred(pred, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
581 : pavelk 870 val PS{pos, vel, ...} = state
582 : pavelk 769 val P.PR{ifstmt, ...} = pred
583 :     in
584 :     case ifstmt
585 : pavelk 770 of P.WITHIN(d) => mkWithinVar("wv", env, pos, d, fn withinVar =>
586 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
587 : pavelk 770 | P.WITHINVEL(d) => mkWithinVar("wv", env, vel, d, fn withinVar =>
588 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
589 : pavelk 769 end
590 :    
591 : pavelk 746 fun trAct (action, env, state, k : particle_state -> IR.stmt) = let
592 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = state
593 : pavelk 746 in
594 :     case action
595 :     of P.BOUNCE{friction, resilience, cutoff, d} => let
596 :     val blk = newBlock (env, k)
597 :     val negOne = IR.newConst("negOne", IR.C_FLOAT ~1.0)
598 :     in
599 : pavelk 903 letPRIM("vs", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn velScale =>
600 : pavelk 746 letPRIM("np", IR.T_VEC, IR.ADD_VEC, [pos, velScale], fn nextPos =>
601 : pavelk 987 mkWithinVar("wcp", env, pos, d, fn withinCurPos =>
602 :     mkWithinVar("wnp", env, nextPos, d, fn withinNextPos =>
603 :     letPRIM("nwcp", IR.T_BOOL, IR.NOT, [withinCurPos], fn notWithinCurPos =>
604 :     letPRIM("sb", IR.T_BOOL, IR.AND, [notWithinCurPos, withinNextPos], fn shouldBounce =>
605 :     IR.mkIF(shouldBounce,
606 : pavelk 746 (*then*)
607 :     normAtPoint("n", d, env, state, fn normAtD => fn state' => let
608 : pavelk 1027 val PS{pos=nextPos, vel=nextVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy} = state'
609 : pavelk 746 in
610 :     letPRIM("negVel", IR.T_VEC, IR.SCALE, [negOne, nextVel], fn negVel =>
611 :     letPRIM("dnv", IR.T_FLOAT, IR.DOT, [negVel, normAtD], fn dotNegVel =>
612 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [dotNegVel, normAtD], fn scaledN =>
613 :     letPRIM("t", IR.T_VEC, IR.SUB_VEC, [negVel, scaledN], fn tang =>
614 :    
615 :     letPRIM("tlsq", IR.T_FLOAT, IR.LEN_SQ, [tang], fn tangLenSq =>
616 :     letPRIM("cosq", IR.T_FLOAT, IR.MULT, [psvToIRVar(env, cutoff), psvToIRVar(env, cutoff)], fn cutoffSq =>
617 :     letPRIM("inco", IR.T_BOOL, IR.GT, [tangLenSq, cutoffSq], fn inCutoff =>
618 :    
619 :     letPRIM("resNorm", IR.T_VEC, IR.SCALE, [psvToIRVar(env, resilience), scaledN], fn resNorm =>
620 :    
621 :     IR.mkIF(inCutoff,
622 :     (*then*)
623 :     letPRIM("fInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), psvToIRVar(env, friction)], fn frictInv =>
624 :     letPRIM("f", IR.T_FLOAT, IR.MULT, [negOne, frictInv], fn modFrict =>
625 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [modFrict, tang], fn frictTang =>
626 :     letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
627 : pavelk 1027 goto(PS{pos=nextPos, vel=newVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy}, blk)
628 : pavelk 746 )))),
629 :     (*else*)
630 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [negOne, tang], fn frictTang =>
631 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
632 : pavelk 1027 goto(PS{pos=nextPos, vel=newVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy}, blk)
633 : pavelk 746 ))
634 :     )))))))))
635 :     end
636 :     ),
637 :     (*else*)
638 : pavelk 987 goto(state, blk))))))))
639 : pavelk 746 end
640 :    
641 : jhr 974 | P.ACCEL dir =>
642 : pavelk 903 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), psvToIRVar(env, dir)], fn theScale =>
643 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [theScale, vel], fn newVel =>
644 : pavelk 1027 k(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy})))
645 : pavelk 746
646 :     | P.MOVE =>
647 : pavelk 903 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn theScale =>
648 : pavelk 902 letPRIM("ps_pos", IR.T_VEC, IR.ADD_VEC, [theScale, pos], fn newPos =>
649 : pavelk 1027 k(PS{pos = newPos, vel = vel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy})))
650 : pavelk 770 (*
651 : pavelk 758 | P.SINK({d, kill_inside}) =>
652 : pavelk 746 mkWithinVar("isWithin", env, state, d, fn withinVal =>
653 :     mkXOR ("shouldNotKill", withinVal, psvToIRVar(env, kill_inside),
654 :     fn shouldNotKill =>
655 : pavelk 758 letPRIM("shouldKill", IR.T_BOOL, IR.NOT, [shouldNotKill], fn shouldKill =>
656 : pavelk 915 letPRIM("isReallyDead", IR.T_BOOL, IR.OR, [shouldKill, ttl], fn isReallyDead =>
657 :     k(PS{pos = pos, vel = vel, size = size, ttl = isReallyDead, color = color})
658 : pavelk 758 ))))
659 : pavelk 770 *)
660 : pavelk 1074 | P.ORBITPOINT {center, mag, maxRad} => let
661 :     val blk = newBlock (env, k)
662 :     in
663 :     letPRIM("toCenter", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, center), pos], fn toCenter =>
664 :     letPRIM("dist", IR.T_FLOAT, IR.LEN, [toCenter], fn dist =>
665 :     letPRIM("radInDist", IR.T_BOOL, IR.GT, [dist, psvToIRVar(env, maxRad)], fn radInDist =>
666 :     IR.mkIF(radInDist,
667 :     (* then *)
668 :     goto(state, blk),
669 :     (* else *)
670 :     letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [dist, psvToIRVar(env, maxRad)], fn magRatio =>
671 :     letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>
672 :     letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>
673 :     letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, PSV.timeStep)], fn totMag =>
674 :     letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, toCenter], fn acc =>
675 :     letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>
676 :     goto(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy}, blk)
677 :     ))))))))))
678 :     end
679 :    
680 : pavelk 746
681 :     | P.ORBITLINESEG {endp1, endp2, maxRad, mag} => let
682 :     val blk = newBlock (env, k)
683 :     in
684 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, endp2), psvToIRVar(env, endp1)], fn subVec =>
685 :     letPRIM("vecToEndP", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, endp1)], fn vecToEndP =>
686 :     letPRIM("basis", IR.T_VEC, IR.NORM, [subVec], fn basis =>
687 :     letPRIM("parDot", IR.T_FLOAT, IR.DOT, [basis, vecToEndP], fn parDot =>
688 :     letPRIM("parVec", IR.T_VEC, IR.SCALE, [parDot, basis], fn parVec =>
689 :     letPRIM("closestP", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, endp1), parVec], fn closestP =>
690 :     letPRIM("vecToP", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn vecToP =>
691 :     letPRIM("distToP", IR.T_FLOAT, IR.LEN, [vecToP], fn distToP =>
692 :     letPRIM("effRad", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, maxRad), distToP], fn effRad =>
693 :     letPRIM("radInDist", IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), effRad], fn radInDist =>
694 :     IR.mkIF(radInDist,
695 :     (*then*)
696 :     goto(state, blk),
697 :     (*else*)
698 :     letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [distToP, psvToIRVar(env, maxRad)], fn magRatio =>
699 :     letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>
700 :     letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>
701 : pavelk 903 letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, PSV.timeStep)], fn totMag =>
702 : pavelk 746 letPRIM("accVec", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn accVec =>
703 :     letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, accVec], fn acc =>
704 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>
705 : pavelk 1027 goto(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy}, blk)
706 : pavelk 746 )))))))
707 :     )))))))))))
708 :     end
709 : pavelk 770
710 :     (* just kill it. *)
711 : pavelk 915 (* | P.DIE => k(PS{pos = pos, vel = vel, size = size, ttl = IR.newConst("falseVar", IR.C_BOOL true), color = color, dummy=dummy}) *)
712 : pavelk 870 | P.DIE => IR.DISCARD
713 : pavelk 746 | _ => raise Fail("Action not implemented...")
714 :     (* end case *)
715 :     end
716 :    
717 : pavelk 868 fun compile (P.PG{
718 : pavelk 1017 emit as P.EMIT{vars=emitVars, ...},
719 : pavelk 868 act as P.PSAE{action=root_act, vars=actionVars},
720 :     render
721 :     }) = let
722 : pavelk 746 val blks = ref[]
723 :     val env = let
724 :     (* add special globals to free vars *)
725 : pavelk 1017 val vars = PSV.Set.union(emitVars, PSV.Set.addList(actionVars, [PSV.numDead, PSV.timeStep, epsilon]))
726 : pavelk 770 fun ins (x as PSV.V{name, ty, binding, id, ...}, map) = let
727 : pavelk 746 val x' = (case (ty, !binding)
728 :     of (PSV.T_BOOL, PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
729 :     | (PSV.T_BOOL, PSV.BOOL boolVal) => IR.newConst(name, IR.C_BOOL(boolVal))
730 :     | (PSV.T_INT, PSV.UNDEF) => IR.newGlobal(x, IR.T_INT)
731 :     | (PSV.T_INT, PSV.INT intVal) => IR.newConst(name, IR.C_INT(intVal))
732 :     | (PSV.T_FLOAT, PSV.UNDEF) => IR.newGlobal(x, IR.T_FLOAT)
733 :     | (PSV.T_FLOAT, PSV.FLOAT floatVal) => IR.newConst(name, IR.C_FLOAT(floatVal))
734 :     | (PSV.T_VEC3F, PSV.UNDEF) => IR.newGlobal(x, IR.T_VEC)
735 :     | (PSV.T_VEC3F, PSV.VEC3F vecVal) => IR.newConst(name, IR.C_VEC(vecVal))
736 : pavelk 972 | _ => raise Fail("Error in setup, type mismatch between PSV vars and their binding.")
737 : pavelk 746 (* end case *))
738 :     in
739 :     PSV.Map.insert (map, x, x')
740 :     end
741 :     in
742 :     TE(blks, PSV.Set.foldl ins PSV.Map.empty vars)
743 :     end
744 : pavelk 867
745 : pavelk 905 fun evalActs f [] state = f [] state
746 : pavelk 867 | evalActs f (psa :: psal) state = (case psa
747 :     of P.SEQ(acts) => (case acts
748 :     of [] => raise Fail "Should never reach here."
749 :     | [act] => trAct(act, env, state, evalActs f psal)
750 :     | act :: rest => trAct(act, env, state, evalActs f (P.SEQ(rest) :: psal))
751 :     (* end case *))
752 :     | P.PRED(pred as P.PR{thenstmt=t, elsestmt=e, ...}) => let
753 :     val cblk = newBlock(env, evalActs f psal)
754 :     fun trPredActs [] state' = goto(state', cblk)
755 :     | trPredActs _ _ = raise Fail "Should never reach here."
756 :     in
757 :     trPred(pred, env, state, evalActs trPredActs t, evalActs trPredActs e)
758 :     end
759 :     (* end case *))
760 :    
761 : pavelk 868 (* At the highest level, we want to return when we reach the end of the action list *)
762 : pavelk 746 fun trActs [] state = let
763 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = state
764 : pavelk 746 in
765 : pavelk 905 IR.mkRETURN (
766 : pavelk 1027 [ pos, vel, size, ttl, color, rot, pos2, dummy ],
767 :     [IR.POS, IR.VEL, IR.SZ, IR.TTL, IR.COLOR, IR.ROT, IR.POS2, IR.DUMMY]
768 : pavelk 905 )
769 : pavelk 746 end (* trActs *)
770 : pavelk 867 | trActs _ _ = raise Fail "Should never reach here"
771 : pavelk 868
772 :     (* The entry block is the first block of the program, or in other words, the emitter. *)
773 :     val entryBlock = newBlock (
774 :     env,
775 :     fn pstate => trEmitter(
776 :     emit,
777 :     env,
778 :     pstate,
779 :     fn state => evalActs trActs root_act state
780 :     )
781 :     )
782 :    
783 : pavelk 972 (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
784 :    
785 :     fun isGlobal(IR.V{scope, ...}) = (case scope
786 :     of IR.S_GLOBAL(v) => true
787 :     | _ => false
788 :     (* end case *))
789 :    
790 :     fun extractVarMap(TE(blks, map)) = map
791 :    
792 : pavelk 866 val outPgm = PSysIR.PGM {
793 : pavelk 972 globals = PSV.Map.filter isGlobal (extractVarMap env),
794 :     emitter = entryBlock,
795 : pavelk 906 physics = List.nth(!blks, 1),
796 : pavelk 866 render = render
797 :     }
798 : pavelk 868
799 : pavelk 906 val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
800 : pavelk 868
801 : pavelk 746 in
802 : pavelk 905 (* IR.outputPgm(TextIO.stdErr, outPgm); *)
803 : pavelk 868 if Checker.checkIR(optimized) then
804 :     printErr "Compilation succeeded." (* Note: it only succeeds if we can optimize, too *)
805 : pavelk 746 else
806 : pavelk 866 ();
807 : pavelk 868 optimized
808 : pavelk 746 end (* compile *)
809 :    
810 :     end (* Translate *)

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