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

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