-
Notifications
You must be signed in to change notification settings - Fork 2
/
LeafComponent.cs
883 lines (724 loc) · 31.3 KB
/
LeafComponent.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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Text;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using Grasshopper.Kernel.Types;
using GH_IO.Serialization;
namespace Leaf {
public class LeafComponent : GH_Component {
public string[][] rules;
public string prevIter;
public Random rand = new Random();
public LeafComponent() : base("Leaf System", "LS",
"Produce a string using the L-system.",
"Leaf", "Main") { }
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddTextParameter("Rules", "R", "Rules to apply.", GH_ParamAccess.list, "");
pManager.AddTextParameter("Axiom", "A", "The starting character or characters.", GH_ParamAccess.item, "");
pManager.AddIntegerParameter("Cycle", "i", "L-System strings can get very large very quickly. Your computer may not be able to handle high cycle values. It's a good idea to use a panel instead of a slider so you don't accidentally crash Grasshopper.", GH_ParamAccess.item, 0);
pManager[0].Optional = false;
pManager[1].Optional = false;
pManager[2].Optional = false;
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {
pManager.AddTextParameter("Shape Code", "S", "The computed L-system string.", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA) {
// create variables that correspond to inputs
List<string> _rules = new List<string>();
string axiom = "";
int cycle = 0;
// reference the inputs to variables
if (!DA.GetDataList(0, _rules)) return;
DA.GetData(1, ref axiom);
if (!DA.GetData(2, ref cycle)) return;
// no rules
if (_rules == null || _rules.Count == 0) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No rules added.");
}
// no axiom
if (axiom == null || axiom == "") {
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Enter an axiom to start the L-System.");
}
// cycle is less than 0
if (cycle < 0) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Cycle cannot be negative.");
return;
}
// set the prevIter value to the axiom
prevIter = axiom;
// Process rules in the global array
ProcessRules(_rules);
string out_string = ProduceLSystem(rules, axiom, cycle);
// assign the output
DA.SetData(0, out_string);
}
void ProcessRules(List<string> r) {
// process the list r to remove any empties
int trueRuleListSize = 0;
for (int i = 0; i < r.Count; i++) {
if (r[i] != null) {
trueRuleListSize++;
}
}
// rules array to return
rules = new string[trueRuleListSize][];
// go through all rules, and process them
for (int i = 0; i < trueRuleListSize; i++) {
string[] rule = r[i].Split('=');
rules[i] = rule;
}
}
string ProcessRule(string prevIter, int c) {
string character = prevIter[c].ToString();
string toRet = "";
for (int r = 0; r < rules.Length; r++) {
string[] rule = rules[r];
// if character not in rule, move onto next rule
if (!rule[0].Contains(character)) continue;
// if rule does not have right hand side, move on
if (rule.Length == 1) continue;
// identify the type of rule
if (rule[0].Length == 1) {
// simple replacement rule
toRet = rule[1];
break;
} else if (rule[0].Contains("<") || rule[0].Contains(">")) {
// context sensitive rule
int indOfLeft = rule[0].IndexOf("<");
int indOfRight = rule[0].IndexOf(">");
// condition before and after symbol
string before = indOfLeft == -1 ? null : rule[0].Substring(0, indOfLeft);
string after = indOfRight == -1 ? null : rule[0].Substring(indOfRight + 1);
// before moving on further, check that character is truly in the context sensitive rule
string cont_character = rule[0][indOfLeft + 1].ToString();
// string too short, rule does not apply
if (before != null) {
int eval = c - before.Length;
if (eval < 0) {
continue;
}
}
if (after != null) {
if (c + after.Length >= prevIter.Length) {
continue;
}
}
// rule has both sides of context present
if (before != null && after != null) {
if (prevIter.Substring(c - before.Length, before.Length + after.Length + 1) == before + cont_character + after) {
toRet = rule[1];
break;
}
} else if (before != null) {
if (prevIter.Substring(c - before.Length, before.Length + 1) == before + cont_character) {
toRet = rule[1];
break;
}
} else if (after != null) {
if (prevIter.Substring(c, after.Length + 1) == cont_character + after) {
toRet = rule[1];
break;
}
}
} else if (rule[0].Contains("(") || rule[0].Contains(")")) {
// probabilistic rule
int indA = rule[0].IndexOf("(") + 1;
int indB = rule[0].IndexOf(")");
int distAB = indB - indA;
float probValue;
if (distAB == 0) {
probValue = 0;
} else {
probValue = float.Parse("0" + rule[0].Substring(indA, distAB));
}
double randVal = rand.NextDouble();
int outcome = (randVal <= probValue) ? 1 : 2;
if (rule.Length > 2) {
toRet = rule[outcome];
break;
} else {
toRet = (outcome == 2) ? "" : rule[1];
break;
}
} else {
// no rules
}
}
return toRet.Length > 0 ? toRet : character;
}
string ProduceLSystem(string[][] rules, string axiom, int cycle) {
for (int i = 0; i < cycle; i++) {
StringBuilder sB = new StringBuilder();
for (int c = 0; c < prevIter.Length; c++) {
sB.Append(ProcessRule(prevIter, c));
}
prevIter = sB.ToString();
}
return prevIter;
}
public override GH_Exposure Exposure => GH_Exposure.primary;
protected override System.Drawing.Bitmap Icon => Properties.Resources.ico.ToBitmap();
public override Guid ComponentGuid => new Guid("94996c1c-90b5-46da-a541-b8400c390e20");
}
public class InstructionsComponent : GH_Component {
public InstructionsComponent() : base("Leaf Instructions", "LI",
"How to use Leaf String Rewriter.",
"Leaf", "Info") { }
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) { }
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {
pManager.AddTextParameter("Instructions", "I", "Recognized symbols and what they do", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Version 1.1.0");
// assign the output
string outString = "";
outString += "Turtle:\n";
outString += "---------------\n";
outString += "pitch up: ^\n";
outString += "pitch down: /\n";
outString += "turn left: -\n";
outString += "turn right: +\n";
outString += "move forward without placing block: _\n";
outString += "all other symbols will cause the turtle to place a block and move forward 1 step.: _\n";
outString += "\nBranching:\n";
outString += "---------------\n";
outString += "start branch: [\n";
outString += "end branch: ]\n";
outString += "\nRule only:\n";
outString += "---------------\n";
outString += "Context left: <\n";
outString += "Context right: >\n";
outString += "Stochastic left: (\n";
outString += "Stochastic right: )\n";
outString += "\nHow to write rules:\n";
outString += "---------------\n";
outString += "To write a simple rule: a=ab (not a = ab)\n";
outString += "To write a context-sensitive rule: a<b>c=d or a<b=c b>c=d. The selected symbol is 'b'. Experiment with this rule to learn more.\n";
outString += "To write a stochastic rule: a(0.5)=b or a(0.5)=b=c. The selected symbol is 'a'. In the first case, 50% chance a is replaced with b, or nothing. In the second case, 50% chance a is replaced with b or c.\n";
outString += "\n";
outString += "All other rules follow the genreal L-System format.\n";
DA.SetData(0, outString);
}
public override GH_Exposure Exposure => GH_Exposure.primary;
protected override System.Drawing.Bitmap Icon => Properties.Resources.helpicon.ToBitmap();
public override Guid ComponentGuid => new Guid("713ae280-7e52-11ec-90d6-0242ac120003");
}
// custom data type
public class LeafGeometryData : Grasshopper.Kernel.Types.IGH_Goo {
private string symbol;
private Brep brep;
private Point3d[] points;
public LeafGeometryData() { }
public LeafGeometryData(string s, Brep b, List<Point3d> p) {
symbol = s;
brep = b;
points = new Point3d[p.Count];
for (int i = 0; i < p.Count; i++) {
points[i] = p[i];
}
}
public string Symbol {
get {
return symbol;
}
set {
symbol = value;
}
}
public Brep Geometry {
get {
return brep;
}
set {
brep = value;
}
}
public Point3d BL {
get {
return points[0];
}
set {
points[0] = value;
}
}
public Point3d TL {
get {
return points[1];
}
set {
points[1] = value;
}
}
public Point3d BR {
get {
return points[2];
}
set {
points[2] = value;
}
}
public Plane OrientPlane {
get {
return new Plane(points[0], points[1], points[2]);
}
}
public double GetVoxelSize {
get {
return Math.Abs(points[0].DistanceTo(points[1]));
}
}
public Point3d Point {
get {
Vector3d vecA = this.TL - this.BL;
vecA.Unitize();
Vector3d vecB = this.BR - this.BL;
vecB.Unitize();
Vector3d vecC = Rhino.Geometry.Vector3d.CrossProduct(vecA, vecB);
vecC.Unitize();
vecC *= -this.GetVoxelSize / 2;
// get a point in the center face
Line l = new Line(this.TL, this.BR);
Point3d p = l.PointAt(0.5);
Transform t = Rhino.Geometry.Transform.Translation(vecC);
p.Transform(t);
return p;
}
}
public bool IsValid {
get {
if (symbol == null || symbol == "") return false;
if (!brep.IsValid) return false;
if (points.Length != 3) return false;
return true;
}
}
public string IsValidWhyNot {
get {
return "Check your inputs.";
}
}
public string TypeName {
get {
return "Leaf Data";
}
}
public string TypeDescription {
get {
return "Some data";
}
}
public bool CastFrom(object source) {
throw new NotImplementedException();
}
public bool CastTo<T>(out T target) {
throw new NotImplementedException();
}
public IGH_Goo Duplicate() {
return new LeafGeometryData();
}
public IGH_GooProxy EmitProxy() {
throw new NotImplementedException();
}
public bool Read(GH_IReader reader) {
throw new NotImplementedException();
}
public object ScriptVariable() {
throw new NotImplementedException();
}
public bool Write(GH_IWriter writer) {
throw new NotImplementedException();
}
}
public class TurtlePointer {
private Point3d point, bl, tl, br;
private double vs, dist, angle;
public TurtlePointer(int x, int y, int z, double v, double d, double a) {
vs = v;
dist = d;
point = new Point3d(x, y, z);
angle = a;
bl = new Point3d(x - v / 2, y + v / 2, z - v / 2);
tl = new Point3d(x - v / 2, y + v / 2, z + v / 2);
br = new Point3d(x + v / 2, y + v / 2, z - v / 2);
}
public TurtlePointer State {
get {
return this.MemberwiseClone() as TurtlePointer;
}
set {
this.point = value.Point;
this.bl = value.BL;
this.tl = value.TL;
this.br = value.BR;
}
}
public Point3d BL {
get {
return bl;
}
}
public Point3d TL {
get {
return tl;
}
}
public Point3d BR {
get {
return br;
}
}
public Point3d Point {
get {
return point;
}
}
public Double Angle {
get {
return angle;
}
set {
angle = value;
}
}
public Plane OrientPlane {
get {
return new Plane(bl, tl, br);
}
}
public double GetVoxelSize {
get {
return this.vs;
}
}
public Point3d[] CurveEnds() {
Vector3d vecA = tl - bl;
vecA.Unitize();
Vector3d vecB = br - bl;
vecB.Unitize();
Vector3d vecC = Rhino.Geometry.Vector3d.CrossProduct(vecA, vecB);
vecC.Unitize();
Vector3d vecForward = vecC * dist / 2;
Vector3d vecBackward = vecC * (dist * -1);
// transforms
var tDirForward = Rhino.Geometry.Transform.Translation(vecForward);
var tDirBackward = Rhino.Geometry.Transform.Translation(vecBackward);
// duplicate the center point
Point3d pForward = new Point3d(point);
Point3d pBackward = new Point3d(point);
// transform the points
//pForward.Transform(tDirForward);
pBackward.Transform(tDirBackward);
// array to return
Point3d[] toRet = new Point3d[2];
toRet[0] = pBackward;
toRet[1] = pForward;
return toRet;
}
public void Move() {
Vector3d vecA = tl - bl;
vecA.Unitize();
Vector3d vecB = br - bl;
vecB.Unitize();
Vector3d vecC = Rhino.Geometry.Vector3d.CrossProduct(vecA, vecB);
vecC.Unitize();
vecC *= dist;
var tDir = Rhino.Geometry.Transform.Translation(vecC);
point.Transform(tDir);
bl.Transform(tDir);
tl.Transform(tDir);
br.Transform(tDir);
}
public void TurnLeft() {
Vector3d vecC = tl - bl;
vecC.Unitize();
var tRot = Rhino.Geometry.Transform.Rotation((Math.PI / 180) * angle, vecC, point);
point.Transform(tRot);
bl.Transform(tRot);
tl.Transform(tRot);
br.Transform(tRot);
}
public void TurnRight() {
Vector3d vecC = tl - bl;
vecC.Unitize();
var tRot = Rhino.Geometry.Transform.Rotation((Math.PI / 180) * -angle, vecC, point);
point.Transform(tRot);
bl.Transform(tRot);
tl.Transform(tRot);
br.Transform(tRot);
}
public void PitchUp() {
Vector3d vecC = br - bl;
vecC.Unitize();
var tRot = Rhino.Geometry.Transform.Rotation((Math.PI / 180) * angle, vecC, point);
point.Transform(tRot);
bl.Transform(tRot);
tl.Transform(tRot);
br.Transform(tRot);
}
public void PitchDown() {
Vector3d vecC = br - bl;
vecC.Unitize();
var tRot = Rhino.Geometry.Transform.Rotation((Math.PI / 180) * -angle, vecC, point);
point.Transform(tRot);
bl.Transform(tRot);
tl.Transform(tRot);
br.Transform(tRot);
}
}
public class TurtleVoxelsComponent : GH_Component {
public TurtleVoxelsComponent() : base("Leaf Voxel Turtle", "LVT",
"A 3D agent based voxel drawing component based around the L-System.",
"Leaf", "Main") { }
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddTextParameter("Shape Code", "S", "L-System string to take in.", GH_ParamAccess.item, "");
pManager.AddNumberParameter("Voxel Size", "V", "Standard voxel size.", GH_ParamAccess.item, 1);
pManager.AddNumberParameter("Angle", "A", "Turn angle.", GH_ParamAccess.item, 90);
pManager.AddNumberParameter("Step Distance", "D", "Distance to move over for each voxel. This value should equal Voxel Size (V).", GH_ParamAccess.item, 1);
pManager.AddGenericParameter("Leaf Geometry", "G", "Geometries to substitute symbols with.", GH_ParamAccess.list);
pManager[0].Optional = false;
pManager[1].Optional = true;
pManager[2].Optional = true;
pManager[3].Optional = true;
pManager[4].Optional = true;
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {
pManager.AddBrepParameter("Geometries", "GS", "The 3D representation of the L-System", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA) {
// create variables that correspond to inputs
string shapeCode = "";
GH_Number voxelSize = new GH_Number(1);
GH_Number angle = new GH_Number(90);
GH_Number stepDistance = new GH_Number(1);
List<LeafGeometryData> mD = new List<LeafGeometryData>();
// reference the inputs to variables
if (!DA.GetData(0, ref shapeCode)) return;
if (!DA.GetData(1, ref voxelSize)) return;
if (!DA.GetData(2, ref angle)) return;
if (!DA.GetData(3, ref stepDistance)) return;
DA.GetDataList(4, mD);
/// make dictionary of geometries
IDictionary<string, int> geoDict = new Dictionary<string, int>();
for (int i = 0; i < mD.Count; i++) {
if (mD[i] != null && mD[i].IsValid) {
geoDict.Add(mD[i].Symbol, i);
}
}
// create turtle pointer
TurtlePointer TP = new TurtlePointer(0, 0, 0, voxelSize.Value, stepDistance.Value, angle.Value);
// create pointer stack limit to 500 steps of recursion. May increase this in the future
TurtlePointer[] pointerStack = new TurtlePointer[500];
int stackPointer = 0;
// list of breps to accumulate. Starts with 5000 slots, adds another 5000 if filled
List<Brep> boxes = new List<Brep>(5000);
for (int i = 0; i < shapeCode.Length; i++) {
string c = shapeCode[i].ToString();
if (c == "^") {
TP.PitchUp();
} else if (c == "/") {
TP.PitchDown();
} else if (c == "-") {
TP.TurnLeft();
} else if (c == "+") {
TP.TurnRight();
} else if (c == "_") {
TP.Move();
} else if (c == "[") {
pointerStack[stackPointer] = TP.State;
stackPointer++;
// if this happens, throw an exception
if (stackPointer >= pointerStack.Length) {
stackPointer = pointerStack.Length - 1;
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Stack overflow!");
return;
}
} else if (c == "]") {
TP.State = pointerStack[stackPointer - 1];
stackPointer--;
if (stackPointer <= 0) {
stackPointer = 0;
}
} else {
// move the pointer forward
TP.Move();
if (geoDict.ContainsKey(c)) {
// replace with geometry
boxes.Add(InsertGeometry(mD[geoDict[c]], TP));
} else {
// replace with block
boxes.Add(CreateBlock(TP));
}
}
}
// assign the output
DA.SetDataList(0, boxes);
}
public Brep CreateBlock(TurtlePointer tP) {
Plane p = new Plane(tP.TL, tP.BL, tP.BR);
Box b = new Box(p, new Interval(0, tP.GetVoxelSize), new Interval(0, tP.GetVoxelSize), new Interval(0, tP.GetVoxelSize));
return b.ToBrep();
}
public Brep InsertGeometry(LeafGeometryData lD, TurtlePointer tP) {
GeometryBase gB = lD.Geometry.Duplicate();
Brep br = Brep.TryConvertBrep(gB);
Plane sourcePlane = lD.OrientPlane;
Plane targetPlane = tP.OrientPlane;
// if br needs to be scaled, scale it
double scaleFactor = tP.GetVoxelSize / lD.GetVoxelSize;
// scale br
if (scaleFactor != 1) {
var trans = Rhino.Geometry.Transform.Scale(lD.Point, scaleFactor);
br.Transform(trans);
sourcePlane.Transform(trans);
}
// orient the two planes
var t = Rhino.Geometry.Transform.PlaneToPlane(sourcePlane, targetPlane);
br.Transform(t);
return br;
}
public override GH_Exposure Exposure => GH_Exposure.primary;
protected override System.Drawing.Bitmap Icon => Properties.Resources.turtleicon.ToBitmap();
public override Guid ComponentGuid => new Guid("3aa93db1-48fc-444f-89e4-c8d6364d8a0a");
}
public class TurtleLineComponent : GH_Component {
// code will be optimized later
public TurtleLineComponent() : base("Leaf Line Turtle", "LLT",
"A 3D agent based line drawing component based around the L-System.",
"Leaf", "Main") { }
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddTextParameter("Shape Code", "S", "L-System string to take in.", GH_ParamAccess.item, "");
pManager.AddNumberParameter("Angle", "A", "Turn angle.", GH_ParamAccess.item, 90);
pManager.AddNumberParameter("Step Distance", "D", "Distance to move over for each line.", GH_ParamAccess.item, 1);
pManager[0].Optional = false;
pManager[1].Optional = true;
pManager[2].Optional = true;
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {
pManager.AddLineParameter("Lines", "L", "The 3D representation of the L-System", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA) {
// create variables that correspond to inputs
string shapeCode = "";
GH_Number angle = new GH_Number(90);
GH_Number stepDistance = new GH_Number(1);
// reference the inputs to variables
if (!DA.GetData(0, ref shapeCode)) return;
if (!DA.GetData(1, ref angle)) return;
if (!DA.GetData(2, ref stepDistance)) return;
// create turtle pointer
TurtlePointer TP = new TurtlePointer(0, 0, 0, stepDistance.Value, stepDistance.Value, angle.Value);
// create pointer stack limit to 500 steps of recursion. May increase this in the future
TurtlePointer[] pointerStack = new TurtlePointer[500];
int stackPointer = 0;
// list of curves to accumulate. Starts with 5000 slots, adds another 5000 if filled
List<Line> lines = new List<Line>(5000);
for (int i = 0; i < shapeCode.Length; i++) {
string c = shapeCode[i].ToString();
if (c == "^") {
TP.PitchUp();
} else if (c == "/") {
TP.PitchDown();
} else if (c == "-") {
TP.TurnLeft();
} else if (c == "+") {
TP.TurnRight();
} else if (c == "_") {
TP.Move();
} else if (c == "[") {
pointerStack[stackPointer] = TP.State;
stackPointer++;
// if this happens, throw an exception
if (stackPointer >= pointerStack.Length) {
stackPointer = pointerStack.Length - 1;
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Stack overflow!");
return;
}
} else if (c == "]") {
TP.State = pointerStack[stackPointer - 1];
stackPointer--;
if (stackPointer <= 0) {
stackPointer = 0;
}
} else {
// move the pointer forward
TP.Move();
// any other symbol means place a curve
lines.Add(CreateLine(TP));
}
}
// assign the output
DA.SetDataList(0, lines);
}
public Line CreateLine(TurtlePointer tP) {
Point3d[] curveEnds = tP.CurveEnds();
// construct a line
Line crv = new Line(curveEnds[0], curveEnds[1]);
return crv;
}
public override GH_Exposure Exposure => GH_Exposure.primary;
protected override System.Drawing.Bitmap Icon => Properties.Resources.turtleicon2.ToBitmap();
public override Guid ComponentGuid => new Guid("f8792a0b-67ba-4c61-bbd3-bbc4b60c1ff8");
}
public class GeometryComponent : GH_Component {
public GeometryComponent() : base("Leaf Geometry", "LG",
"Used to substitute symbols with geometry via the Leaf Turtle component.",
"Leaf", "Geometry") { }
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) {
pManager.AddTextParameter("Symbol", "S", "Symbol to replace", GH_ParamAccess.item, "");
pManager.AddBrepParameter("Geometry", "G", "Geometry to replace with.", GH_ParamAccess.item);
pManager.AddPointParameter("Points", "P", "Points must be on the front face of the voxel, and selected in order: Bottom left, Top left, Bottom right.", GH_ParamAccess.list);
pManager[0].Optional = false;
pManager[1].Optional = false;
pManager[2].Optional = false;
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) {
pManager.AddGenericParameter("Leaf Geometry", "G", "experimental output", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA) {
// create variables that correspond to inputs
string symbol = null;
Brep brep = null;
List<Point3d> points = new List<Point3d>();
// reference the inputs to variables
DA.GetData(0, ref symbol);
DA.GetData(1, ref brep);
DA.GetDataList(2, points);
bool errored = false;
// symbol
if (symbol == null || symbol == "") {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "You must choose a symbol to replace.");
errored = true;
} else if ("^/-+_".Contains(symbol)) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Cannot replace geometry for movement symbols.");
errored = true;
} else if (symbol.Length > 1) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Create a separate Leaf Geometry component to replace more than 1 symbol at a time.");
errored = true;
}
// brep
if (brep == null || !brep.IsValid) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Set a brep geometry to replace with.");
errored = true;
}
// points
if (points[0] == null || points.Count != 3) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Check that you have set exactly 3 points.");
errored = true;
}
if (errored) return;
// TODO check points for squraeness
// package data for sending
LeafGeometryData mD = new LeafGeometryData(symbol, brep, points);
// assign the output
DA.SetData(0, mD);
}
public override GH_Exposure Exposure => GH_Exposure.primary;
protected override System.Drawing.Bitmap Icon => Properties.Resources.leafgeoicon.ToBitmap();
public override Guid ComponentGuid => new Guid("866f12ba-7e53-11ec-90d6-0242ac120003");
}
}