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

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