@@ -126,9 +126,8 @@ export class Topo {
126126 wbounds . expandByVector ( { x : toolDiameter / 2 + resolution , y : toolDiameter / 2 + resolution , z : 0 } ) ;
127127 }
128128
129+ // swap XY vertices (unswap later after polylines generated)
129130 if ( contourY ) {
130- // console.time('swap XY vertices');
131- // swap XY vertices
132131 for ( let i = 0 ; i < vertices . length ; i += 3 ) {
133132 let tmp = vertices [ i + 1 ] ;
134133 vertices [ i + 1 ] = vertices [ i + 0 ] ;
@@ -140,7 +139,6 @@ export class Topo {
140139 ext . x = ext . y ;
141140 ext . y = tmp ;
142141 } ) ;
143- // console.timeEnd('swap XY vertices');
144142 }
145143
146144 let trace = contour . trace ;
@@ -151,14 +149,17 @@ export class Topo {
151149 let xStep = density ;
152150 let yStep = Math . ceil ( toolStep / resolution ) ;
153151 let epsilon = 10e-4 ;
152+ // load the pre-generated tool profile
154153 await gpu . loadTool ( {
155154 sparseData : toolData
156155 } ) ;
156+ // rasterize the terrain to the same resolution
157157 let terrain = await gpu . loadTerrain ( {
158158 triangles : vertices ,
159159 boundsOverride : wbounds
160160 } ) ;
161161 let { gridWidth, positions } = terrain ;
162+ // generate all scanline points passing tool over terrain
162163 let output = await gpu . generateToolpaths ( {
163164 xStep,
164165 yStep,
@@ -179,16 +180,21 @@ export class Topo {
179180 p . ty = ty ;
180181 return p ;
181182 }
183+ // for each toolpath, return a point in part space coordinated
184+ // attach to a polyline, and annotate with terrain coordinates
185+ // so we can later dertermine if the point is on or off the part
182186 for ( let i = 0 ; i < numScanlines ; i ++ ) {
183187 let lineStart = i * pointsPerLine ;
184188 let lineData = pathData . slice ( lineStart , lineStart + pointsPerLine ) ;
185189 let points = Array . from ( lineData ) . map ( ( v , j ) => calcPoint ( j * xmult + xoff , i * ymult + yoff , v , j * xStep , i * yStep ) ) ;
186190 let slice = newSlice ( i ) ;
187191 let poly = newPolygon ( ) . setOpen ( ) ;
188192 let lines = [ ] ;
193+ // track z co-planar skipped points for latent emission
189194 let skip = 0 ;
190195 let lp ;
191196 for ( let p of points ) {
197+ // detect points where no rays intersect the part (off part)
192198 if ( inside && positions [ p . ty * gridWidth + p . tx ] < zBottom - epsilon ) {
193199 if ( poly . length > 1 ) {
194200 lines . push ( poly ) ;
@@ -200,6 +206,7 @@ export class Topo {
200206 skip = 0 ;
201207 continue ;
202208 }
209+ // off part but we want to return zBottom (outside)
203210 if ( p . z < zBottom - epsilon ) {
204211 lp = p ;
205212 if ( poly . length === 0 ) {
@@ -215,6 +222,7 @@ export class Topo {
215222 continue ;
216223 }
217224 } else if ( lp && skip && poly . length === 0 ) {
225+ // latent/last point output
218226 lp . z = zBottom ;
219227 poly . push ( lp ) ;
220228 }
@@ -225,6 +233,7 @@ export class Topo {
225233 if ( poly . length > 1 ) {
226234 lines . push ( poly ) ;
227235 }
236+ // fixup/swap when contouring along Y
228237 if ( contourY ) {
229238 for ( let poly of lines )
230239 for ( let p of poly . points ) {
@@ -252,11 +261,11 @@ export class Topo {
252261 for ( let i = 2 ; i < points . length ; i ++ ) {
253262 let np = points [ i ] ;
254263 let ndz = np . z - lp . z ;
255- if ( Math . abs ( dz - ndz ) > epsilon ) {
264+ if ( Math . abs ( dz - ndz ) > flatness ) {
256265 // slope changed
257266 merged . push ( lp ) ;
258267 dz = ndz ;
259- } else if ( curvesOnly && Math . abs ( ndz ) < epsilon ) {
268+ } else if ( curvesOnly && Math . abs ( ndz ) < flatness ) {
260269 // add empty points as path separators
261270 merged . push ( undefined ) ;
262271 }
@@ -725,7 +734,7 @@ export class Trace {
725734 const slope = Math . atan2 ( dz , dl ) ;
726735 if ( curvesOnly && Math . abs ( dz ) < flatness ) {
727736 end_poly ( newP ) ;
728- } else if ( lastSlope !== undefined && Math . abs ( lastSlope - slope ) < 0.001 ) {
737+ } else if ( lastSlope !== undefined && Math . abs ( lastSlope - slope ) < flatness ) {
729738 latent = newP ;
730739 } else {
731740 if ( latent ) {
0 commit comments