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 1133 - (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 : pavelk 1132 | _ => raise Fail "Unable to create random float for specified distribution"
123 : pavelk 1017 (* 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 : pavelk 1132 | _ => raise Fail ("Cannot generate float in specified domain: " ^ (P.dToStr domain))
137 : pavelk 1017 (* 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 : pavelk 1132 | _ => raise Fail ("Cannot generate point in specified domain: " ^ (P.dToStr domain))
328 : pavelk 746 (* end case *))
329 :    
330 :     (* This function takes an IR boolean, its environment, a particle state, domain,
331 :     * and continuation.
332 :     *
333 :     * We set the boolean to whether or not the current particle given by the particle
334 :     * state is within the domain, and then pass the continuation on.
335 :     *)
336 : pavelk 1120 fun mkVecWithinVar (boolVar, env, var, d : Vec3f.vec3 P.domain, stmt : IR.var -> IR.stmt) = let
337 : pavelk 770 val pos = var
338 : pavelk 746 in
339 :     case d
340 :     of P.D_POINT(pt) =>
341 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn subVec =>
342 :     letPRIM("vecLen", IR.T_FLOAT, IR.LEN, [subVec], fn vecLen =>
343 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), vecLen], stmt)))
344 :    
345 :     (* Take the vectors going from our position to pt1, and pt2. Then
346 :     * after we normalize them, if their dot product is equal to -1, then
347 :     * they are pointing in opposite directions meaning that the position
348 :     * is inbetween pt1 and pt2 as desired.
349 :     *)
350 :     | P.D_LINE{pt1, pt2} =>
351 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn posToPt1 =>
352 :     letPRIM("posToPt1Norm", IR.T_VEC, IR.NORM, [posToPt1], fn posToPt1Norm =>
353 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), pos], fn posToPt2 =>
354 :     letPRIM("posToPt2Norm", IR.T_VEC, IR.NORM, [posToPt2], fn posToPt2Norm =>
355 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt2, posToPt1], fn dotProd =>
356 :     letPRIM("testMe", IR.T_FLOAT, IR.SUB, [dotProd, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn testVal =>
357 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), testVal], stmt)))))))
358 :    
359 :     (* Just see whether or not the dot product between the normal
360 :     * and the vector from a point on the plane to our position is
361 :     * greater than zero. Essentially, we're "within" a plane if we're
362 :     * behind it (with respect to the normal)
363 :     *)
364 :     | P.D_PLANE{pt, normal} =>
365 : pavelk 905 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
366 : pavelk 746 letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
367 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))
368 :    
369 :     (* Similar to checking to see whether or not we're within a plane,
370 :     * here all we have to do is see how far we are from the center
371 :     * of the disc (pt), and then see whther or not we're perpendicular to
372 :     * the normal, and that our distance is greater than irad but less than
373 :     * orad.
374 :     *)
375 :     | P.D_DISC{pt, normal, orad, irad} =>
376 : pavelk 1133 letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
377 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
378 :     letPRIM("inDisc", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.01), dotProd], fn inDisc =>
379 :    
380 :     letPRIM("parPosToP", IR.T_VEC, IR.SCALE, [dotProd, psvToIRVar(env, normal)], fn posToPtParallelToNormal =>
381 :     letPRIM("perpPosToP", IR.T_VEC, IR.SUB_VEC, [posToPt, posToPtParallelToNormal], fn posToPtPerpToNormal =>
382 :     letPRIM("inDiscLen", IR.T_FLOAT, IR.LEN, [posToPtPerpToNormal], fn posToPtLen =>
383 :    
384 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToPtLen], fn inOradGt =>
385 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), posToPtLen], fn inOradEq =>
386 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
387 :    
388 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [posToPtLen, psvToIRVar(env, irad)], fn inIradGt =>
389 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [posToPtLen, psvToIRVar(env, irad)], fn inIradEq =>
390 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
391 :    
392 :     letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
393 :    
394 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inDisc, inBothRad], stmt))))))))))))))
395 : pavelk 987
396 : pavelk 746 (* Simply see whether or not the distance from the center is within the
397 :     * specified bounds.
398 :     *)
399 :     | P.D_SPHERE{center, orad, irad} =>
400 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, center), pos], fn posToC =>
401 :     letPRIM("posToPtLen", IR.T_VEC, IR.LEN, [posToC], fn posToCLen =>
402 :     letPRIM("inOrad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToCLen], fn inOrad =>
403 :     letPRIM("inIrad", IR.T_BOOL, IR.GT, [posToCLen, psvToIRVar(env, irad)], fn inIrad =>
404 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inIrad, inOrad], stmt)))))
405 : pavelk 1060
406 :     | P.D_CYLINDER {pt1, pt2, irad, orad} =>
407 :    
408 :     (* !FIXME! Right now, we see whether or not the point is within the two planes defined
409 :     * by the endpoints of the cylinder, and then testing to see whether or not the smallest
410 :     * distance to the line segment falls within the radii. It might be faster to find the
411 :     * closest point to the line defined by the endpoints and then see whether or not the point
412 :     * is within the segment.
413 :     *)
414 :    
415 :     (* Is it in one plane *)
416 :     letPRIM("plane1Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn plane1Norm =>
417 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt1)], fn posToPt1 =>
418 :     letPRIM("dot1", IR.T_FLOAT, IR.DOT, [posToPt1, plane1Norm], fn dot1Prod =>
419 :     letPRIM("inPlane1", IR.T_BOOL, IR.GT, [dot1Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane1=>
420 :    
421 :     (* Is it in another plane *)
422 :     letPRIM("plane2Norm", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), psvToIRVar(env, pt2)], fn plane2Norm =>
423 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt2)], fn posToPt2 =>
424 :     letPRIM("dot2", IR.T_FLOAT, IR.DOT, [posToPt2, plane2Norm], fn dot2Prod =>
425 :     letPRIM("inPlane2", IR.T_BOOL, IR.GT, [dot2Prod, IR.newConst("zero", IR.C_FLOAT 0.0)], fn inPlane2=>
426 :    
427 :     (* Is it in both planes? *)
428 :     letPRIM("inPlanes", IR.T_BOOL, IR.AND, [inPlane1, inPlane2], fn inPlanes =>
429 :    
430 :     (* Find distance from segment *)
431 :     letPRIM("a", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn a =>
432 :     letPRIM("b", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn b =>
433 :     letPRIM("alen", IR.T_FLOAT, IR.LEN, [a], fn alen =>
434 :     letPRIM("axb", IR.T_VEC, IR.CROSS, [a, b], fn axb =>
435 :     letPRIM("axblen", IR.T_FLOAT, IR.LEN, [axb], fn axblen =>
436 :     letPRIM("dist", IR.T_FLOAT, IR.DIV, [axblen, alen], fn dist =>
437 :    
438 :     (* Is distance in both radii? *)
439 :     letPRIM("inOradGt", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), dist], fn inOradGt =>
440 :     letPRIM("inOradEq", IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, orad), dist], fn inOradEq =>
441 :     letPRIM("inOrad", IR.T_BOOL, IR.OR, [inOradGt, inOradEq], fn inOrad =>
442 :    
443 :     letPRIM("inIradGt", IR.T_BOOL, IR.GT, [dist, psvToIRVar(env, irad)], fn inIradGt =>
444 :     letPRIM("inIradEq", IR.T_BOOL, IR.EQUALS, [dist, psvToIRVar(env, irad)], fn inIradEq =>
445 :     letPRIM("inIrad", IR.T_BOOL, IR.OR, [inIradGt, inIradEq], fn inIrad =>
446 :    
447 :     letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
448 :    
449 :     (* It's in the cylinder (tube) if it's within both radii and in both planes... *)
450 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inPlanes, inBothRad], stmt)
451 :     ))))))))))))))))))))))
452 : pavelk 746 (*
453 :     | P.D_TRIANGLE {pt1: vec3f var, pt2: vec3f var, pt3: vec3f var}
454 :     | P.D_PLANE {pt: vec3f var, normal: vec3f var}
455 :     | P.D_RECT {pt: vec3f var, htvec: vec3f var, wdvec: vec3f var}
456 :     | P.D_BOX {min: vec3f var, max: vec3f var}
457 :     | P.D_SPHERE {center: vec3f var, irad: vec3f var, orad: vec3f var}
458 :     | P.D_CONE {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
459 :     | P.D_BLOB {center: vec3f var, stddev: float var}
460 :     | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
461 :     *)
462 : pavelk 1132 | _ => raise Fail ("Cannot determine within-ness for specified vec3 domain: " ^ (P.dToStr d))
463 : pavelk 746 (* end case *)
464 :     end (*end let *)
465 : pavelk 1120
466 :     fun mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) = (case d
467 :     of P.D_POINT(pt) => letPRIM(boolVar, IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, pt), var], stmt)
468 :     | P.D_BOX {min, max} =>
469 :     letPRIM("bigMin", IR.T_BOOL, IR.GT, [var, psvToIRVar(env, min)], fn bigMin =>
470 :     letPRIM("smallMax", IR.T_BOOL, IR.GT, [psvToIRVar(env, max), var], fn smallMax =>
471 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [bigMin, smallMax], stmt)))
472 : pavelk 1132 | _ => raise Fail ("Cannot determine within-ness for specified float domain: " ^ (P.dToStr d))
473 : pavelk 1120 (* end case *))
474 : pavelk 746
475 : pavelk 1132 fun mkIntBool(env, p1var, p2var, d : Vec3f.vec3 P.domain, state, k : IR.var -> particle_state -> IR.stmt) = let
476 : pavelk 1108 val _ = ()
477 :     in
478 :     (case d
479 :     of P.D_POINT(pt) =>
480 :    
481 :     (* Get vectors *)
482 :     letPRIM("p1ToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar (env, pt), p1var], fn p1ToPt =>
483 :     letPRIM("p2ToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar (env, pt), p2var], fn p2ToPt =>
484 :     letPRIM("p1ToP2", IR.T_VEC, IR.SUB_VEC, [p2var, p1var], fn p1ToP2 =>
485 :    
486 :     (* Get distances *)
487 :     letPRIM("p1ToPtLen", IR.T_FLOAT, IR.LEN, [p1ToPt], fn p1ToPtLen =>
488 :     letPRIM("p2ToPtLen", IR.T_FLOAT, IR.LEN, [p2ToPt], fn p2ToPtLen =>
489 :     letPRIM("p1ToP2Len", IR.T_FLOAT, IR.LEN, [p1ToP2], fn p1ToP2Len =>
490 :    
491 :     (* Add & subtract ... *)
492 :     letPRIM("distSum", IR.T_FLOAT, IR.ADD, [p1ToPtLen, p2ToPtLen], fn distSum =>
493 :     letPRIM("distDiff", IR.T_FLOAT, IR.SUB, [distSum, p1ToP2Len], fn distDiff =>
494 :     letPRIM("distDiffAbs", IR.T_FLOAT, IR.ABS, [distDiff], fn distDiffAbs =>
495 :    
496 :     (* Do the boolean stuff... *)
497 : pavelk 1132 letPRIM("intersect", IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), distDiffAbs], fn intVar => k intVar state)
498 : pavelk 1108
499 :     )))
500 :     )))
501 :     )))
502 : pavelk 1132
503 :     | P.D_PLANE {pt, normal} =>
504 :     letPRIM("d", IR.T_FLOAT, IR.DOT, [psvToIRVar(env, pt), psvToIRVar(env, normal)], fn d =>
505 :     letPRIM("p1d", IR.T_FLOAT, IR.DOT, [p1var, psvToIRVar(env, normal)], fn p1d =>
506 :     letPRIM("p2d", IR.T_FLOAT, IR.DOT, [p2var, psvToIRVar(env, normal)], fn p2d =>
507 :     letPRIM("p1dist", IR.T_FLOAT, IR.SUB, [d, p1d], fn p1dist =>
508 :     letPRIM("p2dist", IR.T_FLOAT, IR.SUB, [d, p2d], fn p2dist =>
509 :     letPRIM("distProd", IR.T_FLOAT, IR.MULT, [p1dist, p2dist], fn distProd =>
510 : pavelk 1133 letPRIM("intersect", IR.T_BOOL, IR.GT, [IR.newConst("zero", IR.C_FLOAT 0.0), distProd], fn intVar => k intVar state)
511 : pavelk 1132 ))))))
512 : pavelk 1108
513 : pavelk 1132 | P.D_DISC {pt, normal, orad, irad} => let
514 :     val boolVar = IR.newLocal("intersect", IR.T_BOOL, (IR.RAND, []))
515 :     val newBlk = newBlockWithArgs(env, state, [boolVar], (k boolVar))
516 :     in
517 :     letPRIM("d", IR.T_FLOAT, IR.DOT, [psvToIRVar(env, pt), psvToIRVar(env, normal)], fn d =>
518 :     letPRIM("p1d", IR.T_FLOAT, IR.DOT, [p1var, psvToIRVar(env, normal)], fn p1d =>
519 :    
520 :     (* Early out... does it intersect the plane?
521 :     *
522 :     * !SPEED! Due to the perceived slowness of branching on
523 :     * GPUs, this might not actually be faster on all runtime environments *)
524 :    
525 :     letPRIM("p2d", IR.T_FLOAT, IR.DOT, [p2var, psvToIRVar(env, normal)], fn p2d =>
526 :     letPRIM("p1dist", IR.T_FLOAT, IR.SUB, [d, p1d], fn p1dist =>
527 :     letPRIM("p2dist", IR.T_FLOAT, IR.SUB, [d, p2d], fn p2dist =>
528 :     letPRIM("distProd", IR.T_FLOAT, IR.MULT, [p1dist, p2dist], fn distProd =>
529 :     letPRIM("earlyOut", IR.T_BOOL, IR.GT, [distProd, IR.newConst("zero", IR.C_FLOAT 0.0)], fn earlyOut =>
530 :     IR.mkIF(earlyOut,
531 :     (* then *)
532 :     IR.mkPRIM(boolVar, IR.NOT, [earlyOut], gotoWithArgs(state, [boolVar], newBlk)),
533 :     (* else *)
534 :     letPRIM("v", IR.T_VEC, IR.SUB_VEC, [p2var, p1var], fn v =>
535 :     letPRIM("vDotn", IR.T_FLOAT, IR.DOT, [v, psvToIRVar(env, normal)], fn vdn =>
536 :     letPRIM("t", IR.T_FLOAT, IR.DIV, [p1dist, vdn], fn t =>
537 :    
538 :     (* !TODO! Add some sort of assert mechanism to make sure that t is
539 :     * in the interval [0, 1]... *)
540 :     letPRIM("vscale", IR.T_VEC, IR.SCALE, [t, v], fn vscale =>
541 :     letPRIM("ppt", IR.T_VEC, IR.ADD_VEC, [p1var, vscale], fn ppt =>
542 :     letPRIM("lenVec", IR.T_VEC, IR.SUB_VEC, [ppt, psvToIRVar(env, pt)], fn cv =>
543 :     letPRIM("len", IR.T_FLOAT, IR.LEN, [cv], fn len =>
544 :    
545 :     (* Check to see whether or not it's within the radius... *)
546 :     letPRIM("gtirad", IR.T_BOOL, IR.GT, [len, psvToIRVar(env, irad)], fn gtirad =>
547 :     letPRIM("ltorad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), len], fn ltorad =>
548 :     IR.mkPRIM(boolVar, IR.AND, [gtirad, ltorad], gotoWithArgs(state, [boolVar], newBlk))
549 :     ))))))))))
550 :     )))))))
551 :     end (* P.D_DISC *)
552 :    
553 :     | _ => raise Fail ("Cannot calculate intersection bool for specified domain: " ^ (P.dToStr d))
554 : pavelk 1108 (* end case *))
555 :    
556 :     end (* mkIntBool *)
557 :    
558 : pavelk 1132 (* We assume that the segment already intersects with the domain. *)
559 : pavelk 1109 fun mkIntPt(env, p1var, p2var, d : Vec3f.vec3 P.domain, k : IR.var -> IR.stmt) = let
560 : pavelk 1108 val _ = ()
561 :     in
562 :     (case d
563 : pavelk 1109 of P.D_POINT(pt) => k (psvToIRVar (env, pt))
564 : pavelk 1132
565 :     | P.D_PLANE {pt, normal} =>
566 :     letPRIM("d", IR.T_FLOAT, IR.DOT, [psvToIRVar(env, pt), psvToIRVar(env, normal)], fn d =>
567 :     letPRIM("p1d", IR.T_FLOAT, IR.DOT, [p1var, psvToIRVar(env, normal)], fn p1d =>
568 :     letPRIM("num", IR.T_FLOAT, IR.SUB, [d, p1d], fn num =>
569 :     letPRIM("v", IR.T_VEC, IR.SUB_VEC, [p2var, p1var], fn v =>
570 :     letPRIM("den", IR.T_FLOAT, IR.DOT, [v, psvToIRVar(env, normal)], fn den =>
571 :     letPRIM("t", IR.T_FLOAT, IR.DIV, [num, den], fn t =>
572 :     letPRIM("vsc", IR.T_VEC, IR.SCALE, [t, v], fn vs =>
573 :     letPRIM("intPt", IR.T_VEC, IR.ADD_VEC, [p1var, vs], k)
574 :     )))))))
575 :    
576 :     (* Since we already know they intersect, the intersection point must be
577 :     * just the point that's on the plane... *)
578 :     | P.D_DISC {pt, normal, orad, irad} => mkIntPt(env, p1var, p2var, P.D_PLANE{pt = pt, normal = normal}, k)
579 :     | _ => raise Fail ("Cannot calculate intersection point for specified domain: " ^ (P.dToStr d))
580 : pavelk 1108 (* end case *))
581 :     end (* mkIntPt *)
582 :    
583 : pavelk 746 (* Find the normal at the given position of the particle for the specified
584 :     * domain. Note, that the particle doesn't necessarily need to be on the
585 : pavelk 1132 * domain, but if it's not then the behavior is undefined. *)
586 : pavelk 1109 fun normAtPoint(retNorm, d, env, pos, state, k : IR.var -> particle_state -> IR.stmt) = let
587 : pavelk 746 val newNorm = IR.newParam("n", IR.T_VEC)
588 : pavelk 1109 val nextBlk = newBlockWithArgs(env, state, [newNorm], k(newNorm))
589 : pavelk 746 in
590 :     (case d
591 :     of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
592 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
593 :     | P.D_DISC{pt, normal, irad, orad} =>
594 : pavelk 1120 mkVecWithinVar("inP", env, pos, d, fn inPlane =>
595 : pavelk 746 IR.mkIF(inPlane,
596 :     (* then *)
597 :     letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
598 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk)),
599 :     (* else *)
600 :     letPRIM(retNorm,
601 :     IR.T_VEC,
602 :     IR.SCALE,
603 :     [IR.newConst("negOne", IR.C_FLOAT ~1.0), psvToIRVar(env, normal)],
604 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
605 :     )
606 :     )
607 :    
608 : pavelk 1109 | P.D_SPHERE{center, irad, orad} =>
609 : pavelk 746 letPRIM("sv", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, center)], fn subVec =>
610 :     letPRIM(retNorm, IR.T_VEC, IR.NORM, [subVec], fn newNormVar => k newNormVar state
611 : pavelk 1109 ))
612 : pavelk 746
613 : pavelk 1132 | _ => raise Fail("Cannot find normal to point of specified domain." ^ (P.dToStr d))
614 : pavelk 746 (* end case *))
615 :     end
616 : pavelk 769
617 : pavelk 1108 fun trExpr(expr, env, state, k : IR.var -> IR.stmt) = (case expr
618 : pavelk 1109 of P.CONSTF f => k (IR.newConst ("c", IR.C_FLOAT f))
619 : pavelk 1108
620 : pavelk 1109 | P.CONST3F v => k (IR.newConst ("c", IR.C_VEC v))
621 : pavelk 1108
622 : pavelk 1109 | P.VAR v => k (psvToIRVar (env, v))
623 : pavelk 1108
624 : pavelk 1109 | P.STATE_VAR sv => k (getIRVarForSV (sv, state))
625 : pavelk 1108
626 : pavelk 1109 | P.GENERATE3F (dom, dist) => genVecVar("genVec", env, dom, dist, k)
627 : pavelk 1108
628 : pavelk 1109 | P.GENERATEF (dom, dist) => genFloatVar("genFlt", env, dom, dist, k)
629 : pavelk 1108
630 : pavelk 1109 | P.ADD(e1, e2) =>
631 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
632 :     trExpr(e2, env, state, fn e2var =>
633 :     let
634 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
635 :     val IR.V{varType=vt2, ...} = e2var
636 : pavelk 1108 in
637 :     (case (vt1, vt2)
638 :     of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("addVar", IR.T_FLOAT, IR.ADD, [e1var, e2var], k)
639 :     | (IR.T_VEC, IR.T_VEC) => letPRIM("addVar", IR.T_VEC, IR.ADD_VEC, [e1var, e2var], k)
640 :     | _ => raise Fail ("Type mismatch to ADD expression")
641 :     (* end case *))
642 :     end))
643 :    
644 : pavelk 1109 | P.SCALE (e1, e2) =>
645 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
646 :     trExpr(e2, env, state, fn e2var =>
647 :     let
648 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
649 :     val IR.V{varType=vt2, ...} = e2var
650 : pavelk 1108 in
651 :     (case (vt1, vt2)
652 :     of (IR.T_FLOAT, IR.T_VEC) => letPRIM("scaleVar", IR.T_VEC, IR.SCALE, [e1var, e2var], k)
653 : pavelk 1130 | (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("scaleVar", IR.T_FLOAT, IR.MULT, [e1var, e2var], k)
654 :     | _ => raise Fail (String.concat["Type mismatch to SCALE expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])
655 : pavelk 1108 (* end case *))
656 :     end))
657 :    
658 : pavelk 1109 | P.DIV (e1, e2) =>
659 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
660 :     trExpr(e2, env, state, fn e2var =>
661 :     let
662 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
663 :     val IR.V{varType=vt2, ...} = e2var
664 : pavelk 1108 in
665 :     (case (vt1, vt2)
666 :     of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("divVar", IR.T_FLOAT, IR.DIV, [e1var, e2var], k)
667 : pavelk 1133 | _ => raise Fail (String.concat["Type mismatch to DIV expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])
668 : pavelk 1108 (* end case *))
669 :     end))
670 : pavelk 866
671 : pavelk 1109 | P.NEG e =>
672 : pavelk 1108 trExpr(e, env, state, fn evar =>
673 :     let
674 : pavelk 1109 val IR.V{varType, ...} = evar
675 : pavelk 1108 in
676 :     (case varType
677 : pavelk 1109 of IR.T_FLOAT => letPRIM("negVar", IR.T_FLOAT, IR.MULT, [evar, IR.newConst("negOne", IR.C_FLOAT ~1.0)], k)
678 : pavelk 1108 | IR.T_VEC => letPRIM("negVar", IR.T_VEC, IR.NEG_VEC, [evar], k)
679 :     | _ => raise Fail ("Type mismatch to NEG expression")
680 :     (* end case *))
681 :     end)
682 :    
683 : pavelk 1109 | P.DOT (e1, e2) =>
684 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
685 :     trExpr(e2, env, state, fn e2var =>
686 :     let
687 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
688 :     val IR.V{varType=vt2, ...} = e2var
689 : pavelk 1108 in
690 :     (case (vt1, vt2)
691 :     of (IR.T_VEC, IR.T_VEC) => letPRIM("dotVar", IR.T_FLOAT, IR.DOT, [e1var, e2var], k)
692 :     | _ => raise Fail ("Type mismatch to DOT expression")
693 :     (* end case *))
694 :     end))
695 :    
696 : pavelk 1109 | P.CROSS (e1, e2) =>
697 : pavelk 1108 trExpr(e1, env, state, fn e1var =>
698 :     trExpr(e2, env, state, fn e2var =>
699 :     let
700 : pavelk 1109 val IR.V{varType=vt1, ...} = e1var
701 :     val IR.V{varType=vt2, ...} = e2var
702 : pavelk 1108 in
703 :     (case (vt1, vt2)
704 :     of (IR.T_VEC, IR.T_VEC) => letPRIM("crossVar", IR.T_VEC, IR.CROSS, [e1var, e2var], k)
705 :     | _ => raise Fail ("Type mismatch to CROSS expression")
706 :     (* end case *))
707 :     end))
708 :    
709 : pavelk 1109 | P.NORMALIZE e =>
710 : pavelk 1108 trExpr(e, env, state, fn evar =>
711 :     let
712 : pavelk 1109 val IR.V{varType, ...} = evar
713 : pavelk 1108 in
714 :     (case varType
715 :     of IR.T_VEC => letPRIM("normVar", IR.T_VEC, IR.NORM, [evar], k)
716 :     | _ => raise Fail ("Type mismatch to NORMALIZE expression")
717 :     (* end case *))
718 :     end)
719 :    
720 : pavelk 1109 | P.LENGTH e =>
721 : pavelk 1108 trExpr(e, env, state, fn evar =>
722 :     let
723 : pavelk 1109 val IR.V{varType, ...} = evar
724 : pavelk 1108 in
725 :     (case varType
726 :     of IR.T_VEC => letPRIM("lenVar", IR.T_VEC, IR.LEN, [evar], k)
727 :     | _ => raise Fail ("Type mismatch to LENGTH expression")
728 :     (* end case *))
729 :     end)
730 :    
731 :     (* !SPEED! We're assuming that there is an intersection here... *)
732 : pavelk 1109 | P.INTERSECT {p1, p2, d} =>
733 : pavelk 1108 trExpr(p1, env, state, fn p1var =>
734 :     trExpr(p2, env, state, fn p2var =>
735 :     let
736 : pavelk 1109 val IR.V{varType=vt1, ...} = p1var
737 :     val IR.V{varType=vt2, ...} = p2var
738 : pavelk 1108 in
739 :     (case (vt1, vt2)
740 :     of (IR.T_VEC, IR.T_VEC) => mkIntPt(env, p1var, p2var, d, k)
741 :     | _ => raise Fail("Type mismatch to INTERSECT expression")
742 :     (* end case *))
743 :     end))
744 :    
745 : pavelk 1109 | P.NORMALTO (e, d) =>
746 : pavelk 1108 trExpr(e, env, state, fn evar =>
747 :     let
748 : pavelk 1109 val IR.V{varType, ...} = evar
749 : pavelk 1108 fun cont s = k s
750 :     in
751 :     (case varType
752 : pavelk 1109 of IR.T_VEC => normAtPoint("normVar", d, env, evar, state, fn var => fn state' => k var)
753 : pavelk 1108 | _ => raise Fail("Type mismatch to NORMALTO expression")
754 :     (* end case *))
755 :     end)
756 :    
757 :     (* end case expr *))
758 : pavelk 1109
759 :     (* generate code to produce a random particle state from a domain *)
760 :     fun newParticle (sv_gens, env, state, k : particle_state -> IR.stmt) = let
761 :    
762 :     fun createVar(P.GEN{var, ...}) = let
763 :     val P.PSV.SV{name, ty, ...} = var
764 :     in
765 :     IR.newLocal("ps_" ^ name, IR.psvTyToIRTy ty, (IR.RAND, []))
766 :     end
767 :    
768 :     val newState = List.map createVar sv_gens
769 :    
770 :     fun genVar((sv_gen, var), cont) = let
771 :     val P.GEN{exp, ...} = sv_gen
772 :     val IR.V{varType, ...} = var
773 :     in
774 : pavelk 1110 (* This is kind of a hack, but it'll get optimized out. *)
775 : pavelk 1109 trExpr(exp, env, state, fn newVal => IR.mkPRIM(var, IR.COPY, [newVal], cont))
776 :     end (* genVar *)
777 :    
778 :     in
779 :     List.foldr (fn (x, y) => genVar(x, y)) (k newState) (ListPair.zipEq (sv_gens, newState))
780 :     end (* new particle *)
781 : pavelk 1108
782 :     fun trEmitter(emit, env, state, k : particle_state -> IR.stmt) = let
783 :     val P.EMIT{freq, sv_gens} = emit
784 : pavelk 1109 val blk = newBlock (env, state, k)
785 :     val ttl = findIRVarByName(state, "ttl")
786 : pavelk 1108 in
787 :     letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>
788 : pavelk 770 IR.mkIF(isDead,
789 :     (* then *)
790 : pavelk 1109 trExpr(freq, env, state, fn t1 =>
791 : pavelk 903 letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
792 : pavelk 770 letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
793 :     letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
794 :     letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
795 :     IR.mkIF(t3,
796 :     (* then *)
797 : pavelk 1109 newParticle (sv_gens, env, state, fn state' => retState state'),
798 : pavelk 770 (* else *)
799 :     IR.DISCARD)))))),
800 :     (* else *)
801 : pavelk 915 retState state))
802 : pavelk 770 end
803 : pavelk 1120
804 :     (* trExpr(expr, env, state, k : IR.var -> IR.stmt) *)
805 :     (* mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) *)
806 :     fun trPred(cond, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
807 :     fun grabVar(cond, env, state, k : IR.var -> IR.stmt) = (case cond
808 :     of P.WITHINF(d, expr) =>
809 :     trExpr(expr, env, state, fn checkMe =>
810 :     mkFloatWithinVar("wv", env, checkMe, d, k))
811 :    
812 :     | P.WITHIN3F(d, expr) =>
813 :     trExpr(expr, env, state, fn checkMe =>
814 :     mkVecWithinVar("wv", env, checkMe, d, k))
815 :    
816 :     | P.DO_INTERSECT {p1, p2, d} =>
817 :     trExpr(p1, env, state, fn p1var =>
818 :     trExpr(p2, env, state, fn p2var =>
819 : pavelk 1132 mkIntBool(env, p1var, p2var, d, state, fn var => fn state' => k var)))
820 : pavelk 1120
821 :     | P.GTHAN (e1, e2) =>
822 :     trExpr(e1, env, state, fn e1var =>
823 :     trExpr(e2, env, state, fn e2var =>
824 :     letPRIM("gtVar", IR.T_BOOL, IR.GT, [e1var, e2var], k)))
825 : pavelk 1129
826 : pavelk 1120 | P.AND(c1, c2) =>
827 :     grabVar(c1, env, state, fn c1Var =>
828 :     grabVar(c2, env, state, fn c2Var =>
829 :     letPRIM("andVar", IR.T_BOOL, IR.AND, [c1Var, c2Var], k)))
830 :    
831 :     | P.OR(c1, c2) =>
832 :     grabVar(c1, env, state, fn c1Var =>
833 :     grabVar(c2, env, state, fn c2Var =>
834 :     letPRIM("andVar", IR.T_BOOL, IR.OR, [c1Var, c2Var], k)))
835 :    
836 :     | P.XOR(c1, c2) =>
837 :     grabVar(c1, env, state, fn c1Var =>
838 :     grabVar(c2, env, state, fn c2Var =>
839 :     mkXOR ("xorVar", c1Var, c2Var, k)))
840 :    
841 :     | P.NOT(c) =>
842 :     grabVar(c, env, state, fn cvar =>
843 :     letPRIM("notVar", IR.T_BOOL, IR.NOT, [cvar], k))
844 :    
845 :     (* end case *))
846 :     in
847 :     grabVar(cond, env, state, fn result =>
848 :     IR.mkIF(result, thenk(state), elsek(state)))
849 :     end
850 :    
851 : pavelk 868 fun compile (P.PG{
852 : pavelk 1107 emit as P.EMIT{freq, sv_gens}, act, render,
853 :     vars, state_vars, render_vars
854 : pavelk 868 }) = let
855 : pavelk 1107 val blks = ref[]
856 : pavelk 1122
857 :     val demand = IR.getDemand(render)
858 :     fun getIRNameForSV (v as PSV.SV{name, ...}) =
859 :     (case (PSV.SVMap.find (render_vars, v))
860 :     of SOME na => let
861 :     fun inDemand n = List.exists (fn x => #1 x = "ps_" ^ n) demand
862 :     in
863 :     (* Sanity check *)
864 :     if not (inDemand na) then
865 :     raise Fail (String.concat["Variable with name ", name," marked for rendering but not in demand."])
866 :     else
867 :     "ps_" ^ na
868 :     end
869 :     | NONE => "ps_" ^ name
870 :     (* end case *))
871 :    
872 :     fun convertToIR (v as PSV.SV{ty, ...}) = IR.newParam(getIRNameForSV v, IR.psvTyToIRTy ty)
873 : pavelk 1107 val env = let
874 : pavelk 746 (* add special globals to free vars *)
875 : pavelk 1107 val pgm_vars = PSV.Set.union(PSV.Set.singleton epsilon, vars)
876 :     fun insv (x as PSV.V{name, ty, binding, id, ...}, map) = let
877 :     val x' = (case (ty, !binding)
878 :     of (PSV.T_BOOL, PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
879 :     | (PSV.T_BOOL, PSV.BOOL boolVal) => IR.newConst(name, IR.C_BOOL(boolVal))
880 :     | (PSV.T_INT, PSV.UNDEF) => IR.newGlobal(x, IR.T_INT)
881 :     | (PSV.T_INT, PSV.INT intVal) => IR.newConst(name, IR.C_INT(intVal))
882 :     | (PSV.T_FLOAT, PSV.UNDEF) => IR.newGlobal(x, IR.T_FLOAT)
883 :     | (PSV.T_FLOAT, PSV.FLOAT floatVal) => IR.newConst(name, IR.C_FLOAT(floatVal))
884 :     | (PSV.T_VEC3F, PSV.UNDEF) => IR.newGlobal(x, IR.T_VEC)
885 :     | (PSV.T_VEC3F, PSV.VEC3F vecVal) => IR.newConst(name, IR.C_VEC(vecVal))
886 :     | _ => raise Fail("Error in setup, type mismatch between PSV vars and their binding.")
887 : pavelk 746 (* end case *))
888 : pavelk 1107 in
889 : pavelk 746 PSV.Map.insert (map, x, x')
890 : pavelk 1107 end (* ins *)
891 : pavelk 746 in
892 : pavelk 1119 TE( blks, PSV.Set.foldl insv PSV.Map.empty pgm_vars )
893 : pavelk 1107 end (* env *)
894 :    
895 : pavelk 1120 fun evalActs theAct state f = (case theAct
896 : pavelk 867 of P.SEQ(acts) => (case acts
897 : pavelk 1120 of [] => f state
898 :     | oneAct :: rest => evalActs oneAct state (fn state' => (evalActs (P.SEQ(rest)) state' f))
899 :     (* end case *))
900 :    
901 :     | P.PRED(cond, thenAct, elseAct) =>
902 :     trPred(cond, env, state,
903 :     fn state' => evalActs thenAct state' f,
904 :     fn state' => evalActs elseAct state' f
905 :     )
906 :    
907 :     | P.DIE => IR.DISCARD
908 :    
909 :     | P.ASSIGN(sv, expr) => let
910 : pavelk 1122 val PSV.SV{ty, ...} = sv
911 : pavelk 1120 fun replaceStateVar (var, []) = [var]
912 :     | replaceStateVar (var, nv :: svars) = let
913 :     val IR.V{name=nvname, ...} = nv
914 :     val IR.V{name=varname, ...} = var
915 :     in
916 :     if nvname = varname then
917 :     var :: svars
918 :     else
919 :     nv :: replaceStateVar(var, svars)
920 :     end
921 :     in
922 :     trExpr(expr, env, state, fn newVar =>
923 : pavelk 1122 letPRIM(getIRNameForSV sv, IR.psvTyToIRTy ty, IR.COPY, [newVar],
924 : pavelk 1120 fn thisVar => f (replaceStateVar(thisVar, state))))
925 :     end
926 :    
927 :     (* end case *))
928 : pavelk 868
929 :     (* The entry block is the first block of the program, or in other words, the emitter. *)
930 :     val entryBlock = newBlock (
931 : pavelk 1091 env,
932 : pavelk 1120 List.map convertToIR (PSV.SVSet.listItems state_vars),
933 : pavelk 868 fn pstate => trEmitter(
934 :     emit,
935 :     env,
936 :     pstate,
937 : pavelk 1120 fn state => evalActs act state retState
938 : pavelk 868 )
939 :     )
940 :    
941 : pavelk 972 (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
942 :    
943 :     fun isGlobal(IR.V{scope, ...}) = (case scope
944 :     of IR.S_GLOBAL(v) => true
945 :     | _ => false
946 :     (* end case *))
947 :    
948 :     fun extractVarMap(TE(blks, map)) = map
949 :    
950 : pavelk 1122 fun convertDemand (name, x) = ("ps_" ^ name, x)
951 :    
952 : pavelk 866 val outPgm = PSysIR.PGM {
953 : pavelk 1107 globals = PSV.Map.filter isGlobal (extractVarMap env),
954 : pavelk 1122 persistents = List.map convertDemand demand,
955 : pavelk 1107 uveOptimized = false,
956 : pavelk 972 emitter = entryBlock,
957 : pavelk 906 physics = List.nth(!blks, 1),
958 : pavelk 866 render = render
959 :     }
960 : pavelk 868
961 : pavelk 906 val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
962 : pavelk 868
963 : pavelk 746 in
964 : pavelk 905 (* IR.outputPgm(TextIO.stdErr, outPgm); *)
965 : pavelk 1107
966 :     (* Note: it only succeeds if we can optimize, too *)
967 :     if Checker.checkIR(optimized) then printErr "Compilation succeeded." else ();
968 :    
969 :     optimized
970 : pavelk 746 end (* compile *)
971 :    
972 :     end (* Translate *)

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