-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mutator.cs
275 lines (239 loc) · 11.9 KB
/
Mutator.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace GCEd
{
static class Mutator
{
public static void Delete(GProgram program, IEnumerable<GOperation> operations)
{
var lnos = program.GetLNOForOperations(operations);
foreach (var (_, node, _, _, _) in lnos)
{
program.Lines.Remove(node);
}
}
public static void AppendLines(GProgram program, GOperation? baseOperation, bool before, IEnumerable<GLine> lines)
{
if (program.Lines.Count == 0)
{
foreach (var line in lines) program.Lines.AddLast(line);
return;
}
LinkedListNode<GLine>? baseNode = null;
LinkedListNode<GLine>? marker = null;
if (baseOperation != null) baseNode = program.Lines.Find(baseOperation.Line);
if (baseNode == null) baseNode = before ? program.Lines.First : program.Lines.Last;
if (baseNode == null) baseNode = marker = program.Lines.AddFirst(new GLine());
else if (before) baseNode = marker = program.Lines.AddBefore(baseNode, new GLine());
foreach (var line in lines) baseNode = program.Lines.AddAfter(baseNode, line);
if (marker != null) program.Lines.Remove(marker);
}
public static void ToggleComment(IEnumerable<GLine> lines)
{
if (!lines.Any()) return;
if (lines.First().Instruction == GInstruction.Comment) UncommentLines(lines);
else CommentLines(lines);
}
public static void CommentLines(IEnumerable<GLine> lines)
{
foreach (var line in lines)
{
if (line.Instruction == GInstruction.Comment) continue;
line.Parse("; " + line.ToString());
}
}
public static void UncommentLines(IEnumerable<GLine> lines)
{
foreach (var line in lines)
{
if (line.Instruction != GInstruction.Comment) continue;
line.Parse(line.ToString().Substring(1).TrimStart());
}
}
public static void ConvertToAbsolute(GProgram program, IEnumerable<GOperation> operations)
{
var lnos = program.GetLNOForOperations(operations.Where(operation => !operation.Absolute));
foreach (var (line, node, operation, previousInExtent, nextInExtent) in lnos)
{
if (line.Instruction == GInstruction.G91) program.Lines.Remove(node);
if (previousInExtent == null && node.Previous != null && node.Previous.Value is GLine previousLine)
{
if (previousLine.Instruction == GInstruction.G91) program.Lines.Remove(node.Previous!);
else if (previousLine.Instruction != GInstruction.G90) program.Lines.AddBefore(node, new GLine { Instruction = GInstruction.G90 });
}
if (nextInExtent == null && node.Next != null && node.Next.Value is GLine nextLine)
{
if (nextLine.Instruction == GInstruction.G90) program.Lines.Remove(node.Next!);
else if (nextLine.Instruction != GInstruction.G91) program.Lines.AddAfter(node, new GLine { Instruction = GInstruction.G91 });
}
if (line.IsVisible) line.XY = operation.AbsEnd;
}
}
public static void ConvertToRelative(GProgram program, IEnumerable<GOperation> operations)
{
var lnos = program.GetLNOForOperations(operations.Where(operation => operation.Absolute));
foreach (var (line, node, operation, previousInExtent, nextInExtent) in lnos)
{
if (line.Instruction == GInstruction.G90) program.Lines.Remove(node);
if (previousInExtent == null && node.Previous != null && node.Previous.Value is GLine previousLine)
{
if (previousLine.Instruction == GInstruction.G90) program.Lines.Remove(node.Previous!);
else if (previousLine.Instruction != GInstruction.G91) program.Lines.AddBefore(node, new GLine { Instruction = GInstruction.G91 });
}
if (nextInExtent == null && node.Next != null && node.Next.Value is GLine nextLine)
{
if (nextLine.Instruction == GInstruction.G91) program.Lines.Remove(node.Next!);
else if (nextLine.Instruction != GInstruction.G90) program.Lines.AddAfter(node, new GLine { Instruction = GInstruction.G90 });
}
if (line.IsVisible) line.XY = operation.AbsEnd - operation.AbsStart;
}
}
public static void Translate(GProgram program, IEnumerable<GOperation> operations, Vector2 offset)
{
var matrix = Matrix3x2.CreateTranslation(offset);
Transform(program, operations, matrix);
}
public static void Rotate(GProgram program, IEnumerable<GOperation> operations, Vector2 center, float angle)
{
var matrix = Matrix3x2.CreateRotation((float)(angle * Math.PI / 180f), center);
Transform(program, operations, matrix);
}
public static void Scale(GProgram program, IEnumerable<GOperation> operations, Vector2 center, float amount)
{
var matrix = Matrix3x2.CreateScale(amount, center);
Transform(program, operations, matrix);
}
private static void Transform(GProgram program, IEnumerable<GOperation> operations, Matrix3x2 matrix)
{
var lnos = program.GetLNOForOperations(operations);
foreach (var (line, node, operation, previousInExtent, nextInExtent) in lnos)
{
var newStart = Vector2.Transform(operation.AbsStart, matrix);
var newEnd = Vector2.Transform(operation.AbsEnd, matrix);
if (operation.Line.Directive == Directive.Line || operation.Line.Directive == Directive.Circle)
{
line.XY = newStart;
line.IJ = newEnd - newStart;
continue;
}
if (operation.Absolute)
{
line.XY = newEnd;
if (previousInExtent == null && operation.AbsStart != newStart)
{
if (node.Previous?.Value is GLine previousLine && previousLine.Instruction == GInstruction.G0) previousLine.XY = newStart;
else if (line.Instruction == GInstruction.G0) line.XY = newEnd;
else program.Lines.AddBefore(node, new GLine { Instruction = GInstruction.G0, XY = newStart });
}
if (nextInExtent == null && operation.AbsEnd != newEnd)
{
if (node.Next?.Value is GLine nextLine && nextLine.Instruction == GInstruction.G0) { }
else if (line.Instruction == GInstruction.G0) line.XY = operation.AbsEnd;
else program.Lines.AddAfter(node, new GLine { Instruction = GInstruction.G0, XY = operation.AbsEnd });
}
}
else
{
line.XY = newEnd - newStart;
if (previousInExtent == null && operation.AbsStart != newStart)
{
if (node.Previous?.Value is GLine previousLine && previousLine.Instruction == GInstruction.G0) previousLine.XY += newStart - operation.AbsStart;
else if (line.Instruction == GInstruction.G0) line.XY = newEnd - operation.AbsStart;
else program.Lines.AddBefore(node, new GLine { Instruction = GInstruction.G0, XY = newStart - operation.AbsStart });
}
if (nextInExtent == null && operation.AbsEnd != newEnd)
{
if (node.Next?.Value is GLine nextLine && nextLine.Instruction == GInstruction.G0) nextLine.XY -= newEnd - operation.AbsEnd;
else if (line.Instruction == GInstruction.G0) line.XY = operation.AbsEnd - newStart;
else program.Lines.AddAfter(node, new GLine { Instruction = GInstruction.G0, XY = operation.AbsEnd - newEnd });
}
}
if (line.IsArc)
{
var newOffset = Vector2.Transform(operation.AbsOffset, matrix);
newOffset -= newStart;
line.IJ = newOffset;
}
}
}
public static void ConvertToOutline(GProgram program, IEnumerable<GOperation> operations, float distance, bool leftFirst, bool skipLeft, bool skipRight, bool bothForward)
{
var leftOutline = new GProgram();
var rightOutline = new GProgram();
var lnos = program.GetLNOForOperations(operations);
foreach (var (line, node, operation, previousInExtent, nextInExtent) in lnos)
{
if (!line.IsVisible) continue;
if (Geometry.LineLength(line.XY) == 0) continue;
var currTangents = Geometry.TangentsForOperation(operation);
var (startTangent, endTangent) = Geometry.TangentsForOperation(operation, previousInExtent, nextInExtent);
var startNormal = Geometry.Normal(startTangent);
var endNormal = Geometry.Normal(endTangent);
var startDistance = distance / Vector2.Dot(currTangents.startTangent, startTangent);
var endDistance = distance / Vector2.Dot(currTangents.endTangent, endTangent);
var leftStartOffset = startNormal * startDistance;
var rightStartOffset = -startNormal * startDistance;
var leftEndOffset = endNormal * endDistance;
var rightEndOffset = -endNormal * endDistance;
var leftStart = operation.AbsStart + leftStartOffset;
var rightStart = operation.AbsStart + rightStartOffset;
var leftEnd = operation.AbsEnd + leftEndOffset;
var rightEnd = operation.AbsEnd + rightEndOffset;
if (previousInExtent == null)
{
if (leftFirst || bothForward) leftOutline.Lines.AddLast(new GLine { Instruction = GInstruction.G0, XY = leftStart });
if (!leftFirst || bothForward) rightOutline.Lines.AddLast(new GLine { Instruction = GInstruction.G0, XY = rightStart });
}
var leftSegment = line.Clone();
leftSegment.XY = leftFirst || bothForward ? leftEnd : leftStart;
if (leftSegment.IsArc)
{
if (!leftFirst && !bothForward) leftSegment.Instruction = leftSegment.Instruction == GInstruction.G2 ? GInstruction.G3 : GInstruction.G2;
var (angle, sweep) = Geometry.AngleAndSweepForArc(operation.AbsStart, operation.AbsEnd, operation.AbsOffset, operation.Line.Instruction == GInstruction.G2);
var radius = Geometry.LineLength(operation.AbsStart, operation.AbsOffset) + (operation.Line.Instruction == GInstruction.G2 ? distance : -distance);
var midpointAngle = Math.PI * (angle + sweep / 2) / 180f;
var midpointOffset = new Vector2((float)(Math.Cos(midpointAngle) * radius), (float)(Math.Sin(midpointAngle) * radius));
var midpoint = operation.AbsOffset + midpointOffset;
var center = Geometry.CircleCenterFromThreePoints(leftEnd, leftStart, midpoint);
leftSegment.IJ = leftFirst || bothForward ? center - leftStart : center - leftEnd;
}
if (leftFirst || bothForward) leftOutline.Lines.AddLast(leftSegment);
else leftOutline.Lines.AddFirst(leftSegment);
var rightSegment = line.Clone();
rightSegment.XY = !leftFirst || bothForward ? rightEnd : rightStart;
if (rightSegment.IsArc)
{
if (leftFirst && !bothForward) rightSegment.Instruction = rightSegment.Instruction == GInstruction.G2 ? GInstruction.G3 : GInstruction.G2;
var (angle, sweep) = Geometry.AngleAndSweepForArc(operation.AbsStart, operation.AbsEnd, operation.AbsOffset, operation.Line.Instruction == GInstruction.G2);
var radius = Geometry.LineLength(operation.AbsStart, operation.AbsOffset) + (operation.Line.Instruction == GInstruction.G2 ? -distance : distance);
var midpointAngle = Math.PI * (angle + sweep / 2) / 180f;
var midpointOffset = new Vector2((float)(Math.Cos(midpointAngle) * radius), (float)(Math.Sin(midpointAngle) * radius));
var midpoint = operation.AbsOffset + midpointOffset;
var center = Geometry.CircleCenterFromThreePoints(rightEnd, rightStart, midpoint);
rightSegment.IJ = !leftFirst || bothForward ? center - rightStart : center - rightEnd;
}
if (!leftFirst || bothForward) rightOutline.Lines.AddLast(rightSegment);
else rightOutline.Lines.AddFirst(rightSegment);
if (nextInExtent == null)
{
if (leftFirst && !bothForward) rightOutline.Lines.AddFirst(new GLine { Instruction = GInstruction.G0, XY = rightEnd });
if (leftFirst) rightOutline.Lines.AddLast(new GLine { Instruction = GInstruction.G0, XY = operation.AbsEnd });
if (!leftFirst && !bothForward) leftOutline.Lines.AddFirst(new GLine { Instruction = GInstruction.G0, XY = leftEnd });
if (!leftFirst) leftOutline.Lines.AddLast(new GLine { Instruction = GInstruction.G0, XY = operation.AbsEnd });
}
if (nextInExtent == null)
{
var insertionPoint = node;
if (leftFirst) foreach (var newLine in leftOutline.Lines) insertionPoint = program.Lines.AddAfter(insertionPoint, newLine);
if (!skipRight) foreach (var newLine in rightOutline.Lines) insertionPoint = program.Lines.AddAfter(insertionPoint, newLine);
if (!leftFirst && !skipLeft) foreach (var newLine in leftOutline.Lines) insertionPoint = program.Lines.AddAfter(insertionPoint, newLine);
rightOutline.Lines.Clear();
leftOutline.Lines.Clear();
}
program.Lines.Remove(node);
}
}
}
}