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 1131 - (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 : pavelk 1131
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 : pavelk 746
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 : pavelk 1131 | P.D_TRIANGLE{pt1, pt2, pt3} =>
199 :    
200 :     letPRIM ("pt1ToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn pt1ToPt2 =>
201 :     letPRIM ("pt1ToPt3", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt3), psvToIRVar(env, pt1)], fn pt1ToPt3 =>
202 :     letPRIM ("randOne", IR.T_FLOAT, IR.RAND, [], fn rand1 =>
203 :     letPRIM ("randTwo", IR.T_FLOAT, IR.RAND, [], fn rand2 =>
204 :     letPRIM ("randTwoInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), rand2], fn rand2Inv =>
205 :     letPRIM ("scaleOne", IR.T_VEC, IR.SCALE, [rand1, pt1ToPt2], fn scale1 =>
206 :     letPRIM ("nextScale1", IR.T_VEC, IR.SCALE, [rand2Inv, scale1], fn nextScale1 =>
207 :     letPRIM ("scaleTwo", IR.T_VEC, IR.SCALE, [rand2, pt1ToPt3], fn scale2 =>
208 :     letPRIM ("tempAdd", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, pt1), nextScale1], fn tempAdd =>
209 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [tempAdd, scale2], stmt))))))))))
210 : pavelk 746
211 :     | P.D_CYLINDER {pt1, pt2, irad, orad} => let
212 :     val normVar = PSV.new("local_ht", PSV.T_VEC3F)
213 :     in
214 :     letPRIM("rand", IR.T_FLOAT, IR.RAND, [], fn ourRand =>
215 :     letPRIM("n", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn normVec =>
216 :     letPRIM("ht", IR.T_FLOAT, IR.LEN, [normVec], fn height =>
217 :     letPRIM("htInv", IR.T_FLOAT, IR.DIV, [IR.newConst("one", IR.C_FLOAT 1.0), height], fn htInv =>
218 :     letPRIM("n", IR.T_VEC, IR.SCALE, [htInv, normVec], fn norm =>
219 :     (* Generate a point in the lower disc. *)
220 : pavelk 1109 genVecVar("ptInDisc",
221 :     insert(env, normVar, norm),
222 :     P.D_DISC{pt = pt1, normal = normVar, irad = irad, orad = orad},
223 :     dist,
224 :     fn ptInDisc =>
225 : pavelk 746 (* Now add this point to a random scaling of the normVec. *)
226 :     letPRIM("s", IR.T_FLOAT, IR.MULT, [height, ourRand], fn scale =>
227 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [scale, normVec], fn scaledNormVec =>
228 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [ptInDisc, scaledNormVec], stmt)))))))))
229 :     end
230 : pavelk 1131
231 :     | P.D_DISC {pt, normal, irad, orad} =>
232 :    
233 :     (* Get a random angle... *)
234 :     letPRIM ("r", IR.T_FLOAT, IR.RAND, [], fn randForAng =>
235 :     letPRIM ("t", IR.T_FLOAT, IR.MULT, [randForAng, IR.newConst("fullCir", IR.C_FLOAT (2.0 * pi))], fn randAng =>
236 :    
237 :     (* Get a random radius *)
238 :     letPRIM ("e0", IR.T_FLOAT, IR.RAND, [], fn newRand =>
239 :     letPRIM ("e0sq", IR.T_FLOAT, IR.MULT, [newRand, newRand], fn randRadSq =>
240 :     letPRIM ("radDiff", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, orad), psvToIRVar(env, irad)], fn radDiff =>
241 :     letPRIM ("newRadDist", IR.T_FLOAT, IR.MULT, [randRadSq, radDiff], fn newRadDist =>
242 :     letPRIM ("newRad", IR.T_FLOAT, IR.ADD, [psvToIRVar(env, irad), newRadDist], fn newRad =>
243 :    
244 :     (* Find a vector in the plane of the disc, and then
245 :     * translate it to the center. *)
246 :     letPRIM ("ntoc", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), psvToIRVar(env, normal)], fn normToCen =>
247 :     letPRIM ("v", IR.T_VEC, IR.CROSS, [psvToIRVar(env, pt), normToCen], fn vecInDisc =>
248 :     letPRIM ("vidn", IR.T_VEC, IR.NORM, [vecInDisc], fn vecInDiscNorm =>
249 :     letPRIM ("p", IR.T_VEC, IR.CROSS, [vecInDiscNorm, psvToIRVar(env, normal)], fn ptInDisc =>
250 :     letPRIM ("pidn", IR.T_VEC, IR.NORM, [ptInDisc], fn ptInDiscNorm =>
251 :    
252 :     (* Figure out x and y values for our new radius and angle *)
253 :     letPRIM ("rx", IR.T_FLOAT, IR.COS, [randAng], fn radX =>
254 :     letPRIM ("ar1", IR.T_FLOAT, IR.MULT, [newRad, radX], fn amtVecOne =>
255 :     letPRIM ("rv1", IR.T_VEC, IR.SCALE, [amtVecOne, vecInDiscNorm], fn resVecOne =>
256 :     letPRIM ("ry", IR.T_FLOAT, IR.SIN, [randAng], fn radY =>
257 :     letPRIM ("ar2", IR.T_FLOAT, IR.MULT, [newRad, radY], fn amtVecTwo =>
258 :     letPRIM ("rv2", IR.T_VEC, IR.SCALE, [amtVecTwo, ptInDiscNorm], fn resVecTwo =>
259 :     letPRIM ("res", IR.T_VEC, IR.ADD_VEC, [resVecOne, resVecTwo], fn result =>
260 :    
261 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [result, psvToIRVar(env, pt)], stmt)
262 :     )))))))))))))))))))
263 : pavelk 746
264 : pavelk 1131 | P.D_CONE{pt1, pt2, irad, orad} => let
265 :     val normVar = PSV.new("local_ht", PSV.T_VEC3F)
266 :     in
267 :     letPRIM("eh", IR.T_FLOAT, IR.RAND, [], fn ourRand =>
268 :     letPRIM("nv", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn normVec =>
269 :     letPRIM("n", IR.T_VEC, IR.NORM, [normVec], fn norm =>
270 :     genVecVar("ptInDisc",
271 :     insert(env, normVar, norm),
272 :     P.D_DISC{pt = pt1, normal = normVar, irad = irad, orad = orad},
273 :     dist,
274 :     fn ptInDisc =>
275 :     letPRIM("gptt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), ptInDisc], fn genPtToTip =>
276 :     letPRIM("gpttlen", IR.T_FLOAT, IR.LEN, [genPtToTip], fn genPtToTipLen =>
277 :     letPRIM("s", IR.T_FLOAT, IR.MULT, [genPtToTipLen, ourRand], fn scale =>
278 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [scale, genPtToTip], fn scaledNormVec =>
279 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [ptInDisc, scaledNormVec], stmt)
280 :     ))))))))
281 :     end
282 : pavelk 1074
283 :     | P.D_SPHERE{center, irad, orad} =>
284 :    
285 : pavelk 1131 (* Source: http://mathworld.wolfram.com/SpherePointPicking.html *)
286 :    
287 :     (* generate two random values... one will be called u and will
288 :     * represent cos(theta), and the other will be called v and will
289 :     * represent a random value in [0, 2 * pi] *)
290 :     letPRIM("randVal", IR.T_FLOAT, IR.RAND, [], fn rv =>
291 :     letPRIM("dblRandVal", IR.T_FLOAT, IR.MULT, [rv, IR.newConst("Two", IR.C_FLOAT 2.0)], fn drv =>
292 :     letPRIM("rand", IR.T_FLOAT, IR.SUB, [drv, IR.newConst("One", IR.C_FLOAT 1.0)], fn u =>
293 :    
294 :     letPRIM("rv2", IR.T_FLOAT, IR.RAND, [], fn rv2 =>
295 :     letPRIM("rand2", IR.T_FLOAT, IR.MULT, [rv2, IR.newConst("TwoPi", IR.C_FLOAT (2.0 * Float.M_PI))], fn theta =>
296 : pavelk 1074
297 : pavelk 1131 letPRIM("cosTheta", IR.T_FLOAT, IR.COS, [theta], fn cosT =>
298 :     letPRIM("sinTheta", IR.T_FLOAT, IR.SIN, [theta], fn sinT =>
299 :    
300 :     letPRIM("usq", IR.T_FLOAT, IR.MULT, [u, u], fn usq =>
301 :     letPRIM("usqInv", IR.T_FLOAT, IR.SUB, [IR.newConst("One", IR.C_FLOAT 1.0), usq], fn usqInv =>
302 :     letPRIM("sinPhi", IR.T_FLOAT, IR.SQRT, [usqInv], fn sinP =>
303 :    
304 :     letPRIM("xVal", IR.T_FLOAT, IR.MULT, [sinP, cosT], fn xVal =>
305 :     letPRIM("yVal", IR.T_FLOAT, IR.MULT, [sinP, sinT], fn yVal =>
306 :     (* zval is just u *)
307 : pavelk 1074
308 : pavelk 1131 letPRIM("vec", IR.T_VEC, IR.GEN_VEC, [xVal, yVal, u], fn vec =>
309 : pavelk 1074
310 :     (* Generate a random radius... *)
311 :     letPRIM("ratio", IR.T_FLOAT, IR.DIV, [psvToIRVar(env, irad), psvToIRVar(env, orad)], fn ratio =>
312 :     letPRIM("invRatio", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), ratio], fn invRatio =>
313 :     letPRIM("randVar", IR.T_FLOAT, IR.RAND, [], fn rand =>
314 :     letPRIM("randScale", IR.T_FLOAT, IR.MULT, [rand, invRatio], fn randScale =>
315 :     letPRIM("randVal", IR.T_FLOAT, IR.ADD, [randScale, ratio], fn randVal =>
316 :     letPRIM("randValSq", IR.T_FLOAT, IR.MULT, [randVal, randVal], fn randValSq =>
317 :     letPRIM("radDiff", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, orad), psvToIRVar(env, irad)], fn radDiff =>
318 :     letPRIM("randRadVal", IR.T_FLOAT, IR.MULT, [radDiff, randValSq], fn randRadVal =>
319 :     letPRIM("rad", IR.T_FLOAT, IR.ADD, [psvToIRVar(env, irad), randRadVal], fn rad =>
320 :    
321 :     (* Normalize the vector and scale it by the radius. *)
322 :     letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [rad, vec], fn sVec =>
323 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [sVec, psvToIRVar(env, center)], stmt)
324 :     ))))))))))
325 : pavelk 1131 )))))))))))))
326 : pavelk 746
327 :     | _ => raise Fail "Cannot generate point in specified domain."
328 :     (* end case *))
329 :     (*
330 :     | generate (Dplane{pt, n}) = Vec3f.unpack pt
331 :     | generate (Drectangle{pt, u, v}) = Vec3f.unpack pt
332 :     | generate (Dsphere{c, orad, irad}) = Vec3f.unpack c
333 :     | generate (Dblob{c, stddev}) = Vec3f.unpack c
334 :     *)
335 :    
336 :     (* This function takes an IR boolean, its environment, a particle state, domain,
337 :     * and continuation.
338 :     *
339 :     * We set the boolean to whether or not the current particle given by the particle
340 :     * state is within the domain, and then pass the continuation on.
341 :     *)
342 : pavelk 1120 fun mkVecWithinVar (boolVar, env, var, d : Vec3f.vec3 P.domain, stmt : IR.var -> IR.stmt) = let
343 : pavelk 770 val pos = var
344 : pavelk 746 in
345 :     case d
346 :     of P.D_POINT(pt) =>
347 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn subVec =>
348 :     letPRIM("vecLen", IR.T_FLOAT, IR.LEN, [subVec], fn vecLen =>
349 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), vecLen], stmt)))
350 :    
351 :     (* Take the vectors going from our position to pt1, and pt2. Then
352 :     * after we normalize them, if their dot product is equal to -1, then
353 :     * they are pointing in opposite directions meaning that the position
354 :     * is inbetween pt1 and pt2 as desired.
355 :     *)
356 :     | P.D_LINE{pt1, pt2} =>
357 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn posToPt1 =>
358 :     letPRIM("posToPt1Norm", IR.T_VEC, IR.NORM, [posToPt1], fn posToPt1Norm =>
359 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), pos], fn posToPt2 =>
360 :     letPRIM("posToPt2Norm", IR.T_VEC, IR.NORM, [posToPt2], fn posToPt2Norm =>
361 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt2, posToPt1], fn dotProd =>
362 :     letPRIM("testMe", IR.T_FLOAT, IR.SUB, [dotProd, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn testVal =>
363 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), testVal], stmt)))))))
364 :    
365 :     (* Just see whether or not the dot product between the normal
366 :     * and the vector from a point on the plane to our position is
367 :     * greater than zero. Essentially, we're "within" a plane if we're
368 :     * behind it (with respect to the normal)
369 :     *)
370 :     | P.D_PLANE{pt, normal} =>
371 : pavelk 905 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
372 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
373 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))
374 :    
375 :     (* Similar to checking to see whether or not we're within a plane,
376 :     * here all we have to do is see how far we are from the center
377 :     * of the disc (pt), and then see whther or not we're perpendicular to
378 :     * the normal, and that our distance is greater than irad but less than
379 :     * orad.
380 :     *)
381 :     | P.D_DISC{pt, normal, orad, irad} =>
382 : pavelk 907 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
383 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
384 :     letPRIM("inDisc", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.01), dotProd], fn inDisc =>
385 : pavelk 987
386 :     letPRIM("parPosToP", IR.T_VEC, IR.SCALE, [dotProd, psvToIRVar(env, normal)], fn posToPtParallelToNormal =>
387 :     letPRIM("perpPosToP", IR.T_VEC, IR.SUB_VEC, [posToPt, posToPtParallelToNormal], fn posToPtPerpToNormal =>
388 :     letPRIM("inDiscLen", IR.T_FLOAT, IR.LEN, [posToPtPerpToNormal], fn posToPtLen =>
389 :    
390 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToPtLen], fn inOradGt =>
391 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), posToPtLen], fn inOradEq =>
392 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
393 :    
394 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [posToPtLen, psvToIRVar(env, irad)], fn inIradGt =>
395 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [posToPtLen, psvToIRVar(env, irad)], fn inIradEq =>
396 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
397 :    
398 : pavelk 746 letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
399 : pavelk 987
400 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inDisc, inBothRad], stmt))))))))))))))
401 :    
402 : pavelk 746 (* Simply see whether or not the distance from the center is within the
403 :     * specified bounds.
404 :     *)
405 :     | P.D_SPHERE{center, orad, irad} =>
406 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, center), pos], fn posToC =>
407 :     letPRIM("posToPtLen", IR.T_VEC, IR.LEN, [posToC], fn posToCLen =>
408 :     letPRIM("inOrad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToCLen], fn inOrad =>
409 :     letPRIM("inIrad", IR.T_BOOL, IR.GT, [posToCLen, psvToIRVar(env, irad)], fn inIrad =>
410 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inIrad, inOrad], stmt)))))
411 : pavelk 1060
412 :     | P.D_CYLINDER {pt1, pt2, irad, orad} =>
413 :    
414 :     (* !FIXME! Right now, we see whether or not the point is within the two planes defined
415 :     * by the endpoints of the cylinder, and then testing to see whether or not the smallest
416 :     * distance to the line segment falls within the radii. It might be faster to find the
417 :     * closest point to the line defined by the endpoints and then see whether or not the point
418 :     * is within the segment.
419 :     *)
420 :    
421 :     (* Is it in one plane *)
422 :     letPRIM("plane1Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn plane1Norm =>
423 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt1)], fn posToPt1 =>
424 :     letPRIM("dot1", IR.T_FLOAT, IR.DOT, [posToPt1, plane1Norm], fn dot1Prod =>
425 :     letPRIM("inPlane1", IR.T_BOOL, IR.GT, [dot1Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane1=>
426 :    
427 :     (* Is it in another plane *)
428 :     letPRIM("plane2Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), psvToIRVar(env, pt2)], fn plane2Norm =>
429 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt2)], fn posToPt2 =>
430 :     letPRIM("dot2", IR.T_FLOAT, IR.DOT, [posToPt2, plane2Norm], fn dot2Prod =>
431 :     letPRIM("inPlane2", IR.T_BOOL, IR.GT, [dot2Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane2=>
432 :    
433 :     (* Is it in both planes? *)
434 :     letPRIM("inPlanes", IR.T_BOOL, IR.AND, [inPlane1, inPlane2], fn inPlanes =>
435 :    
436 :     (* Find distance from segment *)
437 :     letPRIM("a", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn a =>
438 :     letPRIM("b", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn b =>
439 :     letPRIM("alen", IR.T_FLOAT, IR.LEN, [a], fn alen =>
440 :     letPRIM("axb", IR.T_VEC, IR.CROSS, [a, b], fn axb =>
441 :     letPRIM("axblen", IR.T_FLOAT, IR.LEN, [axb], fn axblen =>
442 :     letPRIM("dist", IR.T_FLOAT, IR.DIV, [axblen, alen], fn dist =>
443 :    
444 :     (* Is distance in both radii? *)
445 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), dist], fn inOradGt =>
446 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), dist], fn inOradEq =>
447 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
448 :    
449 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [dist, psvToIRVar(env, irad)], fn inIradGt =>
450 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [dist, psvToIRVar(env, irad)], fn inIradEq =>
451 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
452 :    
453 :     letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
454 :    
455 :     (* It's in the cylinder (tube) if it's within both radii and in both planes... *)
456 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inPlanes, inBothRad], stmt)
457 :     ))))))))))))))))))))))
458 : pavelk 746 (*
459 :     | P.D_TRIANGLE {pt1: vec3f var, pt2: vec3f var, pt3: vec3f var}
460 :     | P.D_PLANE {pt: vec3f var, normal: vec3f var}
461 :     | P.D_RECT {pt: vec3f var, htvec: vec3f var, wdvec: vec3f var}
462 :     | P.D_BOX {min: vec3f var, max: vec3f var}
463 :     | P.D_SPHERE {center: vec3f var, irad: vec3f var, orad: vec3f var}
464 :     | P.D_CONE {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
465 :     | P.D_BLOB {center: vec3f var, stddev: float var}
466 :     | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
467 :     *)
468 : pavelk 1120 | _ => raise Fail "Cannot determine within-ness for specified vec3 domain."
469 : pavelk 746 (* end case *)
470 :     end (*end let *)
471 : pavelk 1120
472 :     fun mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) = (case d
473 :     of P.D_POINT(pt) => letPRIM(boolVar, IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, pt), var], stmt)
474 :     | P.D_BOX {min, max} =>
475 :     letPRIM("bigMin", IR.T_BOOL, IR.GT, [var, psvToIRVar(env, min)], fn bigMin =>
476 :     letPRIM("smallMax", IR.T_BOOL, IR.GT, [psvToIRVar(env, max), var], fn smallMax =>
477 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [bigMin, smallMax], stmt)))
478 :     | _ => raise Fail "Cannot determine within-ness for specified float domain."
479 :     (* end case *))
480 : pavelk 746
481 : pavelk 1109 fun mkIntBool(env, p1var, p2var, d : Vec3f.vec3 P.domain, k : IR.var -> IR.stmt) = let
482 : pavelk 1108 val _ = ()
483 :     in
484 :     (case d
485 :     of P.D_POINT(pt) =>
486 :    
487 :     (* Get vectors *)
488 :     letPRIM("p1ToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar (env, pt), p1var], fn p1ToPt =>
489 :     letPRIM("p2ToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar (env, pt), p2var], fn p2ToPt =>
490 :     letPRIM("p1ToP2", IR.T_VEC, IR.SUB_VEC, [p2var, p1var], fn p1ToP2 =>
491 :    
492 :     (* Get distances *)
493 :     letPRIM("p1ToPtLen", IR.T_FLOAT, IR.LEN, [p1ToPt], fn p1ToPtLen =>
494 :     letPRIM("p2ToPtLen", IR.T_FLOAT, IR.LEN, [p2ToPt], fn p2ToPtLen =>
495 :     letPRIM("p1ToP2Len", IR.T_FLOAT, IR.LEN, [p1ToP2], fn p1ToP2Len =>
496 :    
497 :     (* Add & subtract ... *)
498 :     letPRIM("distSum", IR.T_FLOAT, IR.ADD, [p1ToPtLen, p2ToPtLen], fn distSum =>
499 :     letPRIM("distDiff", IR.T_FLOAT, IR.SUB, [distSum, p1ToP2Len], fn distDiff =>
500 :     letPRIM("distDiffAbs", IR.T_FLOAT, IR.ABS, [distDiff], fn distDiffAbs =>
501 :    
502 :     (* Do the boolean stuff... *)
503 :     letPRIM("intersect", IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), distDiffAbs], k)
504 :    
505 :     )))
506 :     )))
507 :     )))
508 :    
509 :     | _ => raise Fail ("Cannot calculate intersection for specified domain")
510 :     (* end case *))
511 :    
512 :     end (* mkIntBool *)
513 :    
514 : pavelk 1109 fun mkIntPt(env, p1var, p2var, d : Vec3f.vec3 P.domain, k : IR.var -> IR.stmt) = let
515 : pavelk 1108 val _ = ()
516 :     in
517 :     (case d
518 : pavelk 1109 of P.D_POINT(pt) => k (psvToIRVar (env, pt))
519 : pavelk 1108 | _ => raise Fail ("Cannot calculate intersection for specified domain")
520 :     (* end case *))
521 :     end (* mkIntPt *)
522 :    
523 : pavelk 746 (* Find the normal at the given position of the particle for the specified
524 :     * domain. Note, that the particle doesn't necessarily need to be on the
525 :     * domain, but if it's not then the behavior is undefined.
526 :     *)
527 : pavelk 1109 fun normAtPoint(retNorm, d, env, pos, state, k : IR.var -> particle_state -> IR.stmt) = let
528 : pavelk 746 val newNorm = IR.newParam("n", IR.T_VEC)
529 : pavelk 1109 val nextBlk = newBlockWithArgs(env, state, [newNorm], k(newNorm))
530 : pavelk 746 in
531 :     (case d
532 :     of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
533 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
534 :     | P.D_DISC{pt, normal, irad, orad} =>
535 : pavelk 1120 mkVecWithinVar("inP", env, pos, d, fn inPlane =>
536 : pavelk 746 IR.mkIF(inPlane,
537 :     (* then *)
538 :     letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
539 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk)),
540 :     (* else *)
541 :     letPRIM(retNorm,
542 :     IR.T_VEC,
543 :     IR.SCALE,
544 :     [IR.newConst("negOne", IR.C_FLOAT ~1.0), psvToIRVar(env, normal)],
545 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
546 :     )
547 :     )
548 :    
549 : pavelk 1109 | P.D_SPHERE{center, irad, orad} =>
550 : pavelk 746 letPRIM("sv", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, center)], fn subVec =>
551 :     letPRIM(retNorm, IR.T_VEC, IR.NORM, [subVec], fn newNormVar => k newNormVar state
552 : pavelk 1109 ))
553 : pavelk 746
554 :     | _ => raise Fail("Cannot find normal to point of specified domain.")
555 :     (* end case *))
556 :     end
557 : pavelk 769
558 : pavelk 1108 fun trExpr(expr, env, state, k : IR.var -> IR.stmt) = (case expr
559 : pavelk 1109 of P.CONSTF f => k (IR.newConst ("c", IR.C_FLOAT f))
560 : pavelk 1108
561 : pavelk 1109 | P.CONST3F v => k (IR.newConst ("c", IR.C_VEC v))
562 : pavelk 1108
563 : pavelk 1109 | P.VAR v => k (psvToIRVar (env, v))
564 : pavelk 1108
565 : pavelk 1109 | P.STATE_VAR sv => k (getIRVarForSV (sv, state))
566 : pavelk 1108
567 : pavelk 1109 | P.GENERATE3F (dom, dist) => genVecVar("genVec", env, dom, dist, k)
568 : pavelk 1108
569 : pavelk 1109 | P.GENERATEF (dom, dist) => genFloatVar("genFlt", env, dom, dist, k)
570 : pavelk 1108
571 : pavelk 1109 | P.ADD(e1, e2) =>
572 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
573 :     trExpr(e2, env, state, fn e2var =>
574 :     let
575 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
576 :     val IR.V{varType=vt2, ...} = e2var
577 : pavelk 1108 in
578 :     (case (vt1, vt2)
579 :     of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("addVar", IR.T_FLOAT, IR.ADD, [e1var, e2var], k)
580 :     | (IR.T_VEC, IR.T_VEC) => letPRIM("addVar", IR.T_VEC, IR.ADD_VEC, [e1var, e2var], k)
581 :     | _ => raise Fail ("Type mismatch to ADD expression")
582 :     (* end case *))
583 :     end))
584 :    
585 : pavelk 1109 | P.SCALE (e1, e2) =>
586 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
587 :     trExpr(e2, env, state, fn e2var =>
588 :     let
589 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
590 :     val IR.V{varType=vt2, ...} = e2var
591 : pavelk 1108 in
592 :     (case (vt1, vt2)
593 :     of (IR.T_FLOAT, IR.T_VEC) => letPRIM("scaleVar", IR.T_VEC, IR.SCALE, [e1var, e2var], k)
594 : pavelk 1130 | (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("scaleVar", IR.T_FLOAT, IR.MULT, [e1var, e2var], k)
595 :     | _ => raise Fail (String.concat["Type mismatch to SCALE expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])
596 : pavelk 1108 (* end case *))
597 :     end))
598 :    
599 : pavelk 1109 | P.DIV (e1, e2) =>
600 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
601 :     trExpr(e2, env, state, fn e2var =>
602 :     let
603 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
604 :     val IR.V{varType=vt2, ...} = e2var
605 : pavelk 1108 in
606 :     (case (vt1, vt2)
607 :     of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("divVar", IR.T_FLOAT, IR.DIV, [e1var, e2var], k)
608 :     | _ => raise Fail ("Type mismatch to DIV expression")
609 :     (* end case *))
610 :     end))
611 : pavelk 866
612 : pavelk 1109 | P.NEG e =>
613 : pavelk 1108 trExpr(e, env, state, fn evar =>
614 :     let
615 : pavelk 1109 val IR.V{varType, ...} = evar
616 : pavelk 1108 in
617 :     (case varType
618 : pavelk 1109 of IR.T_FLOAT => letPRIM("negVar", IR.T_FLOAT, IR.MULT, [evar, IR.newConst("negOne", IR.C_FLOAT ~1.0)], k)
619 : pavelk 1108 | IR.T_VEC => letPRIM("negVar", IR.T_VEC, IR.NEG_VEC, [evar], k)
620 :     | _ => raise Fail ("Type mismatch to NEG expression")
621 :     (* end case *))
622 :     end)
623 :    
624 : pavelk 1109 | P.DOT (e1, e2) =>
625 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
626 :     trExpr(e2, env, state, fn e2var =>
627 :     let
628 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
629 :     val IR.V{varType=vt2, ...} = e2var
630 : pavelk 1108 in
631 :     (case (vt1, vt2)
632 :     of (IR.T_VEC, IR.T_VEC) => letPRIM("dotVar", IR.T_FLOAT, IR.DOT, [e1var, e2var], k)
633 :     | _ => raise Fail ("Type mismatch to DOT expression")
634 :     (* end case *))
635 :     end))
636 :    
637 : pavelk 1109 | P.CROSS (e1, e2) =>
638 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
639 :     trExpr(e2, env, state, fn e2var =>
640 :     let
641 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
642 :     val IR.V{varType=vt2, ...} = e2var
643 : pavelk 1108 in
644 :     (case (vt1, vt2)
645 :     of (IR.T_VEC, IR.T_VEC) => letPRIM("crossVar", IR.T_VEC, IR.CROSS, [e1var, e2var], k)
646 :     | _ => raise Fail ("Type mismatch to CROSS expression")
647 :     (* end case *))
648 :     end))
649 :    
650 : pavelk 1109 | P.NORMALIZE 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("normVar", IR.T_VEC, IR.NORM, [evar], k)
657 :     | _ => raise Fail ("Type mismatch to NORMALIZE expression")
658 :     (* end case *))
659 :     end)
660 :    
661 : pavelk 1109 | P.LENGTH e =>
662 : pavelk 1108 trExpr(e, env, state, fn evar =>
663 :     let
664 : pavelk 1109 val IR.V{varType, ...} = evar
665 : pavelk 1108 in
666 :     (case varType
667 :     of IR.T_VEC => letPRIM("lenVar", IR.T_VEC, IR.LEN, [evar], k)
668 :     | _ => raise Fail ("Type mismatch to LENGTH expression")
669 :     (* end case *))
670 :     end)
671 :    
672 :     (* !SPEED! We're assuming that there is an intersection here... *)
673 : pavelk 1109 | P.INTERSECT {p1, p2, d} =>
674 : pavelk 1108 trExpr(p1, env, state, fn p1var =>
675 :     trExpr(p2, env, state, fn p2var =>
676 :     let
677 : pavelk 1109 val IR.V{varType=vt1, ...} = p1var
678 :     val IR.V{varType=vt2, ...} = p2var
679 : pavelk 1108 in
680 :     (case (vt1, vt2)
681 :     of (IR.T_VEC, IR.T_VEC) => mkIntPt(env, p1var, p2var, d, k)
682 :     | _ => raise Fail("Type mismatch to INTERSECT expression")
683 :     (* end case *))
684 :     end))
685 :    
686 : pavelk 1109 | P.NORMALTO (e, d) =>
687 : pavelk 1108 trExpr(e, env, state, fn evar =>
688 :     let
689 : pavelk 1109 val IR.V{varType, ...} = evar
690 : pavelk 1108 fun cont s = k s
691 :     in
692 :     (case varType
693 : pavelk 1109 of IR.T_VEC => normAtPoint("normVar", d, env, evar, state, fn var => fn state' => k var)
694 : pavelk 1108 | _ => raise Fail("Type mismatch to NORMALTO expression")
695 :     (* end case *))
696 :     end)
697 :    
698 :     (* end case expr *))
699 : pavelk 1109
700 :     (* generate code to produce a random particle state from a domain *)
701 :     fun newParticle (sv_gens, env, state, k : particle_state -> IR.stmt) = let
702 :    
703 :     fun createVar(P.GEN{var, ...}) = let
704 :     val P.PSV.SV{name, ty, ...} = var
705 :     in
706 :     IR.newLocal("ps_" ^ name, IR.psvTyToIRTy ty, (IR.RAND, []))
707 :     end
708 :    
709 :     val newState = List.map createVar sv_gens
710 :    
711 :     fun genVar((sv_gen, var), cont) = let
712 :     val P.GEN{exp, ...} = sv_gen
713 :     val IR.V{varType, ...} = var
714 :     in
715 : pavelk 1110 (* This is kind of a hack, but it'll get optimized out. *)
716 : pavelk 1109 trExpr(exp, env, state, fn newVal => IR.mkPRIM(var, IR.COPY, [newVal], cont))
717 :     end (* genVar *)
718 :    
719 :     in
720 :     List.foldr (fn (x, y) => genVar(x, y)) (k newState) (ListPair.zipEq (sv_gens, newState))
721 :     end (* new particle *)
722 : pavelk 1108
723 :     fun trEmitter(emit, env, state, k : particle_state -> IR.stmt) = let
724 :     val P.EMIT{freq, sv_gens} = emit
725 : pavelk 1109 val blk = newBlock (env, state, k)
726 :     val ttl = findIRVarByName(state, "ttl")
727 : pavelk 1108 in
728 :     letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>
729 : pavelk 770 IR.mkIF(isDead,
730 :     (* then *)
731 : pavelk 1109 trExpr(freq, env, state, fn t1 =>
732 : pavelk 903 letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
733 : pavelk 770 letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
734 :     letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
735 :     letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
736 :     IR.mkIF(t3,
737 :     (* then *)
738 : pavelk 1109 newParticle (sv_gens, env, state, fn state' => retState state'),
739 : pavelk 770 (* else *)
740 :     IR.DISCARD)))))),
741 :     (* else *)
742 : pavelk 915 retState state))
743 : pavelk 770 end
744 : pavelk 1120
745 :     (* trExpr(expr, env, state, k : IR.var -> IR.stmt) *)
746 :     (* mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) *)
747 :     fun trPred(cond, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
748 :     fun grabVar(cond, env, state, k : IR.var -> IR.stmt) = (case cond
749 :     of P.WITHINF(d, expr) =>
750 :     trExpr(expr, env, state, fn checkMe =>
751 :     mkFloatWithinVar("wv", env, checkMe, d, k))
752 :    
753 :     | P.WITHIN3F(d, expr) =>
754 :     trExpr(expr, env, state, fn checkMe =>
755 :     mkVecWithinVar("wv", env, checkMe, d, k))
756 :    
757 :     | P.DO_INTERSECT {p1, p2, d} =>
758 :     trExpr(p1, env, state, fn p1var =>
759 :     trExpr(p2, env, state, fn p2var =>
760 :     mkIntBool(env, p1var, p2var, d, k)))
761 :    
762 :     | P.GTHAN (e1, e2) =>
763 :     trExpr(e1, env, state, fn e1var =>
764 :     trExpr(e2, env, state, fn e2var =>
765 :     letPRIM("gtVar", IR.T_BOOL, IR.GT, [e1var, e2var], k)))
766 : pavelk 1129
767 : pavelk 1120 | P.AND(c1, c2) =>
768 :     grabVar(c1, env, state, fn c1Var =>
769 :     grabVar(c2, env, state, fn c2Var =>
770 :     letPRIM("andVar", IR.T_BOOL, IR.AND, [c1Var, c2Var], k)))
771 :    
772 :     | P.OR(c1, c2) =>
773 :     grabVar(c1, env, state, fn c1Var =>
774 :     grabVar(c2, env, state, fn c2Var =>
775 :     letPRIM("andVar", IR.T_BOOL, IR.OR, [c1Var, c2Var], k)))
776 :    
777 :     | P.XOR(c1, c2) =>
778 :     grabVar(c1, env, state, fn c1Var =>
779 :     grabVar(c2, env, state, fn c2Var =>
780 :     mkXOR ("xorVar", c1Var, c2Var, k)))
781 :    
782 :     | P.NOT(c) =>
783 :     grabVar(c, env, state, fn cvar =>
784 :     letPRIM("notVar", IR.T_BOOL, IR.NOT, [cvar], k))
785 :    
786 :     (* end case *))
787 :     in
788 :     grabVar(cond, env, state, fn result =>
789 :     IR.mkIF(result, thenk(state), elsek(state)))
790 :     end
791 :    
792 : pavelk 868 fun compile (P.PG{
793 : pavelk 1107 emit as P.EMIT{freq, sv_gens}, act, render,
794 :     vars, state_vars, render_vars
795 : pavelk 868 }) = let
796 : pavelk 1107 val blks = ref[]
797 : pavelk 1122
798 :     val demand = IR.getDemand(render)
799 :     fun getIRNameForSV (v as PSV.SV{name, ...}) =
800 :     (case (PSV.SVMap.find (render_vars, v))
801 :     of SOME na => let
802 :     fun inDemand n = List.exists (fn x => #1 x = "ps_" ^ n) demand
803 :     in
804 :     (* Sanity check *)
805 :     if not (inDemand na) then
806 :     raise Fail (String.concat["Variable with name ", name," marked for rendering but not in demand."])
807 :     else
808 :     "ps_" ^ na
809 :     end
810 :     | NONE => "ps_" ^ name
811 :     (* end case *))
812 :    
813 :     fun convertToIR (v as PSV.SV{ty, ...}) = IR.newParam(getIRNameForSV v, IR.psvTyToIRTy ty)
814 : pavelk 1107 val env = let
815 : pavelk 746 (* add special globals to free vars *)
816 : pavelk 1107 val pgm_vars = PSV.Set.union(PSV.Set.singleton epsilon, vars)
817 :     fun insv (x as PSV.V{name, ty, binding, id, ...}, map) = let
818 :     val x' = (case (ty, !binding)
819 :     of (PSV.T_BOOL, PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
820 :     | (PSV.T_BOOL, PSV.BOOL boolVal) => IR.newConst(name, IR.C_BOOL(boolVal))
821 :     | (PSV.T_INT, PSV.UNDEF) => IR.newGlobal(x, IR.T_INT)
822 :     | (PSV.T_INT, PSV.INT intVal) => IR.newConst(name, IR.C_INT(intVal))
823 :     | (PSV.T_FLOAT, PSV.UNDEF) => IR.newGlobal(x, IR.T_FLOAT)
824 :     | (PSV.T_FLOAT, PSV.FLOAT floatVal) => IR.newConst(name, IR.C_FLOAT(floatVal))
825 :     | (PSV.T_VEC3F, PSV.UNDEF) => IR.newGlobal(x, IR.T_VEC)
826 :     | (PSV.T_VEC3F, PSV.VEC3F vecVal) => IR.newConst(name, IR.C_VEC(vecVal))
827 :     | _ => raise Fail("Error in setup, type mismatch between PSV vars and their binding.")
828 : pavelk 746 (* end case *))
829 : pavelk 1107 in
830 : pavelk 746 PSV.Map.insert (map, x, x')
831 : pavelk 1107 end (* ins *)
832 : pavelk 746 in
833 : pavelk 1119 TE( blks, PSV.Set.foldl insv PSV.Map.empty pgm_vars )
834 : pavelk 1107 end (* env *)
835 :    
836 : pavelk 1120 fun evalActs theAct state f = (case theAct
837 : pavelk 867 of P.SEQ(acts) => (case acts
838 : pavelk 1120 of [] => f state
839 :     | oneAct :: rest => evalActs oneAct state (fn state' => (evalActs (P.SEQ(rest)) state' f))
840 :     (* end case *))
841 :    
842 :     | P.PRED(cond, thenAct, elseAct) =>
843 :     trPred(cond, env, state,
844 :     fn state' => evalActs thenAct state' f,
845 :     fn state' => evalActs elseAct state' f
846 :     )
847 :    
848 :     | P.DIE => IR.DISCARD
849 :    
850 :     | P.ASSIGN(sv, expr) => let
851 : pavelk 1122 val PSV.SV{ty, ...} = sv
852 : pavelk 1120 fun replaceStateVar (var, []) = [var]
853 :     | replaceStateVar (var, nv :: svars) = let
854 :     val IR.V{name=nvname, ...} = nv
855 :     val IR.V{name=varname, ...} = var
856 :     in
857 :     if nvname = varname then
858 :     var :: svars
859 :     else
860 :     nv :: replaceStateVar(var, svars)
861 :     end
862 :     in
863 :     trExpr(expr, env, state, fn newVar =>
864 : pavelk 1122 letPRIM(getIRNameForSV sv, IR.psvTyToIRTy ty, IR.COPY, [newVar],
865 : pavelk 1120 fn thisVar => f (replaceStateVar(thisVar, state))))
866 :     end
867 :    
868 :     (* end case *))
869 : pavelk 868
870 :     (* The entry block is the first block of the program, or in other words, the emitter. *)
871 :     val entryBlock = newBlock (
872 : pavelk 1091 env,
873 : pavelk 1120 List.map convertToIR (PSV.SVSet.listItems state_vars),
874 : pavelk 868 fn pstate => trEmitter(
875 :     emit,
876 :     env,
877 :     pstate,
878 : pavelk 1120 fn state => evalActs act state retState
879 : pavelk 868 )
880 :     )
881 :    
882 : pavelk 972 (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
883 :    
884 :     fun isGlobal(IR.V{scope, ...}) = (case scope
885 :     of IR.S_GLOBAL(v) => true
886 :     | _ => false
887 :     (* end case *))
888 :    
889 :     fun extractVarMap(TE(blks, map)) = map
890 :    
891 : pavelk 1122 fun convertDemand (name, x) = ("ps_" ^ name, x)
892 :    
893 : pavelk 866 val outPgm = PSysIR.PGM {
894 : pavelk 1107 globals = PSV.Map.filter isGlobal (extractVarMap env),
895 : pavelk 1122 persistents = List.map convertDemand demand,
896 : pavelk 1107 uveOptimized = false,
897 : pavelk 972 emitter = entryBlock,
898 : pavelk 906 physics = List.nth(!blks, 1),
899 : pavelk 866 render = render
900 :     }
901 : pavelk 868
902 : pavelk 906 val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
903 : pavelk 868
904 : pavelk 746 in
905 : pavelk 905 (* IR.outputPgm(TextIO.stdErr, outPgm); *)
906 : pavelk 1107
907 :     (* Note: it only succeeds if we can optimize, too *)
908 :     if Checker.checkIR(optimized) then printErr "Compilation succeeded." else ();
909 :    
910 :     optimized
911 : pavelk 746 end (* compile *)
912 :    
913 :     end (* Translate *)

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