|
1 | 1 | # Concentric infill pattern implementation for Polyslice. |
2 | 2 |
|
3 | 3 | coders = require('../../gcode/coders') |
| 4 | +clipping = require('../../utils/clipping') |
4 | 5 | paths = require('../../utils/paths') |
5 | 6 | combing = require('../../geometry/combing') |
6 | | -primitives = require('../../utils/primitives') |
7 | 7 |
|
8 | 8 | module.exports = |
9 | 9 |
|
@@ -50,93 +50,187 @@ module.exports = |
50 | 50 |
|
51 | 51 | continue if currentLoop.length < 3 |
52 | 52 |
|
53 | | - # Check if this loop should be skipped because it's inside a hole. |
54 | | - # Sample multiple points on the loop to ensure accurate detection. |
55 | | - skipLoop = false |
56 | | - |
57 | 53 | if holeInnerWalls.length > 0 |
58 | 54 |
|
59 | | - # Sample points evenly distributed around the loop. |
60 | | - sampleCount = Math.min(8, currentLoop.length) |
| 55 | + # Clip each edge of the loop against hole walls to prevent |
| 56 | + # infill from being generated inside holes. |
| 57 | + validSegments = [] |
61 | 58 |
|
62 | | - pointsInHoles = 0 |
| 59 | + for i in [0...currentLoop.length] |
63 | 60 |
|
64 | | - for sampleIdx in [0...sampleCount] |
| 61 | + segStart = currentLoop[i] |
| 62 | + segEnd = currentLoop[(i + 1) % currentLoop.length] |
65 | 63 |
|
66 | | - # Distribute samples evenly across the loop's length. |
67 | | - pointIdx = Math.floor(sampleIdx * currentLoop.length / sampleCount) |
68 | | - testPoint = currentLoop[pointIdx] |
| 64 | + clippedSegs = clipping.clipLineWithHoles(segStart, segEnd, infillBoundary, holeInnerWalls) |
69 | 65 |
|
70 | | - # Check if this point is inside any hole. |
71 | | - for holeWall in holeInnerWalls |
| 66 | + for seg in clippedSegs |
72 | 67 |
|
73 | | - if holeWall.length >= 3 and primitives.pointInPolygon(testPoint, holeWall) |
| 68 | + validSegments.push(seg) |
74 | 69 |
|
75 | | - pointsInHoles++ |
76 | | - break |
| 70 | + continue if validSegments.length is 0 |
77 | 71 |
|
78 | | - # Skip loop only if majority of sampled points are inside holes. |
79 | | - # This prevents skipping loops that merely pass near holes. |
80 | | - if pointsInHoles > sampleCount / 2 |
| 72 | + # Group consecutive segments into polylines to minimize travel moves. |
| 73 | + polylines = [] |
| 74 | + currentPolyline = [validSegments[0].start, validSegments[0].end] |
81 | 75 |
|
82 | | - skipLoop = true |
| 76 | + for segIdx in [1...validSegments.length] |
83 | 77 |
|
84 | | - continue if skipLoop |
| 78 | + seg = validSegments[segIdx] |
| 79 | + prevEnd = currentPolyline[currentPolyline.length - 1] |
85 | 80 |
|
86 | | - # Find optimal start point on this loop if we have a last position. |
87 | | - startIndex = 0 |
| 81 | + dx = seg.start.x - prevEnd.x |
| 82 | + dy = seg.start.y - prevEnd.y |
88 | 83 |
|
89 | | - if lastEndPoint? |
| 84 | + if dx * dx + dy * dy < 0.001 * 0.001 |
90 | 85 |
|
91 | | - minDistSq = Infinity |
| 86 | + currentPolyline.push(seg.end) |
92 | 87 |
|
93 | | - for i in [0...currentLoop.length] |
| 88 | + else |
| 89 | + |
| 90 | + polylines.push(currentPolyline) |
| 91 | + currentPolyline = [seg.start, seg.end] |
| 92 | + |
| 93 | + polylines.push(currentPolyline) |
| 94 | + |
| 95 | + # Select and render polylines in nearest-neighbour order to minimize travel. |
| 96 | + remainingPolylines = polylines.slice() |
| 97 | + |
| 98 | + while remainingPolylines.length > 0 |
| 99 | + |
| 100 | + if lastEndPoint? |
| 101 | + |
| 102 | + # Find the polyline start/end closest to the current position. |
| 103 | + bestIdx = 0 |
| 104 | + bestFlipped = false |
| 105 | + minDistSq = Infinity |
| 106 | + |
| 107 | + for plIdx in [0...remainingPolylines.length] |
| 108 | + |
| 109 | + pl = remainingPolylines[plIdx] |
| 110 | + continue if pl.length < 2 |
| 111 | + |
| 112 | + startDistSq = (pl[0].x - lastEndPoint.x) ** 2 + (pl[0].y - lastEndPoint.y) ** 2 |
| 113 | + endDistSq = (pl[pl.length - 1].x - lastEndPoint.x) ** 2 + (pl[pl.length - 1].y - lastEndPoint.y) ** 2 |
| 114 | + |
| 115 | + if startDistSq < minDistSq |
| 116 | + |
| 117 | + minDistSq = startDistSq |
| 118 | + bestIdx = plIdx |
| 119 | + bestFlipped = false |
| 120 | + |
| 121 | + if endDistSq < minDistSq |
| 122 | + |
| 123 | + minDistSq = endDistSq |
| 124 | + bestIdx = plIdx |
| 125 | + bestFlipped = true |
| 126 | + |
| 127 | + polyline = remainingPolylines.splice(bestIdx, 1)[0] |
| 128 | + |
| 129 | + if bestFlipped |
| 130 | + |
| 131 | + polyline = polyline.slice().reverse() |
| 132 | + |
| 133 | + else |
| 134 | + |
| 135 | + polyline = remainingPolylines.shift() |
| 136 | + |
| 137 | + continue if polyline.length < 2 |
| 138 | + |
| 139 | + startPoint = polyline[0] |
| 140 | + |
| 141 | + combingPath = combing.findCombingPath(lastEndPoint or startPoint, startPoint, holeOuterWalls, infillBoundary, nozzleDiameter) |
| 142 | + |
| 143 | + for i in [0...combingPath.length - 1] |
| 144 | + |
| 145 | + waypoint = combingPath[i + 1] |
| 146 | + offsetWaypointX = waypoint.x + centerOffsetX |
| 147 | + offsetWaypointY = waypoint.y + centerOffsetY |
| 148 | + |
| 149 | + slicer.gcode += coders.codeLinearMovement(slicer, offsetWaypointX, offsetWaypointY, z, null, travelSpeedMmMin).replace(slicer.newline, (if verbose then "; Moving to concentric loop" + slicer.newline else slicer.newline)) |
| 150 | + |
| 151 | + prevPoint = polyline[0] |
| 152 | + |
| 153 | + for ptIdx in [1...polyline.length] |
| 154 | + |
| 155 | + point = polyline[ptIdx] |
| 156 | + |
| 157 | + dx = point.x - prevPoint.x |
| 158 | + dy = point.y - prevPoint.y |
| 159 | + |
| 160 | + distance = Math.sqrt(dx * dx + dy * dy) |
| 161 | + |
| 162 | + if distance > 0.001 |
| 163 | + |
| 164 | + extrusionDelta = slicer.calculateExtrusion(distance, nozzleDiameter) |
| 165 | + slicer.cumulativeE += extrusionDelta |
| 166 | + |
| 167 | + offsetX = point.x + centerOffsetX |
| 168 | + offsetY = point.y + centerOffsetY |
| 169 | + |
| 170 | + slicer.gcode += coders.codeLinearMovement(slicer, offsetX, offsetY, z, slicer.cumulativeE, infillSpeedMmMin) |
| 171 | + |
| 172 | + prevPoint = point |
| 173 | + |
| 174 | + lastEndPoint = prevPoint |
| 175 | + |
| 176 | + else |
| 177 | + |
| 178 | + # No holes: use optimized full-loop rendering with start point selection. |
| 179 | + |
| 180 | + # Find optimal start point on this loop if we have a last position. |
| 181 | + startIndex = 0 |
| 182 | + |
| 183 | + if lastEndPoint? |
| 184 | + |
| 185 | + minDistSq = Infinity |
| 186 | + |
| 187 | + for i in [0...currentLoop.length] |
94 | 188 |
|
95 | | - point = currentLoop[i] |
96 | | - distSq = (point.x - lastEndPoint.x) ** 2 + (point.y - lastEndPoint.y) ** 2 |
| 189 | + point = currentLoop[i] |
| 190 | + distSq = (point.x - lastEndPoint.x) ** 2 + (point.y - lastEndPoint.y) ** 2 |
97 | 191 |
|
98 | | - if distSq < minDistSq |
| 192 | + if distSq < minDistSq |
99 | 193 |
|
100 | | - minDistSq = distSq |
101 | | - startIndex = i |
| 194 | + minDistSq = distSq |
| 195 | + startIndex = i |
102 | 196 |
|
103 | | - # Travel to start point with combing. |
104 | | - firstPoint = currentLoop[startIndex] |
| 197 | + # Travel to start point with combing. |
| 198 | + firstPoint = currentLoop[startIndex] |
105 | 199 |
|
106 | | - combingPath = combing.findCombingPath(lastEndPoint or firstPoint, firstPoint, holeOuterWalls, infillBoundary, nozzleDiameter) |
| 200 | + combingPath = combing.findCombingPath(lastEndPoint or firstPoint, firstPoint, holeOuterWalls, infillBoundary, nozzleDiameter) |
107 | 201 |
|
108 | | - for i in [0...combingPath.length - 1] |
| 202 | + for i in [0...combingPath.length - 1] |
109 | 203 |
|
110 | | - waypoint = combingPath[i + 1] |
111 | | - offsetWaypointX = waypoint.x + centerOffsetX |
112 | | - offsetWaypointY = waypoint.y + centerOffsetY |
| 204 | + waypoint = combingPath[i + 1] |
| 205 | + offsetWaypointX = waypoint.x + centerOffsetX |
| 206 | + offsetWaypointY = waypoint.y + centerOffsetY |
113 | 207 |
|
114 | | - slicer.gcode += coders.codeLinearMovement(slicer, offsetWaypointX, offsetWaypointY, z, null, travelSpeedMmMin).replace(slicer.newline, (if verbose then "; Moving to concentric loop" + slicer.newline else slicer.newline)) |
| 208 | + slicer.gcode += coders.codeLinearMovement(slicer, offsetWaypointX, offsetWaypointY, z, null, travelSpeedMmMin).replace(slicer.newline, (if verbose then "; Moving to concentric loop" + slicer.newline else slicer.newline)) |
115 | 209 |
|
116 | | - # Print the loop starting from startIndex. |
117 | | - prevPoint = currentLoop[startIndex] |
| 210 | + # Print the loop starting from startIndex. |
| 211 | + prevPoint = currentLoop[startIndex] |
118 | 212 |
|
119 | | - for i in [1..currentLoop.length] |
| 213 | + for i in [1..currentLoop.length] |
120 | 214 |
|
121 | | - currentIndex = (startIndex + i) % currentLoop.length |
122 | | - point = currentLoop[currentIndex] |
| 215 | + currentIndex = (startIndex + i) % currentLoop.length |
| 216 | + point = currentLoop[currentIndex] |
123 | 217 |
|
124 | | - dx = point.x - prevPoint.x |
125 | | - dy = point.y - prevPoint.y |
| 218 | + dx = point.x - prevPoint.x |
| 219 | + dy = point.y - prevPoint.y |
126 | 220 |
|
127 | | - distance = Math.sqrt(dx * dx + dy * dy) |
| 221 | + distance = Math.sqrt(dx * dx + dy * dy) |
128 | 222 |
|
129 | | - if distance > 0.001 |
| 223 | + if distance > 0.001 |
130 | 224 |
|
131 | | - extrusionDelta = slicer.calculateExtrusion(distance, nozzleDiameter) |
132 | | - slicer.cumulativeE += extrusionDelta |
| 225 | + extrusionDelta = slicer.calculateExtrusion(distance, nozzleDiameter) |
| 226 | + slicer.cumulativeE += extrusionDelta |
133 | 227 |
|
134 | | - offsetX = point.x + centerOffsetX |
135 | | - offsetY = point.y + centerOffsetY |
| 228 | + offsetX = point.x + centerOffsetX |
| 229 | + offsetY = point.y + centerOffsetY |
136 | 230 |
|
137 | | - slicer.gcode += coders.codeLinearMovement(slicer, offsetX, offsetY, z, slicer.cumulativeE, infillSpeedMmMin) |
| 231 | + slicer.gcode += coders.codeLinearMovement(slicer, offsetX, offsetY, z, slicer.cumulativeE, infillSpeedMmMin) |
138 | 232 |
|
139 | | - prevPoint = point |
| 233 | + prevPoint = point |
140 | 234 |
|
141 | | - # Update last end point for next loop. |
142 | | - lastEndPoint = prevPoint |
| 235 | + # Update last end point for next loop. |
| 236 | + lastEndPoint = prevPoint |
0 commit comments