Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor connect edges #113

Merged
merged 17 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 79 additions & 45 deletions demo/js/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,15 @@
return sortedEvents;
}

var Contour = function Contour () {

this.points = [];
this.holes = [];
this.external = true;
this.precomputedCC = false;

};

/**
* @param {Array.<SweepEvent>} sortedEvents
* @return {Array.<SweepEvent>}
Expand Down Expand Up @@ -1709,19 +1718,23 @@
* @param {Object>} processed
* @return {Number}
*/
function nextPos(pos, resultEvents, processed, origIndex) {
var p, p1;
var newPos = pos + 1;
function nextPos(pos, resultEvents, processed) {
var newPos = pos + 1,
p = resultEvents[pos].point,
p1;
var length = resultEvents.length;

p = resultEvents[pos].point;

if (newPos < length)
{ p1 = resultEvents[newPos].point; }


// while in range and not the current one by value
while (newPos < length && p1[0] === p[0] && p1[1] === p[1]) {

// if (newPos === -1) {
// throw `Problem with pos, ${pos}, ${newPos}`;
// }
if (!processed[newPos]) {
return newPos;
} else {
Expand All @@ -1732,9 +1745,10 @@

newPos = pos - 1;

while (processed[newPos] && newPos >= origIndex) {
while (processed[newPos]) {
newPos--;
}

return newPos;
}

Expand All @@ -1743,68 +1757,74 @@
* @param {Array.<SweepEvent>} sortedEvents
* @return {Array.<*>} polygons
*/
function connectEdges(sortedEvents, operation) {
function connectEdges(sortedEvents) {
var i, len;
var resultEvents = orderEvents(sortedEvents);

// "false"-filled array
var processed = {};
var result = [];
var event;
var depth = [];
var holeOf = [];

for (i = 0, len = resultEvents.length; i < len; i++) {
if (processed[i]) { continue; }
var contour = [[]];

if (!resultEvents[i].isExteriorRing) {
if (operation === DIFFERENCE && !resultEvents[i].isSubject && result.length === 0) {
result.push(contour);
} else if (result.length === 0) {
result.push([[contour]]);
} else {
result[result.length - 1].push(contour[0]);
if (processed[i]) { continue; }
var contour = new Contour();
result.push(contour);
var contourId = result.length - 1;
depth.push(0);
holeOf.push(-1);

if (resultEvents[i].prevInResult) {
var lowerContourId = resultEvents[i].prevInResult.contourId;
if (!resultEvents[i].prevInResult.resultInOut) {
result[lowerContourId].holes.push(contourId);
holeOf[contourId] = lowerContourId;
depth[contourId] = depth[lowerContourId] + 1;
contour.external = false;
} else if (!result[lowerContourId].external) {
result[holeOf[lowerContourId]].holes.push(contourId);
holeOf[contourId] = holeOf[lowerContourId];
depth[contourId] = depth[lowerContourId];
contour.external = false;
}
} else if (operation === DIFFERENCE && !resultEvents[i].isSubject && result.length > 1) {
result[result.length - 1].push(contour[0]);
} else {
result.push(contour);
}

var ringId = result.length - 1;
var pos = i;

var initial = resultEvents[i].point;
contour[0].push(initial);
contour.points.push(initial);

while (pos >= i) {
event = resultEvents[pos];
processed[pos] = true;

if (event.left) {
event.resultInOut = false;
event.contourId = ringId;
} else {
event.otherEvent.resultInOut = true;
event.otherEvent.contourId = ringId;
}
while (resultEvents[pos] && resultEvents[pos].otherEvent.point !== initial) {
event = resultEvents[pos];
processed[pos] = true;

pos = event.pos;
processed[pos] = true;
contour[0].push(resultEvents[pos].point);
pos = nextPos(pos, resultEvents, processed, i);
}
if (event.left) {
event.resultInOut = false;
event.contourId = contourId;
} else {
event.otherEvent.resultInOut = true;
event.otherEvent.contourId = contourId;
}

pos = event.pos;
processed[pos] = true;
contour.points.push(resultEvents[pos].point);
pos = nextPos(pos, resultEvents, processed);
}
pos = pos === -1 ? i : pos;

event = resultEvents[pos];
processed[pos] = processed[event.pos] = true;
event.otherEvent.resultInOut = true;
event.otherEvent.contourId = ringId;
event.otherEvent.contourId = contourId;

contour.points.push(contour.points[0]);
}

// Handle if the result is a polygon (eg not multipoly)
// Commented it again, let's see what do we mean by that
// if (result.length === 1) result = result[0];
return result;
}

Expand Down Expand Up @@ -2018,22 +2038,36 @@
var sbbox = [Infinity, Infinity, -Infinity, -Infinity];
var cbbox = [Infinity, Infinity, -Infinity, -Infinity];

//console.time('fill queue');
// console.time('fill queue');
var eventQueue = fillQueue(subject, clipping, sbbox, cbbox, operation);
//console.timeEnd('fill queue');

trivial = compareBBoxes(subject, clipping, sbbox, cbbox, operation);
if (trivial) {
return trivial === EMPTY ? null : trivial;
}
//console.time('subdivide edges');
// console.time('subdivide edges');
var sortedEvents = subdivide(eventQueue, subject, clipping, sbbox, cbbox, operation);
//console.timeEnd('subdivide edges');

//console.time('connect vertices');
// console.time('connect vertices');
var result = connectEdges(sortedEvents, operation);
// console.log(result)
//console.timeEnd('connect vertices');
return result;

var out = [];
for (var i = 0; i < result.length; i++) {
var contour = result[i];
if (contour.external) {
var outCoords = [contour.points];
for (var ii = 0; ii < contour.holes.length; ii++) {
contour.holes[ii];
outCoords.push(result[contour.holes[ii]].points);
}
out.push(outCoords);
}
}
return out;
}

function union (subject, clipping) {
Expand Down Expand Up @@ -2223,7 +2257,7 @@

//if (op === OPERATIONS.UNION) result = result[0];
console.log('result', result);
// console.log(JSON.stringify(result))
console.log(JSON.stringify(result));
results.clearLayers();

if (result !== null) {
Expand Down
2 changes: 1 addition & 1 deletion demo/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function run (op) {

//if (op === OPERATIONS.UNION) result = result[0];
console.log('result', result);
// console.log(JSON.stringify(result))
console.log(JSON.stringify(result))
results.clearLayers();

if (result !== null) {
Expand Down
2 changes: 1 addition & 1 deletion dist/martinez.min.js

Large diffs are not rendered by default.

Loading