forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopsAndBottoms.cs
254 lines (213 loc) · 11.5 KB
/
TopsAndBottoms.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
This file is part of MatterSlice. A commandline utility for
generating 3D printing GCode.
Copyright (C) 2013 David Braam
Copyright (c) 2014, Lars Brubaker
MatterSlice is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using MatterSlice.ClipperLib;
using System;
using System.Collections.Generic;
namespace MatterHackers.MatterSlice
{
using Polygons = List<List<IntPoint>>;
public static class TopsAndBottoms
{
readonly static double cleanDistance_um = 10;
public static void GenerateTopAndBottom(int layerIndex, SliceVolumeStorage storage, int extrusionWidth, ConfigSettings config)
{
var downLayerCount = config.numberOfBottomLayers;
var upLayerCount = config.numberOfTopLayers;
SliceLayer layer = storage.layers[layerIndex];
for (int partIndex = 0; partIndex < layer.parts.Count; partIndex++)
{
SliceLayerPart part = layer.parts[partIndex];
Polygons insetWithOffset = part.Insets[part.Insets.Count - 1].Offset(-extrusionWidth / 2);
Polygons infillOutlines = new Polygons(insetWithOffset);
// calculate the bottom outlines
if (downLayerCount > 0)
{
Polygons bottomOutlines = new Polygons(insetWithOffset);
if (layerIndex - 1 >= 0)
{
bottomOutlines = RemoveAdditionalOutlinesForPart(storage.layers[layerIndex - 1], part, bottomOutlines);
RemoveSmallAreas(extrusionWidth, bottomOutlines);
}
infillOutlines = infillOutlines.CreateDifference(bottomOutlines);
infillOutlines = Clipper.CleanPolygons(infillOutlines, cleanDistance_um);
part.SolidBottomOutlines = bottomOutlines;
}
// calculate the top outlines
if(upLayerCount > 0)
{
Polygons topOutlines = new Polygons(insetWithOffset);
topOutlines = topOutlines.CreateDifference(part.SolidBottomOutlines);
topOutlines = Clipper.CleanPolygons(topOutlines, cleanDistance_um);
if (part.Insets.Count > 1)
{
// Add thin wall filling by taking the area between the insets.
Polygons thinWalls = part.Insets[0].Offset(-extrusionWidth / 2).CreateDifference(part.Insets[1].Offset(extrusionWidth / 2));
topOutlines.AddAll(thinWalls);
}
if (layerIndex + 1 < storage.layers.Count)
{
topOutlines = RemoveAdditionalOutlinesForPart(storage.layers[layerIndex + 1], part, topOutlines);
RemoveSmallAreas(extrusionWidth, topOutlines);
}
infillOutlines = infillOutlines.CreateDifference(topOutlines);
infillOutlines = Clipper.CleanPolygons(infillOutlines, cleanDistance_um);
part.SolidTopOutlines = topOutlines;
}
// calculate the solid infill outlines
if (upLayerCount > 1 || downLayerCount > 1)
{
var solidInfillOutlines = new Polygons(insetWithOffset);
solidInfillOutlines = solidInfillOutlines.CreateDifference(part.SolidBottomOutlines);
solidInfillOutlines = Clipper.CleanPolygons(solidInfillOutlines, cleanDistance_um);
solidInfillOutlines = solidInfillOutlines.CreateDifference(part.SolidTopOutlines);
solidInfillOutlines = Clipper.CleanPolygons(solidInfillOutlines, cleanDistance_um);
var upStart = layerIndex + 2;
var upEnd = layerIndex + upLayerCount + 1;
var downStart = layerIndex - 1;
var downEnd = layerIndex - downLayerCount;
if (upEnd <= storage.layers.Count && downEnd >= 0)
{
var makeInfillSolid = false;
var totalPartsToRemove = new Polygons(insetWithOffset);
for (var layerToTest = upStart; layerToTest < upEnd; layerToTest++)
{
totalPartsToRemove = AddAllOutlines(storage.layers[layerToTest], part, totalPartsToRemove, ref makeInfillSolid, config);
totalPartsToRemove = Clipper.CleanPolygons(totalPartsToRemove, cleanDistance_um);
if (makeInfillSolid) break;
}
for (var layerToTest = downStart; layerToTest >= downEnd; layerToTest--)
{
totalPartsToRemove = AddAllOutlines(storage.layers[layerToTest], part, totalPartsToRemove, ref makeInfillSolid, config);
totalPartsToRemove = Clipper.CleanPolygons(totalPartsToRemove, cleanDistance_um);
if (makeInfillSolid) break;
}
if (!makeInfillSolid)
{
solidInfillOutlines = solidInfillOutlines.CreateDifference(totalPartsToRemove);
RemoveSmallAreas(extrusionWidth, solidInfillOutlines);
}
solidInfillOutlines = Clipper.CleanPolygons(solidInfillOutlines, cleanDistance_um);
}
part.SolidInfillOutlines = solidInfillOutlines;
infillOutlines = infillOutlines.CreateDifference(solidInfillOutlines);
Polygons totalInfillOutlines = null;
double totalInfillArea = 0.0;
if (config.infillSolidProportion > 0)
{
totalInfillOutlines = infillOutlines.CreateUnion(solidInfillOutlines);
totalInfillArea = totalInfillOutlines.TotalArea();
}
if (config.infillSolidProportion > 0)
{
var solidInfillArea = solidInfillOutlines.TotalArea();
if (solidInfillArea > totalInfillArea * config.infillSolidProportion)
{
solidInfillOutlines = solidInfillOutlines.CreateUnion(infillOutlines);
infillOutlines = new Polygons();
part.SolidInfillOutlines = solidInfillOutlines;
}
var solidTopOutlinesArea = part.SolidTopOutlines.TotalArea();
if (totalInfillArea < solidTopOutlinesArea * config.infillSolidProportion / 2)
{
var totalSolidTop = totalInfillOutlines.CreateUnion(part.SolidTopOutlines);
part.SolidTopOutlines = totalSolidTop;
part.SolidInfillOutlines = new Polygons();
infillOutlines = part.InfillOutlines = new Polygons();
}
var solidBottomOutlinesArea = part.SolidBottomOutlines.TotalArea();
if (totalInfillArea < solidBottomOutlinesArea * config.infillSolidProportion / 2)
{
var totalSolidBottom = totalInfillOutlines.CreateUnion(part.SolidBottomOutlines);
part.SolidBottomOutlines = totalSolidBottom;
part.SolidInfillOutlines = new Polygons();
infillOutlines = part.InfillOutlines = new Polygons();
}
}
if (config.minInfillArea_mm2 > 0)
{
var infillArea = infillOutlines.TotalArea()/1e6; // convert from um2 to mm2
if (infillArea < config.minInfillArea_mm2)
{
solidInfillOutlines = solidInfillOutlines.CreateUnion(infillOutlines);
infillOutlines = new Polygons();
part.SolidInfillOutlines = solidInfillOutlines;
}
}
}
RemoveSmallAreas(extrusionWidth, infillOutlines);
infillOutlines = Clipper.CleanPolygons(infillOutlines, cleanDistance_um);
part.InfillOutlines = infillOutlines;
}
}
private static void RemoveSmallAreas(int extrusionWidth, Polygons solidInfillOutlinesUp)
{
double minAreaSize = (2 * Math.PI * (extrusionWidth / 1000.0) * (extrusionWidth / 1000.0)) * 0.3;
for (int outlineIndex = 0; outlineIndex < solidInfillOutlinesUp.Count; outlineIndex++)
{
double area = Math.Abs(solidInfillOutlinesUp[outlineIndex].Area()) / 1000.0 / 1000.0;
if (area < minAreaSize) // Only create an up/down Outline if the area is large enough. So you do not create tiny blobs of "trying to fill"
{
solidInfillOutlinesUp.RemoveAt(outlineIndex);
outlineIndex -= 1;
}
}
}
private static Polygons RemoveAdditionalOutlinesForPart(SliceLayer layerToSubtract, SliceLayerPart partToUseAsBounds, Polygons polygonsToSubtractFrom)
{
for (int partIndex = 0; partIndex < layerToSubtract.parts.Count; partIndex++)
{
if (partToUseAsBounds.BoundingBox.Hit(layerToSubtract.parts[partIndex].BoundingBox))
{
polygonsToSubtractFrom = polygonsToSubtractFrom.CreateDifference(layerToSubtract.parts[partIndex].Insets[layerToSubtract.parts[partIndex].Insets.Count - 1]);
polygonsToSubtractFrom = Clipper.CleanPolygons(polygonsToSubtractFrom, cleanDistance_um);
}
}
return polygonsToSubtractFrom;
}
private static Polygons AddAllOutlines(SliceLayer layerToAdd, SliceLayerPart partToUseAsBounds, Polygons polysToAddTo, ref bool makeInfillSolid, ConfigSettings config)
{
Polygons polysToIntersect = new Polygons();
for (int partIndex = 0; partIndex < layerToAdd.parts.Count; partIndex++)
{
var partToConsider = layerToAdd.parts[partIndex];
if (config.smallProtrusionProportion > 0)
{
// If the part under consideration intersects the part from the current layer and
// the area of that intersection is less than smallProtrusionProportion % of the part under consideration solidify the infill
var intersection = partToUseAsBounds.TotalOutline.CreateIntersection(partToConsider.TotalOutline);
if (intersection.Count > 0) // They do intersect
{
if (intersection.TotalArea() < partToConsider.TotalOutline.TotalArea() * config.smallProtrusionProportion)
{
makeInfillSolid = true;
return polysToAddTo;
}
}
}
if (partToUseAsBounds.BoundingBox.Hit(partToConsider.BoundingBox))
{
polysToIntersect =
polysToIntersect.CreateUnion(
layerToAdd.parts[partIndex].Insets[partToConsider.Insets.Count - 1]);
polysToIntersect = Clipper.CleanPolygons(polysToIntersect, cleanDistance_um);
}
}
polysToAddTo = polysToAddTo.CreateIntersection(polysToIntersect);
return polysToAddTo;
}
}
}