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

1 : pavelk 746 (* translate.sml
2 :     *
3 :     * 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 : pavelk 866 val compile : Particles.particle_group -> 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 :     datatype particle_state = PS of {
24 : jhr 750 pos : IR.var, (* vec3 *)
25 :     vel : IR.var, (* vec3 *)
26 :     size : IR.var, (* float *)
27 :     isDead : IR.var, (* bool *)
28 : pavelk 863 color : IR.var, (* vec3 (NOTE: should be vector4) *)
29 :     dummy : IR.var
30 : pavelk 746 }
31 :    
32 :     (* special PSV global variables *)
33 :     val timeStep = PSV.new("g_timeStep", PSV.T_FLOAT) (* physics timestep *)
34 :     val numDead = PSV.new("g_numDead", PSV.T_INT) (* # of dead particles *)
35 :     val epsilon = PSV.constf(0.00001)
36 :    
37 :     (* constants *)
38 :     val pi = 3.14159265358979
39 :    
40 :     (* dummy placeholder *)
41 :     fun dummy (state, k) =
42 : pavelk 747 IR.mkPRIM(
43 : pavelk 746 IR.newLocal(
44 :     "temp",
45 :     IR.T_BOOL,
46 :     (IR.COPY, [IR.newConst("c", IR.C_BOOL false)])
47 :     ),
48 : pavelk 747 IR.COPY,
49 :     [IR.newConst("c", IR.C_BOOL false)],
50 : pavelk 746 k state
51 :     )
52 :    
53 :     (* translation environment *)
54 :     datatype env = TE of (IR.block list ref * IR.var PSV.Map.map)
55 :    
56 : pavelk 770 fun psvToIRVar (TE(_, env), x as PSV.V{name, id, ...}) = (case PSV.Map.find(env, x)
57 : pavelk 746 of SOME x' => x'
58 : pavelk 770 | NONE => raise Fail (String.concat["unknown variable ", name, " with ID ", Int.toString id])
59 : pavelk 746 (* end case *))
60 :    
61 :     fun insert (TE(blks, env), x, x') = TE(blks, PSV.Map.insert (env, x, x'))
62 :    
63 :     (* create a block that implements the given continuation *)
64 :     fun newBlock (TE(blks, _), k : particle_state -> IR.stmt) = let
65 :     val pos = IR.newParam ("ps_pos", IR.T_VEC)
66 :     val vel = IR.newParam ("ps_vel", IR.T_VEC)
67 :     val size = IR.newParam ("ps_size", IR.T_FLOAT)
68 :     val isDead = IR.newParam ("ps_isDead", IR.T_BOOL)
69 :     val color = IR.newParam ("ps_color", IR.T_VEC)
70 : pavelk 864 val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)
71 :     val state = PS{pos=pos, vel=vel, size=size, isDead=isDead, color=color, dummy=dummy}
72 : pavelk 863 val blk = IR.newBlock ([pos, vel, size, isDead, color, dummy], k state)
73 : pavelk 746 in
74 :     blks := blk :: !blks;
75 :     blk
76 :     end
77 :    
78 :     fun newBlockWithArgs (TE(blks, _), args, k : particle_state -> IR.stmt) = let
79 :     val pos = IR.newParam ("ps_pos", IR.T_VEC)
80 :     val vel = IR.newParam ("ps_vel", IR.T_VEC)
81 :     val size = IR.newParam ("ps_size", IR.T_FLOAT)
82 :     val isDead = IR.newParam ("ps_isDead", IR.T_BOOL)
83 :     val color = IR.newParam ("ps_color", IR.T_VEC)
84 : pavelk 864 val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)
85 :     val state = PS{pos=pos, vel=vel, size=size, isDead=isDead, color=color, dummy = dummy}
86 : pavelk 863 val blk = IR.newBlock ([pos, vel, size, isDead, color, dummy] @ args, k state)
87 : pavelk 746 in
88 :     blks := blk :: !blks;
89 :     blk
90 :     end
91 :    
92 : pavelk 863 fun goto (PS{pos, vel, size, isDead, color, dummy}, blk) =
93 :     IR.mkGOTO(blk, [pos, vel, size, isDead, color, dummy])
94 : pavelk 746
95 : pavelk 863 fun gotoWithArgs(PS{pos, vel, size, isDead, color, dummy}, args, blk) =
96 :     IR.mkGOTO(blk, [pos, vel, size, isDead, color, dummy] @ args)
97 : pavelk 746
98 :     fun letPRIM (x, ty, p, args, body) = let
99 :     val x' = IR.newLocal(x, ty, (p, args))
100 :     in
101 :     IR.mkPRIM(x', p, args, body x')
102 :     end
103 :    
104 :     (* prim bound to state variable (S_LOCAL for now) *)
105 :     fun letSPRIM(x, ty, p, args, body) = let
106 : pavelk 862 val x' = IR.new(x, IR.S_LOCAL(ref (p, args)), ty)
107 : pavelk 746 in
108 :     IR.mkPRIM(x', p, args, body x')
109 :     end
110 :    
111 :     (* Not sure if this should be made into a primitive or not, but
112 :     * basically this creates the XOR'd value of var1 and var2 and
113 :     * stores it in result.
114 :     *)
115 :     fun mkXOR (result, var1, var2, stmt : IR.var -> IR.stmt) =
116 :     letPRIM("testOR", IR.T_BOOL, IR.OR, [var1, var2], fn testOR =>
117 :     letPRIM("testAND", IR.T_BOOL, IR.AND, [var1, var2], fn testAND =>
118 :     letPRIM("testNAND", IR.T_BOOL, IR.NOT, [testAND], fn testNAND =>
119 :     letPRIM(result, IR.T_BOOL, IR.AND, [testOR, testNAND], stmt))))
120 :    
121 :     (* Generates a random vector within the given domain and puts it in vecVar *)
122 :     fun genVecVar (vecVar, env, domain, stmt : IR.var -> IR.stmt) = (case domain
123 :     of P.D_POINT(pt) =>
124 :     (* Our options here are pretty limited... *)
125 :     letPRIM (vecVar, IR.T_VEC, IR.COPY, [psvToIRVar(env, pt)], stmt)
126 :    
127 :     | P.D_LINE({pt1, pt2}) =>
128 :     (* Lerp between the points. *)
129 :     letPRIM ("randVal", IR.T_FLOAT, IR.RAND, [], fn randVal =>
130 :     letPRIM ("randInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), randVal], fn randInv =>
131 :     letPRIM ("pt1s", IR.T_VEC, IR.SCALE, [randVal, psvToIRVar(env, pt1)], fn pt1ScaleVec =>
132 :     letPRIM ("pt2s", IR.T_VEC, IR.SCALE, [randInv, psvToIRVar(env, pt2)], fn pt2ScaleVec =>
133 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [pt1ScaleVec, pt2ScaleVec], stmt)))))
134 :    
135 :     (* This is a bit more complicated if we're trying to avoid accessing
136 :     * the vector variables themselves. Basically the way we can do it is to
137 :     * decompose the vector connecting min and max into the basis vectors,
138 :     * scale them independently, and then add them back together.
139 :     *
140 :     * !FIXME! Actually do that. Don't have time right now...
141 :     *)
142 :     | P.D_BOX{max, min} => raise Fail "Cannot generate point in D_BOX."
143 :    
144 :     | P.D_TRIANGLE{pt1, pt2, pt3} =>
145 :     letPRIM ("pt1ToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn pt1ToPt2 =>
146 :     letPRIM ("pt1ToPt3", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt3), psvToIRVar(env, pt1)], fn pt1ToPt3 =>
147 :     letPRIM ("randOne", IR.T_FLOAT, IR.RAND, [], fn rand1 =>
148 :     letPRIM ("randTwo", IR.T_FLOAT, IR.RAND, [], fn rand2 =>
149 :     letPRIM ("randTwoInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), rand2], fn rand2Inv =>
150 :     letPRIM ("scaleOne", IR.T_VEC, IR.SCALE, [rand1, pt1ToPt2], fn scale1 =>
151 :     letPRIM ("nextScale1", IR.T_VEC, IR.SCALE, [rand2Inv, scale1], fn nextScale1 =>
152 :     letPRIM ("scaleTwo", IR.T_VEC, IR.SCALE, [rand2, pt1ToPt3], fn scale2 =>
153 :     letPRIM ("tempAdd", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, pt1), nextScale1], fn tempAdd =>
154 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [tempAdd, scale2], stmt))))))))))
155 :    
156 :     | P.D_CYLINDER {pt1, pt2, irad, orad} => let
157 :     val normVar = PSV.new("local_ht", PSV.T_VEC3F)
158 :     in
159 :     letPRIM("rand", IR.T_FLOAT, IR.RAND, [], fn ourRand =>
160 :     letPRIM("n", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn normVec =>
161 :     letPRIM("ht", IR.T_FLOAT, IR.LEN, [normVec], fn height =>
162 :     letPRIM("htInv", IR.T_FLOAT, IR.DIV, [IR.newConst("one", IR.C_FLOAT 1.0), height], fn htInv =>
163 :     letPRIM("n", IR.T_VEC, IR.SCALE, [htInv, normVec], fn norm =>
164 :     (* Generate a point in the lower disc. *)
165 :     genVecVar("ptInDisc", insert(env, normVar, norm), P.D_DISC{pt = pt1, normal = normVar, irad = irad, orad = orad}, fn ptInDisc =>
166 :     (* Now add this point to a random scaling of the normVec. *)
167 :     letPRIM("s", IR.T_FLOAT, IR.MULT, [height, ourRand], fn scale =>
168 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [scale, normVec], fn scaledNormVec =>
169 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [ptInDisc, scaledNormVec], stmt)))))))))
170 :     end
171 :    
172 :     | P.D_DISC {pt, normal, irad, orad} =>
173 :     (* Get a random angle... *)
174 :     letPRIM ("r", IR.T_FLOAT, IR.RAND, [], fn randForAng =>
175 :     letPRIM ("t", IR.T_FLOAT, IR.MULT, [randForAng, IR.newConst("fullCir", IR.C_FLOAT (2.0 * pi))], fn randAng =>
176 :     (* Get a random radius *)
177 :     letPRIM ("e0", IR.T_FLOAT, IR.RAND, [], fn newRand =>
178 :     letPRIM ("e0sq", IR.T_FLOAT, IR.MULT, [newRand, newRand], fn randRadSq =>
179 :     letPRIM ("radDiff", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, orad), psvToIRVar(env, irad)], fn radDiff =>
180 :     letPRIM ("newRadDist", IR.T_FLOAT, IR.MULT, [randRadSq, radDiff], fn newRadDist =>
181 :     letPRIM ("newRad", IR.T_FLOAT, IR.ADD, [psvToIRVar(env, irad), newRadDist], fn newRad =>
182 :     (* Find a vector in the plane of the disc, and then
183 :     * translate it to the center.
184 :     *)
185 :     letPRIM ("ntoc", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), psvToIRVar(env, normal)], fn normToCen =>
186 :     letPRIM ("v", IR.T_VEC, IR.CROSS, [psvToIRVar(env, pt), normToCen], fn vecInDisc =>
187 :     letPRIM ("vidn", IR.T_VEC, IR.NORM, [vecInDisc], fn vecInDiscNorm =>
188 :     letPRIM ("p", IR.T_VEC, IR.CROSS, [vecInDiscNorm, psvToIRVar(env, normal)], fn ptInDisc =>
189 :     letPRIM ("pidn", IR.T_VEC, IR.NORM, [ptInDisc], fn ptInDiscNorm =>
190 :     (* Figure out x and y values for our new radius and angle *)
191 :     letPRIM ("rx", IR.T_FLOAT, IR.COS, [randAng], fn radX =>
192 :     letPRIM ("ar1", IR.T_FLOAT, IR.MULT, [newRad, radX], fn amtVecOne =>
193 :     letPRIM ("rv1", IR.T_VEC, IR.SCALE, [amtVecOne, vecInDiscNorm], fn resVecOne =>
194 :     letPRIM ("ry", IR.T_FLOAT, IR.SIN, [randAng], fn radY =>
195 :     letPRIM ("ar2", IR.T_FLOAT, IR.MULT, [newRad, radY], fn amtVecTwo =>
196 :     letPRIM ("rv2", IR.T_VEC, IR.SCALE, [amtVecTwo, ptInDiscNorm], fn resVecTwo =>
197 :     letPRIM ("res", IR.T_VEC, IR.ADD_VEC, [resVecOne, resVecTwo], fn result =>
198 :     letPRIM (vecVar, IR.T_VEC, IR.ADD_VEC, [result, psvToIRVar(env, pt)], stmt))))))))))))))))))))
199 :    
200 :     | P.D_CONE{pt1, pt2, irad, orad} => let
201 :     val normVar = PSV.new("local_ht", PSV.T_VEC3F)
202 :     in
203 :     letPRIM("eh", IR.T_FLOAT, IR.RAND, [], fn ourRand =>
204 :     letPRIM("nv", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), psvToIRVar(env, pt1)], fn normVec =>
205 :     letPRIM("n", IR.T_VEC, IR.NORM, [normVec], fn norm =>
206 :     genVecVar("ptInDisc", insert(env, normVar, norm), P.D_DISC{pt = pt1, normal = normVar, irad = irad, orad = orad}, fn ptInDisc =>
207 :     letPRIM("gptt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), ptInDisc], fn genPtToTip =>
208 :     letPRIM("gpttlen", IR.T_FLOAT, IR.LEN, [genPtToTip], fn genPtToTipLen =>
209 :     letPRIM("s", IR.T_FLOAT, IR.MULT, [genPtToTipLen, ourRand], fn scale =>
210 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [scale, genPtToTip], fn scaledNormVec =>
211 :     letPRIM(vecVar, IR.T_VEC, IR.ADD_VEC, [ptInDisc, scaledNormVec], stmt)))))))))
212 :     end
213 :    
214 :     | _ => raise Fail "Cannot generate point in specified domain."
215 :     (* end case *))
216 :     (*
217 :     | generate (Dplane{pt, n}) = Vec3f.unpack pt
218 :     | generate (Drectangle{pt, u, v}) = Vec3f.unpack pt
219 :     | generate (Dsphere{c, orad, irad}) = Vec3f.unpack c
220 :     | generate (Dblob{c, stddev}) = Vec3f.unpack c
221 :     *)
222 :    
223 :    
224 :     (* This function takes an IR boolean, its environment, a particle state, domain,
225 :     * and continuation.
226 :     *
227 :     * We set the boolean to whether or not the current particle given by the particle
228 :     * state is within the domain, and then pass the continuation on.
229 :     *)
230 : pavelk 770 fun mkWithinVar (boolVar, env, var, d, stmt : IR.var -> IR.stmt) = let
231 :     val pos = var
232 : pavelk 746 in
233 :     case d
234 :     of P.D_POINT(pt) =>
235 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn subVec =>
236 :     letPRIM("vecLen", IR.T_FLOAT, IR.LEN, [subVec], fn vecLen =>
237 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), vecLen], stmt)))
238 :    
239 :     (* Take the vectors going from our position to pt1, and pt2. Then
240 :     * after we normalize them, if their dot product is equal to -1, then
241 :     * they are pointing in opposite directions meaning that the position
242 :     * is inbetween pt1 and pt2 as desired.
243 :     *)
244 :     | P.D_LINE{pt1, pt2} =>
245 :     letPRIM("posToPt1", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt1), pos], fn posToPt1 =>
246 :     letPRIM("posToPt1Norm", IR.T_VEC, IR.NORM, [posToPt1], fn posToPt1Norm =>
247 :     letPRIM("posToPt2", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt2), pos], fn posToPt2 =>
248 :     letPRIM("posToPt2Norm", IR.T_VEC, IR.NORM, [posToPt2], fn posToPt2Norm =>
249 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt2, posToPt1], fn dotProd =>
250 :     letPRIM("testMe", IR.T_FLOAT, IR.SUB, [dotProd, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn testVal =>
251 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), testVal], stmt)))))))
252 :    
253 :     (* Just see whether or not the dot product between the normal
254 :     * and the vector from a point on the plane to our position is
255 :     * greater than zero. Essentially, we're "within" a plane if we're
256 :     * behind it (with respect to the normal)
257 :     *)
258 :     | P.D_PLANE{pt, normal} =>
259 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn posToPt =>
260 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
261 :     letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))
262 :    
263 :     (* Similar to checking to see whether or not we're within a plane,
264 :     * here all we have to do is see how far we are from the center
265 :     * of the disc (pt), and then see whther or not we're perpendicular to
266 :     * the normal, and that our distance is greater than irad but less than
267 :     * orad.
268 :     *)
269 :     | P.D_DISC{pt, normal, orad, irad} =>
270 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn posToPt =>
271 :     letPRIM("posToPtLen", IR.T_FLOAT, IR.LEN, [posToPt], fn posToPtLen =>
272 :     letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
273 :     letPRIM("inDisc", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.01), dotProd], fn inDisc =>
274 :     letPRIM("inOrad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToPtLen], fn inOrad =>
275 :     letPRIM("inIrad", IR.T_BOOL, IR.GT, [posToPtLen, psvToIRVar(env, irad)], fn inIrad =>
276 :     letPRIM("inBothRad", IR.T_BOOL, IR.AND, [inIrad, inOrad], fn inBothRad =>
277 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inDisc, inBothRad], stmt))))))))
278 :    
279 :     (* Simply see whether or not the distance from the center is within the
280 :     * specified bounds.
281 :     *)
282 :     | P.D_SPHERE{center, orad, irad} =>
283 :     letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, center), pos], fn posToC =>
284 :     letPRIM("posToPtLen", IR.T_VEC, IR.LEN, [posToC], fn posToCLen =>
285 :     letPRIM("inOrad", IR.T_BOOL, IR.GT, [psvToIRVar(env, orad), posToCLen], fn inOrad =>
286 :     letPRIM("inIrad", IR.T_BOOL, IR.GT, [posToCLen, psvToIRVar(env, irad)], fn inIrad =>
287 :     letPRIM(boolVar, IR.T_BOOL, IR.AND, [inIrad, inOrad], stmt)))))
288 :     (*
289 :     | P.D_TRIANGLE {pt1: vec3f var, pt2: vec3f var, pt3: vec3f var}
290 :     | P.D_PLANE {pt: vec3f var, normal: vec3f var}
291 :     | P.D_RECT {pt: vec3f var, htvec: vec3f var, wdvec: vec3f var}
292 :     | P.D_BOX {min: vec3f var, max: vec3f var}
293 :     | P.D_SPHERE {center: vec3f var, irad: vec3f var, orad: vec3f var}
294 :     | P.D_CYLINDER {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
295 :     | P.D_CONE {pt1: vec3f var, pt2: vec3f var, irad: float var, orad: float var}
296 :     | P.D_BLOB {center: vec3f var, stddev: float var}
297 :     | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
298 :     *)
299 :     | _ => raise Fail "Cannot determine within-ness for specified domain."
300 :     (* end case *)
301 :     end (*end let *)
302 :    
303 :    
304 :     (* generate code to produce a random particle state from a domain *)
305 :     fun newParticle (posDomain, velDomain, colDomain, env, k : particle_state -> IR.stmt) =
306 :     (* genVecVar (vecVar, env, domain, stmt) *)
307 :     genVecVar("ps_pos", env, posDomain, fn newPos =>
308 :     genVecVar("ps_vel", env, velDomain, fn newVel =>
309 :     genVecVar("ps_col", env, colDomain, fn newCol =>
310 :     letSPRIM ("ps_size", IR.T_FLOAT, IR.RAND, [], fn newSize =>
311 :     letSPRIM ("ps_isDead", IR.T_BOOL, IR.COPY, [IR.newConst("fbool", IR.C_BOOL false)], fn newIsDead =>
312 : pavelk 863 k(PS{pos = newPos, vel = newVel, size = newSize, isDead = newIsDead, color = newCol, dummy = IR.newConst("dmy", IR.C_FLOAT 0.01)}))))))
313 : pavelk 746
314 :     (* Find the normal at the given position of the particle for the specified
315 :     * domain. Note, that the particle doesn't necessarily need to be on the
316 :     * domain, but if it's not then the behavior is undefined.
317 :     *)
318 :     fun normAtPoint(retNorm, d, env, state, k : IR.var -> particle_state -> IR.stmt) = let
319 :     val newNorm = IR.newParam("n", IR.T_VEC)
320 :     val nextBlk = newBlockWithArgs(env, [newNorm], k(newNorm))
321 : pavelk 770 val PS{pos, ...} = state
322 : pavelk 746 in
323 :     (case d
324 :     of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
325 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
326 :     | P.D_DISC{pt, normal, irad, orad} =>
327 : pavelk 770 mkWithinVar("inP", env, pos, d, fn inPlane =>
328 : pavelk 746 IR.mkIF(inPlane,
329 :     (* then *)
330 :     letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
331 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk)),
332 :     (* else *)
333 :     letPRIM(retNorm,
334 :     IR.T_VEC,
335 :     IR.SCALE,
336 :     [IR.newConst("negOne", IR.C_FLOAT ~1.0), psvToIRVar(env, normal)],
337 :     fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
338 :     )
339 :     )
340 :    
341 :     | P.D_SPHERE{center, irad, orad} => let
342 : pavelk 863 val PS{pos, vel, size, isDead, color, ...} = state
343 : pavelk 746 in
344 :     letPRIM("sv", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, center)], fn subVec =>
345 :     letPRIM(retNorm, IR.T_VEC, IR.NORM, [subVec], fn newNormVar => k newNormVar state
346 :     ))
347 :     end
348 :    
349 :     | _ => raise Fail("Cannot find normal to point of specified domain.")
350 :     (* end case *))
351 :     end
352 : pavelk 769
353 : pavelk 770 fun trEmitter(emit, env, state, k : particle_state -> IR.stmt) = let
354 : pavelk 866 fun retState s = let
355 :     val PS{pos, vel, size, isDead, color, dummy} = s
356 :     in
357 :     IR.mkRETURN [pos, vel, size, isDead, color, dummy]
358 :     end
359 :    
360 : pavelk 864 val PS{pos, vel, size, isDead, color, dummy} = state
361 : pavelk 770 val P.EMIT{maxNum, posDomain, velDomain, colDomain, ...} = emit
362 :     val blk = newBlock (env, k)
363 :     in
364 :     IR.mkIF(isDead,
365 :     (* then *)
366 :     letPRIM("t1", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, maxNum)], fn t1 =>
367 :     letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, numDead)], fn t2 =>
368 :     letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
369 :     letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
370 :     letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
371 :     IR.mkIF(t3,
372 :     (* then *)
373 :     newParticle (posDomain, velDomain, colDomain, env,
374 : pavelk 866 fn state' => retState state'),
375 : pavelk 770 (* else *)
376 :     IR.DISCARD)))))),
377 :     (* else *)
378 : pavelk 866 IR.DISCARD)
379 : pavelk 770 end
380 :    
381 : pavelk 769 fun trPred(pred, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
382 : pavelk 863 val PS{pos, vel, size, isDead, color, ...} = state
383 : pavelk 769 val P.PR{ifstmt, ...} = pred
384 :     in
385 :     case ifstmt
386 : pavelk 770 of P.WITHIN(d) => mkWithinVar("wv", env, pos, d, fn withinVar =>
387 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
388 : pavelk 770 | P.WITHINVEL(d) => mkWithinVar("wv", env, vel, d, fn withinVar =>
389 : pavelk 867 IR.mkIF(withinVar, thenk(state), elsek(state)))
390 : pavelk 769 end
391 :    
392 : pavelk 746 fun trAct (action, env, state, k : particle_state -> IR.stmt) = let
393 : pavelk 863 val PS{pos, vel, size, isDead, color, dummy} = state
394 : pavelk 746 in
395 :     case action
396 :     of P.BOUNCE{friction, resilience, cutoff, d} => let
397 :     val blk = newBlock (env, k)
398 :     val negOne = IR.newConst("negOne", IR.C_FLOAT ~1.0)
399 :     in
400 :     letPRIM("vs", IR.T_VEC, IR.SCALE, [psvToIRVar(env, timeStep), vel], fn velScale =>
401 :     letPRIM("np", IR.T_VEC, IR.ADD_VEC, [pos, velScale], fn nextPos =>
402 : pavelk 770 mkWithinVar("wnp", env, pos, d, fn withinNextPos =>
403 : pavelk 746 IR.mkIF(withinNextPos,
404 :     (*then*)
405 :     normAtPoint("n", d, env, state, fn normAtD => fn state' => let
406 : pavelk 863 val PS{pos=nextPos, vel=nextVel, size=nextSize, isDead=nextIsDead, color=nextColor, dummy=nextDummy} = state'
407 : pavelk 746 in
408 :     letPRIM("negVel", IR.T_VEC, IR.SCALE, [negOne, nextVel], fn negVel =>
409 :     letPRIM("dnv", IR.T_FLOAT, IR.DOT, [negVel, normAtD], fn dotNegVel =>
410 :     letPRIM("sn", IR.T_VEC, IR.SCALE, [dotNegVel, normAtD], fn scaledN =>
411 :     letPRIM("t", IR.T_VEC, IR.SUB_VEC, [negVel, scaledN], fn tang =>
412 :    
413 :     letPRIM("tlsq", IR.T_FLOAT, IR.LEN_SQ, [tang], fn tangLenSq =>
414 :     letPRIM("cosq", IR.T_FLOAT, IR.MULT, [psvToIRVar(env, cutoff), psvToIRVar(env, cutoff)], fn cutoffSq =>
415 :     letPRIM("inco", IR.T_BOOL, IR.GT, [tangLenSq, cutoffSq], fn inCutoff =>
416 :    
417 :     letPRIM("resNorm", IR.T_VEC, IR.SCALE, [psvToIRVar(env, resilience), scaledN], fn resNorm =>
418 :    
419 :     IR.mkIF(inCutoff,
420 :     (*then*)
421 :     letPRIM("fInv", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), psvToIRVar(env, friction)], fn frictInv =>
422 :     letPRIM("f", IR.T_FLOAT, IR.MULT, [negOne, frictInv], fn modFrict =>
423 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [modFrict, tang], fn frictTang =>
424 :     letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
425 : pavelk 863 goto(PS{pos=nextPos, vel=newVel, size=nextSize, isDead=nextIsDead, color=nextColor, dummy=nextDummy}, blk)
426 : pavelk 746 )))),
427 :     (*else*)
428 :     letPRIM("fTang", IR.T_VEC, IR.SCALE, [negOne, tang], fn frictTang =>
429 :     letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
430 : pavelk 863 goto(PS{pos=nextPos, vel=newVel, size=nextSize, isDead=nextIsDead, color=nextColor, dummy=nextDummy}, blk)
431 : pavelk 746 ))
432 :     )))))))))
433 :     end
434 :     ),
435 :     (*else*)
436 :     goto(state, blk)))))
437 :     end
438 :    
439 :     | P.GRAVITY(dir) =>
440 :     letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, timeStep), psvToIRVar(env, dir)], fn theScale =>
441 :     letPRIM("nextVel", IR.T_VEC, IR.ADD_VEC, [theScale, vel], fn newVel =>
442 : pavelk 863 k(PS{pos = pos, vel = newVel, size = size, isDead = isDead, color = color, dummy=dummy})))
443 : pavelk 746
444 :     | P.MOVE =>
445 : pavelk 770 letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, timeStep), vel], fn theScale =>
446 : pavelk 746 letPRIM("nextPos", IR.T_VEC, IR.ADD_VEC, [theScale, pos], fn newPos =>
447 : pavelk 863 k(PS{pos = newPos, vel = vel, size = size, isDead = isDead, color = color, dummy=dummy})))
448 : pavelk 770 (*
449 : pavelk 758 | P.SINK({d, kill_inside}) =>
450 : pavelk 746 mkWithinVar("isWithin", env, state, d, fn withinVal =>
451 :     mkXOR ("shouldNotKill", withinVal, psvToIRVar(env, kill_inside),
452 :     fn shouldNotKill =>
453 : pavelk 758 letPRIM("shouldKill", IR.T_BOOL, IR.NOT, [shouldNotKill], fn shouldKill =>
454 :     letPRIM("isReallyDead", IR.T_BOOL, IR.OR, [shouldKill, isDead], fn isReallyDead =>
455 :     k(PS{pos = pos, vel = vel, size = size, isDead = isReallyDead, color = color})
456 :     ))))
457 : pavelk 770 *)
458 : pavelk 746
459 :     | P.ORBITLINESEG {endp1, endp2, maxRad, mag} => let
460 :     val blk = newBlock (env, k)
461 :     in
462 :     letPRIM("subVec", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, endp2), psvToIRVar(env, endp1)], fn subVec =>
463 :     letPRIM("vecToEndP", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, endp1)], fn vecToEndP =>
464 :     letPRIM("basis", IR.T_VEC, IR.NORM, [subVec], fn basis =>
465 :     letPRIM("parDot", IR.T_FLOAT, IR.DOT, [basis, vecToEndP], fn parDot =>
466 :     letPRIM("parVec", IR.T_VEC, IR.SCALE, [parDot, basis], fn parVec =>
467 :     letPRIM("closestP", IR.T_VEC, IR.ADD_VEC, [psvToIRVar(env, endp1), parVec], fn closestP =>
468 :     letPRIM("vecToP", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn vecToP =>
469 :     letPRIM("distToP", IR.T_FLOAT, IR.LEN, [vecToP], fn distToP =>
470 :     letPRIM("effRad", IR.T_FLOAT, IR.SUB, [psvToIRVar(env, maxRad), distToP], fn effRad =>
471 :     letPRIM("radInDist", IR.T_BOOL, IR.GT, [psvToIRVar(env, epsilon), effRad], fn radInDist =>
472 :     IR.mkIF(radInDist,
473 :     (*then*)
474 :     goto(state, blk),
475 :     (*else*)
476 :     letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [distToP, psvToIRVar(env, maxRad)], fn magRatio =>
477 :     letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>
478 :     letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>
479 :     letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, timeStep)], fn totMag =>
480 :     letPRIM("accVec", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn accVec =>
481 :     letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, accVec], fn acc =>
482 :     letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>
483 : pavelk 863 goto(PS{pos = pos, vel = newVel, size = size, isDead = isDead, color = color, dummy=dummy}, blk)
484 : pavelk 746 )))))))
485 :     )))))))))))
486 :     end
487 : pavelk 770
488 :     (* just kill it. *)
489 : pavelk 863 | P.DIE => k(PS{pos = pos, vel = vel, size = size, isDead = IR.newConst("falseVar", IR.C_BOOL true), color = color, dummy=dummy})
490 : pavelk 746 | _ => raise Fail("Action not implemented...")
491 :     (* end case *)
492 :     end
493 :    
494 : pavelk 868 fun compile (P.PG{
495 :     emit as P.EMIT{maxNum, vars=emitVars, ...},
496 :     act as P.PSAE{action=root_act, vars=actionVars},
497 :     render
498 :     }) = let
499 : pavelk 746 val blks = ref[]
500 :     val env = let
501 :     (* add special globals to free vars *)
502 : pavelk 770 val vars = PSV.Set.union(emitVars, PSV.Set.addList(actionVars, [maxNum, numDead, timeStep, epsilon]))
503 :     fun ins (x as PSV.V{name, ty, binding, id, ...}, map) = let
504 : pavelk 746 val x' = (case (ty, !binding)
505 :     of (PSV.T_BOOL, PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
506 :     | (PSV.T_BOOL, PSV.BOOL boolVal) => IR.newConst(name, IR.C_BOOL(boolVal))
507 :     | (PSV.T_INT, PSV.UNDEF) => IR.newGlobal(x, IR.T_INT)
508 :     | (PSV.T_INT, PSV.INT intVal) => IR.newConst(name, IR.C_INT(intVal))
509 :     | (PSV.T_FLOAT, PSV.UNDEF) => IR.newGlobal(x, IR.T_FLOAT)
510 :     | (PSV.T_FLOAT, PSV.FLOAT floatVal) => IR.newConst(name, IR.C_FLOAT(floatVal))
511 :     | (PSV.T_VEC3F, PSV.UNDEF) => IR.newGlobal(x, IR.T_VEC)
512 :     | (PSV.T_VEC3F, PSV.VEC3F vecVal) => IR.newConst(name, IR.C_VEC(vecVal))
513 :     | _ => raise Fail("Error in setup, type mismatch between IR and PSV vars.")
514 :     (* end case *))
515 :     in
516 :     PSV.Map.insert (map, x, x')
517 :     end
518 :     in
519 :     TE(blks, PSV.Set.foldl ins PSV.Map.empty vars)
520 :     end
521 : pavelk 867
522 : pavelk 868
523 : pavelk 867 fun evalActs f [] state = f [] state
524 :     | evalActs f (psa :: psal) state = (case psa
525 :     of P.SEQ(acts) => (case acts
526 :     of [] => raise Fail "Should never reach here."
527 :     | [act] => trAct(act, env, state, evalActs f psal)
528 :     | act :: rest => trAct(act, env, state, evalActs f (P.SEQ(rest) :: psal))
529 :     (* end case *))
530 :     | P.PRED(pred as P.PR{thenstmt=t, elsestmt=e, ...}) => let
531 :     val cblk = newBlock(env, evalActs f psal)
532 :     fun trPredActs [] state' = goto(state', cblk)
533 :     | trPredActs _ _ = raise Fail "Should never reach here."
534 :     in
535 :     trPred(pred, env, state, evalActs trPredActs t, evalActs trPredActs e)
536 :     end
537 :     (* end case *))
538 :    
539 : pavelk 868 (* At the highest level, we want to return when we reach the end of the action list *)
540 : pavelk 746 fun trActs [] state = let
541 : pavelk 863 val PS{pos, vel, size, isDead, color, dummy} = state
542 : pavelk 746 in
543 : pavelk 863 IR.mkRETURN[ pos, vel, size, isDead, color, dummy ]
544 : pavelk 746 end (* trActs *)
545 : pavelk 867 | trActs _ _ = raise Fail "Should never reach here"
546 : pavelk 868
547 :     (* The entry block is the first block of the program, or in other words, the emitter. *)
548 :     val entryBlock = newBlock (
549 :     env,
550 :     fn pstate => trEmitter(
551 :     emit,
552 :     env,
553 :     pstate,
554 :     fn state => evalActs trActs root_act state
555 :     )
556 :     )
557 :    
558 :     (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
559 : pavelk 866 val outPgm = PSysIR.PGM {
560 :     emitter = entryBlock,
561 :     physics = List.drop(!blks, 1),
562 :     render = render
563 :     }
564 : pavelk 868
565 :     val optimized = if (Checker.checkIR(outPgm)) then Optimize.optimizeIR(outPgm) else outPgm
566 :    
567 : pavelk 746 in
568 : pavelk 868 IR.outputPgm(TextIO.stdErr, outPgm);
569 :     if Checker.checkIR(optimized) then
570 :     printErr "Compilation succeeded." (* Note: it only succeeds if we can optimize, too *)
571 : pavelk 746 else
572 : pavelk 866 ();
573 : pavelk 868 IR.outputPgm(TextIO.stdErr, optimized);
574 :     optimized
575 : pavelk 746 end (* compile *)
576 :    
577 :     end (* Translate *)

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