-
Notifications
You must be signed in to change notification settings - Fork 2
/
jitbot-ir.stanza
320 lines (262 loc) · 9.69 KB
/
jitbot-ir.stanza
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
;; TODO:
;; INFIX OPS
defn print-list-infix (o:OutputStream, name:String, s:Streamable) :
var first? = true
for e in s do :
if first?: first? = false else: print-all(o, [" " name " "])
print(o, e)
defn do-print-list (o:OutputStream, name:String, s:Streamable, do-print:(OutputStream, ?) -> False) :
print-all(o, [name "("])
var first? = true
for e in s do :
if first?: first? = false else: print(o, ", ")
do-print(o, e)
print(o, ")")
defn print-list (o:OutputStream, name:String, s:Streamable) :
do-print-list(o, name, s, print)
defn print-list-list (o:OutputStream, outer:String, inner:String, s:Streamable<Streamable>) :
do-print-list(o, outer, s, print-list{_, inner, _})
definterface Exp
defstruct Op <: Exp :
name: String
args: Streamable<ExpT>
infix?: True|False
defmethod print (o:OutputStream, e:Op) :
if infix?(e):
print-list-infix(o, name(e), args(e))
else :
print-list(o, name(e), args(e))
definterface ExpT
defmulti exp<?T> (t:?T&ExpT) -> Exp
defstruct NumT <: ExpT :
exp: Exp with: (as-method => true)
defstruct StrT <: ExpT :
exp: Exp with: (as-method => true)
defstruct Vec2T <: ExpT :
exp: Exp with: (as-method => true)
defstruct Vec3T <: ExpT :
exp: Exp with: (as-method => true)
defstruct MatT <: ExpT :
exp: Exp with: (as-method => true)
definterface Geom2T <: ExpT
definterface Geom3T <: Geom2T
defstruct PolygonT <: Geom2T :
exp: Exp with: (as-method => true)
defstruct PolylineT <: Geom3T :
exp: Exp with: (as-method => true)
defstruct MeshT <: Geom3T :
exp: Exp with: (as-method => true)
defn pprint (a:PolygonT) -> PolygonT :
PolygonT(Op("pprint", [a], false))
defn bit-or (a:PolygonT, b:PolygonT) -> PolygonT :
PolygonT(Op("|", [a, b], true))
defn bit-and (a:PolygonT, b:PolygonT) -> PolygonT :
PolygonT(Op("&", [a, b], true))
defn minus (a:PolygonT, b:PolygonT) -> PolygonT :
PolygonT(Op("\\", [a, b], true))
defn times (a:MatT, b:PolygonT) -> PolygonT :
PolygonT(Op("*", [a, b], true))
defn mag (a:Vec2T, b:PolygonT) -> PolygonT :
PolygonT(Op("mag", [a, b], false))
defn mag1 (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("mag1", [a, b], false))
defn xmag (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("xmag", [a, b], false))
defn ymag (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("ymag", [a, b], false))
defn mov (a:Vec2T, b:PolygonT) -> PolygonT :
PolygonT(Op("mov", [a, b], false))
defn xmov (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("xmov", [a, b], false))
defn ymov (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("ymov", [a, b], false))
defn rot (a:Vec2T, b:PolygonT) -> PolygonT :
PolygonT(Op("rot", [a, b], false))
defn xrot (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("xrot", [a, b], false))
defn yrot (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("yrot", [a, b], false))
defn extrude (a:NumT, b:PolygonT) -> MeshT :
MeshT(Op("extrude", [a, b], false))
defn revolve (p:PolygonT) -> MeshT :
MeshT(Op("revolve", [p], false))
defn offset (a:NumT, b:PolygonT) -> PolygonT :
PolygonT(Op("offset", [a, b], false))
defn pprint (a:PolylineT) -> PolylineT :
PolylineT(Op("pprint", [a], false))
defn times (a:MatT, b:PolylineT) -> PolylineT :
PolylineT(Op("*", [a, b], true))
defn mag (a:Vec3T, b:PolylineT) -> PolylineT :
PolylineT(Op("mag", [a, b], false))
defn mag1 (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("mag1", [a, b], false))
defn xmag (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("xmag", [a, b], false))
defn ymag (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("ymag", [a, b], false))
defn zmag (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("zmag", [a, b], false))
defn mov (a:Vec3T, b:PolylineT) -> PolylineT :
PolylineT(Op("mov", [a, b], false))
defn xmov (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("xmov", [a, b], false))
defn ymov (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("ymov", [a, b], false))
defn zmov (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("zmov", [a, b], false))
defn rot (a:Vec3T, b:PolylineT) -> PolylineT :
PolylineT(Op("rot", [a, b], false))
defn xrot (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("xrot", [a, b], false))
defn yrot (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("yrot", [a, b], false))
defn zrot (a:NumT, b:PolylineT) -> PolylineT :
PolylineT(Op("zrot", [a, b], false))
defn offset (a:NumT, b:PolylineT) -> PolygonT :
PolygonT(Op("offset", [a, b], false))
defn pprint (a:MeshT) -> MeshT :
MeshT(Op("pprint", [a], false))
defn bit-or (a:MeshT, b:MeshT) -> MeshT :
MeshT(Op("|", [a, b], true))
defn bit-and (a:MeshT, b:MeshT) -> MeshT :
MeshT(Op("&", [a, b], true))
defn minus (a:MeshT, b:MeshT) -> MeshT :
MeshT(Op("\\", [a, b], true))
defn times (a:MatT, b:MeshT) -> MeshT :
MeshT(Op("*", [a, b], true))
defn mag (a:Vec3T, b:MeshT) -> MeshT :
MeshT(Op("mag", [a, b], false))
defn mag1 (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("mag1", [a, b], false))
defn xmag (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("xmag", [a, b], false))
defn ymag (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("ymag", [a, b], false))
defn zmag (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("zmag", [a, b], false))
defn mov (a:Vec3T, b:MeshT) -> MeshT :
MeshT(Op("mov", [a, b], false))
defn xmov (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("xmov", [a, b], false))
defn ymov (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("ymov", [a, b], false))
defn zmov (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("zmov", [a, b], false))
defn rot (a:Vec3T, b:MeshT) -> MeshT :
MeshT(Op("rot", [a, b], false))
defn xrot (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("xrot", [a, b], false))
defn yrot (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("yrot", [a, b], false))
defn zrot (a:NumT, b:MeshT) -> MeshT :
MeshT(Op("zrot", [a, b], false))
defn slice (a:NumT, b:MeshT) -> PolygonT :
PolygonT(Op("slice", [a, b], false))
defmethod print (o:OutputStream, t:ExpT) :
print(o, exp(t))
defstruct NumE <: Exp :
x: Float
defn num (x:Float) -> NumT : NumT(NumE(x))
defn num (x:Int) -> NumT : num(to-float(x))
defmethod print (o:OutputStream, e:NumE) :
print(o, x(e))
defn letter (a:StrT) -> PolylineT :
PolylineT(Op("letter", [a], false))
defn text (a:StrT) -> PolylineT :
PolylineT(Op("text", [a], false))
defn circle (r:NumT) -> PolygonT :
PolygonT(Op("circle", [r], false))
defn square (r:NumT) -> PolygonT :
PolygonT(Op("square", [r], false))
defn sphere (r:NumT) -> MeshT :
MeshT(Op("sphere", [r], false))
defn cube (r:NumT) -> MeshT :
MeshT(Op("square", [r], false))
defn load (s:StrT) -> MeshT :
MeshT(Op("load", [s], false))
defn save (s:StrT, m:MeshT) -> MeshT :
MeshT(Op("save", [s, m], false))
defstruct StrE <: Exp :
x: String
defn Str (x:String) -> StrT : StrT(StrE(x))
defmethod print (o:OutputStream, e:StrE) :
print(o, x(e))
defstruct Vec2E <: Exp :
x: NumT
y: NumT
defn vec (x:NumT,y:NumT) -> Vec2T : Vec2T(Vec2E(x,y))
defn vec (x:Float,y:Float) -> Vec2T : vec(num(x),num(y))
defmethod print (o:OutputStream, e:Vec2E) :
print-all(o, ["vec(" x(e) "," y(e) ")"])
defstruct Vec3E <: Exp :
x: NumT
y: NumT
z: NumT
defn vec (x:NumT,y:NumT,z:NumT) -> Vec3T : Vec3T(Vec3E(x,y,z))
defn vec (x:Float,y:Float,z:Float) -> Vec3T : vec(num(x),num(y),num(z))
defmethod print (o:OutputStream, e:Vec3E) :
print-all(o, ["vec(" x(e) "," y(e) "," z(e) ")"])
defstruct MatE <: Exp :
elts: Streamable<NumT>
defn mat (elts:Streamable<NumT>) -> MatT : MatT(MatE(elts))
defmethod print (o:OutputStream, e:MatE) :
print-list(o, "mat", elts(e))
defstruct PolylineE <: Exp :
elts: Streamable<Streamable<Vec3T>>
defn polyline (elts:Streamable<Streamable<Vec3T>>) -> PolylineT : PolylineT(PolylineE(elts))
defmethod print (o:OutputStream, e:PolylineE) :
print-list-list(o, "polyline", "line3", elts(e))
defstruct PolygonE <: Exp :
elts: Streamable<Streamable<Vec2T>>
defn polygon (elts:Streamable<Streamable<Vec2T>>) -> PolygonT : PolygonT(PolygonE(elts))
defn polygon (elts:Streamable<Vec2T>) -> PolygonT : polygon(list(elts))
defmethod print (o:OutputStream, e:PolygonE) :
print-list-list(o, "polygon", "contour", elts(e))
defstruct MeshE <: Exp :
faces: Streamable<Vec3T>
vertices: Streamable<Vec3T>
defn mesh (faces:Streamable<Vec3T>,vertices:Streamable<Vec3T>) -> MeshT :
MeshT(MeshE(faces, vertices))
defmethod print (o:OutputStream, e:MeshE) :
print(o, "Mesh(")
print-list(o, "faces", faces(e))
print(o, ", ")
print-list(o, "vertices", vertices(e))
print(o, ")")
val pi = 3.1415
defn star (n:Int, rmin:Float, rmax:Float) -> PolygonT :
val points = generate<Vec2T> :
for i in 0 to 2 * n do :
val a = (2.0 * pi * to-float(i)) / (2.0 * to-float(n))
val r = if i % 2 == 0 : rmax else : rmin
yield(vec(num(r * cos(a)), num(r * sin(a))))
polygon(points)
;; println(union(circle(num(1.0)),square(num(2.0))))
;; println(polygon([[vec(-1.0,-1.0),vec(0.0,1.0),vec(1.0,-1.0)]]))
;; println(square(num(1.0)) | circle(num(1.5)))
;; println(extrude(num(16.0),square(num(2)) - square(num(1))))
println(extrude(num(10), star(7, 3.0, 5.0) - circle(num(2))))
definterface GeomContainer<T>
defmulti elts<?T> (c:GeomContainer<?T>) -> Array<T>
defclass Joint
defmulti name (j:Joint) -> String
defmulti pose (j:Joint) -> Mat
defmulti limits (j:Joint) -> Tuple(Float, Float)
definterface Layer <: GeomContainer<Geometry>
defmulti color (l:Layer)
definterface Block <: GeomContainer<Layer>
defmulti pose (b:Block) -> Mat
definterface Layout<T> <: GeomContainer<T&Block>
defmulti solved<?T> (l:T?&Layout) -> T
definterface Body <: Block
defmulti joints (b:Body)
deftype Constraint String
defstruct JointRef :
mechanism: Mechanism
body: Body
joint: Joint
definterface Mechanism <: Layout<Body>
defmulti connections (m:Mechanism) -> Array<JointRef>
defmulti state (m:Mechanism) -> Array<Float>
defmulti constraints (m:Mechanism) -> Array<Constraint>
defmulti children (m:Mechanism) -> Array<Mechanism>