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 1119 - (view) (download)

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

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