Skip to content

Commit b3233f0

Browse files
beizhangphilogb
authored andcommitted
Code clean up 7
1 parent 993698b commit b3233f0

File tree

7 files changed

+210
-457
lines changed

7 files changed

+210
-457
lines changed

Source/Geometry/Geometry.js

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ $jit.geometry = {
3232

3333
/**
3434
*
35-
* @param {Number} a1
36-
* @param {Number} a2
37-
* @param {Number} a3
35+
* @param {Complex} p1
36+
* @param {Complex} p2
37+
* @param {Complex} p3
3838
*/
39-
weightOfPoint: function(a1, a2, a3) {
40-
return (a3 - a1) / (a2 - a1);
39+
weightOfPoint: function(p1, p2, p3) {
40+
return (p3.x - p1.x) / (p2.x - p1.x);
4141
},
4242

4343
/**
@@ -95,7 +95,7 @@ $jit.geometry = {
9595
* Reserves those l[0],l[1],c[i] is counter-clockwise
9696
*
9797
* @param {Complex[]} convex
98-
* @param {Complex[][]} l as a ex line
98+
* @param {Complex[]} l as a ex line
9999
* @param {?Object} attached
100100
*/
101101
convexCut : function(convex, l, attached) {
@@ -268,14 +268,30 @@ $jit.geometry = {
268268
* The intersection of two segments
269269
* @param {Complex[]} l1
270270
* @param {Complex[]} l2
271+
* @param {Boolean} [constraint]
271272
* @returns {Complex}
272273
*/
273-
intersectionSeg : function(l1, l2) {
274+
intersectionSeg : function(l1, l2, constraint) {
274275
var c1 = Geometry.cross(l1[0], l2[0], l2[1]),
275276
c2 = Geometry.cross(l1[1], l2[0], l2[1]), k;
276-
if (c1 === c2)
277+
if (c1 == c2) {
277278
return null;
279+
}
278280
k = c1 / (c1 - c2);
281+
if (constraint) {
282+
if (k < 0 || k > 1) {
283+
return null;
284+
}
285+
var c3 = Geometry.cross(l2[0], l1[0], l1[1]),
286+
c4 = Geometry.cross(l2[1], l1[0], l1[1]);
287+
if (c3 == c4) {
288+
return null;
289+
}
290+
var k2 = c3 / (c3 - c4);
291+
if (k2 < 0 || k2 > 1) {
292+
return null;
293+
}
294+
}
279295
return Geometry.weightedPoint(l1[0], l1[1], k);
280296
},
281297

@@ -326,32 +342,6 @@ $jit.geometry = {
326342
return ((dx > 0) ^ (cdx > 0));
327343
},
328344

329-
/**
330-
*
331-
* @param {Complex} p1
332-
* @param {Complex} orig
333-
* @param {Complex} p2
334-
*/
335-
angleBisectorLH: function(p1, orig, p2, dist) {
336-
dist = dist || 1;
337-
p1 = $C(orig.x - p1.x, orig.y - p1.y);
338-
p2 = $C(p2.x - orig.x, p2.y - orig.y);
339-
p1.$scale(1 / p1.norm());
340-
p2.$scale(3 / p2.norm());
341-
var oab = Geometry.weightedPoint(p1, p2, 0.25);
342-
oab.$scale(- dist / oab.norm());
343-
oab.$prod(Complex.IM);
344-
return [orig, orig.add(oab)];
345-
},
346-
347-
/**
348-
* http://en.wikipedia.org/wiki/Straight_skeleton
349-
* @param {Complex[]} polygon
350-
*/
351-
straightSkeleton: function (polygon) {
352-
353-
},
354-
355345
offsetConvex : function(convex, offset) {
356346
if (convex.length < 3) {
357347
return [];
@@ -400,37 +390,29 @@ $jit.geometry = {
400390
for (start = first_line.next; start != first_line; start = start.next) {
401391
result.push(start.cline[1]);
402392
}
393+
if (offset < 0) return Geometry.cleanConvexHull(result);
394+
return result;
395+
},
403396

404-
if (offset < 0) {
405-
if (result.length < 3) {
406-
return Geometry.centroid(convex);
407-
}
408-
var p1 = result[0], p2 = result[1], pos = 1;
409-
for (i = 2; i < result.length; i++) {
410-
var curr = result[i];
411-
if (Geometry.cross(p1, p2, curr) < 0) {
412-
for (i = i + 1; i < result.length; i++) {
413-
var curr2 = result[i];
414-
if (Geometry.cross(p1, p2, curr2) > 0) {
415-
p2 = result[pos] = Geometry.intersectionSeg([p1, p2], [curr, curr2]);
416-
curr = curr2;
417-
break;
418-
} else {
419-
curr = curr2;
420-
}
421-
}
422-
if (i == result.length) {
423-
397+
cleanConvexHull: function(convex) {
398+
var last = convex[0], stack = [last], found = [], inter, np;
399+
for (var i = 1; i <= convex.length; i++) {
400+
var curr = convex[i] || convex[0];
401+
for (var j = stack.length - 1; j > 0; j --) {
402+
var p1 = stack[j], p2 = stack[j - 1];
403+
if (inter = Geometry.intersectionSeg([last, curr], [p1, p2], true)) {
404+
if(Geometry.area(np = [inter].concat(stack.slice(j))) > 0) {
405+
found.push(np);
424406
}
407+
stack.length = j;
408+
stack.push(inter);
425409
}
426-
427-
result[pos++] = curr;
428-
p1 = p2;
429-
p2 = curr;
430410
}
411+
stack.push(curr);
412+
last = curr;
431413
}
432-
433-
return result;
414+
if (Geometry.area(stack)) found.push(stack);
415+
return found[0] || Geometry.centroid(convex);
434416
},
435417

436418
randPointInTriangle : function(t) {
@@ -487,8 +469,8 @@ $jit.geometry = {
487469
* {[Array}
488470
*/
489471
convexHull : function (sites) {
490-
var stack_top = 0, i, temp, base, result;
491-
for (i = 1; i < sites.length; i++) {
472+
var stack_top = 0, i, temp, base, result, N = sites.length;
473+
for (i = 1; i < N; i++) {
492474
if (sites[i].y < sites[0].y
493475
|| (sites[i].y == sites[0].y && sites[i].x < sites[0].x)) {
494476
temp = sites[i];
@@ -499,15 +481,13 @@ $jit.geometry = {
499481
base = sites[0];
500482
sites.sort(Geometry.convexHull.polarComp(base));
501483
result = [sites[0], sites[1]];
502-
stack_top = 1;
503484
for (i = 2; i < N; i++) {
504-
while (stack_top
505-
&& Geometry.cross(result[stack_top - 1], result[stack_top], sites[i]) <= 0) {
506-
stack_top--;
485+
while (result.length && Geometry.cross(result[result.length - 2], result[result.length - 1], sites[i]) <= 0) {
486+
result.pop();
507487
}
508-
result[++stack_top] = sites[i];
488+
result.push(sites[i]);
509489
}
510-
return result.slice(stack_top + 1);
490+
return result;
511491
},
512492

513493
/**

Source/Layouts/Layouts.TM.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Layouts.TM.SliceAndDice = new Class({
3535
});
3636

3737
var config = this.config,
38-
offst = config.offset,
38+
offset = config.offset,
3939
width = par.getData('width', prop),
4040
height = Math.max(par.getData('height', prop) - config.titleHeight, 0),
4141
fact = par == ch? 1 : (ch.getData('area', prop) / totalArea);
@@ -625,19 +625,75 @@ Layouts.TM.Voronoi = new Class({
625625

626626
centroid : function(sites, bound) {
627627
var tdist = 2, polygons;
628-
while (tdist > 1) {
628+
while (tdist > 1e-3) {
629629
polygons = Geometry.voronoi(sites, bound);
630630
tdist = 0;
631631
sites = polygons.map(function(p, j) {
632632
var c = Geometry.centroid(p);
633+
tdist += Geometry.dist2(c, sites[j]);
634+
return c;
635+
});
636+
}
637+
return sites;
638+
},
639+
640+
doLayoutPressure: function () {
641+
var me = this,
642+
root = me.graph.getNode(me.clickedNode && me.clickedNode.id || me.root);
643+
root.eachNode(function() {
644+
645+
});
646+
},
647+
648+
weightedCentroid : function(sites, bound) {
649+
// sites = this.centroid(sites, bound);
650+
var pascal = [];
651+
var tdist = 2, polygons, totalArea = Geometry.area(bound), totalWeight = 0, adjust;
652+
$.each(sites, function(site, i) {
653+
totalWeight += site.area;
654+
});
655+
adjust = totalArea / totalWeight;
656+
polygons = Geometry.voronoi(sites, bound);
657+
$.each(polygons, function(p, i) {
658+
pascal[i] = sites[i].area * adjust / Geometry.area(p);
659+
});
660+
var s = 0, s2 = 0;
661+
$.each(pascal, function(p, i) {
662+
s += p;
663+
s2 += p * p;
664+
});
665+
console.log('from ' + (s2 - s * s / pascal.length) / pascal.length);
666+
while (tdist > 1e-3) {
667+
polygons = Geometry.voronoi(sites, bound);
668+
$.each(polygons, function(p, i) {
669+
pascal[i] = sites[i].area * adjust / Geometry.area(p);
670+
});
671+
tdist = 0;
672+
sites = polygons.map(function(p, j) {
673+
var c = $C(0, 0), totalW = 0;
674+
$.each(p, function(v, i) {
675+
var poly = [sites[j], v, p[i + 1] || p[0]],
676+
targetPascal = (v.attached) ? pascal[v.attached[0]] : 1,
677+
w = Geometry.area(poly) * Math.exp((pascal[j] - targetPascal) * 2);
678+
totalW += w;
679+
c.$add(v.add(poly[2]).scale(0.5 * w));
680+
});
681+
c.$scale(1 / totalW);
682+
c.area = sites[j].area;
633683
tdist += Geometry.dist(c, sites[j]);
634684
return c;
635685
});
636686
}
687+
var s = 0, s2 = 0;
688+
$.each(pascal, function(p, i) {
689+
s += p;
690+
s2 += p * p;
691+
});
692+
console.log('to ' + (s2 - s * s / pascal.length) / pascal.length);
637693
return sites;
638694
},
639695

640-
pressure : function(sites, bound) {
696+
byArea : function(sites, bound) {
641697
sites = this.centroid(sites, bound);
642698
var tw = 0, iter = 0, polygons, pressure, polygons;
643699
$jit.util.each(sites, function(s) {

Source/Visualizations/Voronoi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ TM.Voronoi = new Class({
133133
Implements : [ Loader, Extras, TM.Base, Layouts.TM.Voronoi ],
134134
initialize : function(controller) {
135135
var config = {
136-
centroidType : "centroid",
136+
centroidType : "weightedCentroid",
137137
Node : {
138138
type : 'polygon',
139139
props : 'node-property:width:height:vertices'

Tests/Voronoi/test1.js

Lines changed: 98 additions & 271 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)