forked from ProjectSilkworm/Silkworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SilkwormSkein.cs
851 lines (613 loc) · 27.2 KB
/
SilkwormSkein.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
using System;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Grasshopper.Kernel.Data;
using Rhino.Geometry;
using Rhino.Collections;
using Rhino.Geometry.Intersect;
namespace Silkworm
{
class SilkwormSkein
{
/// <summary>::ABOUT::
/// This class does all the processing of solid geometry types for Silkworm.
/// It contains the Slicer, Skinner and Filler Methods for operating on open or closed 3d and 2d Breps.
/// </summary>
//Error Checking Values
public bool validPos = false;
public bool validZ = false;
public bool openBreps = false;
public List<string> ErrorMessages = new List<string>();
public List<string> WarningMessages = new List<string>();
//Settings
public Dictionary<string, string> Configuration = new Dictionary<string, string>();
public double Tolerance = 0.001;
public double crossSec = 0.6;
public int Shell;
public int solidlayers;
public double rot;
public double rotation;
public double fillD;
public double layerOffset;
//Store Solids here
List<Brep> Breps = new List<Brep>();
List<Mesh> solidMeshes = new List<Mesh>();
//Store Slices here
List<Brep> planarBreps = new List<Brep>();
//Planes to slice with
public List<Plane> slicePlanes = new List<Plane>();
//Infill Curves
public List<Curve>[] curvePerimeter = new List<Curve>[0];
public List<Curve>[] curveInfill = new List<Curve>[0];
//Region Breps
public List<Curve>[] openRegions = new List<Curve>[0];
public List<Brep>[] Regions = new List<Brep>[0];
public List<Brep>[] regionPerimeter = new List<Brep>[0];
public List<Brep>[] regionInfill = new List<Brep>[0];
public List<Brep>[] regionBridges = new List<Brep>[0];
public List<Brep>[] regionOverhangs = new List<Brep>[0];
public List<Curve> zCurves = new List<Curve>();
//Empty Constructor
public SilkwormSkein()
{
}
#region Constructors
//3d Region Skein (Can be open or closed)
public SilkwormSkein(Dictionary<string, string> Settings, List<Brep> things, List<Plane> sliceplanes, int shell, double layeroffset)
{
Configuration = Settings;
Shell = shell;
layerOffset = layeroffset;
slicePlanes = sliceplanes;
Breps = things;
}
//2D Region Skein
public SilkwormSkein(Dictionary<string, string> Settings, List<Brep> planarthings)
{
Configuration = Settings;
planarBreps = planarthings;
}
#endregion
#region Tool Methods
//Breps Open or Closed
public void BrepSlice(bool overhangregions)
{
bool horizontalslices = true;
#region Check if Planes are Horizontal
int untrue = 0;
foreach (Plane sliceplane in slicePlanes)
{
if (sliceplane.ZAxis != Plane.WorldXY.ZAxis)
{
untrue += 1;
}
}
if (untrue > 0)
{
horizontalslices = false;
WarningMessages.Add("Slice Planes are not aligned with World Z Axis (horizontal), some slicing options will be unavailable");
}
#endregion
double layerHeight = layerOffset;
int perimeters = Shell;
double offsetDistance = perimeters * crossSec;
BoundingBox b_box = bboxAll(Breps);
Box bbox = new Box(Plane.WorldXY, b_box);
if (slicePlanes.Count<2 && horizontalslices)
{
int noLevels = (int)Math.Round((bbox.Z.Max / layerHeight), 0);
for (int i = 1; i < noLevels; i++)
{
Point3d zPt = new Point3d(0, 0, i * layerHeight);
Plane zPlane = new Plane(zPt, Plane.WorldXY.ZAxis);
slicePlanes.Add(zPlane);
}
}
//Regions
List<Curve>[] openregions = new List<Curve>[slicePlanes.Count];
List<Brep>[] regions = new List<Brep>[slicePlanes.Count];
List<Brep>[] r_PerimeterR = new List<Brep>[slicePlanes.Count];
List<Brep>[] r_InfillR = new List<Brep>[slicePlanes.Count];
//List<Brep>[] r_BridgesR = new List<Brep>[slicePlanes.Count];
//List<Brep>[] r_OverhangsR = new List<Brep>[slicePlanes.Count];
if (validPosition(Configuration, bbox))
{
for (int j = 0; j < slicePlanes.Count; j++)
{
List<Curve> openR = new List<Curve>();
List<Brep> regionsR = new List<Brep>();
List<Brep> r_PerR = new List<Brep>();
List<Brep> r_InfR = new List<Brep>();
//List<Brep> r_BrdgR = new List<Brep>();
//List<Brep> r_OverhgR = new List<Brep>();
#region Slice
SliceUnionRegions(Breps, slicePlanes[j], out regionsR, out openR);
#endregion
#region Skein Region
if (Shell > 0 && horizontalslices)
{
foreach (Brep reg in regionsR)
{
Boolean isClosed = false;
List<Curve> offsetloops = new List<Curve>();
//List<Curve> aloops = (reg.DuplicateEdgeCurves(true).ToList());
//List<Curve> loops = Curve.JoinCurves(aloops,Tolerance, true).ToList();
var loops = reg.Loops;
#region Divide into Regions
#region Find Perimiter Regions
foreach (BrepLoop iloop in loops)
{
Curve loop = iloop.To3dCurve();
Plane frame = horizFrame(loop, 0.5);
List<Curve> offLoops = new List<Curve>();
for (int o = 1; o <= perimeters; o++)
{
if (loop.Offset(frame, (crossSec * o), Tolerance, CurveOffsetCornerStyle.Sharp) == null)
{
break;
}
offLoops = loop.Offset(frame, (crossSec * o), Tolerance, CurveOffsetCornerStyle.Sharp).ToList();
}
if (offLoops.Count > 1)
{
List<Curve> joinLoops = Curve.JoinCurves(offLoops, Tolerance, true).ToList();
offsetloops.AddRange(joinLoops);
}
else if (offLoops.Count == 1)
{ offsetloops.AddRange(offLoops); }
else
{
WarningMessages.Add("Object contains elements that are too small to slice. Try a smaller nozzle.");
continue;
}
}
#endregion
//Create Perimeter Skin Region
if (!(offsetloops.Count < 1))
{
for (int l = 0; l < loops.Count; l++)
{
Curve loop = loops[l].To3dCurve();
if (loop.IsClosed && offsetloops[l].IsClosed)
{
isClosed = (loop.IsClosed && offsetloops[l].IsClosed);
List<Curve> perHatch = new List<Curve>();
if (loops[l].LoopType == BrepLoopType.Inner)
{
loop.Reverse();
offsetloops[l].Reverse();
}
perHatch.Add(loop);
perHatch.Add(offsetloops[l]);
r_PerR.AddRange(Brep.CreatePlanarBreps(perHatch).ToList());
}
else
{
continue;
}
}
#endregion
}
r_InfR.AddRange(Brep.CreatePlanarBreps(offsetloops).ToList());
}
//TODO other region detection
r_PerimeterR[j] = r_PerR;
r_InfillR[j] = r_InfR;
}
else if (overhangregions && horizontalslices)
{
continue;
}
//Otherwise just add undivided regions
else
{
regions[j] = regionsR;
}
openregions[j] = openR;
#endregion
}
}
openRegions = openregions;
Regions = regions;
regionPerimeter = r_PerimeterR;
regionInfill = r_InfillR;
//regionBridges = r_BridgesR;
//regionOverhangs = r_OverhangsR;
}
//Meshes Open or Closed
//TODO
public void SliceUnionRegions(List<Brep> things, Plane ZPlane, out List<Brep> closedRegions, out List<Curve> openRegions)
{
List<Brep> sepregions = new List<Brep>();
List<Brep> regions = new List<Brep>();
List<Curve> openregions = new List<Curve>();
//Slice Solid Breps with Planes
for (int k = 0; k < things.Count; k++)
{
if (things[k].IsSolid)
{
#region Slice
Curve[] iCurves;
Point3d[] iPts;
Intersection.BrepPlane(things[k], ZPlane, Tolerance, out iCurves, out iPts);
List<Brep> iRegion = Brep.CreatePlanarBreps(iCurves).ToList();
#endregion
#region Check Normal is natural
//Make sure Normal and World Z axis are aligned
foreach (Brep reg in iRegion)
{
Vector3d normal = reg.Faces[0].NormalAt(0, 0);
//unitize normal to compare with world Z axis
normal.Unitize();
if (normal.X != Plane.WorldXY.ZAxis.X || normal.Y != Plane.WorldXY.ZAxis.Y || normal.Z != Plane.WorldXY.ZAxis.Z)
{
reg.Flip();
Vector3d normalcheck = reg.Faces[0].NormalAt(0, 0);
}
}
#endregion
sepregions.AddRange(iRegion);
}
//If Brep is Open, create slice curves
if (!things[k].IsSolid)
{
openBreps = true;
#region Slice
Curve[] iCurves;
Point3d[] iPts;
Intersection.BrepPlane(things[k], ZPlane, Tolerance, out iCurves, out iPts);
#endregion
openregions.AddRange(iCurves.ToList());
}
}
//Union of Regions
if (sepregions.Count>0)
{
regions = Brep.CreateBooleanUnion(sepregions, 0.01).ToList();
if (regions.Count < 1)
{
regions = sepregions;
}
}
closedRegions = regions;
openRegions = openregions;
}
//HATCH FILL REGION - infills a planar region with a hatching pattern
public void Filler(List<double> infDens, List<double> infRot)
{
double tolerance = 0.001;
//double overlaptol = 0.0;
double crossSecHyp = Math.Sqrt(Math.Pow(crossSec, 2) * 2);
curveInfill = new List<Curve>[planarBreps.Count];
//Create List of Infill Curves for each Planar Region
for (int u = 0; u < planarBreps.Count; u++)
{
//Rotate Plane for Filling
double rotationRad = infRot[u] * 0.0174532925;
Plane plane = Plane.WorldXY;
plane.Rotate(rotationRad, Plane.WorldXY.ZAxis);
//Create Bounding Box
Box bbox = new Box();
planarBreps[u].GetBoundingBox(plane, out bbox);
//Get Corners
Point3d[] cornerPts = bbox.GetCorners();
//Draw Parallel Lines
LineCurve baseLine = new LineCurve(cornerPts[0], cornerPts[1]);
LineCurve baseLine2 = new LineCurve(cornerPts[3], cornerPts[2]);
//int nPts = (int)Math.Round((baseLine.Line.Length/crossSec),0);
Point3d[] basePts = new Point3d[0];
Point3d[] basePts2 = new Point3d[0];
double length = baseLine.Line.Length;
double floatdivisions = length / crossSec;
double density = infDens[u];
int divisions = (int)Math.Round((floatdivisions * density));
//Divide Lines by Fill Density ratio
baseLine.DivideByCount(divisions, true, out basePts);
baseLine2.DivideByCount(divisions, true, out basePts2);
if (divisions == 0)
{
curveInfill[u] = new List<Curve>();
return;
}
Curve[][] intCurve = new Curve[basePts.Length][];
List<Curve> intCurves = new List<Curve>();
for (int i = 0; i < basePts.Length; i++)
{
LineCurve intLine = new LineCurve(basePts[i], basePts2[i]);
Point3d[] intPts = new Point3d[0];
BrepFace r_Infill = planarBreps[u].Faces[0];
Curve[] int_Curve = new Curve[0];
//Intersect Curves with Regions
Intersection.CurveBrepFace(intLine, r_Infill, tolerance, out int_Curve, out intPts);
intCurve[i] = int_Curve;
//Convert resulting Curves into LineCurves
for (int j = 0; j < int_Curve.Length; j++)
{
LineCurve line = new LineCurve(int_Curve[j].PointAtStart, int_Curve[j].PointAtEnd);
intCurve[i][j] = line;
//intCurves[j].Add(int_Curve[j]);
intCurves.Add(line);
}
}
//Rotate Array
List<Curve>[] int_Curves = RotatetoListArray(intCurve);
List<Curve> joinLines = new List<Curve>();
List<Curve> p_lines = new List<Curve>();
for (int l = 0; l < int_Curves.Length; l++)
{
for (int k = 1; k < int_Curves[l].Count; k += 2)
{
int_Curves[l][k].Reverse();
}
}
//Create a list of points for all connected lines in the infill. Do this for each seperate string of segments
for (int l = 0; l < int_Curves.Length; l++)
{
List<Point3d> plinePts = new List<Point3d>();
if (int_Curves[l].Count > 0)
{
plinePts.Add(int_Curves[l][0].PointAtStart);
for (int k = 1; k < int_Curves[l].Count; k++)
{
plinePts.Add(int_Curves[l][k - 1].PointAtEnd);
plinePts.Add(int_Curves[l][k].PointAtStart);
plinePts.Add(int_Curves[l][k].PointAtEnd);
}
PolylineCurve plCurve = new PolylineCurve(plinePts);
Curve curve = plCurve.ToNurbsCurve();
p_lines.Add(curve);
}
}
List<Curve> curve_s = p_lines;
curveInfill[u] = p_lines;
}
}
//RING FILL REGION - infills a region with a decreasing perimiter ring, this creates a spiral from the outer shape
public void Skinner(List<double> spacing)
{
double tolerance = 0.001;
List<Curve>[] outPerimeter = new List<Curve>[planarBreps.Count];
//For each planar region extract the outer edge and offset until the curve is no longer in the region, or is invalid
for (int z = 0; z < planarBreps.Count; z++ )
{
Curve Shape = planarBreps[z].Loops[0].To3dCurve();
outPerimeter[z] = new List<Curve>();
List<Curve> startshapes = new List<Curve>();
startshapes.Add(Shape);
bool stop = false;
do
{
Plane frame = horizFrame(Shape, 0.5);
List<Curve> Shapes = new List<Curve>();
//Catch possibility of offset not working
if (Shape.Offset(frame, spacing[z], Tolerance, CurveOffsetCornerStyle.Sharp) == null)
{
break;
}
Shapes = Shape.Offset(frame, spacing[z], Tolerance, CurveOffsetCornerStyle.Sharp).ToList();
Curve[] iCurves = new Curve[0];
Point3d[] iPoints = new Point3d[0];
Intersection.CurveBrep(Shapes[0], planarBreps[z], tolerance, out iCurves, out iPoints);
if (iCurves.Length < 1)
{
stop = true;
break;
}
if (iCurves.Length > 0)
{
if (iCurves[0] == null)
{
stop = true;
break;
}
//if (!iCurves[0].IsClosed)
//{
// stop = true;
// break;
//}
}
Shape = Shapes[0];
startshapes.Add(Shape);
} while (!stop);
List<Curve> unclosed = new List<Curve>();
//open the resulting offset closed curves, to join into a spiral path
foreach (Curve closed in startshapes)
{
if (unClose(closed, crossSec) != null)
{
unclosed.Add(unClose(closed, crossSec));
}
}
List<Curve> joinedPerimeter = new List<Curve>();
joinedPerimeter.Add(unclosed[0]);
//Join ends of offset curves together to create spiral path
for (int j = 1; j < unclosed.Count; j++)
{
LineCurve join = new LineCurve(unclosed[j - 1].PointAtEnd, unclosed[j].PointAtStart);
joinedPerimeter.Add(join);
joinedPerimeter.Add(unclosed[j]);
}
//Catch exception
if (Curve.JoinCurves(joinedPerimeter, 0.1, false) == null)
{
outPerimeter[z].AddRange(joinedPerimeter);
continue;
}
Curve[] allLines = Curve.JoinCurves(joinedPerimeter, 0.1, false);
outPerimeter[z].AddRange(allLines.ToList());
}
curvePerimeter = outPerimeter;
}
#endregion
#region Utility Classes
public bool lineWithinRegion(Brep region, LineCurve line)
{
//Check if line is completely within a planar region
int agreement = 0;
Point3d midPt = line.PointAt((line.Domain.Max-line.Domain.Min)/2);
foreach (BrepLoop loop in region.Loops)
{
if (loop.LoopType == BrepLoopType.Outer)
{
Curve iLoop = loop.To3dCurve();
PointContainment ptcontain = iLoop.Contains(midPt);
if (ptcontain != PointContainment.Inside)
{
agreement += 1;
}
}
if (loop.LoopType == BrepLoopType.Inner)
{
Curve iLoop = loop.To3dCurve();
PointContainment ptcontain = iLoop.Contains(midPt);
if (ptcontain != PointContainment.Outside)
{
agreement += 1;
}
}
if (loop.LoopType == BrepLoopType.Slit)
{
break;
}
if (loop.LoopType == BrepLoopType.Unknown)
{
break;
}
}
if (agreement == 0)
{
bool inside = true;
return inside;
}
else
{
bool inside = false;
return inside;
}
}
public Curve unClose(Curve curve, double offsetends)
{
//Open closed curve seam
Curve unClosed = null;
//Exit if input curve is not closed
if (!curve.IsClosed)
{
return unClosed;
}
double minparamA = (curve.Domain.Min + offsetends);
double maxparamA = (curve.Domain.Max - offsetends);
if (minparamA > maxparamA)
{
return unClosed;
}
if (curve.Split(new List<double> { minparamA, maxparamA }) == null)
{
return unClosed;
}
Curve[] curvesA = curve.Split(new List<double> { minparamA, maxparamA });
return unClosed = curvesA[1];
}
public bool validPosition(Dictionary<string, string> Settings, Box bbox)
{
//Check if bounding box of an object is within a specific 3d domain.
Interval xDomain = new Interval(0, 200);
Interval yDomain = new Interval(0, 200);
Interval zDomain = new Interval(0, 125);
if (xDomain.IncludesInterval(bbox.X) && yDomain.IncludesInterval(bbox.Y) && zDomain.IncludesInterval(bbox.Z))
{
if (!(zDomain.Min == bbox.Z.Min))
{
ErrorMessages.Add("Object is not Aligned with PrintBed!");
return validZ = false;
}
return validPos = true;
}
else
{
ErrorMessages.Add("Object is outside of Printable Area!");
}
return validPos = false;
}
public Plane horizFrame(Curve C, double t)
{
//Create a plane aligned with the world z-axis
Vector3d Tangent = C.TangentAt(t);
Vector3d Z = Vector3d.ZAxis;
Z.Reverse();
Vector3d Perp = Vector3d.CrossProduct(Z, Tangent);
Plane frame = new Plane(C.PointAt(t), Tangent, Perp);
return frame;
}
private static Curve[][] Rotate(Curve[][] input)
{
//Rotate 2d array of curves(flips rows and columns)
int length = input.GetLength(0);
Curve[][] retVal = new Curve[length][];
for (int x = 0; x < length; x++)
{
retVal[x] = input.Select(p => p[x]).ToArray();
}
return retVal;
}
public List<Curve>[] RotatetoListArray(Curve[][] input)
{
//Rotates 2d array to array of lists (flip rows and columns)
int longLength = 0;
for (int i = 0; i < input.Length; i++)
{
for (int j = 0; j < input[i].Length; j++)
{
if (j > longLength)
{
longLength = j;
}
else { continue; }
}
}
List<Curve>[] retValue = new List<Curve>[longLength + 1];
for (int m = 0; m < retValue.Length; m++)
{
retValue[m] = new List<Curve>();
}
for (int k = 0; k < input.Length; k++)
{
for (int l = 0; l < input[k].Length; l++)
{
retValue[l].Add(input[k][l]);
}
}
return retValue;
}
public BoundingBox bboxAll(List<Brep> things)
{
//Find the bounding box of a list of objects
BoundingBox bbox;
Point3d maxPt = new Point3d(0, 0, 0);
Point3d minPt = new Point3d(0, 0, 0);
foreach (Brep thing in things)
{
BoundingBox b_box = thing.GetBoundingBox(Plane.WorldXY);
if (b_box.Max.X > maxPt.X && b_box.Max.Y > maxPt.Y && b_box.Max.Z > maxPt.Z)
{
maxPt = b_box.Max;
}
if (b_box.Min.X < minPt.X && b_box.Min.Y < minPt.Y && b_box.Min.Z < minPt.Z)
{
minPt = b_box.Min;
}
}
bbox = new BoundingBox(minPt, maxPt);
return bbox;
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
#endregion
}
}