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

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