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

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