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

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