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

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