forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infill.cs
157 lines (127 loc) · 6.64 KB
/
infill.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
/*
Copyright (C) 2013 David Braam
Copyright (c) 2014, Lars Brubaker
This file is part of MatterSlice.
MatterSlice is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MatterSlice 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with MatterSlice. 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 Infill
{
public static void GenerateLinePaths(Polygons in_outline, ref Polygons result, int extrusionWidth_um, int lineSpacing, int infillExtendIntoPerimeter_um, double rotation, long rotationOffset = 0)
{
if (in_outline.Count > 0)
{
Polygons outlines = in_outline.Offset(infillExtendIntoPerimeter_um);
if (outlines.Count > 0)
{
PointMatrix matrix = new PointMatrix(-(rotation + 90)); // we are rotating the part so we rotate by the negative so the lines go the way we expect
outlines.applyMatrix(matrix);
AABB boundary = new AABB(outlines);
boundary.min.X = ((boundary.min.X / lineSpacing) - 1) * lineSpacing - rotationOffset;
int lineCount = (int)((boundary.max.X - boundary.min.X + (lineSpacing - 1)) / lineSpacing);
Polygons unclipedPatern = new Polygons();
long firstX = boundary.min.X / lineSpacing * lineSpacing;
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++)
{
Polygon line = new Polygon();
line.Add(new IntPoint(firstX + lineIndex * lineSpacing, boundary.min.Y));
line.Add(new IntPoint(firstX + lineIndex * lineSpacing, boundary.max.Y));
unclipedPatern.Add(line);
}
PolyTree ret = new PolyTree();
Clipper clipper = new Clipper();
clipper.AddPaths(unclipedPatern, PolyType.ptSubject, false);
clipper.AddPaths(outlines, PolyType.ptClip, true);
clipper.Execute(ClipType.ctIntersection, ret, PolyFillType.pftPositive, PolyFillType.pftEvenOdd);
Polygons newSegments = Clipper.OpenPathsFromPolyTree(ret);
PointMatrix inversematrix = new PointMatrix((rotation + 90));
newSegments.applyMatrix(inversematrix);
result.AddRange(newSegments);
}
}
}
public static void GenerateLineInfill(ConfigSettings config, Polygons partOutline, ref Polygons fillPolygons, int extrusionWidth_um, double fillAngle, int linespacing_um = 0)
{
if (linespacing_um == 0)
{
if (config.infillPercent <= 0)
{
throw new Exception("infillPercent must be gerater than 0.");
}
linespacing_um = (int)(config.extrusionWidth_um / (config.infillPercent / 100));
}
GenerateLinePaths(partOutline, ref fillPolygons, extrusionWidth_um, linespacing_um, config.infillExtendIntoPerimeter_um, fillAngle);
}
public static void GenerateGridInfill(ConfigSettings config, Polygons partOutline, ref Polygons fillPolygons, int extrusionWidth_um, double fillAngle, int linespacing_um = 0)
{
if (config.infillPercent <= 0)
{
throw new Exception("infillPercent must be gerater than 0.");
}
if (linespacing_um == 0)
{
linespacing_um = (int)(config.extrusionWidth_um / (config.infillPercent / 100) * 2);
}
Infill.GenerateLinePaths(partOutline, ref fillPolygons, config.extrusionWidth_um, linespacing_um, config.infillExtendIntoPerimeter_um, fillAngle);
fillAngle += 90;
if (fillAngle > 360)
{
fillAngle -= 360;
}
Infill.GenerateLinePaths(partOutline, ref fillPolygons, extrusionWidth_um, linespacing_um, config.infillExtendIntoPerimeter_um, fillAngle);
}
public static void GenerateTriangleInfill(ConfigSettings config, Polygons partOutline, ref Polygons fillPolygons, int extrusionWidth_um, double fillAngle, long printZ)
{
if (config.infillPercent <= 0)
{
throw new Exception("infillPercent must be gerater than 0.");
}
int linespacing_um = (int)(config.extrusionWidth_um / (config.infillPercent / 100) * 3);
//long offset = printZ % linespacing_um;
long offset = linespacing_um / 2;
Infill.GenerateLinePaths(partOutline, ref fillPolygons, config.extrusionWidth_um, linespacing_um, config.infillExtendIntoPerimeter_um, fillAngle, offset);
fillAngle += 60;
if (fillAngle > 360)
{
fillAngle -= 360;
}
Infill.GenerateLinePaths(partOutline, ref fillPolygons, extrusionWidth_um, linespacing_um, config.infillExtendIntoPerimeter_um, fillAngle, offset);
fillAngle += 60;
if (fillAngle > 360)
{
fillAngle -= 360;
}
Infill.GenerateLinePaths(partOutline, ref fillPolygons, extrusionWidth_um, linespacing_um, config.infillExtendIntoPerimeter_um, fillAngle, offset);
}
public static void generateConcentricInfill(ConfigSettings config, Polygons partOutline, ref Polygons fillPolygons, int extrusionWidth_um, double fillAngle)
{
Polygons outlineCopy = new Polygons(partOutline);
int linespacing_um = (int)(config.extrusionWidth_um / (config.infillPercent / 100));
while (outlineCopy.Count > 0)
{
for (int outlineIndex = 0; outlineIndex < outlineCopy.Count; outlineIndex++)
{
Polygon r = outlineCopy[outlineIndex];
fillPolygons.Add(r);
}
outlineCopy = outlineCopy.Offset(-linespacing_um);
}
}
}
}