@@ -24,13 +24,13 @@ export class Job {
2424 private indexers : Indexer [ ] ;
2525 inprogressPath : Path | undefined ;
2626
27- constructor ( state ?: State ) {
27+ constructor ( opts : { state ?: State ; minLayerThreshold ?: number } = { } ) {
2828 this . paths = [ ] ;
29- this . state = state || State . initial ;
29+ this . state = opts . state || State . initial ;
3030 this . layersPaths = [ [ ] ] ;
3131 this . indexers = [
3232 new TravelTypeIndexer ( { travel : this . travelPaths , extrusion : this . extrusionPaths } ) ,
33- new LayersIndexer ( this . layersPaths )
33+ new LayersIndexer ( this . layersPaths , opts . minLayerThreshold )
3434 ] ;
3535 }
3636
@@ -61,7 +61,7 @@ export class Job {
6161 }
6262
6363 isPlanar ( ) : boolean {
64- return this . paths . find ( ( path ) => path . travelType === PathType . Extrusion && path . hasVerticalMoves ( ) ) === undefined ;
64+ return this . layersPaths !== null ;
6565 }
6666
6767 private indexPath ( path : Path ) : void {
@@ -84,7 +84,7 @@ export class Job {
8484}
8585
8686class NonApplicableIndexer extends Error { }
87- class Indexer {
87+ export class Indexer {
8888 protected indexes : Record < string , Path [ ] > | Path [ ] [ ] ;
8989 constructor ( indexes : Record < string , Path [ ] > | Path [ ] [ ] ) {
9090 this . indexes = indexes ;
@@ -95,7 +95,7 @@ class Indexer {
9595 }
9696}
9797
98- class TravelTypeIndexer extends Indexer {
98+ export class TravelTypeIndexer extends Indexer {
9999 protected declare indexes : Record < string , Path [ ] > ;
100100 constructor ( indexes : Record < string , Path [ ] > ) {
101101 super ( indexes ) ;
@@ -115,10 +115,13 @@ class NonPlanarPathError extends NonApplicableIndexer {
115115 super ( "Non-planar paths can't be indexed by layer" ) ;
116116 }
117117}
118- class LayersIndexer extends Indexer {
118+ export class LayersIndexer extends Indexer {
119+ static readonly DEFAULT_TOLERANCE = 0.05 ;
119120 protected declare indexes : Path [ ] [ ] ;
120- constructor ( indexes : Path [ ] [ ] ) {
121+ private tolerance : number ;
122+ constructor ( indexes : Path [ ] [ ] , tolerance : number = LayersIndexer . DEFAULT_TOLERANCE ) {
121123 super ( indexes ) ;
124+ this . tolerance = tolerance ;
122125 }
123126
124127 sortIn ( path : Path ) : void {
@@ -129,10 +132,17 @@ class LayersIndexer extends Indexer {
129132 if ( path . travelType === PathType . Extrusion ) {
130133 this . lastLayer ( ) . push ( path ) ;
131134 } else {
132- if (
133- path . vertices . some ( ( _ , i , arr ) => i % 3 === 2 && arr [ i ] !== arr [ 2 ] ) &&
134- this . lastLayer ( ) . find ( ( p ) => p . travelType === PathType . Extrusion )
135- ) {
135+ const verticalTravels = path . vertices
136+ . map ( ( _ , i , arr ) => {
137+ if ( i % 3 === 2 && arr [ i ] - arr [ 2 ] > this . tolerance ) {
138+ return arr [ i ] - arr [ 2 ] ;
139+ }
140+ } )
141+ . filter ( ( z ) => z !== undefined ) ;
142+ const hasVerticalTravel = verticalTravels . length > 0 ;
143+ const hasExtrusions = this . lastLayer ( ) . find ( ( p ) => p . travelType === PathType . Extrusion ) ;
144+
145+ if ( hasVerticalTravel && hasExtrusions ) {
136146 this . createLayer ( ) ;
137147 }
138148 this . lastLayer ( ) . push ( path ) ;
0 commit comments