Skip to content

yet another contour algorithm bug fix #2091

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

Merged
merged 3 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 30 additions & 31 deletions src/traces/contour/find_all_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,13 @@ function makePath(pi, loc, edgeflag, xtol, ytol) {
if(cnt === 10000) {
Lib.log('Infinite loop in contour?');
}
var closedpath = equalPts(pts[0], pts[pts.length - 1], xtol, ytol),
totaldist = 0,
distThresholdFactor = 0.2 * pi.smoothing,
alldists = [],
cropstart = 0,
distgroup,
cnt2,
cnt3,
newpt,
ptcnt,
ptavg,
thisdist;
var closedpath = equalPts(pts[0], pts[pts.length - 1], xtol, ytol);
var totaldist = 0;
var distThresholdFactor = 0.2 * pi.smoothing;
var alldists = [];
var cropstart = 0;
var distgroup, cnt2, cnt3, newpt, ptcnt, ptavg, thisdist,
i, j, edgepathi, edgepathj;

/*
* Check for points that are too close together (<1/5 the average dist
Expand Down Expand Up @@ -198,41 +193,45 @@ function makePath(pi, loc, edgeflag, xtol, ytol) {

// edge path - does it start where an existing edge path ends, or vice versa?
var merged = false;
pi.edgepaths.forEach(function(edgepath, edgei) {
if(!merged && equalPts(edgepath[0], pts[pts.length - 1], xtol, ytol)) {
for(i = 0; i < pi.edgepaths.length; i++) {
edgepathi = pi.edgepaths[i];
if(!merged && equalPts(edgepathi[0], pts[pts.length - 1], xtol, ytol)) {
pts.pop();
merged = true;

// now does it ALSO meet the end of another (or the same) path?
var doublemerged = false;
pi.edgepaths.forEach(function(edgepath2, edgei2) {
if(!doublemerged && equalPts(
edgepath2[edgepath2.length - 1], pts[0], xtol, ytol)) {
for(j = 0; j < pi.edgepaths.length; j++) {
edgepathj = pi.edgepaths[j];
if(equalPts(edgepathj[edgepathj.length - 1], pts[0], xtol, ytol)) {
doublemerged = true;
pts.splice(0, 1);
pi.edgepaths.splice(edgei, 1);
if(edgei2 === edgei) {
pts.shift();
pi.edgepaths.splice(i, 1);
if(j === i) {
// the path is now closed
pi.paths.push(pts.concat(edgepath2));
pi.paths.push(pts.concat(edgepathj));
}
else {
pi.edgepaths[edgei2] =
pi.edgepaths[edgei2].concat(pts, edgepath2);
if(j > i) j--;
pi.edgepaths[j] = edgepathj.concat(pts, edgepathi);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the end it was all about these two lines - getting the right index (if(j > i) j--;) and using the right paths (previously we were doubling up edgepath2 <-> edgepathj but because of the poor usage it was hard to tell that. Surprising that we hadn't hit this before in our tests, but now we do!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Then this also applies to carpet contour plots which use the same function. 🎉

}
break;
}
});
}
if(!doublemerged) {
pi.edgepaths[edgei] = pts.concat(edgepath);
pi.edgepaths[i] = pts.concat(edgepathi);
}
}
});
pi.edgepaths.forEach(function(edgepath, edgei) {
if(!merged && equalPts(edgepath[edgepath.length - 1], pts[0], xtol, ytol)) {
pts.splice(0, 1);
pi.edgepaths[edgei] = edgepath.concat(pts);
}
for(i = 0; i < pi.edgepaths.length; i++) {
if(merged) break;
edgepathi = pi.edgepaths[i];
if(equalPts(edgepathi[edgepathi.length - 1], pts[0], xtol, ytol)) {
pts.shift();
pi.edgepaths[i] = edgepathi.concat(pts);
merged = true;
}
});
}

if(!merged) pi.edgepaths.push(pts);
}
Expand Down
Binary file added test/image/baselines/contour_doublemerge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions test/image/mocks/contour_doublemerge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"data": [{
"z": [
[2.38,2.34,2.36,2.37,2.36,2.37,2.36,2.36,2.34,2.35,2.34,2.36,2.38,2.37,2.35,2.34,2.35,2.36,2.36,2.34,2.33,2.34,2.33,2.37],
[2.38,2.35,2.37,2.38,2.35,2.35,2.36,2.38,2.34,2.35,2.35,2.35,2.38,2.36,2.35,2.35,2.34,2.35,2.35,2.33,2.34,2.34,2.33,2.36],
[2.36,2.33,2.32,2.33,2.34,2.34,2.35,2.31,2.31,2.32,2.30,2.33,2.35,2.33,2.32,2.31,2.35,2.33,2.33,2.31,2.32,2.30,2.31,2.34],
[2.36,2.34,2.32,2.31,2.32,2.34,2.35,2.33,2.31,2.34,2.33,2.33,2.35,2.33,2.32,2.32,2.35,2.33,2.33,2.33,2.33,2.30,2.32,2.34],
[2.35,2.35,2.32,2.31,2.30,2.31,2.32,2.32,2.31,2.31,2.32,2.33,2.32,2.32,2.32,2.29,2.32,2.31,2.31,2.31,2.30,2.31,2.30,2.32],
[2.34,2.33,2.31,2.35,2.31,2.33,2.34,2.34,2.32,2.31,2.32,2.33,2.33,2.32,2.33,2.33,2.33,2.33,2.32,2.32,2.32,2.33,2.33,2.32],
[2.33,2.32,2.32,2.31,2.31,2.32,2.33,2.31,2.32,2.31,2.30,2.31,2.34,2.32,2.32,2.31,2.32,2.32,2.32,2.31,2.31,2.31,2.30,2.34],
[2.34,2.32,2.33,2.33,2.32,2.32,2.32,2.31,2.32,2.30,2.32,2.33,2.33,2.32,2.32,2.32,2.32,2.32,2.33,2.32,2.32,2.31,2.32,2.32],
[2.32,2.32,2.31,2.30,2.31,2.31,2.31,2.29,2.31,2.32,2.30,2.33,2.33,2.29,2.31,2.32,2.31,2.33,2.32,2.31,2.31,2.31,2.30,2.32],
[2.33,2.33,2.32,2.33,2.33,2.33,2.32,2.32,2.32,2.32,2.30,2.32,2.31,2.33,2.32,2.33,2.32,2.33,2.34,2.31,2.32,2.31,2.31,2.32],
[2.33,2.31,2.31,2.30,2.30,2.31,2.32,2.33,2.29,2.29,2.30,2.31,2.32,2.32,2.32,2.31,2.32,2.30,2.32,2.30,2.31,2.29,2.29,2.30],
[2.32,2.31,2.32,2.32,2.31,2.32,2.33,2.33,2.31,2.31,2.32,2.33,2.33,2.32,2.33,2.33,2.30,2.32,2.31,2.30,2.30,2.31,2.31,2.31],
[2.33,2.32,2.32,2.30,2.30,2.30,2.30,2.31,2.30,2.31,2.31,2.30,2.32,2.31,2.29,2.31,2.31,2.31,2.31,2.31,2.31,2.30,2.30,2.32],
[2.33,2.33,2.32,2.32,2.30,2.33,2.31,2.31,2.32,2.32,2.32,2.32,2.34,2.31,2.32,2.32,2.32,2.33,2.32,2.31,2.31,2.31,2.31,2.32],
[2.33,2.33,2.30,2.33,2.31,2.32,2.31,2.31,2.30,2.31,2.30,2.32,2.33,2.32,2.31,2.30,2.31,2.33,2.31,2.32,2.30,2.31,2.31,2.32],
[2.33,2.33,2.31,2.32,2.32,2.33,2.33,2.32,2.32,2.34,2.33,2.33,2.33,2.34,2.32,2.31,2.31,2.32,2.32,2.32,2.30,2.32,2.32,2.32],
[2.32,2.31,2.32,2.33,2.31,2.33,2.32,2.32,2.32,2.31,2.31,2.37,2.32,2.31,2.31,2.30,2.31,2.32,2.31,2.31,2.30,2.31,2.31,2.35],
[2.33,2.32,2.32,2.31,2.34,2.35,2.32,2.33,2.33,2.32,2.33,2.34,2.33,2.33,2.32,2.32,2.33,2.34,2.32,2.32,2.31,2.31,2.32,2.33],
[2.32,2.34,2.30,2.33,2.30,2.32,2.33,2.33,2.32,2.31,2.30,2.31,2.32,2.30,2.31,2.29,2.31,2.31,2.31,2.31,2.30,2.32,2.30,2.31],
[2.32,2.32,2.32,2.32,2.32,2.33,2.34,2.34,2.32,2.32,2.32,2.32,2.34,2.30,2.31,2.34,2.32,2.33,2.32,2.30,2.31,2.34,2.31,2.32],
[2.31,2.30,2.33,2.30,2.29,2.30,2.31,2.32,2.30,2.30,2.32,2.31,2.32,2.32,2.31,2.31,2.30,2.32,2.31,2.31,2.31,2.31,2.30,2.29],
[2.32,2.32,2.31,2.31,2.31,2.34,2.32,2.32,2.33,2.30,2.32,2.33,2.32,2.32,2.31,2.31,2.31,2.32,2.29,2.32,2.31,2.31,2.31,2.32],
[2.32,2.30,2.31,2.31,2.29,2.32,2.32,2.33,2.30,2.31,2.30,2.30,2.31,2.32,2.30,2.30,2.31,2.31,2.32,2.31,2.30,2.31,2.32,2.31],
[2.32,2.31,2.32,2.34,2.32,2.32,2.32,2.32,2.32,2.32,2.32,2.34,2.34,2.32,2.32,2.34,2.31,2.30,2.33,2.31,2.34,2.31,2.33,2.31],
[2.33,2.31,2.29,2.31,2.32,2.30,2.34,2.31,2.31,2.32,2.32,2.31,2.31,2.30,2.31,2.29,2.31,2.31,2.31,2.29,2.31,2.31,2.33,2.30],
[2.33,2.30,2.31,2.31,2.31,2.32,2.35,2.33,2.32,2.31,2.31,2.31,2.32,2.32,2.31,2.32,2.31,2.33,2.31,2.33,2.33,2.33,2.32,2.33],
[2.30,2.30,2.31,2.33,2.30,2.31,2.31,2.31,2.29,2.32,2.29,2.31,2.31,2.31,2.31,2.30,2.29,2.32,2.31,2.31,2.31,2.32,2.32,2.30],
[2.32,2.31,2.31,2.31,2.32,2.33,2.32,2.34,2.30,2.32,2.33,2.31,2.33,2.32,2.31,2.32,2.32,2.32,2.32,2.30,2.32,2.32,2.32,2.32],
[2.34,2.33,2.30,2.30,2.33,2.30,2.31,2.31,2.31,2.30,2.31,2.31,2.35,2.31,2.32,2.32,2.30,2.31,2.32,2.32,2.31,2.31,2.31,2.31],
[2.35,2.32,2.30,2.32,2.31,2.32,2.33,2.34,2.34,2.31,2.33,2.32,2.35,2.36,2.31,2.34,2.33,2.33,2.32,2.33,2.32,2.32,2.33,2.34],
[2.34,2.31,2.30,2.31,2.32,2.34,2.34,2.29,2.29,2.30,2.30,2.32,2.31,2.32,2.32,2.31,2.30,2.31,2.33,2.32,2.33,2.32,2.29,2.32],
[2.33,2.34,2.33,2.33,2.32,2.34,2.34,2.33,2.32,2.33,2.33,2.33,2.33,2.32,2.33,2.33,2.31,2.33,2.33,2.32,2.33,2.34,2.32,2.31],
[2.33,2.32,2.31,2.30,2.32,2.31,2.31,2.32,2.31,2.30,2.29,2.32,2.33,2.31,2.33,2.30,2.30,2.31,2.31,2.31,2.32,2.32,2.34,2.32],
[2.32,2.32,2.31,2.32,2.32,2.32,2.35,2.33,2.33,2.31,2.32,2.34,2.32,2.32,2.36,2.33,2.33,2.33,2.32,2.34,2.36,2.32,2.34,2.32],
[2.32,2.30,2.30,2.32,2.32,2.32,2.31,2.31,2.31,2.29,2.30,2.34,2.32,2.32,2.30,2.31,2.34,2.32,2.32,2.32,2.32,2.30,2.30,2.30],
[2.32,2.30,2.33,2.33,2.34,2.32,2.32,2.31,2.32,2.31,2.33,2.32,2.34,2.33,2.31,2.33,2.33,2.32,2.32,2.31,2.33,2.31,2.31,2.32],
[2.30,2.33,2.30,2.31,2.32,2.30,2.31,2.31,2.31,2.30,2.31,2.33,2.32,2.30,2.30,2.32,2.30,2.31,2.31,2.31,2.30,2.30,2.30,2.31],
[2.34,2.32,2.33,2.34,2.33,2.33,2.33,2.32,2.32,2.32,2.33,2.34,2.32,2.32,2.32,2.31,2.34,2.32,2.31,2.31,2.32,2.31,2.31,2.33],
[2.32,2.32,2.31,2.30,2.31,2.30,2.31,2.30,2.31,2.31,2.30,2.32,2.33,2.31,2.31,2.30,2.31,2.32,2.31,2.31,2.31,2.30,2.31,2.32],
[2.33,2.32,2.33,2.32,2.32,2.33,2.32,2.32,2.33,2.31,2.33,2.32,2.32,2.32,2.33,2.31,2.32,2.34,2.33,2.31,2.33,2.31,2.34,2.34],
[2.34,2.31,2.31,2.32,2.30,2.34,2.32,2.31,2.30,2.30,2.31,2.32,2.33,2.31,2.30,2.32,2.30,2.32,2.32,2.32,2.29,2.32,2.31,2.31],
[2.33,2.33,2.32,2.34,2.34,2.33,2.32,2.31,2.32,2.33,2.33,2.31,2.33,2.32,2.32,2.36,2.31,2.33,2.31,2.32,2.31,2.32,2.33,2.31],
[2.33,2.31,2.30,2.31,2.31,2.31,2.31,2.31,2.31,2.29,2.30,2.31,2.31,2.30,2.29,2.30,2.31,2.31,2.31,2.30,2.31,2.30,2.30,2.33],
[2.32,2.31,2.33,2.29,2.30,2.32,2.31,2.32,2.31,2.30,2.32,2.33,2.32,2.32,2.32,2.33,2.31,2.32,2.32,2.32,2.32,2.32,2.30,2.32],
[2.33,2.32,2.32,2.32,2.30,2.33,2.31,2.32,2.31,2.30,2.31,2.33,2.33,2.32,2.32,2.31,2.31,2.31,2.31,2.31,2.29,2.31,2.31,2.31],
[2.33,2.33,2.33,2.32,2.31,2.33,2.33,2.32,2.34,2.33,2.34,2.34,2.33,2.33,2.31,2.32,2.31,2.33,2.34,2.31,2.32,2.32,2.32,2.33],
[2.35,2.33,2.31,2.32,2.32,2.34,2.35,2.31,2.32,2.30,2.31,2.33,2.33,2.32,2.32,2.32,2.32,2.40,2.33,2.32,2.34,2.31,2.31,2.34],
[2.35,2.33,2.33,2.32,2.33,2.34,2.34,2.32,2.33,2.32,2.33,2.34,2.36,2.33,2.33,2.34,2.34,2.34,2.34,2.34,2.34,2.32,2.34,2.35]],
"type": "contour",
"contours": {
"start": 2.2,
"end": 2.4,
"size": 0.02
}
}],
"layout": {"width": 400, "height": 600}
}