More getConvexHull() clean up.

This commit is contained in:
Jürg Lehni 2013-05-24 21:47:19 -07:00
parent 6374164de7
commit 44be94e55e

View file

@ -288,76 +288,71 @@ function clipFatLine(v1, v2, range2) {
*/ */
function getConvexHull(dq0, dq1, dq2, dq3) { function getConvexHull(dq0, dq1, dq2, dq3) {
var distq1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), var distq1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1),
distq2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), distq2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2);
hull;
// Check if [1/3, dq1] and [2/3, dq2] are on the same side of line // Check if [1/3, dq1] and [2/3, dq2] are on the same side of line
// [0,dq0, 1,dq3] // [0,dq0, 1,dq3]
if (distq1 * distq2 < 0) { if (distq1 * distq2 < 0) {
// dq1 and dq2 lie on different sides on [0, q0, 1, q3]. The hull is a // dq1 and dq2 lie on different sides on [0, q0, 1, q3]. The hull is a
// quadrilateral and line [0, q0, 1, q3] is NOT part of the hull so we // quadrilateral and line [0, q0, 1, q3] is NOT part of the hull so we
// are pretty much done here. // are pretty much done here.
hull = [ return [
[ 0, dq0, 1 / 3, dq1 ], [ 0, dq0, 1 / 3, dq1 ],
[ 1 / 3, dq1, 1, dq3 ], [ 1 / 3, dq1, 1, dq3 ],
[ 2 / 3, dq2, 0, dq0 ], [ 2 / 3, dq2, 0, dq0 ],
[ 1, dq3, 2 / 3, dq2 ] [ 1, dq3, 2 / 3, dq2 ]
]; ];
} else { }
// dq1 and dq2 lie on the same sides on [0, q0, 1, q3]. The hull can be // dq1 and dq2 lie on the same sides on [0, q0, 1, q3]. The hull can be
// a triangle or a quadrilateral and line [0, q0, 1, q3] is part of the // a triangle or a quadrilateral and line [0, q0, 1, q3] is part of the
// hull. Check if the hull is a triangle or a quadrilateral. // hull. Check if the hull is a triangle or a quadrilateral.
distq1 = Math.abs(distq1); var dqMaxX, dqMaxY, vqa1a2X, vqa1a2Y, vqa1MaxX, vqa1MaxY, vqa1MinX, vqa1MinY;
distq2 = Math.abs(distq2); if (Math.abs(distq1) > Math.abs(distq2)) {
var dqmax, vqa1a2x, vqa1a2y, vqa1Maxx, vqa1Maxy, vqa1Minx, vqa1Miny; dqMaxX = 1 / 3;
if (distq1 > distq2) { dqMaxY = dq1;
dqmax = [ 1 / 3, dq1 ];
// apex is dq3 and the other apex point is dq0 vector // apex is dq3 and the other apex point is dq0 vector
// dqapex->dqapex2 or base vector which is already part of the hull. // dqapex->dqapex2 or base vector which is already part of the hull.
vqa1a2x = 1; vqa1a2X = 1;
vqa1a2y = dq3 - dq0; vqa1a2Y = dq3 - dq0;
// vector dqapex->dqmax // vector dqapex->dqMax
vqa1Maxx = 2 / 3; vqa1MaxX = 2 / 3;
vqa1Maxy = dq3 - dq1; vqa1MaxY = dq3 - dq1;
// vector dqapex->dqmin // vector dqapex->dqmin
vqa1Minx = 1 / 3; vqa1MinX = 1 / 3;
vqa1Miny = dq3 - dq2; vqa1MinY = dq3 - dq2;
} else { } else {
dqmax = [ 2 / 3, dq2 ]; dqMaxX = 2 / 3;
dqMaxY = dq2;
// apex is dq0 in this case, and the other apex point is dq3 vector // apex is dq0 in this case, and the other apex point is dq3 vector
// dqapex->dqapex2 or base vector which is already part of the hull. // dqapex->dqapex2 or base vector which is already part of the hull.
vqa1a2x = -1; vqa1a2X = -1;
vqa1a2y = dq0 - dq3; vqa1a2Y = dq0 - dq3;
// vector dqapex->dqmax // vector dqapex->dqMax
vqa1Maxx = -2 / 3; vqa1MaxX = -2 / 3;
vqa1Maxy = dq0 - dq2; vqa1MaxY = dq0 - dq2;
// vector dqapex->dqmin // vector dqapex->dqmin
vqa1Minx = -1 / 3; vqa1MinX = -1 / 3;
vqa1Miny = dq0 - dq1; vqa1MinY = dq0 - dq1;
} }
// Compare cross products of these vectors to determine, if // Compare cross products of these vectors to determine, if
// point is in triangles [ dq3, dqMax, dq0 ] or [ dq0, dqMax, dq3 ] // point is in triangles [ dq3, dqMax, dq0 ] or [ dq0, dqMax, dq3 ]
var vcrossa1a2_a1Min = vqa1a2x * vqa1Miny - vqa1a2y * vqa1Minx; var a1a2_a1Min = vqa1a2X * vqa1MinY - vqa1a2Y * vqa1MinX,
var vcrossa1Max_a1Min = vqa1Maxx * vqa1Miny - vqa1Maxy * vqa1Minx; a1Max_a1Min = vqa1MaxX * vqa1MinY - vqa1MaxY * vqa1MinX;
if (vcrossa1Max_a1Min * vcrossa1a2_a1Min < 0) { return a1a2_a1Min * a1Max_a1Min < 0
// Point [2/3, dq2] is inside the triangle, the hull is a triangle. // Point [2/3, dq2] is inside the triangle, the hull is a triangle.
hull = [ ? [
[ 0, dq0, dqmax[0], dqmax[1] ], [ 0, dq0, dqMaxX, dqMaxY ],
[ dqmax[0], dqmax[1], 1, dq3 ], [ dqMaxX, dqMaxY, 1, dq3 ],
[ 1, dq3, 0, dq0 ] [ 1, dq3, 0, dq0 ]
]; ]
} else {
// Convexhull is a quadrilateral and we need all lines in the // Convexhull is a quadrilateral and we need all lines in the
// correct order where line [0, q0, 1, q3] is part of the hull. // correct order where line [0, q0, 1, q3] is part of the hull.
hull = [ : [
[ 0, dq0, 1 / 3, dq1 ], [ 0, dq0, 1 / 3, dq1 ],
[ 1 / 3, dq1, 2 / 3, dq2 ], [ 1 / 3, dq1, 2 / 3, dq2 ],
[ 2 / 3, dq2, 1, dq3 ], [ 2 / 3, dq2, 1, dq3 ],
[ 1, dq3, 0, dq0 ] [ 1, dq3, 0, dq0 ]
]; ];
} }
}
return hull;
}
// This is basically an "unrolled" version of #Line.getDistance() with sign // This is basically an "unrolled" version of #Line.getDistance() with sign
// May be a static method could be better! // May be a static method could be better!