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 1060 - (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 : pavelk 1060
387 :     | P.D_CYLINDER {pt1, pt2, irad, orad} =>
388 :    
389 :     (* !FIXME! Right now, we see whether or not the point is within the two planes defined
390 :     * by the endpoints of the cylinder, and then testing to see whether or not the smallest
391 :     * distance to the line segment falls within the radii. It might be faster to find the
392 :     * closest point to the line defined by the endpoints and then see whether or not the point
393 :     * is within the segment.
394 :     *)
395 :    
396 :     (* Is it in one plane *)
397 :     letPRIM("plane1Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn plane1Norm =>
398 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt1)], fn posToPt1 =>
399 :     letPRIM("dot1", IR.T_FLOAT, IR.DOT, [posToPt1, plane1Norm], fn dot1Prod =>
400 :     letPRIM("inPlane1", IR.T_BOOL, IR.GT, [dot1Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane1=>
401 :    
402 :     (* Is it in another plane *)
403 :     letPRIM("plane2Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), psvToIRVar(env, pt2)], fn plane2Norm =>
404 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt2)], fn posToPt2 =>
405 :     letPRIM("dot2", IR.T_FLOAT, IR.DOT, [posToPt2, plane2Norm], fn dot2Prod =>
406 :     letPRIM("inPlane2", IR.T_BOOL, IR.GT, [dot2Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane2=>
407 :    
408 :     (* Is it in both planes? *)
409 :     letPRIM("inPlanes", IR.T_BOOL, IR.AND, [inPlane1, inPlane2], fn inPlanes =>
410 :    
411 :     (* Find distance from segment *)
412 :     letPRIM("a", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn a =>
413 :     letPRIM("b", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn b =>
414 :     letPRIM("alen", IR.T_FLOAT, IR.LEN, [a], fn alen =>
415 :     letPRIM("axb", IR.T_VEC, IR.CROSS, [a, b], fn axb =>
416 :     letPRIM("axblen", IR.T_FLOAT, IR.LEN, [axb], fn axblen =>
417 :     letPRIM("dist", IR.T_FLOAT, IR.DIV, [axblen, alen], fn dist =>
418 :    
419 :     (* Is distance in both radii? *)
420 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), dist], fn inOradGt =>
421 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), dist], fn inOradEq =>
422 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
423 :    
424 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [dist, psvToIRVar(env, irad)], fn inIradGt =>
425 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [dist, psvToIRVar(env, irad)], fn inIradEq =>
426 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
427 :    
428 :     letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
429 :    
430 :     (* It's in the cylinder (tube) if it's within both radii and in both planes... *)
431 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inPlanes, inBothRad], stmt)
432 :     ))))))))))))))))))))))
433 : pavelk 746 (*
434 :     | P.D_TRIANGLE {pt1: vec3f var, pt2: vec3f var, pt3: vec3f var}
435 :     | P.D_PLANE {pt: vec3f var, normal: vec3f var}
436 :     | P.D_RECT {pt: vec3f var, htvec: vec3f var, wdvec: vec3f var}
437 :     | P.D_BOX {min: vec3f var, max: vec3f var}
438 :     | P.D_SPHERE {center: vec3f var, irad: vec3f var, orad: vec3f var}
439 :     | P.D_CONE {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
440 :     | P.D_BLOB {center: vec3f var, stddev: float var}
441 :     | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
442 :     *)
443 :     | _ => raise Fail "Cannot determine within-ness for specified domain."
444 :     (* end case *)
445 :     end (*end let *)
446 :    
447 :    
448 :     (* generate code to produce a random particle state from a domain *)
449 :     fun newParticle (posDomain, velDomain, colDomain, env, k : particle_state -> IR.stmt) =
450 :     (* genVecVar (vecVar, env, domain, stmt) *)
451 :     genVecVar("ps_pos", env, posDomain, fn newPos =>
452 :     genVecVar("ps_vel", env, velDomain, fn newVel =>
453 :     genVecVar("ps_col", env, colDomain, fn newCol =>
454 : pavelk 1008 letPRIM ("randSize", IR.T_FLOAT, IR.RAND, [], fn randSize =>
455 :     letPRIM ("halvedSize", IR.T_FLOAT, IR.MULT, [randSize, IR.newConst("halver", IR.C_FLOAT 0.5)], fn halfSize =>
456 :     letSPRIM("ps_size", IR.T_FLOAT, IR.ADD, [halfSize, IR.newConst("scalar", IR.C_FLOAT 0.5)], fn newSize =>
457 : pavelk 915 letSPRIM ("ps_ttl", IR.T_FLOAT, IR.COPY, [IR.newConst("fbool", IR.C_FLOAT 10000.0)], fn newIsDead =>
458 : pavelk 873 k(PS{pos = newPos,
459 :     vel = newVel,
460 :     size = newSize,
461 : pavelk 915 ttl = newIsDead,
462 : pavelk 900 color = newCol,
463 : pavelk 1027 rot = IR.newConst("ps_rot", IR.C_FLOAT 0.0),
464 : pavelk 905 pos2 = IR.newConst("ps_pos2", IR.C_VEC {x=0.0, y=0.0, z=0.0}),
465 :     dummy = IR.newConst("ps_dummy", IR.C_FLOAT 0.01)})
466 : pavelk 1008 )))))))
467 : pavelk 746
468 :     (* Find the normal at the given position of the particle for the specified
469 :     * domain. Note, that the particle doesn't necessarily need to be on the
470 :     * domain, but if it's not then the behavior is undefined.
471 :     *)
472 :     fun normAtPoint(retNorm, d, env, state, k : IR.var -> particle_state -> IR.stmt) = let
473 :     val newNorm = IR.newParam("n", IR.T_VEC)
474 :     val nextBlk = newBlockWithArgs(env, [newNorm], k(newNorm))
475 : pavelk 770 val PS{pos, ...} = state
476 : pavelk 746 in
477 :     (case d
478 :     of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
479 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
480 :     | P.D_DISC{pt, normal, irad, orad} =>
481 : pavelk 770 mkWithinVar("inP", env, pos, d, fn inPlane =>
482 : pavelk 746 IR.mkIF(inPlane,
483 :     (* then *)
484 :     letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
485 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk)),
486 :     (* else *)
487 :     letPRIM(retNorm,
488 :     IR.T_VEC,
489 :     IR.SCALE,
490 :     [IR.newConst("negOne", IR.C_FLOAT ~1.0), psvToIRVar(env, normal)],
491 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
492 :     )
493 :     )
494 :    
495 :     | P.D_SPHERE{center, irad, orad} => let
496 : pavelk 870 val PS{pos, ...} = state
497 : pavelk 746 in
498 :     letPRIM("sv", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, center)], fn subVec =>
499 :     letPRIM(retNorm, IR.T_VEC, IR.NORM, [subVec], fn newNormVar => k newNormVar state
500 :     ))
501 :     end
502 :    
503 :     | _ => raise Fail("Cannot find normal to point of specified domain.")
504 :     (* end case *))
505 :     end
506 : pavelk 769
507 : pavelk 770 fun trEmitter(emit, env, state, k : particle_state -> IR.stmt) = let
508 : pavelk 866
509 : pavelk 915 val PS{ttl, ...} = state
510 : pavelk 1017 val P.EMIT{range, dist, posDomain, velDomain, colDomain, ...} = emit
511 : pavelk 770 val blk = newBlock (env, k)
512 :     in
513 : pavelk 918 letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>
514 : pavelk 770 IR.mkIF(isDead,
515 :     (* then *)
516 : pavelk 1017 genFloatVar("t1", env, range, dist, fn t1 =>
517 : pavelk 903 letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
518 : pavelk 770 letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
519 :     letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
520 :     letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
521 :     IR.mkIF(t3,
522 :     (* then *)
523 :     newParticle (posDomain, velDomain, colDomain, env,
524 : pavelk 866 fn state' => retState state'),
525 : pavelk 770 (* else *)
526 :     IR.DISCARD)))))),
527 :     (* else *)
528 : pavelk 915 retState state))
529 : pavelk 770 end
530 :    
531 : pavelk 769 fun trPred(pred, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
532 : pavelk 870 val PS{pos, vel, ...} = state
533 : pavelk 769 val P.PR{ifstmt, ...} = pred
534 :     in
535 :     case ifstmt
536 : pavelk 770 of P.WITHIN(d) => mkWithinVar("wv", env, pos, d, fn withinVar =>
537 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
538 : pavelk 770 | P.WITHINVEL(d) => mkWithinVar("wv", env, vel, d, fn withinVar =>
539 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
540 : pavelk 769 end
541 :    
542 : pavelk 746 fun trAct (action, env, state, k : particle_state -> IR.stmt) = let
543 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = state
544 : pavelk 746 in
545 :     case action
546 :     of P.BOUNCE{friction, resilience, cutoff, d} => let
547 :     val blk = newBlock (env, k)
548 :     val negOne = IR.newConst("negOne", IR.C_FLOAT ~1.0)
549 :     in
550 : pavelk 903 letPRIM("vs", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn velScale =>
551 : pavelk 746 letPRIM("np", IR.T_VEC, IR.ADD_VEC, [pos, velScale], fn nextPos =>
552 : pavelk 987 mkWithinVar("wcp", env, pos, d, fn withinCurPos =>
553 :     mkWithinVar("wnp", env, nextPos, d, fn withinNextPos =>
554 :     letPRIM("nwcp", IR.T_BOOL, IR.NOT, [withinCurPos], fn notWithinCurPos =>
555 :     letPRIM("sb", IR.T_BOOL, IR.AND, [notWithinCurPos, withinNextPos], fn shouldBounce =>
556 :     IR.mkIF(shouldBounce,
557 : pavelk 746 (*then*)
558 :     normAtPoint("n", d, env, state, fn normAtD => fn state' => let
559 : pavelk 1027 val PS{pos=nextPos, vel=nextVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy} = state'
560 : pavelk 746 in
561 :     letPRIM("negVel", IR.T_VEC, IR.SCALE, [negOne, nextVel], fn negVel =>
562 :     letPRIM("dnv", IR.T_FLOAT, IR.DOT, [negVel, normAtD], fn dotNegVel =>
563 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [dotNegVel, normAtD], fn scaledN =>
564 :     letPRIM("t", IR.T_VEC, IR.SUB_VEC, [negVel, scaledN], fn tang =>
565 :    
566 :     letPRIM("tlsq", IR.T_FLOAT, IR.LEN_SQ, [tang], fn tangLenSq =>
567 :     letPRIM("cosq", IR.T_FLOAT, IR.MULT, [psvToIRVar(env, cutoff), psvToIRVar(env, cutoff)], fn cutoffSq =>
568 :     letPRIM("inco", IR.T_BOOL, IR.GT, [tangLenSq, cutoffSq], fn inCutoff =>
569 :    
570 :     letPRIM("resNorm", IR.T_VEC, IR.SCALE, [psvToIRVar(env, resilience), scaledN], fn resNorm =>
571 :    
572 :     IR.mkIF(inCutoff,
573 :     (*then*)
574 :     letPRIM("fInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), psvToIRVar(env, friction)], fn frictInv =>
575 :     letPRIM("f", IR.T_FLOAT, IR.MULT, [negOne, frictInv], fn modFrict =>
576 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [modFrict, tang], fn frictTang =>
577 :     letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
578 : pavelk 1027 goto(PS{pos=nextPos, vel=newVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy}, blk)
579 : pavelk 746 )))),
580 :     (*else*)
581 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [negOne, tang], fn frictTang =>
582 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
583 : pavelk 1027 goto(PS{pos=nextPos, vel=newVel, size=nextSize, ttl=nextIsDead, color=nextColor, rot=nextRot, pos2=nextPos2, dummy=nextDummy}, blk)
584 : pavelk 746 ))
585 :     )))))))))
586 :     end
587 :     ),
588 :     (*else*)
589 : pavelk 987 goto(state, blk))))))))
590 : pavelk 746 end
591 :    
592 : jhr 974 | P.ACCEL dir =>
593 : pavelk 903 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), psvToIRVar(env, dir)], fn theScale =>
594 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [theScale, vel], fn newVel =>
595 : pavelk 1027 k(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy})))
596 : pavelk 746
597 :     | P.MOVE =>
598 : pavelk 903 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn theScale =>
599 : pavelk 902 letPRIM("ps_pos", IR.T_VEC, IR.ADD_VEC, [theScale, pos], fn newPos =>
600 : pavelk 1027 k(PS{pos = newPos, vel = vel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy})))
601 : pavelk 770 (*
602 : pavelk 758 | P.SINK({d, kill_inside}) =>
603 : pavelk 746 mkWithinVar("isWithin", env, state, d, fn withinVal =>
604 :     mkXOR ("shouldNotKill", withinVal, psvToIRVar(env, kill_inside),
605 :     fn shouldNotKill =>
606 : pavelk 758 letPRIM("shouldKill", IR.T_BOOL, IR.NOT, [shouldNotKill], fn shouldKill =>
607 : pavelk 915 letPRIM("isReallyDead", IR.T_BOOL, IR.OR, [shouldKill, ttl], fn isReallyDead =>
608 :     k(PS{pos = pos, vel = vel, size = size, ttl = isReallyDead, color = color})
609 : pavelk 758 ))))
610 : pavelk 770 *)
611 : pavelk 746
612 :     | P.ORBITLINESEG {endp1, endp2, maxRad, mag} => let
613 :     val blk = newBlock (env, k)
614 :     in
615 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, endp2), psvToIRVar(env, endp1)], fn subVec =>
616 :     letPRIM("vecToEndP", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, endp1)], fn vecToEndP =>
617 :     letPRIM("basis", IR.T_VEC, IR.NORM, [subVec], fn basis =>
618 :     letPRIM("parDot", IR.T_FLOAT, IR.DOT, [basis, vecToEndP], fn parDot =>
619 :     letPRIM("parVec", IR.T_VEC, IR.SCALE, [parDot, basis], fn parVec =>
620 :     letPRIM("closestP", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, endp1), parVec], fn closestP =>
621 :     letPRIM("vecToP", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn vecToP =>
622 :     letPRIM("distToP", IR.T_FLOAT, IR.LEN, [vecToP], fn distToP =>
623 :     letPRIM("effRad", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, maxRad), distToP], fn effRad =>
624 :     letPRIM("radInDist", IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), effRad], fn radInDist =>
625 :     IR.mkIF(radInDist,
626 :     (*then*)
627 :     goto(state, blk),
628 :     (*else*)
629 :     letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [distToP, psvToIRVar(env, maxRad)], fn magRatio =>
630 :     letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>
631 :     letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>
632 : pavelk 903 letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, PSV.timeStep)], fn totMag =>
633 : pavelk 746 letPRIM("accVec", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn accVec =>
634 :     letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, accVec], fn acc =>
635 : pavelk 902 letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>
636 : pavelk 1027 goto(PS{pos = pos, vel = newVel, size = size, ttl = ttl, color = color, rot=rot, pos2=pos2, dummy=dummy}, blk)
637 : pavelk 746 )))))))
638 :     )))))))))))
639 :     end
640 : pavelk 770
641 :     (* just kill it. *)
642 : pavelk 915 (* | P.DIE => k(PS{pos = pos, vel = vel, size = size, ttl = IR.newConst("falseVar", IR.C_BOOL true), color = color, dummy=dummy}) *)
643 : pavelk 870 | P.DIE => IR.DISCARD
644 : pavelk 746 | _ => raise Fail("Action not implemented...")
645 :     (* end case *)
646 :     end
647 :    
648 : pavelk 868 fun compile (P.PG{
649 : pavelk 1017 emit as P.EMIT{vars=emitVars, ...},
650 : pavelk 868 act as P.PSAE{action=root_act, vars=actionVars},
651 :     render
652 :     }) = let
653 : pavelk 746 val blks = ref[]
654 :     val env = let
655 :     (* add special globals to free vars *)
656 : pavelk 1017 val vars = PSV.Set.union(emitVars, PSV.Set.addList(actionVars, [PSV.numDead, PSV.timeStep, epsilon]))
657 : pavelk 770 fun ins (x as PSV.V{name, ty, binding, id, ...}, map) = let
658 : pavelk 746 val x' = (case (ty, !binding)
659 :     of (PSV.T_BOOL, PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
660 :     | (PSV.T_BOOL, PSV.BOOL boolVal) => IR.newConst(name, IR.C_BOOL(boolVal))
661 :     | (PSV.T_INT, PSV.UNDEF) => IR.newGlobal(x, IR.T_INT)
662 :     | (PSV.T_INT, PSV.INT intVal) => IR.newConst(name, IR.C_INT(intVal))
663 :     | (PSV.T_FLOAT, PSV.UNDEF) => IR.newGlobal(x, IR.T_FLOAT)
664 :     | (PSV.T_FLOAT, PSV.FLOAT floatVal) => IR.newConst(name, IR.C_FLOAT(floatVal))
665 :     | (PSV.T_VEC3F, PSV.UNDEF) => IR.newGlobal(x, IR.T_VEC)
666 :     | (PSV.T_VEC3F, PSV.VEC3F vecVal) => IR.newConst(name, IR.C_VEC(vecVal))
667 : pavelk 972 | _ => raise Fail("Error in setup, type mismatch between PSV vars and their binding.")
668 : pavelk 746 (* end case *))
669 :     in
670 :     PSV.Map.insert (map, x, x')
671 :     end
672 :     in
673 :     TE(blks, PSV.Set.foldl ins PSV.Map.empty vars)
674 :     end
675 : pavelk 867
676 : pavelk 905 fun evalActs f [] state = f [] state
677 : pavelk 867 | evalActs f (psa :: psal) state = (case psa
678 :     of P.SEQ(acts) => (case acts
679 :     of [] => raise Fail "Should never reach here."
680 :     | [act] => trAct(act, env, state, evalActs f psal)
681 :     | act :: rest => trAct(act, env, state, evalActs f (P.SEQ(rest) :: psal))
682 :     (* end case *))
683 :     | P.PRED(pred as P.PR{thenstmt=t, elsestmt=e, ...}) => let
684 :     val cblk = newBlock(env, evalActs f psal)
685 :     fun trPredActs [] state' = goto(state', cblk)
686 :     | trPredActs _ _ = raise Fail "Should never reach here."
687 :     in
688 :     trPred(pred, env, state, evalActs trPredActs t, evalActs trPredActs e)
689 :     end
690 :     (* end case *))
691 :    
692 : pavelk 868 (* At the highest level, we want to return when we reach the end of the action list *)
693 : pavelk 746 fun trActs [] state = let
694 : pavelk 1027 val PS{pos, vel, size, ttl, color, rot, pos2, dummy} = state
695 : pavelk 746 in
696 : pavelk 905 IR.mkRETURN (
697 : pavelk 1027 [ pos, vel, size, ttl, color, rot, pos2, dummy ],
698 :     [IR.POS, IR.VEL, IR.SZ, IR.TTL, IR.COLOR, IR.ROT, IR.POS2, IR.DUMMY]
699 : pavelk 905 )
700 : pavelk 746 end (* trActs *)
701 : pavelk 867 | trActs _ _ = raise Fail "Should never reach here"
702 : pavelk 868
703 :     (* The entry block is the first block of the program, or in other words, the emitter. *)
704 :     val entryBlock = newBlock (
705 :     env,
706 :     fn pstate => trEmitter(
707 :     emit,
708 :     env,
709 :     pstate,
710 :     fn state => evalActs trActs root_act state
711 :     )
712 :     )
713 :    
714 : pavelk 972 (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
715 :    
716 :     fun isGlobal(IR.V{scope, ...}) = (case scope
717 :     of IR.S_GLOBAL(v) => true
718 :     | _ => false
719 :     (* end case *))
720 :    
721 :     fun extractVarMap(TE(blks, map)) = map
722 :    
723 : pavelk 866 val outPgm = PSysIR.PGM {
724 : pavelk 972 globals = PSV.Map.filter isGlobal (extractVarMap env),
725 :     emitter = entryBlock,
726 : pavelk 906 physics = List.nth(!blks, 1),
727 : pavelk 866 render = render
728 :     }
729 : pavelk 868
730 : pavelk 906 val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
731 : pavelk 868
732 : pavelk 746 in
733 : pavelk 905 (* IR.outputPgm(TextIO.stdErr, outPgm); *)
734 : pavelk 868 if Checker.checkIR(optimized) then
735 :     printErr "Compilation succeeded." (* Note: it only succeeds if we can optimize, too *)
736 : pavelk 746 else
737 : pavelk 866 ();
738 : pavelk 868 optimized
739 : pavelk 746 end (* compile *)
740 :    
741 :     end (* Translate *)

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