-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
591 lines (553 loc) · 23.8 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="static/style.css">
<style>
.photo {
border: solid;
}
.wrapper {
float: left;
}
.axis path,
.axis line {
fill: none;
stroke: #557799;
shape-rendering: crispEdges;
}
.info {
margin: 40px 4px 0 4px;
float: right;
width: 112px;
height: 40px;
color: #7fdbff;
}
</style>
<script src="static/d3.js" charset="utf-8"></script>
<script src="static/li.js" charset="utf-8"></script>
<script src="static/jquery-2.2.0.js" charset="utf-8"></script>
<script>
var margin_length = 40;
var margin = {
top: margin_length, right: margin_length,
bottom: margin_length, left: margin_length
};
//this variables is used to construct two rectangles:
// the attract is to hold related pics inside, the push is to push nonrelated pics outside
var scale_rate = {attract: 2, push: 2.5};
//this is the size of display plane
var v = {height: 980, width: 1700};
var coverage = {max: .82, min: .25, stable: .81};
var rate = {max: .9, min: .05, default: .3, step: .035, selected: 1};
var o = {x: 0, y: 0, h: v.height, w: v.width};
var step = 100;
// var base_dir = "mingphoto/Archive/";
// var base_dir = "photodir/";
var base_dir = "xiphoto/test/";
var info_file = "info.csv";
var sim_info_hist = "sim_info_hist.csv";
var sim_info_cosine = "sim_info_cosine.csv";
var sim_info_ghc = "sim_info_ghc.csv";
var sim_matrix;
var dataset;
var push_vertexes = [
{x: 175, y: 42.922},
{x: 1525, y: 42.922},
{x: 1525, y: 937.078},
{x: 175, y: 937.078}];
// puhlic variable that store the related pictures list
var related = [];
var selected = {h: 715.325, w: 1080, x: 310, y: 132.338};
var interval = 1000;
// canvas
var svga;
var x = d3.scale.linear().range([0, v.width]).domain([0, v.width]);
var y = d3.scale.linear().range([0, v.height]).domain([0, v.height]);
var modi = d3.scale.linear().range([0, 3]).domain([3, 0]);
function cal_coverage() {
var total_area = v.height * v.width;
var cover_area = 0.0;
dataset.forEach(function (d) {
cover_area += d.i.rate * d.ExifImageWidth * d.i.rate * d.ExifImageHeight;
});
cvr = cover_area / total_area;
}
//record the coverage rate, more precisely, we sum all pictures' area and
// divide it by the area of displaying plane
var cvr = 0.0;
d3.text(base_dir + sim_info_hist, function (text) {
var data = d3.csv.parseRows(text).map(function (row) {
return row.map(function (value) {
return +value;
});
});
// console.log(data);
sim_matrix = data.slice();
});
d3.csv(base_dir + info_file, function (error, data) {
if (error) throw error;
dataset = data.slice();
base();
draw_gallery();
})
function exceeds(d, o) {
return d.i.top().y < o.y || d.i.right().x > o.x + o.w || d.i.bottom().y > o.y + o.h || d.i.left().x < o.x;
}
function draw_gallery() {
init();
// console.dir(dataset);
judge0(0);
}
function overlap(d0, d1) {
return doPolygonsIntersect(d0.i.vertexes(),
d1.i.selected && ($.inArray(parseInt(d0.FileName.substr(3, 4)), related)) < 0 ? push_vertexes : d1.i.vertexes())
&& (d0.FileName != d1.FileName);
}
function base() {
svga = d3.select(".wrapper").append("svg")
.attr("width", v.width + margin.left + margin.right)
.attr("height", v.height + margin.top + margin.bottom)
.append("g").attr("class", "g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var xAxis = d3.svg.axis().scale(x)
.orient("top");
var yAxis = d3.svg.axis().scale(y)
.orient("left");
svga.append("g")
.attr("class", "x axis")
// .attr("transform", "translate(0," + 2 * radius + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", v.width)
.attr("y", -6)
.style("text-anchor", "end")
.text("x axis");
svga.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", -12)
.attr("x", 12 - v.height)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("y axis")
}
function init() {
dataset.forEach(function (d) {
d.i = {
translate_x: 0,
translate_y: 0,
rate: rate.default,
rotate: 0,
selected: false
};
d.i.select = function () {
dataset.forEach(function (di) {
di.i.selected = false;
})
d.i.selected = true;
}
d.i.locate = function (x, y, a) {
d.i.translate_x = x - d.i.w() / 2;
d.i.translate_y = y - d.i.h() / 2;
}
d.i.update_rate = function (r, a) {
d.i.rate = r;
}
d.i.update_rotate = function (r, a) {
d.i.rotate = r;
}
d.i.w = function () {
return d.ExifImageWidth * d.i.rate;
// return d.ExifImageWidth;
}
d.i.h = function () {
return d.ExifImageHeight * d.i.rate;
// return d.ExifImageHeight;
}
/**Orientation indication
1 2 3 4 5 6 7 8
888888 888888 88 88 8888888888 88 88 8888888888
88 88 88 88 88 88 88 88 88 88 88 88
8888 8888 8888 8888 88 8888888888 8888888888 88
88 88 88 88
88 88 888888 888888
*/
switch (d.Orientation) {
case "1":
d.i.rotate = 0;
break;
case "3":
d.i.rotate = 180;
break;
case "6":
d.i.rotate = 90;
break;
case "8":
d.i.rotate = -90;
break;
default:
d.i.rotate = 0;
}
d.i.x = function () {
return d.i.center().x;
}
d.i.y = function () {
return d.i.center().y;
}
// distance from one vertex to center of the rectangle
d.i.radius = function () {
return Math.sqrt(Math.pow(d.i.w() / 2, 2) + Math.pow(d.i.h() / 2, 2));
};
// the angle between (center.x-x0,center.y-y0) and (x,0)
d.i.base_angle = function () {
return rad2deg(Math.atan(d.ExifImageHeight / d.ExifImageWidth));
};
d.i.center = function () {
// center is always this value
// translate_x and translate_y is always this value, no matter rotate or not
return {
x: d.i.translate_x + d.i.w() / 2,
y: d.i.translate_y + d.i.h() / 2
};
};
// return the four vertexes, in original clockwise order
// (0:top-left, 1:top-right,2:bottom-right,3"bottom-left)
d.i.vertexes = function () {
var vertexes = [];
var cent = d.i.center();
vertexes.push({
x: cent.x - d.i.radius() * Math.cos(deg2rad(d.i.rotate + d.i.base_angle())),
y: cent.y - d.i.radius() * Math.sin(deg2rad(d.i.rotate + d.i.base_angle()))
});
vertexes.push({
x: vertexes[0].x + d.i.w() * Math.cos(deg2rad(d.i.rotate)),
y: vertexes[0].y + d.i.w() * Math.sin(deg2rad(d.i.rotate))
});
vertexes.push({
x: vertexes[1].x - d.i.h() * Math.sin(deg2rad(d.i.rotate)),
y: vertexes[1].y + d.i.h() * Math.cos(deg2rad(d.i.rotate))
});
vertexes.push({
x: vertexes[0].x - d.i.h() * Math.sin(deg2rad(d.i.rotate)),
y: vertexes[0].y + d.i.h() * Math.cos(deg2rad(d.i.rotate))
});
return vertexes;
}
d.i.top = function () {
var k = (4 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
}
d.i.right = function () {
var k = (5 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
}
d.i.bottom = function () {
var k = (6 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
}
d.i.left = function () {
var k = (7 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
}
});
dataset.forEach(function (d, i) {
var x = Math.random() * v.width;
var y = Math.random() * v.height;
d.i.locate(x, y);
});
svga.selectAll(".photo").data(dataset, function (p) {
return p.FileName;
}).enter().append("image").attr("transform", function (p, i) {
return "translate(" + p.i.translate_x + ","
+ p.i.translate_y + ")"
+ ",rotate("
+ p.i.rotate
+ "," + p.i.w() / 2 + "," + p.i.h() / 2
+ "),scale(" + p.i.rate + ")"
;
}).attr("width", function (d) {
return d.ExifImageWidth;
}).attr("height", function (d) {
return d.ExifImageHeight;
}).attr("xlink:href", function (p) {
return base_dir + p.FileName;
}).on("click", function (d) {
dataset.forEach(function (d0) {
if (d0.i.selected) {
var cent = d.i.center();
d0.i.update_rate(d.i.rate);
d0.i.locate(cent.x, cent.y);
d0.i.update_rate(rate.default);
}
})
d.i.select();
d.i.update_rate(rate.selected);
d.i.locate(v.width / 2, v.height / 2);
related = get_related(d);
var attr = d.i.vertexes().map(function (k) {
return {
x: d.i.center().x + scale_rate.attract * (k.x - d.i.center().x),
y: d.i.center().y + scale_rate.attract * (k.y - d.i.center().y)
}
});
var push = d.i.vertexes().map(function (k) {
return {
x: d.i.center().x + scale_rate.push * (k.x - d.i.center().x),
y: d.i.center().y + scale_rate.push * (k.y - d.i.center().y)
}
});
var push_rect = {
x: push[0].x,
y: push[0].y,
h: push[2].y - push[0].y,
w: push[2].x - push[0].x
}
/*
selected = {
x: attr[0].x,
y: attr[0].y,
h: attr[2].y - attr[0].y,
w: attr[2].x - attr[0].x
};
push_vertexes = d.i.vertexes().map(function (k) {
return {
x: d.i.center().x + scale_rate.push * (k.x - d.i.center().x),
y: d.i.center().y + scale_rate.push * (k.y - d.i.center().y)
}
})
//*/
// console.dir(selected);
// console.dir(push_vertexes);
/*
svga.selectAll("rect").remove();
svga.append("rect")
.attr("x", selected.x)
.attr("y", selected.y)
.attr("height", selected.h)
.attr("width", selected.w).style("fill-opacity", .1);
svga.append("rect")
.attr("x", push_rect.x)
.attr("y", push_rect.y)
.attr("height", push_rect.h)
.attr("width", push_rect.w).style("fill-opacity", .1);
// */
dataset = dataset.map(function (d) {
if (!d.i.selected) {
d.i.update_rate(rate.default);
}
return d;
})
judge0(0);
})
.attr("class", "photo");
}
/**
* Helper function to determine whether there is an intersection between the two polygons described
* by the lists of vertices. Uses the Separating Axis Theorem
*
* @param a an array of connected points [{x:, y:}, {x:, y:},...] that form a closed polygon
* @param b an array of connected points [{x:, y:}, {x:, y:},...] that form a closed polygon
* @return true if there is any intersection between the 2 polygons, false otherwise
*/
function doPolygonsIntersect(a, b) {
var polygons = [a, b];
var minA, maxA, projected, i, i1, j, minB, maxB;
for (i = 0; i < polygons.length; i++) {
// for each polygon, look at each edge of the polygon, and determine if it separates
// the two shapes
var polygon = polygons[i];
for (i1 = 0; i1 < polygon.length; i1++) {
// grab 2 vertices to create an edge
var i2 = (i1 + 1) % polygon.length;
var p1 = polygon[i1];
var p2 = polygon[i2];
// find the line perpendicular to this edge
var normal = {x: p2.y - p1.y, y: p1.x - p2.x};
minA = maxA = undefined;
// for each vertex in the first shape, project it onto the line perpendicular to the edge
// and keep track of the min and max of these values
for (j = 0; j < a.length; j++) {
projected = normal.x * a[j].x + normal.y * a[j].y;
if (isUndefined(minA) || projected < minA) {
minA = projected;
}
if (isUndefined(maxA) || projected > maxA) {
maxA = projected;
}
}
// for each vertex in the second shape, project it onto the line perpendicular to the edge
// and keep track of the min and max of these values
minB = maxB = undefined;
for (j = 0; j < b.length; j++) {
projected = normal.x * b[j].x + normal.y * b[j].y;
if (isUndefined(minB) || projected < minB) {
minB = projected;
}
if (isUndefined(maxB) || projected > maxB) {
maxB = projected;
}
}
// if there is no overlap between the projects, the edge we are looking at separates the two
// polygons, and we know there is no overlap
if (maxA < minB || maxB < minA) {
// console.log("polygons don't intersect!");
return false;
}
}
}
return true;
}
function isUndefined(a) {
return a == undefined;
}
function rad2deg(r) {
return r * 180 / Math.PI
}
function deg2rad(d) {
return d * Math.PI / 180;
}
//this function is called after the system init, to solve exceed and overlap
function judge0(count) {
if (((dataset.some(function (d) {
return d.i.selected;
}) || cvr > coverage.stable) && dataset.every(function (d0) {
return !exceeds(d0, o) &&
dataset.every(function (d1) {
return !overlap(d0, d1);
});
}))
|| count > 120) {
// console.log("stable at step:" + count);
update_draw(count);
} else {
cycle(count);
}
}
function cycle(count) {
cal_coverage();
update_draw(count);
setTimeout(function () {
dataset.filter(function (d) {
return !d.i.selected;
}).sort(function (a, b) {
var ea = exceeds(a, o), eb = exceeds(b, o);
if (ea && !eb) {
return 1;
}
if (eb && !ea) {
return -1;
}
return 0;
}).forEach(function (d) {
/*solve exceeds*/
var solved = solve_exceeds($.inArray(parseInt(d.FileName.substr(3, 4)), related) >= 0 ?
selected : o, d, {x: d.i.x(), y: d.i.y()});
var x = solved.x;
var y = solved.y;
var adjacent = dataset.filter(function (d1) {
return overlap(d, d1);
}).slice();
if (adjacent.length == 0) {
if (d.i.rate < rate.max && cvr < coverage.max && !d.i.selected) {
d.i.update_rate(d.i.rate + rate.step);
}
} else {
/* solve overlap*/
dataset.forEach(function (d1) {
if (overlap(d, d1)) {
//we see this rectangle as a circle with radius (width+height)/2 to make this process easier
var r = (d.i.w() + d.i.h()) / 2;
var r1 = (d1.i.w() + d1.i.h()) / 2;
r1 = d1.i.selected ? r1 * scale_rate.push : r1;
//vx and vy denote the move direction vector
var vx = d.i.x() - d1.i.x();
var vy = d.i.y() - d1.i.y();
//min distance between two circles to ensure no overlap
var vd = Math.sqrt(vx * vx + vy * vy);
x += vd == 0 ? 0 : ( vx / vd) * (r + r1 - vd) / step;
y += vd == 0 ? 0 : ( vy / vd) * (r + r1 - vd) / step;
}
});
if (d.i.rate > rate.min && cvr > coverage.min && !d.i.selected) {
d.i.update_rate(d.i.rate - rate.step / 2);
}
}
x = d.i.x() + (x - d.i.x());
y = d.i.y() + (y - d.i.y());
d.i.locate(x, y);
});
judge0(count + 1);
}, 0);
}
function update_draw(count) {
d3.select(".cvr").text(Math.round(cvr * 1000) / 1000);
d3.select(".cnt").text(count);
update_draw_dataset(dataset, interval);
}
function get_related(d) {
var index = parseInt(d.FileName.substr(3, 4));
var result = sim_matrix[index].map(function (m, i) {
return {index: i, value: m};
}).sort(function (a, b) {
return b.value - a.value;
}).filter(function (m, i) {
// return the first 12 photos in photo set
return i < 12;
}).map(function (m) {
return m.index;
});
return result;
}
function update_draw_dataset(dataset, interval) {
svga.selectAll(".photo").data(dataset, function (p) {
return p.FileName;
}).style("fill", function (p) {
return $.inArray(parseInt(p.FileName.substr(3, 4)), related) >= 0 ? "red" : "blue";
}).transition().duration(interval).ease("linear").attrTween("transform", function (p, index) {
var current = d3.select(this).attr("transform");
var to = "translate("
+ p.i.translate_x + ","
+ p.i.translate_y + "),rotate("
+ p.i.rotate
+ "," + p.i.w() / 2 + "," + p.i.h() / 2
+ "),scale(" + p.i.rate + ")";
return d3.interpolateString(current, to);
});
}
//o is the container rectangle, d is the displaying pic
function solve_exceeds(o, d, obj) {
var x = obj.x, y = obj.y;
if (0 < o.y - d.i.top().y) {
t = true;
y += o.y - d.i.top().y;
}
if (0 > o.w + o.x - d.i.right().x) {
r = true;
x += o.w + o.x - d.i.right().x;
}
if (0 > o.h + o.y - d.i.bottom().y) {
b = true;
y += o.h + o.y - d.i.bottom().y;
}
if (0 < o.x - d.i.left().x) {
l = true;
x += o.x - d.i.left().x;
}
return {x: x, y: y};
}
</script>
</head>
<body>
<div class="wrapper"></div>
<div class="info">
<h2 class="cvr"></h2>
<h2 class="cnt"></h2>
</div>
</body>
</html>