-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
242 lines (219 loc) · 10 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>P-generative WebPPL Runner</title>
<link rel="stylesheet" href="https://probmods.github.io/webppl-editor/webppl-editor.css">
<link rel="stylesheet" href="https://probmods.github.io/webppl-viz/webppl-viz.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.css">
<script src="./lib/webppl-v0.9.15.js"></script>
<script src="https://probmods.github.io/webppl-editor/jquery.js"></script>
<script src="https://probmods.org/assets/js/webppl-editor.min.js"></script>
<script src="https://probmods.org/assets/js/webppl-viz.min.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
div.result div.custom {
display: inline-block;
}
.drawCanvas {
border: 1px solid #ccc;
border-radius: 4px;
padding: .25em;
margin: .25em;
}
</style>
</head>
<body>
<header>
<h1>P-generative WebPPL Runner</h>
<p>Please insert code into the box below and click run.</p>
<b style="color: red">Warning - <u>there is no saving functionality!</u>
Please copy your code to clipboard frequently and paste it back in the original file to save any changes</b>
</header>
<pre></pre>
<script src="https://probmods.org/assets/js/paper-full.js"></script>
<script src="https://probmods.org/assets/js/draw.js"></script>
<script src="https://probmods.org/assets/js/box2d.js"></script>
<script src="https://probmods.org/assets/js/physics.js"></script>
<script>
// from some prodmods js asset (physics.js I think), modified to accommodate drawing rotated rectangles and also drawing worlds run through the physics engine
// not used by shapes-vision.wppl.js
applyWorld = function (initialWorld) {
var worldList = initialWorld;
_.each(
worldList,
function (obj) {
var shape = obj.shape,
dims = obj.dims,
velocity = obj.velocity || [0, 0];
bodyDef.type = obj.static ? b2Body.b2_staticBody : b2Body.b2_dynamicBody;
if (shape == "circle") {
var r = dims[0] / SCALE;
fixDef.shape = new b2CircleShape(r);
} else if (shape == "rect") {
var w = dims[0] / SCALE;
var h = dims[1] / SCALE;
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsOrientedBox(w, h, new b2Vec2(0, 0), 0.0);
} else {
throw new Error('unknown shape ' + shape);
}
bodyDef.position.x = obj.x / SCALE;
bodyDef.position.y = obj.y / SCALE;
bodyDef.linearVelocity.x = velocity[0] / SCALE;
bodyDef.linearVelocity.y = velocity[1] / SCALE;
world.CreateBody(bodyDef).CreateFixture(fixDef);
}
)
return initialWorld;
}
churchWorld_from_bodyList = function (body) {
var worldList = [];
while (body) {
var isStatic = !(body.GetType() == 2);
var shapeInt = body.GetFixtureList().GetType();
var shape;
var dims;
if (shapeInt == 0) {
shape = "circle";
dims = [body.GetFixtureList().GetShape().GetRadius() * SCALE];
} else {
shape = "rect";
var vertices = body.GetFixtureList().GetShape().GetVertices();
dims = [vertices[2].x * SCALE, vertices[2].y * SCALE];
angle = body.GetAngle() * 180 / Math.PI;
}
var x = body.GetPosition().x * SCALE;
var y = body.GetPosition().y * SCALE;
worldList.push({
shape: shape,
static: isStatic,
dims: dims,
x: x,
y: y,
angle: angle
});
body = body.GetNext();
}
return worldList;
}
// adapted/modified from draw.js
DrawObject.prototype.rectangle = function (x1, y1, x2, y2, stroke='black', fill='white', opacity=1, angle=0, strokeWidth=1) {
var rect = new this.paper.Path.Rectangle(this.newPoint(x1, y1), this.newPoint(x2, y2));
rect.fillColor = (fill == 'none' ? new paper.Color(1, 1, 1, 0) : fill);
rect.strokeColor = stroke;
rect.strokeWidth = strokeWidth;
rect.opacity = opacity;
rect.rotate(angle, (x1 + x2) / 2, (y1 + y2) / 2); // rotate about the center of the rectangle
this.redraw();
};
DrawObject.prototype.circle = function (x, y, radius, stroke='black', fill='black', opacity=1, strokeWidth=1) {
var point = this.newPoint(x, y);
var circle = new this.paper.Path.Circle(point, radius);
circle.fillColor = (fill == 'none' ? new paper.Color(1, 1, 1, 0) : fill);
circle.strokeColor = stroke;
circle.strokeWidth = strokeWidth;
circle.opacity = opacity;
this.redraw();
};
DrawObject.prototype.triangle = function (x1, y1, x2, y2, x3, y3, stroke='black', fill='white', opacity=1, strokeWidth=1) {
var tri = new this.paper.Path();
tri.add(this.newPoint(x1, y1));
tri.add(this.newPoint(x2, y2));
tri.add(this.newPoint(x3, y3));
tri.closed = true;
tri.fillColor = (fill == 'none' ? new paper.Color(1, 1, 1, 0) : fill);
tri.strokeColor = stroke;
tri.strokeWidth = strokeWidth;
tri.opacity = opacity;
this.redraw();
};
// modified from draw.js to have raster image fill the canvas
function loadImage(s, k, a, drawObject, url, fill) {
// Synchronous loading - only continue with computation once image is loaded
var context = drawObject.canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
var raster = new drawObject.paper.Raster(imageObj);
raster.position = drawObject.paper.view.center;
raster.fitBounds(drawObject.paper.view.bounds, fill);
drawObject.redraw();
resumeTrampoline(function() { return k(s) });
};
imageObj.src = url;
}
// from dippl.org/examples/vision.html, used by the DrawObject.prototype.distance function
function euclideanDistance(v1, v2){
var i;
var d = 0;
for (i = 0; i < v1.length; i++) {
d += (v1[i] - v2[i])*(v1[i] - v2[i]);
}
return Math.sqrt(d);
};
// formula from https://stackoverflow.com/a/596243
function getLuminosity([r,g,b,a]) {
// ignore alpha; should always be 100% for images, which is what we're detecting edges in
return (0.299*r + 0.587*g + 0.114*b);
}
// adapted from https://codepen.io/taylorcoffelt/pen/eYNZvZ
function detectEdges(s, k, a, drawObject, threshold=0.2) {
var height = drawObject.canvas.height;
var width = drawObject.canvas.width;
var imageData = drawObject.toArray();
function getPixel(x,y) {
var start = (x + y * width) * 4;
return imageData.slice(start, start + 4);
}
// debugger;
var points = drawObject.canvas.getContext('2d').createImageData(width, height);
for(y=0;y<height;y++) {
for(x=0;x<width;x++){
pixel = getLuminosity(getPixel(x,y));
left = getLuminosity(getPixel(x-1,y));
right = getLuminosity(getPixel(x+1,y));
top = getLuminosity(getPixel(x,y-1));
bottom = getLuminosity(getPixel(x,y+1));
//Compare it all.
var index = (x + y * width) * 4;
if(/*x == 0 || x == width-1 || y == 0 || y == height-1
||*/ pixel>left+threshold
|| pixel<left-threshold
|| pixel>right+threshold
|| pixel<right-threshold
|| pixel>top+threshold
|| pixel<top-threshold
|| pixel>bottom+threshold
|| pixel<bottom-threshold) {
value = 0;
} else {
value = 255;
}
points.data[index + 0] = value;
points.data[index + 1] = value;
points.data[index + 2] = value;
points.data[index + 3] = value;
points.data[index + 3] = 255;
}
}
return k(s, points);
}
DrawObject.prototype.setImageData = function(imageData) {
this.canvas.getContext('2d').putImageData(imageData, 0, 0);
}
</script>
<script>
// find all <pre> elements and set up the editor on them
var preEls = Array.prototype.slice.call(document.querySelectorAll("pre"));
preEls.map(function (el) { editor.setup(el, { language: 'webppl' }); });
</script>
</div>
</body>
</html>