|
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,148 @@ 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 = [] |
| 58 | + |
| 59 | + for i in [0...currentLoop.length] |
61 | 60 |
|
62 | | - pointsInHoles = 0 |
| 61 | + segStart = currentLoop[i] |
| 62 | + segEnd = currentLoop[(i + 1) % currentLoop.length] |
63 | 63 |
|
64 | | - for sampleIdx in [0...sampleCount] |
| 64 | + clippedSegs = clipping.clipLineWithHoles(segStart, segEnd, infillBoundary, holeInnerWalls) |
65 | 65 |
|
66 | | - # Distribute samples evenly across the loop's length. |
67 | | - pointIdx = Math.floor(sampleIdx * currentLoop.length / sampleCount) |
68 | | - testPoint = currentLoop[pointIdx] |
| 66 | + for seg in clippedSegs |
69 | 67 |
|
70 | | - # Check if this point is inside any hole. |
71 | | - for holeWall in holeInnerWalls |
| 68 | + validSegments.push(seg) |
72 | 69 |
|
73 | | - if holeWall.length >= 3 and primitives.pointInPolygon(testPoint, holeWall) |
| 70 | + continue if validSegments.length is 0 |
74 | 71 |
|
75 | | - pointsInHoles++ |
76 | | - break |
| 72 | + # Group consecutive segments into polylines to minimize travel moves. |
| 73 | + polylines = [] |
| 74 | + currentPolyline = [validSegments[0].start, validSegments[0].end] |
77 | 75 |
|
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 |
| 76 | + for segIdx in [1...validSegments.length] |
81 | 77 |
|
82 | | - skipLoop = true |
| 78 | + seg = validSegments[segIdx] |
| 79 | + prevEnd = currentPolyline[currentPolyline.length - 1] |
83 | 80 |
|
84 | | - continue if skipLoop |
| 81 | + dx = seg.start.x - prevEnd.x |
| 82 | + dy = seg.start.y - prevEnd.y |
85 | 83 |
|
86 | | - # Find optimal start point on this loop if we have a last position. |
87 | | - startIndex = 0 |
| 84 | + if dx * dx + dy * dy < 0.001 * 0.001 |
88 | 85 |
|
89 | | - if lastEndPoint? |
| 86 | + currentPolyline.push(seg.end) |
90 | 87 |
|
91 | | - minDistSq = Infinity |
| 88 | + else |
92 | 89 |
|
93 | | - for i in [0...currentLoop.length] |
| 90 | + polylines.push(currentPolyline) |
| 91 | + currentPolyline = [seg.start, seg.end] |
| 92 | + |
| 93 | + polylines.push(currentPolyline) |
| 94 | + |
| 95 | + # Render each polyline with combing travel and extrusion. |
| 96 | + for polyline in polylines |
| 97 | + |
| 98 | + continue if polyline.length < 2 |
| 99 | + |
| 100 | + startPoint = polyline[0] |
| 101 | + |
| 102 | + combingPath = combing.findCombingPath(lastEndPoint or startPoint, startPoint, holeOuterWalls, infillBoundary, nozzleDiameter) |
| 103 | + |
| 104 | + for i in [0...combingPath.length - 1] |
| 105 | + |
| 106 | + waypoint = combingPath[i + 1] |
| 107 | + offsetWaypointX = waypoint.x + centerOffsetX |
| 108 | + offsetWaypointY = waypoint.y + centerOffsetY |
| 109 | + |
| 110 | + 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)) |
| 111 | + |
| 112 | + prevPoint = polyline[0] |
| 113 | + |
| 114 | + for ptIdx in [1...polyline.length] |
| 115 | + |
| 116 | + point = polyline[ptIdx] |
| 117 | + |
| 118 | + dx = point.x - prevPoint.x |
| 119 | + dy = point.y - prevPoint.y |
| 120 | + |
| 121 | + distance = Math.sqrt(dx * dx + dy * dy) |
| 122 | + |
| 123 | + if distance > 0.001 |
| 124 | + |
| 125 | + extrusionDelta = slicer.calculateExtrusion(distance, nozzleDiameter) |
| 126 | + slicer.cumulativeE += extrusionDelta |
| 127 | + |
| 128 | + offsetX = point.x + centerOffsetX |
| 129 | + offsetY = point.y + centerOffsetY |
| 130 | + |
| 131 | + slicer.gcode += coders.codeLinearMovement(slicer, offsetX, offsetY, z, slicer.cumulativeE, infillSpeedMmMin) |
| 132 | + |
| 133 | + prevPoint = point |
| 134 | + |
| 135 | + lastEndPoint = prevPoint |
| 136 | + |
| 137 | + else |
| 138 | + |
| 139 | + # No holes: use optimized full-loop rendering with start point selection. |
| 140 | + |
| 141 | + # Find optimal start point on this loop if we have a last position. |
| 142 | + startIndex = 0 |
| 143 | + |
| 144 | + if lastEndPoint? |
| 145 | + |
| 146 | + minDistSq = Infinity |
| 147 | + |
| 148 | + for i in [0...currentLoop.length] |
94 | 149 |
|
95 | | - point = currentLoop[i] |
96 | | - distSq = (point.x - lastEndPoint.x) ** 2 + (point.y - lastEndPoint.y) ** 2 |
| 150 | + point = currentLoop[i] |
| 151 | + distSq = (point.x - lastEndPoint.x) ** 2 + (point.y - lastEndPoint.y) ** 2 |
97 | 152 |
|
98 | | - if distSq < minDistSq |
| 153 | + if distSq < minDistSq |
99 | 154 |
|
100 | | - minDistSq = distSq |
101 | | - startIndex = i |
| 155 | + minDistSq = distSq |
| 156 | + startIndex = i |
102 | 157 |
|
103 | | - # Travel to start point with combing. |
104 | | - firstPoint = currentLoop[startIndex] |
| 158 | + # Travel to start point with combing. |
| 159 | + firstPoint = currentLoop[startIndex] |
105 | 160 |
|
106 | | - combingPath = combing.findCombingPath(lastEndPoint or firstPoint, firstPoint, holeOuterWalls, infillBoundary, nozzleDiameter) |
| 161 | + combingPath = combing.findCombingPath(lastEndPoint or firstPoint, firstPoint, holeOuterWalls, infillBoundary, nozzleDiameter) |
107 | 162 |
|
108 | | - for i in [0...combingPath.length - 1] |
| 163 | + for i in [0...combingPath.length - 1] |
109 | 164 |
|
110 | | - waypoint = combingPath[i + 1] |
111 | | - offsetWaypointX = waypoint.x + centerOffsetX |
112 | | - offsetWaypointY = waypoint.y + centerOffsetY |
| 165 | + waypoint = combingPath[i + 1] |
| 166 | + offsetWaypointX = waypoint.x + centerOffsetX |
| 167 | + offsetWaypointY = waypoint.y + centerOffsetY |
113 | 168 |
|
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)) |
| 169 | + 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 | 170 |
|
116 | | - # Print the loop starting from startIndex. |
117 | | - prevPoint = currentLoop[startIndex] |
| 171 | + # Print the loop starting from startIndex. |
| 172 | + prevPoint = currentLoop[startIndex] |
118 | 173 |
|
119 | | - for i in [1..currentLoop.length] |
| 174 | + for i in [1..currentLoop.length] |
120 | 175 |
|
121 | | - currentIndex = (startIndex + i) % currentLoop.length |
122 | | - point = currentLoop[currentIndex] |
| 176 | + currentIndex = (startIndex + i) % currentLoop.length |
| 177 | + point = currentLoop[currentIndex] |
123 | 178 |
|
124 | | - dx = point.x - prevPoint.x |
125 | | - dy = point.y - prevPoint.y |
| 179 | + dx = point.x - prevPoint.x |
| 180 | + dy = point.y - prevPoint.y |
126 | 181 |
|
127 | | - distance = Math.sqrt(dx * dx + dy * dy) |
| 182 | + distance = Math.sqrt(dx * dx + dy * dy) |
128 | 183 |
|
129 | | - if distance > 0.001 |
| 184 | + if distance > 0.001 |
130 | 185 |
|
131 | | - extrusionDelta = slicer.calculateExtrusion(distance, nozzleDiameter) |
132 | | - slicer.cumulativeE += extrusionDelta |
| 186 | + extrusionDelta = slicer.calculateExtrusion(distance, nozzleDiameter) |
| 187 | + slicer.cumulativeE += extrusionDelta |
133 | 188 |
|
134 | | - offsetX = point.x + centerOffsetX |
135 | | - offsetY = point.y + centerOffsetY |
| 189 | + offsetX = point.x + centerOffsetX |
| 190 | + offsetY = point.y + centerOffsetY |
136 | 191 |
|
137 | | - slicer.gcode += coders.codeLinearMovement(slicer, offsetX, offsetY, z, slicer.cumulativeE, infillSpeedMmMin) |
| 192 | + slicer.gcode += coders.codeLinearMovement(slicer, offsetX, offsetY, z, slicer.cumulativeE, infillSpeedMmMin) |
138 | 193 |
|
139 | | - prevPoint = point |
| 194 | + prevPoint = point |
140 | 195 |
|
141 | | - # Update last end point for next loop. |
142 | | - lastEndPoint = prevPoint |
| 196 | + # Update last end point for next loop. |
| 197 | + lastEndPoint = prevPoint |
0 commit comments