-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
366 lines (312 loc) · 13.9 KB
/
Program.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using ContourHatcher.Utils;
using MarkGeometriesLib.Classes;
using MarkGeometriesLib.Classes;
using MarkGeometriesLib.Classes.DXFParser;
using MSolvLib.Classes.MarkGeometries.Classes.Helpers;
using MSolvLib.MarkGeometry;
using SharpGLShader.Utils;
using STLSlicer;
using STLSlicer.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace MarkGeometriesLib
{
class Program
{
static void Main(string[] args)
{
GeometryToImageConverter.RotateImage4Bpp(
@"C:\Users\Chibuike.Okpaluba\Downloads\Test.bmp",
@"C:\Users\Chibuike.Okpaluba\Downloads\Test.out.bmp",
10
);
//var result1 = GeometryToImageConverter.To1BppImageComplexRetainBounds(
// @"C:\Users\Chibuike.Okpaluba\Downloads\Test.dxf",
// @"C:\Users\Chibuike.Okpaluba\Downloads\Test.bmp",
// null,
// 320, 320
//);
//var result2 = GeometryToImageConverter.To1BppImageComplexRetainBounds(
// @"C:\Users\Chibuike.Okpaluba\Downloads\Test.dxf",
// @"C:\Users\Chibuike.Okpaluba\Downloads\Test30.bmp",
// null,
// 320, 320,
// angle: 0
//);
//var result3 = GeometryToImageConverter.To1BppImageComplexRetainBounds(
// @"C:\Users\Chibuike.Okpaluba\Downloads\Test.dxf",
// @"C:\Users\Chibuike.Okpaluba\Downloads\Test30b.bmp",
// null,
// 320, 160,
// angle: 0
//);
//Console.WriteLine("Done.");
//Console.ReadLine();
}
private static void PrintSlicesCount(string tag, List<ContourStructure>[] slices)
{
Console.WriteLine($"{tag} has {slices.Length} slices");
for (int i=0; i< slices.Length; i++)
{
Console.WriteLine($"\tSlice {i} has {slices[i].Count} contours");
for(int j=0; j<slices[i].Count; j++)
Console.WriteLine($"\t\tContour {j} has {slices[i][j].IntersectionList.Count} joints");
}
}
private static void PrintSlicesCount(string tag, List<MLinkedList<IntersectionStructure>>[] slices)
{
Console.WriteLine($"{tag} has {slices.Length} slices");
for (int i = 0; i < slices.Length; i++)
{
Console.WriteLine($"\tSlice {i} has {slices[i].Count} contours");
for (int j = 0; j < slices[i].Count; j++)
Console.WriteLine($"\t\tContour {j} has {slices[i][j].Count} joints");
}
}
private static MarkGeometryPath ToPath(IEnumerable<MVertex> vertices)
{
return new MarkGeometryPath(vertices.Select(v => new MarkGeometryPoint(v.X, v.Y)));
}
private static List<MarkGeometryLine> ToLines(IEnumerable<MVertex> vertices)
{
return GeometricArithmeticModule.ToLines(vertices.Select(v => new MarkGeometryPoint(v.X, v.Y)).ToArray());
}
private static void Println(string tag, LineEquation eqn)
{
Console.WriteLine($"{tag}: {{Gradient: {Math.Round(eqn.Gradient, 4)}, Intercept: {Math.Round(eqn.YIntercept, 4)}}}");
}
private static void Println<T>(string tag, IEnumerable<T> values)
{
Console.WriteLine($"{tag}, Count: {values.Count()}");
foreach(var value in values)
Console.WriteLine($"\t{value?.ToString()}");
}
private static void Println(string tag, double val, int round = 3)
{
Console.WriteLine($"{tag}: {Math.Round(val, round)}");
}
private static void Println(string tag, object value)
{
Console.WriteLine($"{tag}: {value?.ToString()}");
}
private static void ApplyTransform(IEnumerable<IMarkGeometry> geometries, Matrix4x4 transformationMatrix)
{
foreach (var geometry in geometries)
geometry.Transform(transformationMatrix);
}
private static void PrintExtents(string tag, IMarkGeometry geometry)
{
PrintExtents(tag, geometry.Extents);
Console.WriteLine($"Area: {Math.Round(geometry.Area, 4)}");
Console.WriteLine($"Perimeter: {Math.Round(geometry.Perimeter, 4)}");
}
private static void PrintExtents(string tag, GeometryExtents<double> extents)
{
Console.WriteLine($"\n{tag}");
Console.WriteLine($"Centre: {Math.Round(extents.Centre.X, 4)}, {Math.Round(extents.Centre.Y, 4)}");
Console.WriteLine($"Size: {Math.Round(extents.Width, 4)}, {Math.Round(extents.Height, 4)}");
Console.WriteLine($"Extents: {extents}");
}
private static void Timeit(string tag, Action action, int howmany=5)
{
var st = DateTime.Now;
for (int i = 0; i < howmany; i++)
action();
var et = DateTime.Now;
Console.WriteLine($"{tag} time : {Math.Round((et - st).TotalMilliseconds) / howmany, 5}ms");
}
private static void TestSlicing()
{
var _slicer = new MSTLSlicer();
_slicer.Load(@"C:\MSOLV\STLs\Ice Cream Type 2.stl");
//_slicer.Load(@"C:\MSOLV\STLs\tray.stl");
//_slicer.Load(@"C:\MSOLV\STLs\flange.stl");
//_slicer.Load(@"C:\MSOLV\STLs\TestBinaryKIT.stl");
List<ContourStructure>[] control = new List<ContourStructure>[] { };
List<MContourStructure>[] slices = new List<MContourStructure>[] { };
Console.WriteLine(
PerformanceHelper.Compare(
() =>
{
control = _slicer.Slice();
},
() =>
{
slices = _slicer.SliceParallel();
},
compareRepeat: 5,
timingRepeat: 5,
tagA: "Control",
tagB: "Slices"
)
);
//PrintSlicesCount("Control", control);
//PrintSlicesCount("Slices", slices);
}
private static void TestQuadTree()
{
var _slicer = new MSTLSlicer();
_slicer.Load(@"C:\MSOLV\STLs\Ice Cream Type 2.stl");
//_slicer.Load(@"C:\MSOLV\STLs\tray.stl");
var slices = _slicer.Slice();
IEnumerable<MVertex> contour = slices[7][0].ToVertices().Concat(slices[5][0].ToVertices());
//IEnumerable<MVertex> contour = slices[5][1].ToVertices();
var reference = ToPath(contour);
var intersectingLine = new MarkGeometryLine(
reference.Extents.MinimumPoint,
reference.Extents.MaximumPoint
);
List<MarkGeometryPoint> controlResults = new List<MarkGeometryPoint>();
List<MarkGeometryPoint> quadTreeResults = new List<MarkGeometryPoint>();
var lines = ToLines(contour);
var contourQuadTree = new ContourQuadTree(contour);
Console.WriteLine(
PerformanceHelper.Compare(
() =>
{
// setup up
controlResults.Clear();
MarkGeometryPoint intersection;
for (int i = 0; i < lines.Count; i++)
if ((
intersection = GeometricArithmeticModule.CalculateIntersection2D(
intersectingLine,
lines[i]
)) != null
)
controlResults.Add(intersection);
},
() =>
{
// setup up
quadTreeResults.Clear();
quadTreeResults = contourQuadTree.Intersect(intersectingLine).ToList();
},
tagA: "Lines (Control)",
tagB: "Quad Tree"
)
);
Println("Control", controlResults);
Println("Quad Tree Intersections", quadTreeResults);
contourQuadTree.SaveImage(@"C:\Users\Chibuike.Okpaluba\Downloads\quad_tree_v2.png");
}
private static void Obsolete2()
{
#region Section: Not Used
//List<MarkGeometryPath> openGeometries = new List<MarkGeometryPath>();
//List<IMarkGeometry> closedGeometries = new List<IMarkGeometry>();
//foreach (var kv in GeometricArithmeticModule.ExtractLabelledGeometriesFromDXF(@"C:\Users\Chibuike.Okpaluba\Downloads\#PLR printer data input assesment2_LibrecadPOints.ipt.dxf"))
//{
// for (int i = 0; i < kv.Value.Count; i++)
// {
// if (kv.Value[i] is MarkGeometryLine line)
// {
// openGeometries.Add(new MarkGeometryPath(line));
// }
// else if (kv.Value[i] is MarkGeometryArc arc)
// {
// if (Math.Abs(arc.Sweep % (2 * Math.PI)) <= 0.0001)
// closedGeometries.Add(new MarkGeometryPath(arc));
// else
// openGeometries.Add(new MarkGeometryPath(arc));
// }
// else if (kv.Value[i] is MarkGeometryPath path)
// {
// if (path.IsClosed)
// closedGeometries.Add(path);
// else
// openGeometries.Add(path);
// }
// else
// {
// closedGeometries.Add(kv.Value[i]);
// }
// }
//}
//while(openGeometries.Count > 0)
//{
// MarkGeometryPath reference = openGeometries[0];
// openGeometries.RemoveAt(0);
// var (trace, ___) = Trace(reference, openGeometries, 0.001);
// closedGeometries.Add(trace);
//}
#endregion
var slicer = new MSTLSlicer();
//Timeit(
// "Load",
// () => {
// //slicer.Load(@"C:\MSOLV\STLs\TestASCII.stl");
// slicer.Load(@"C:\MSOLV\STLs\tray.stl");
// }
//);
//Console.WriteLine($"{slicer.Facets?.Count}");
//Timeit(
// "Slice",
// () => {
// slicer.Slice(slicer.MinZ + 15, 0.1, 0.001);
// }
//);
}
private static void Obsolete()
{
//var parser = new DXFParser(@"C:\MSOLV\DXF\Flongle v6 shallow trench Mask04 (202004013) ONT V4_ForGerber.dxf");
//var parser = new DXFParser(@"C:\MSOLV\DXF\Thermal Shock Test Panel - LaserAblation.dxf");
//var parser = new DXFParser(@"C:\MSOLV\DXF\arc-city.dxf");
var parser = new DXFParser(@"C:\MSOLV\DXF\473-00001-DXF-1- Shield with fiducials.dxf");
var layerNames = parser.ReadLayers();
foreach (var name in layerNames)
Console.WriteLine(name);
//Console.WriteLine(parser.ReadVersion());
//var startTime = DateTime.Now;
//try
//{
// Dictionary<string, List<IMarkGeometry>> geometries = parser.ReadGeometries(-1);
// //Dictionary<string, List<IMarkGeometry>> geometries = new Dictionary<string, List<IMarkGeometry>>()
// //{
// // {"test", new List<IMarkGeometry>(){ new MarkGeometryArc(new MarkGeometryPoint(), 50, -180 * Math.PI / 180, -270 * Math.PI / 180) } }
// //};
// Console.WriteLine($"Count: {parser.Count}");
// Console.WriteLine($"Time Taken: {Math.Round((DateTime.Now - startTime).TotalSeconds, 4)} seconds");
// startTime = DateTime.Now;
// SaveImage(geometries, parser.Count);
//}
//catch(Exception exp)
//{
// Log(exp);
//}
//finally
//{
// Console.WriteLine($"Count: {parser.Count}");
// Console.WriteLine($"Time Taken: {Math.Round((DateTime.Now - startTime).TotalSeconds, 4)} seconds");
//}
}
private static void SaveImage(Dictionary<string, List<IMarkGeometry>> geometriesIn, int capacity)
{
var _geometries = new List<IMarkGeometry>(capacity);
foreach(var items in geometriesIn.Values)
{
_geometries.AddRange(items);
}
var extents = GeometricArithmeticModule.CalculateExtents(_geometries);
Console.WriteLine($"Extents: {extents}");
GeometryToImageConverter.To1BppImage(
_geometries,
@"D:\Downloads\demo.tiff",
pixelSize: 25.4,
dpiX: 720,
dpiY: 720,
lineWidth: -1 // smallest possible
);
}
private static void Log(Exception exp)
{
Console.WriteLine(exp.ToString());
if (exp.InnerException != null)
{
Log(exp.InnerException);
}
}
}
}