1111var Lib = require ( '../../lib' ) ;
1212var constants = require ( './constants' ) ;
1313
14- module . exports = function findAllPaths ( pathinfo ) {
14+ module . exports = function findAllPaths ( pathinfo , xtol , ytol ) {
1515 var cnt ,
1616 startLoc ,
1717 i ,
1818 pi ,
1919 j ;
2020
21+ // Default just passes these values through as they were before:
22+ xtol = xtol || 0.01 ;
23+ ytol = ytol || 0.01 ;
24+
2125 for ( i = 0 ; i < pathinfo . length ; i ++ ) {
2226 pi = pathinfo [ i ] ;
2327
2428 for ( j = 0 ; j < pi . starts . length ; j ++ ) {
2529 startLoc = pi . starts [ j ] ;
26- makePath ( pi , startLoc , 'edge' ) ;
30+ makePath ( pi , startLoc , 'edge' , xtol , ytol ) ;
2731 }
2832
2933 cnt = 0 ;
3034 while ( Object . keys ( pi . crossings ) . length && cnt < 10000 ) {
3135 cnt ++ ;
3236 startLoc = Object . keys ( pi . crossings ) [ 0 ] . split ( ',' ) . map ( Number ) ;
33- makePath ( pi , startLoc ) ;
37+ makePath ( pi , startLoc , undefined , xtol , ytol ) ;
3438 }
3539 if ( cnt === 10000 ) Lib . log ( 'Infinite loop in contour?' ) ;
3640 }
3741} ;
3842
39- function equalPts ( pt1 , pt2 ) {
40- return Math . abs ( pt1 [ 0 ] - pt2 [ 0 ] ) < 0.01 &&
41- Math . abs ( pt1 [ 1 ] - pt2 [ 1 ] ) < 0.01 ;
43+ function equalPts ( pt1 , pt2 , xtol , ytol ) {
44+ return Math . abs ( pt1 [ 0 ] - pt2 [ 0 ] ) < xtol &&
45+ Math . abs ( pt1 [ 1 ] - pt2 [ 1 ] ) < ytol ;
4246}
4347
4448function ptDist ( pt1 , pt2 ) {
@@ -47,17 +51,17 @@ function ptDist(pt1, pt2) {
4751 return Math . sqrt ( dx * dx + dy * dy ) ;
4852}
4953
50- function makePath ( pi , loc , edgeflag ) {
51- var startLocStr = loc . join ( ',' ) ,
52- locStr = startLocStr ,
53- mi = pi . crossings [ locStr ] ,
54- marchStep = startStep ( mi , edgeflag , loc ) ,
55- // start by going backward a half step and finding the crossing point
56- pts = [ getInterpPx ( pi , loc , [ - marchStep [ 0 ] , - marchStep [ 1 ] ] ) ] ,
57- startStepStr = marchStep . join ( ',' ) ,
58- m = pi . z . length ,
59- n = pi . z [ 0 ] . length ,
60- cnt ;
54+ function makePath ( pi , loc , edgeflag , xtol , ytol ) {
55+ var startLocStr = loc . join ( ',' ) ;
56+ var locStr = startLocStr ;
57+ var mi = pi . crossings [ locStr ] ;
58+ var marchStep = startStep ( mi , edgeflag , loc ) ;
59+ // start by going backward a half step and finding the crossing point
60+ var pts = [ getInterpPx ( pi , loc , [ - marchStep [ 0 ] , - marchStep [ 1 ] ] ) ] ;
61+ var startStepStr = marchStep . join ( ',' ) ;
62+ var m = pi . z . length ;
63+ var n = pi . z [ 0 ] . length ;
64+ var cnt ;
6165
6266 // now follow the path
6367 for ( cnt = 0 ; cnt < 10000 ; cnt ++ ) { // just to avoid infinite loops
@@ -81,7 +85,7 @@ function makePath(pi, loc, edgeflag) {
8185 loc [ 1 ] += marchStep [ 1 ] ;
8286
8387 // don't include the same point multiple times
84- if ( equalPts ( pts [ pts . length - 1 ] , pts [ pts . length - 2 ] ) ) pts . pop ( ) ;
88+ if ( equalPts ( pts [ pts . length - 1 ] , pts [ pts . length - 2 ] , xtol , ytol ) ) pts . pop ( ) ;
8589 locStr = loc . join ( ',' ) ;
8690
8791 var atEdge = ( marchStep [ 0 ] && ( loc [ 0 ] < 0 || loc [ 0 ] > n - 2 ) ) ||
@@ -97,7 +101,7 @@ function makePath(pi, loc, edgeflag) {
97101 if ( cnt === 10000 ) {
98102 Lib . log ( 'Infinite loop in contour?' ) ;
99103 }
100- var closedpath = equalPts ( pts [ 0 ] , pts [ pts . length - 1 ] ) ,
104+ var closedpath = equalPts ( pts [ 0 ] , pts [ pts . length - 1 ] , xtol , ytol ) ,
101105 totaldist = 0 ,
102106 distThresholdFactor = 0.2 * pi . smoothing ,
103107 alldists = [ ] ,
@@ -186,15 +190,15 @@ function makePath(pi, loc, edgeflag) {
186190 // edge path - does it start where an existing edge path ends, or vice versa?
187191 var merged = false ;
188192 pi . edgepaths . forEach ( function ( edgepath , edgei ) {
189- if ( ! merged && equalPts ( edgepath [ 0 ] , pts [ pts . length - 1 ] ) ) {
193+ if ( ! merged && equalPts ( edgepath [ 0 ] , pts [ pts . length - 1 ] , xtol , ytol ) ) {
190194 pts . pop ( ) ;
191195 merged = true ;
192196
193197 // now does it ALSO meet the end of another (or the same) path?
194198 var doublemerged = false ;
195199 pi . edgepaths . forEach ( function ( edgepath2 , edgei2 ) {
196200 if ( ! doublemerged && equalPts (
197- edgepath2 [ edgepath2 . length - 1 ] , pts [ 0 ] ) ) {
201+ edgepath2 [ edgepath2 . length - 1 ] , pts [ 0 ] , xtol , ytol ) ) {
198202 doublemerged = true ;
199203 pts . splice ( 0 , 1 ) ;
200204 pi . edgepaths . splice ( edgei , 1 ) ;
@@ -214,7 +218,7 @@ function makePath(pi, loc, edgeflag) {
214218 }
215219 } ) ;
216220 pi . edgepaths . forEach ( function ( edgepath , edgei ) {
217- if ( ! merged && equalPts ( edgepath [ edgepath . length - 1 ] , pts [ 0 ] ) ) {
221+ if ( ! merged && equalPts ( edgepath [ edgepath . length - 1 ] , pts [ 0 ] , xtol , ytol ) ) {
218222 pts . splice ( 0 , 1 ) ;
219223 pi . edgepaths [ edgei ] = edgepath . concat ( pts ) ;
220224 merged = true ;
@@ -257,6 +261,7 @@ function getInterpPx(pi, loc, step) {
257261
258262 if ( step [ 1 ] ) {
259263 var dx = ( pi . level - zxy ) / ( pi . z [ locy ] [ locx + 1 ] - zxy ) ;
264+
260265 return [ xa . c2p ( ( 1 - dx ) * pi . x [ locx ] + dx * pi . x [ locx + 1 ] , true ) ,
261266 ya . c2p ( pi . y [ locy ] , true ) ] ;
262267 }
0 commit comments