Skip to content

Commit

Permalink
remaining fixes for flat input
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Mar 26, 2015
1 parent c461d09 commit 30b1732
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 71 deletions.
14 changes: 2 additions & 12 deletions bench/basic.js

Large diffs are not rendered by default.

38 changes: 11 additions & 27 deletions bench/bench.js

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/earcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ function earcut(data, holeIndices, dim) {

dim = dim || 2;

var outerLen = holeIndices && holeIndices.length ? holeIndices[0] : data.length,
var hasHoles = holeIndices && holeIndices.length,
outerLen = hasHoles ? holeIndices[0] * dim : data.length,
outerNode = filterPoints(data, linkedList(data, 0, outerLen, dim, true)),
triangles = [];

if (!outerNode) return triangles;

var minX, minY, maxX, maxY, x, y, size;

if (holeIndices && holeIndices.length) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);

// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
if (data.length > 80 * dim) {
Expand Down Expand Up @@ -321,8 +322,8 @@ function eliminateHoles(data, holeIndices, outerNode, dim) {
i, len, start, end, list;

for (i = 0, len = holeIndices.length; i < len; i++) {
start = holeIndices[i];
end = i < len - 1 ? holeIndices[i + 1] : data.length;
start = holeIndices[i] * dim;
end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
list = filterPoints(data, linkedList(data, start, end, dim, false));
if (list) queue.push(getLeftmost(data, list));
}
Expand Down Expand Up @@ -380,8 +381,8 @@ function findHoleBridge(data, holeNode, outerNode) {
// if there are no points found, we have a valid connection;
// otherwise choose the point of the minimum angle with the ray as connection point

var bx = data[mNode],
by = data[mNode + 1],
var bx = data[mNode.i],
by = data[mNode.i + 1],
pbd = px * by - py * bx,
pcd = px * py - py * qMax,
cpy = py - py,
Expand Down Expand Up @@ -536,7 +537,7 @@ function getLeftmost(data, start) {
function isValidDiagonal(data, a, b) {
return !intersectsPolygon(data, a, a.i, b.i) &&
locallyInside(data, a, b) && locallyInside(data, b, a) &&
middleInside(data, a, a.p, b.p);
middleInside(data, a, a.i, b.i);
}

// winding order of triangle formed by 3 given points
Expand Down
7 changes: 0 additions & 7 deletions test/fixtures/indices-2d.json

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixtures/indices-3d.json

This file was deleted.

27 changes: 20 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ areaTest('empty-square');
areaTest('issue16');
areaTest('issue17');

test('indices-2d', function (t) {
var indices = earcut([10, 0, 0, 50, 60, 60, 70, 10]);
t.same(indices, [1, 0, 3, 3, 2, 1]);
t.end();
});

test('indices-3d', function (t) {
var indices = earcut([10, 0, 0, 0, 50, 0, 60, 60, 0, 70, 10, 0], null, 3);
t.same(indices, [1, 0, 3, 3, 2, 1]);
t.end();
});

// indicesCreationTest('indices-2d');
// indicesCreationTest('indices-3d');

Expand All @@ -32,10 +44,11 @@ function areaTest(filename, expectedDeviation) {
console.log(filename);

var data = JSON.parse(fs.readFileSync(path.join(__dirname, '/fixtures/' + filename + '.json'))),
result = flattenedEarcut(data),
vertices = result.vertices,
indices = result.indices,
dim = result.dim,
data2 = flattenData(data),
vertices = data2.vertices,
holes = data2.holes,
dim = data2.dim,
indices = earcut(vertices, holes, dim),
expectedArea = polygonArea(data),
area = 0;

Expand All @@ -55,7 +68,7 @@ function areaTest(filename, expectedDeviation) {
});
}

function flattenedEarcut(data) {
function flattenData(data) {
var flat = [],
holes = [],
dim = data[0][0].length,
Expand All @@ -69,13 +82,13 @@ function flattenedEarcut(data) {
}
if (i > 0) {
holeIndex += data[i - 1].length;
holes.push(holeIndex * dim);
holes.push(holeIndex);
}
}

return {
vertices: flat,
indices: earcut(flat, holes, dim),
holes: holes,
dim: dim
};
}
Expand Down
35 changes: 31 additions & 4 deletions viz/viz.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30b1732

Please sign in to comment.