-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
392 lines (341 loc) · 11.2 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!doctype html>
<html>
<head>
<title>GKZ Associahedron Realizer</title>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/three.js/r74/three.js"></script>
<script type="text/javascript" src="polyk.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<script type="text/javascript" src="TrackballControls.js"></script>
<script type="text/javascript" src="Detector.js"></script>
<script type="text/javascript" src="numeric.js"></script>
<script type="text/javascript" src="THREE.MeshLine.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style>
body {
background-color: #eeeeee;
}
#displays {
display: flex;
}
#c1c,#c2c {
flex: 1;
min-width: 250px;
}
#c1,#c2{
width:100%;
height:auto;
max-width:500px;
margin: 0 auto;
display: block;
}
.playspacer{
margin:10px;
padding:10px;
position:relative;
}
.infopanel {
margin:10px;
}
#reset_hex {
position:absolute;
right:10px;
bottom:10px;
}
.helptext {
font-family:sans-serif;
font-size:10px;
text-align:center;
position:absolute;
bottom:10px;
left:10px;
right:10px;
}
@media (max-width: 600px) {
#draghelp {
text-align:left;
}
}
.separator {
margin-top:10px;
margin-bottom:10px;
border-bottom: 1px solid #eee;
}
.footer {
color: gray;
font-style: italic;
}
path.polygon {
fill: #ccc;
}
path.polygon_fg {
stroke: #000;
fill: transparent;
stroke-width: 5px;
stroke-linecap: round;
}
path.triangulation {
fill: transparent;
stroke-width: 3px;
stroke-linecap: round;
pointer-events:none;
}
circle.polygon_v {
fill: #ddd;
stroke: #000;
stroke-width:1px;
cursor: move;
}
text.vertex_areas{
font-family:sans-serif;
font-size:12px;
text-anchor:middle;
pointer-events:none;
}
</style>
</head>
<body>
<div class="info"><div class="infopanel panel panel-default">
<div class="panel-body">
<h1>GKZ Associahedron Realizer</h1>
<h4>By <strong><a href="http://www.hexahedria.com" target="_blank">Daniel Johnson</a></strong>, <strong><a href="https://github.com/URJudged" target="_blank">Justin Lee</a></strong>, and <strong><a href="https://github.com/jwarley" target="_blank">Jackson Warley</a></strong>.</h4>
<div class="separator"></div>
<p>Given a convex hexagon and a <a href="https://en.wikipedia.org/wiki/Polygon_triangulation" target="_blank">triangulation</a> of it, each vertex can be assigned a number (shown left) given by the sum of the areas of the triangles incident to it. Together, each triangulation of the hexagon yields a coordinate in \(\mathbb{R}^6\). By the <a href="http://www.maa.org/press/maa-reviews/discriminants-resultants-and-multidimensional-determinants" target="_blank">Gelfand-Kapranov-Zelevinsky theory on secondary polytopes</a>, taking the convex hull of these coordinates results in a geometric realization of the <a href="https://en.wikipedia.org/wiki/Associahedron" target="_blank">associahedron</a> (shown right), lying in a 3D subspace of \(\mathbb{R}^6\). As one deforms the underlying hexagon, the associahedron also deforms continuously. Notice that each facet of the associahedron corresponds to a diagonal of the hexagon (colored appropriately), and each vertex corresponds to a specific triangulation of the hexagon.</p>
<p>Read our paper presented at the Symposium on Computational Geometry <a href="http://drops.dagstuhl.de/opus/volltexte/2018/8788/pdf/LIPIcs-SoCG-2018-75.pdf" target="_blank">here</a>.</p>
</div>
</div></div>
<div id="displays">
<div id="c1c">
<div class="panel panel-default playspacer">
<svg id="c1" width="500" height="500" viewBox="0 0 500 500"></svg>
<button id="reset_hex" class="btn btn-warning">Reset</button>
<div id="draghelp" class="helptext">Drag vertices to deform hexagon.</div>
</div>
</div>
<div id="c2c">
<div class="panel panel-default playspacer">
<canvas id="c2" width="500" height="500"></canvas>
<div id="cur_vector" class="helptext"></div>
</div>
</div>
</div>
<script type="text/javascript" src="associahedron_data.js"></script>
<script type="text/javascript" src="graphics3d.js"></script>
<script>
function flatten_arr(array){
return array.reduce(function(a,b){return a.concat(b);},[]);
}
var width = 500, height=500;
function reset_poly(){
for (var i = 0; i < 6; i++) {
polygon[i] = [(width/2)+200*Math.cos(2*i*Math.PI/6), (height/2)+200*Math.sin(2*i*Math.PI/6)];
}
}
var polygon = [];
reset_poly();
var fp = flatten_arr(polygon);
console.log(fp,PolyK.IsSimple(fp), PolyK.IsConvex(fp));
var svg = d3.select("#c1");
var polygon_path = svg.append("g").selectAll("path.polygon");
var polygon_path_fg = svg.append("g").selectAll("path.polygon_fg");
var triangulation_path = svg.append("g").selectAll("path.triangulation");
var polygon_verts = svg.append("g").selectAll("circle.polygon_v");
var vertex_areas = svg.append("g").selectAll("text.vertex_areas");
document.getElementById("reset_hex").addEventListener("click",function(){
reset_poly();
reset_view_fn();
redraw();
});
///////////
function dragmove(d,i) {
console.log("Drag");
var oldx = d[0];
var oldy = d[1];
d[0] = d3.event.x;
d[1] = d3.event.y;
var padding = 40;
if(d[0]<padding) d[0]=padding;
if(d[0]>width-padding) d[0]=width-padding;
if(d[1]<padding) d[1]=padding;
if(d[1]>height-padding) d[1]=height-padding;
var fp = flatten_arr(polygon);
if(!(PolyK.IsSimple(fp) && PolyK.IsConvex(fp))){
d[0] = oldx;
d[1] = oldy;
}
redraw();
}
var dragpoly = d3.behavior.drag().on("drag", dragmove)
function redraw(){
polygon_path = polygon_path.data([polygon]);
polygon_path.exit().remove();
polygon_path.enter().append("path").attr("class","polygon");
polygon_path.attr("d", poly_d_fn);
polygon_path_fg = polygon_path_fg.data([polygon]);
polygon_path_fg.exit().remove();
polygon_path_fg.enter().append("path").attr("class","polygon_fg");
polygon_path_fg.attr("d", poly_d_fn);
polygon_verts = polygon_verts.data(polygon);
polygon_verts.exit().remove();
polygon_verts.enter().append("circle");
polygon_verts.attr("class","polygon_v")
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 6)
.call(dragpoly);
var cdiags = [];
var careas = [];
var vect_text;
if(active_triangulation !== null){
var T = triangulations[active_triangulation];
var cdiags = triangulation_faces[active_triangulation];
careas = area_vector(T);
vect_text = "Hover over vertices to display area vector.<br>Current selection: (" + careas.map(function(x){return Math.round(x/1000)}).join(", ") + ")";
} else {
vect_text = "";
}
document.getElementById("cur_vector").innerHTML = vect_text;
triangulation_path = triangulation_path.data(cdiags);
triangulation_path.exit().remove();
triangulation_path.enter().append("path").attr("class","triangulation");
triangulation_path.attr("d", function(d){
var apt = polygon[diagonals[d][0]];
var bpt = polygon[diagonals[d][1]];
var path = [apt, bpt];
return "M" + apt + "L" + bpt;
});
triangulation_path.attr("stroke", function(d){
var h = d/asschdron_faces.length;
return "hsl("+(h*360)+",100%,80%)";
});
vertex_areas = vertex_areas.data(careas);
vertex_areas.exit().remove();
vertex_areas.enter().append("text").attr("class","vertex_areas");
vertex_areas.text(function(d){
return Math.round(d/1000);
});
var pcx = 0, pcy = 0;
for (var i = 0; i < polygon.length; i++) {
var p = polygon[i];
pcx += p[0]/6;
pcy += p[1]/6;
}
vertex_areas.attr("x",function(d,i){
var dvec = numeric.sub(polygon[i],[pcx,pcy]);
return numeric.add(numeric.mul(numeric.div(dvec,numeric.norm2(dvec)),20), polygon[i])[0];
}).attr("y",function(d,i){
var dvec = numeric.sub(polygon[i],[pcx,pcy]);
return numeric.add(numeric.mul(numeric.div(dvec,numeric.norm2(dvec)),20), polygon[i])[1];
});
var assoc_points = [];
for (var i = 0; i < triangulations.length; i++) {
var T = triangulations[i];
assoc_points.push(area_vector(T));
}
var assoc;
var axes = gs_orthonormalize(assoc_points);
console.log(axes);
assoc = project_all(assoc_points,axes);
console.log(assoc);
update_asschdron(assoc, false);
}
function poly_d_fn(d) {
return "M" + d.join("L") + "Z";
}
// Computes the area sum of vertex v under triangulation T
function area_sum(v, T) {
var area = 0;
// For every triangle in T
for (var i = 0; i < T.length; i++) {
// If v is in the triangle
if (T[i].indexOf(v) != -1) {
// Flatten the triangle for PolyK
var incidentTriangle = [];
for (var j = 0; j < 3; ++j) {
incidentTriangle[j] = polygon[T[i][j]];
}
var ft = flatten_arr(incidentTriangle);
// Add the area
area += PolyK.GetArea(ft);
}
}
return area;
}
function area_vector(T) {
var av = [];
for (var i = 0; i < polygon.length; ++i) {
av[i] = area_sum(i, T);
}
return av;
}
function pcaR6toR3(associahedron) {
var pca = new PCA();
associahedron = pca.scale(associahedron,true,true);
pc = pca.pca(associahedron,3);
output = [];
for (var i = 0; i < pc.length; i++) {
output[i] = pc[i].slice(0,3);
}
return output;
}
function axis_transpose(vects, transpose){
var newvects = [];
for (var i = 0; i < vects.length; i++) {
var vect = vects[i]
var nvect = [];
for (var j = 0; j < vect.length; j++) {
nvect[j] = vect[transpose[j]];
}
newvects.push(nvect);
}
return newvects;
}
function project_all(vects, axes){
var newvects = [];
for (var i = 0; i < vects.length; i++) {
var vect = numeric.sub(vects[i], vects[vects.length - 1]);
var nvect = [];
for (var j = 0; j < axes.length; j++) {
var axis = axes[j];
nvect[j] = scalar_project(vect, axis);
}
while(nvect.length < 6){
nvect.push(0);
}
newvects.push(nvect);
}
return newvects;
}
function scalar_project(v,axis){
return numeric.dot(v, axis) / numeric.norm2(axis);
}
// Returns an orthonormal set of vectors spanning
// the same subspace as the input vectors
function gs_orthonormalize(vectors) {
var orth = [];
for (var i = 0; i < vectors.length - 1; i++) {
orth[i] = numeric.sub(vectors[i], vectors[vectors.length - 1]);
}
for (var i = 0; i < orth.length; i++) {
var magnitude = numeric.norm2(orth[i]);
if (magnitude < .0000001) {
orth[i] = null;
}
else{
for (var j = i + 1; j < orth.length; j++) {
innerProductQuotient = (numeric.dot(orth[j], orth[i]) /
numeric.dot(orth[i], orth[i]));
proj = numeric.mul(orth[i], innerProductQuotient);
orth[j] = numeric.sub(orth[j], proj);
}
orth[i] = numeric.div(orth[i], magnitude);
}
}
return orth.filter(function isNull(elem) {return elem != null;});
}
///////////
init_graphics3d(width,height,document.getElementById("c2"));
redraw();
animate3d();
</script>
</body>
</html>