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 1050 - (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 :    
301 :     | _ => raise Fail "Cannot generate point in specified domain."
302 :     (* end case *))
303 :     (*
304 :     | generate (Dplane{pt, n}) = Vec3f.unpack pt
305 :     | generate (Drectangle{pt, u, v}) = Vec3f.unpack pt
306 :     | generate (Dsphere{c, orad, irad}) = Vec3f.unpack c
307 :     | generate (Dblob{c, stddev}) = Vec3f.unpack c
308 :     *)
309 :    
310 :    
311 :     (* This function takes an IR boolean, its environment, a particle state, domain,
312 :     * and continuation.
313 :     *
314 :     * We set the boolean to whether or not the current particle given by the particle
315 :     * state is within the domain, and then pass the continuation on.
316 :     *)
317 : pavelk 770 fun mkWithinVar (boolVar, env, var, d, stmt : IR.var -> IR.stmt) = let
318 :     val pos = var
319 : pavelk 746 in
320 :     case d
321 :     of P.D_POINT(pt) =>
322 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn subVec =>
323 :     letPRIM("vecLen", IR.T_FLOAT, IR.LEN, [subVec], fn vecLen =>
324 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), vecLen], stmt)))
325 :    
326 :     (* Take the vectors going from our position to pt1, and pt2. Then
327 :     * after we normalize them, if their dot product is equal to -1, then
328 :     * they are pointing in opposite directions meaning that the position
329 :     * is inbetween pt1 and pt2 as desired.
330 :     *)
331 :     | P.D_LINE{pt1, pt2} =>
332 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn posToPt1 =>
333 :     letPRIM("posToPt1Norm", IR.T_VEC, IR.NORM, [posToPt1], fn posToPt1Norm =>
334 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), pos], fn posToPt2 =>
335 :     letPRIM("posToPt2Norm", IR.T_VEC, IR.NORM, [posToPt2], fn posToPt2Norm =>
336 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt2, posToPt1], fn dotProd =>
337 :     letPRIM("testMe", IR.T_FLOAT, IR.SUB, [dotProd, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn testVal =>
338 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), testVal], stmt)))))))
339 :    
340 :     (* Just see whether or not the dot product between the normal
341 :     * and the vector from a point on the plane to our position is
342 :     * greater than zero. Essentially, we're "within" a plane if we're
343 :     * behind it (with respect to the normal)
344 :     *)
345 :     | P.D_PLANE{pt, normal} =>
346 : pavelk 905 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
347 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
348 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))
349 :    
350 :     (* Similar to checking to see whether or not we're within a plane,
351 :     * here all we have to do is see how far we are from the center
352 :     * of the disc (pt), and then see whther or not we're perpendicular to
353 :     * the normal, and that our distance is greater than irad but less than
354 :     * orad.
355 :     *)
356 :     | P.D_DISC{pt, normal, orad, irad} =>
357 : pavelk 907 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
358 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
359 :     letPRIM("inDisc", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.01), dotProd], fn inDisc =>
360 : pavelk 987
361 :     letPRIM("parPosToP", IR.T_VEC, IR.SCALE, [dotProd, psvToIRVar(env, normal)], fn posToPtParallelToNormal =>
362 :     letPRIM("perpPosToP", IR.T_VEC, IR.SUB_VEC, [posToPt, posToPtParallelToNormal], fn posToPtPerpToNormal =>
363 :     letPRIM("inDiscLen", IR.T_FLOAT, IR.LEN, [posToPtPerpToNormal], fn posToPtLen =>
364 :    
365 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToPtLen], fn inOradGt =>
366 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), posToPtLen], fn inOradEq =>
367 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
368 :    
369 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [posToPtLen, psvToIRVar(env, irad)], fn inIradGt =>
370 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [posToPtLen, psvToIRVar(env, irad)], fn inIradEq =>
371 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
372 :    
373 : pavelk 746 letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
374 : pavelk 987
375 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inDisc, inBothRad], stmt))))))))))))))
376 :    
377 : pavelk 746 (* Simply see whether or not the distance from the center is within the
378 :     * specified bounds.
379 :     *)
380 :     | P.D_SPHERE{center, orad, irad} =>
381 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, center), pos], fn posToC =>
382 :     letPRIM("posToPtLen", IR.T_VEC, IR.LEN, [posToC], fn posToCLen =>
383 :     letPRIM("inOrad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToCLen], fn inOrad =>
384 :     letPRIM("inIrad", IR.T_BOOL, IR.GT, [posToCLen, psvToIRVar(env, irad)], fn inIrad =>
385 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inIrad, inOrad], stmt)))))
386 :     (*
387 :     | P.D_TRIANGLE {pt1: vec3f var, pt2: vec3f var, pt3: vec3f var}
388 :     | P.D_PLANE {pt: vec3f var, normal: vec3f var}
389 :     | P.D_RECT {pt: vec3f var, htvec: vec3f var, wdvec: vec3f var}
390 :     | P.D_BOX {min: vec3f var, max: vec3f var}
391 :     | P.D_SPHERE {center: vec3f var, irad: vec3f var, orad: vec3f var}
392 :     | P.D_CYLINDER {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
393 :     | P.D_CONE {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
394 :     | P.D_BLOB {center: vec3f var, stddev: float var}
395 :     | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
396 :     *)
397 :     | _ => raise Fail "Cannot determine within-ness for specified domain."
398 :     (* end case *)
399 :     end (*end let *)
400 :    
401 :    
402 :     (* generate code to produce a random particle state from a domain *)
403 :     fun newParticle (posDomain, velDomain, colDomain, env, k : particle_state -> IR.stmt) =
404 :     (* genVecVar (vecVar, env, domain, stmt) *)
405 :     genVecVar("ps_pos", env, posDomain, fn newPos =>
406 :     genVecVar("ps_vel", env, velDomain, fn newVel =>
407 :     genVecVar("ps_col", env, colDomain, fn newCol =>
408 : pavelk 1008 letPRIM ("randSize", IR.T_FLOAT, IR.RAND, [], fn randSize =>
409 :     letPRIM ("halvedSize", IR.T_FLOAT, IR.MULT, [randSize, IR.newConst("halver", IR.C_FLOAT 0.5)], fn halfSize =>
410 :     letSPRIM("ps_size", IR.T_FLOAT, IR.ADD, [halfSize, IR.newConst("scalar", IR.C_FLOAT 0.5)], fn newSize =>
411 : pavelk 915 letSPRIM ("ps_ttl", IR.T_FLOAT, IR.COPY, [IR.newConst("fbool", IR.C_FLOAT 10000.0)], fn newIsDead =>
412 : pavelk 873 k(PS{pos = newPos,
413 :     vel = newVel,
414 :     size = newSize,
415 : pavelk 915 ttl = newIsDead,
416 : pavelk 900 color = newCol,
417 : pavelk 1027 rot = IR.newConst("ps_rot", IR.C_FLOAT 0.0),
418 : pavelk 905 pos2 = IR.newConst("ps_pos2", IR.C_VEC {x=0.0, y=0.0, z=0.0}),
419 :     dummy = IR.newConst("ps_dummy", IR.C_FLOAT 0.01)})
420 : pavelk 1008 )))))))
421 : pavelk 746
422 :     (* Find the normal at the given position of the particle for the specified
423 :     * domain. Note, that the particle doesn't necessarily need to be on the
424 :     * domain, but if it's not then the behavior is undefined.
425 :     *)
426 :     fun normAtPoint(retNorm, d, env, state, k : IR.var -> particle_state -> IR.stmt) = let
427 :     val newNorm = IR.newParam("n", IR.T_VEC)
428 :     val nextBlk = newBlockWithArgs(env, [newNorm], k(newNorm))
429 : pavelk 770 val PS{pos, ...} = state
430 : pavelk 746 in
431 :     (case d
432 :     of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
433 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
434 :     | P.D_DISC{pt, normal, irad, orad} =>
435 : pavelk 770 mkWithinVar("inP", env, pos, d, fn inPlane =>
436 : pavelk 746 IR.mkIF(inPlane,
437 :     (* then *)
438 :     letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
439 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk)),
440 :     (* else *)
441 :     letPRIM(retNorm,
442 :     IR.T_VEC,
443 :     IR.SCALE,
444 :     [IR.newConst("negOne", IR.C_FLOAT ~1.0), psvToIRVar(env, normal)],
445 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
446 :     )
447 :     )
448 :    
449 :     | P.D_SPHERE{center, irad, orad} => let
450 : pavelk 870 val PS{pos, ...} = state
451 : pavelk 746 in
452 :     letPRIM("sv", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, center)], fn subVec =>
453 :     letPRIM(retNorm, IR.T_VEC, IR.NORM, [subVec], fn newNormVar => k newNormVar state
454 :     ))
455 :     end
456 :    
457 :     | _ => raise Fail("Cannot find normal to point of specified domain.")
458 :     (* end case *))
459 :     end
460 : pavelk 769
461 : pavelk 770 fun trEmitter(emit, env, state, k : particle_state -> IR.stmt) = let
462 : pavelk 866
463 : pavelk 915 val PS{ttl, ...} = state
464 : pavelk 1017 val P.EMIT{range, dist, posDomain, velDomain, colDomain, ...} = emit
465 : pavelk 770 val blk = newBlock (env, k)
466 :     in
467 : pavelk 918 letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>
468 : pavelk 770 IR.mkIF(isDead,
469 :     (* then *)
470 : pavelk 1017 genFloatVar("t1", env, range, dist, fn t1 =>
471 : pavelk 903 letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
472 : pavelk 770 letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
473 :     letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
474 :     letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
475 :     IR.mkIF(t3,
476 :     (* then *)
477 :     newParticle (posDomain, velDomain, colDomain, env,
478 : pavelk 866 fn state' => retState state'),
479 : pavelk 770 (* else *)
480 :     IR.DISCARD)))))),
481 :     (* else *)
482 : pavelk 915 retState state))
483 : pavelk 770 end
484 :    
485 : pavelk 769 fun trPred(pred, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
486 : pavelk 870 val PS{pos, vel, ...} = state
487 : pavelk 769 val P.PR{ifstmt, ...} = pred
488 :     in
489 :     case ifstmt
490 : pavelk 770 of P.WITHIN(d) => mkWithinVar("wv", env, pos, d, fn withinVar =>
491 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
492 : pavelk 770 | P.WITHINVEL(d) => mkWithinVar("wv", env, vel, d, fn withinVar =>
493 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
494 : pavelk 769 end
495 :    
496 : pavelk 746 fun trAct (action, env, state, k : particle_state -> IR.stmt) = let
497 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = state
498 : pavelk 746 in
499 :     case action
500 :     of P.BOUNCE{friction, resilience, cutoff, d} => let
501 :     val blk = newBlock (env, k)
502 :     val negOne = IR.newConst("negOne", IR.C_FLOAT ~1.0)
503 :     in
504 : pavelk 903 letPRIM("vs", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn velScale =>
505 : pavelk 746 letPRIM("np", IR.T_VEC, IR.ADD_VEC, [pos, velScale], fn nextPos =>
506 : pavelk 987 mkWithinVar("wcp", env, pos, d, fn withinCurPos =>
507 :     mkWithinVar("wnp", env, nextPos, d, fn withinNextPos =>
508 :     letPRIM("nwcp", IR.T_BOOL, IR.NOT, [withinCurPos], fn notWithinCurPos =>
509 :     letPRIM("sb", IR.T_BOOL, IR.AND, [notWithinCurPos, withinNextPos], fn shouldBounce =>
510 :     IR.mkIF(shouldBounce,
511 : pavelk 746 (*then*)
512 :     normAtPoint("n", d, env, state, fn normAtD => fn state' => let
513 : pavelk 1027 val PS{pos=nextPos, vel=nextVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy} = state'
514 : pavelk 746 in
515 :     letPRIM("negVel", IR.T_VEC, IR.SCALE, [negOne, nextVel], fn negVel =>
516 :     letPRIM("dnv", IR.T_FLOAT, IR.DOT, [negVel, normAtD], fn dotNegVel =>
517 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [dotNegVel, normAtD], fn scaledN =>
518 :     letPRIM("t", IR.T_VEC, IR.SUB_VEC, [negVel, scaledN], fn tang =>
519 :    
520 :     letPRIM("tlsq", IR.T_FLOAT, IR.LEN_SQ, [tang], fn tangLenSq =>
521 :     letPRIM("cosq", IR.T_FLOAT, IR.MULT, [psvToIRVar(env, cutoff), psvToIRVar(env, cutoff)], fn cutoffSq =>
522 :     letPRIM("inco", IR.T_BOOL, IR.GT, [tangLenSq, cutoffSq], fn inCutoff =>
523 :    
524 :     letPRIM("resNorm", IR.T_VEC, IR.SCALE, [psvToIRVar(env, resilience), scaledN], fn resNorm =>
525 :    
526 :     IR.mkIF(inCutoff,
527 :     (*then*)
528 :     letPRIM("fInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), psvToIRVar(env, friction)], fn frictInv =>
529 :     letPRIM("f", IR.T_FLOAT, IR.MULT, [negOne, frictInv], fn modFrict =>
530 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [modFrict, tang], fn frictTang =>
531 :     letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
532 : pavelk 1027 goto(PS{pos=nextPos, vel=newVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy}, blk)
533 : pavelk 746 )))),
534 :     (*else*)
535 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [negOne, tang], fn frictTang =>
536 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
537 : pavelk 1027 goto(PS{pos=nextPos, vel=newVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy}, blk)
538 : pavelk 746 ))
539 :     )))))))))
540 :     end
541 :     ),
542 :     (*else*)
543 : pavelk 987 goto(state, blk))))))))
544 : pavelk 746 end
545 :    
546 : jhr 974 | P.ACCEL dir =>
547 : pavelk 903 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), psvToIRVar(env, dir)], fn theScale =>
548 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [theScale, vel], fn newVel =>
549 : pavelk 1027 k(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy})))
550 : pavelk 746
551 :     | P.MOVE =>
552 : pavelk 903 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn theScale =>
553 : pavelk 902 letPRIM("ps_pos", IR.T_VEC, IR.ADD_VEC, [theScale, pos], fn newPos =>
554 : pavelk 1027 k(PS{pos = newPos, vel = vel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy})))
555 : pavelk 770 (*
556 : pavelk 758 | P.SINK({d, kill_inside}) =>
557 : pavelk 746 mkWithinVar("isWithin", env, state, d, fn withinVal =>
558 :     mkXOR ("shouldNotKill", withinVal, psvToIRVar(env, kill_inside),
559 :     fn shouldNotKill =>
560 : pavelk 758 letPRIM("shouldKill", IR.T_BOOL, IR.NOT, [shouldNotKill], fn shouldKill =>
561 : pavelk 915 letPRIM("isReallyDead", IR.T_BOOL, IR.OR, [shouldKill, ttl], fn isReallyDead =>
562 :     k(PS{pos = pos, vel = vel, size = size, ttl = isReallyDead, color = color})
563 : pavelk 758 ))))
564 : pavelk 770 *)
565 : pavelk 746
566 :     | P.ORBITLINESEG {endp1, endp2, maxRad, mag} => let
567 :     val blk = newBlock (env, k)
568 :     in
569 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, endp2), psvToIRVar(env, endp1)], fn subVec =>
570 :     letPRIM("vecToEndP", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, endp1)], fn vecToEndP =>
571 :     letPRIM("basis", IR.T_VEC, IR.NORM, [subVec], fn basis =>
572 :     letPRIM("parDot", IR.T_FLOAT, IR.DOT, [basis, vecToEndP], fn parDot =>
573 :     letPRIM("parVec", IR.T_VEC, IR.SCALE, [parDot, basis], fn parVec =>
574 :     letPRIM("closestP", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, endp1), parVec], fn closestP =>
575 :     letPRIM("vecToP", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn vecToP =>
576 :     letPRIM("distToP", IR.T_FLOAT, IR.LEN, [vecToP], fn distToP =>
577 :     letPRIM("effRad", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, maxRad), distToP], fn effRad =>
578 :     letPRIM("radInDist", IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), effRad], fn radInDist =>
579 :     IR.mkIF(radInDist,
580 :     (*then*)
581 :     goto(state, blk),
582 :     (*else*)
583 :     letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [distToP, psvToIRVar(env, maxRad)], fn magRatio =>
584 :     letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>
585 :     letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>
586 : pavelk 903 letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, PSV.timeStep)], fn totMag =>
587 : pavelk 746 letPRIM("accVec", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn accVec =>
588 :     letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, accVec], fn acc =>
589 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>
590 : pavelk 1027 goto(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy}, blk)
591 : pavelk 746 )))))))
592 :     )))))))))))
593 :     end
594 : pavelk 770
595 :     (* just kill it. *)
596 : pavelk 915 (* | P.DIE => k(PS{pos = pos, vel = vel, size = size, ttl = IR.newConst("falseVar", IR.C_BOOL true), color = color, dummy=dummy}) *)
597 : pavelk 870 | P.DIE => IR.DISCARD
598 : pavelk 746 | _ => raise Fail("Action not implemented...")
599 :     (* end case *)
600 :     end
601 :    
602 : pavelk 868 fun compile (P.PG{
603 : pavelk 1017 emit as P.EMIT{vars=emitVars, ...},
604 : pavelk 868 act as P.PSAE{action=root_act, vars=actionVars},
605 :     render
606 :     }) = let
607 : pavelk 746 val blks = ref[]
608 :     val env = let
609 :     (* add special globals to free vars *)
610 : pavelk 1017 val vars = PSV.Set.union(emitVars, PSV.Set.addList(actionVars, [PSV.numDead, PSV.timeStep, epsilon]))
611 : pavelk 770 fun ins (x as PSV.V{name, ty, binding, id, ...}, map) = let
612 : pavelk 746 val x' = (case (ty, !binding)
613 :     of (PSV.T_BOOL, PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
614 :     | (PSV.T_BOOL, PSV.BOOL boolVal) => IR.newConst(name, IR.C_BOOL(boolVal))
615 :     | (PSV.T_INT, PSV.UNDEF) => IR.newGlobal(x, IR.T_INT)
616 :     | (PSV.T_INT, PSV.INT intVal) => IR.newConst(name, IR.C_INT(intVal))
617 :     | (PSV.T_FLOAT, PSV.UNDEF) => IR.newGlobal(x, IR.T_FLOAT)
618 :     | (PSV.T_FLOAT, PSV.FLOAT floatVal) => IR.newConst(name, IR.C_FLOAT(floatVal))
619 :     | (PSV.T_VEC3F, PSV.UNDEF) => IR.newGlobal(x, IR.T_VEC)
620 :     | (PSV.T_VEC3F, PSV.VEC3F vecVal) => IR.newConst(name, IR.C_VEC(vecVal))
621 : pavelk 972 | _ => raise Fail("Error in setup, type mismatch between PSV vars and their binding.")
622 : pavelk 746 (* end case *))
623 :     in
624 :     PSV.Map.insert (map, x, x')
625 :     end
626 :     in
627 :     TE(blks, PSV.Set.foldl ins PSV.Map.empty vars)
628 :     end
629 : pavelk 867
630 : pavelk 905 fun evalActs f [] state = f [] state
631 : pavelk 867 | evalActs f (psa :: psal) state = (case psa
632 :     of P.SEQ(acts) => (case acts
633 :     of [] => raise Fail "Should never reach here."
634 :     | [act] => trAct(act, env, state, evalActs f psal)
635 :     | act :: rest => trAct(act, env, state, evalActs f (P.SEQ(rest) :: psal))
636 :     (* end case *))
637 :     | P.PRED(pred as P.PR{thenstmt=t, elsestmt=e, ...}) => let
638 :     val cblk = newBlock(env, evalActs f psal)
639 :     fun trPredActs [] state' = goto(state', cblk)
640 :     | trPredActs _ _ = raise Fail "Should never reach here."
641 :     in
642 :     trPred(pred, env, state, evalActs trPredActs t, evalActs trPredActs e)
643 :     end
644 :     (* end case *))
645 :    
646 : pavelk 868 (* At the highest level, we want to return when we reach the end of the action list *)
647 : pavelk 746 fun trActs [] state = let
648 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = state
649 : pavelk 746 in
650 : pavelk 905 IR.mkRETURN (
651 : pavelk 1027 [ pos, vel, size, ttl, color, rot, pos2, dummy ],
652 :     [IR.POS, IR.VEL, IR.SZ, IR.TTL, IR.COLOR, IR.ROT, IR.POS2, IR.DUMMY]
653 : pavelk 905 )
654 : pavelk 746 end (* trActs *)
655 : pavelk 867 | trActs _ _ = raise Fail "Should never reach here"
656 : pavelk 868
657 :     (* The entry block is the first block of the program, or in other words, the emitter. *)
658 :     val entryBlock = newBlock (
659 :     env,
660 :     fn pstate => trEmitter(
661 :     emit,
662 :     env,
663 :     pstate,
664 :     fn state => evalActs trActs root_act state
665 :     )
666 :     )
667 :    
668 : pavelk 972 (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
669 :    
670 :     fun isGlobal(IR.V{scope, ...}) = (case scope
671 :     of IR.S_GLOBAL(v) => true
672 :     | _ => false
673 :     (* end case *))
674 :    
675 :     fun extractVarMap(TE(blks, map)) = map
676 :    
677 : pavelk 866 val outPgm = PSysIR.PGM {
678 : pavelk 972 globals = PSV.Map.filter isGlobal (extractVarMap env),
679 :     emitter = entryBlock,
680 : pavelk 906 physics = List.nth(!blks, 1),
681 : pavelk 866 render = render
682 :     }
683 : pavelk 868
684 : pavelk 906 val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
685 : pavelk 868
686 : pavelk 746 in
687 : pavelk 905 (* IR.outputPgm(TextIO.stdErr, outPgm); *)
688 : pavelk 868 if Checker.checkIR(optimized) then
689 :     printErr "Compilation succeeded." (* Note: it only succeeds if we can optimize, too *)
690 : pavelk 746 else
691 : pavelk 866 ();
692 : pavelk 868 optimized
693 : pavelk 746 end (* compile *)
694 :    
695 :     end (* Translate *)

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