forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskin.cs
159 lines (137 loc) · 6.95 KB
/
skin.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
/*
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 System;
using System.Collections.Generic;
using System.Diagnostics;
using MatterSlice.ClipperLib;
namespace MatterHackers.MatterSlice
{
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
public static class Skin
{
public static void generateTopAndBottomLayers(int layerIndex, SliceVolumeStorage storage, int extrusionWidth, int downSkinCount, int upSkinCount)
{
SliceLayer layer = storage.layers[layerIndex];
for (int partNr = 0; partNr < layer.parts.Count; partNr++)
{
SliceLayerPart part = layer.parts[partNr];
Polygons upskin = part.insets[part.insets.Count - 1].Offset(-extrusionWidth / 2);
Polygons downskin = upskin;
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));
upskin.AddAll(thinWalls);
downskin.AddAll(thinWalls);
}
if (layerIndex - downSkinCount >= 0)
{
SliceLayer layer2 = storage.layers[layerIndex - downSkinCount];
for (int partIndex = 0; partIndex < layer2.parts.Count; partIndex++)
{
if (part.boundaryBox.hit(layer2.parts[partIndex].boundaryBox))
{
downskin = downskin.CreateDifference(layer2.parts[partIndex].insets[layer2.parts[partIndex].insets.Count - 1]);
}
}
}
if (layerIndex + upSkinCount < storage.layers.Count)
{
SliceLayer layer2 = storage.layers[layerIndex + upSkinCount];
for (int partIndex = 0; partIndex < layer2.parts.Count; partIndex++)
{
if (part.boundaryBox.hit(layer2.parts[partIndex].boundaryBox))
{
upskin = upskin.CreateDifference(layer2.parts[partIndex].insets[layer2.parts[partIndex].insets.Count - 1]);
}
}
}
part.skinOutline = upskin.CreateUnion(downskin);
double minAreaSize = (2 * Math.PI * (extrusionWidth / 1000.0) * (extrusionWidth / 1000.0)) * 0.3;
for (int outlineIndex = 0; outlineIndex < part.skinOutline.Count; outlineIndex++)
{
double area = Math.Abs(part.skinOutline[outlineIndex].Area()) / 1000.0 / 1000.0;
if (area < minAreaSize) // Only create an up/down skin if the area is large enough. So you do not create tiny blobs of "trying to fill"
{
part.skinOutline.RemoveAt(outlineIndex);
outlineIndex -= 1;
}
}
}
}
public static void generateSparse(int layerIndex, SliceVolumeStorage storage, int extrusionWidth, int downSkinCount, int upSkinCount)
{
SliceLayer layer = storage.layers[layerIndex];
for (int partNr = 0; partNr < layer.parts.Count; partNr++)
{
SliceLayerPart part = layer.parts[partNr];
Polygons sparse = part.insets[part.insets.Count - 1].Offset(-extrusionWidth / 2);
Polygons downskin = sparse;
Polygons upskin = sparse;
if ((int)(layerIndex - downSkinCount) >= 0)
{
SliceLayer layer2 = storage.layers[layerIndex - downSkinCount];
for (int partNr2 = 0; partNr2 < layer2.parts.Count; partNr2++)
{
if (part.boundaryBox.hit(layer2.parts[partNr2].boundaryBox))
{
if (layer2.parts[partNr2].insets.Count > 1)
{
downskin = downskin.CreateDifference(layer2.parts[partNr2].insets[layer2.parts[partNr2].insets.Count - 2]);
}
else
{
downskin = downskin.CreateDifference(layer2.parts[partNr2].insets[layer2.parts[partNr2].insets.Count - 1]);
}
}
}
}
if ((int)(layerIndex + upSkinCount) < (int)storage.layers.Count)
{
SliceLayer layer2 = storage.layers[layerIndex + upSkinCount];
for (int partNr2 = 0; partNr2 < layer2.parts.Count; partNr2++)
{
if (part.boundaryBox.hit(layer2.parts[partNr2].boundaryBox))
{
if (layer2.parts[partNr2].insets.Count > 1)
{
upskin = upskin.CreateDifference(layer2.parts[partNr2].insets[layer2.parts[partNr2].insets.Count - 2]);
}
else
{
upskin = upskin.CreateDifference(layer2.parts[partNr2].insets[layer2.parts[partNr2].insets.Count - 1]);
}
}
}
}
Polygons result = upskin.CreateUnion(downskin);
double minAreaSize = 3.0;//(2 * M_PI * ((double)(config.extrusionWidth) / 1000.0) * ((double)(config.extrusionWidth) / 1000.0)) * 3;
for (int i = 0; i < result.Count; i++)
{
double area = Math.Abs(result[i].Area()) / 1000.0 / 1000.0;
if (area < minAreaSize) /* Only create an up/down skin if the area is large enough. So you do not create tiny blobs of "trying to fill" */
{
result.RemoveAt(i);
i -= 1;
}
}
part.sparseOutline = sparse.CreateDifference(result);
}
}
}
}