-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGHComponents.cs
1335 lines (1247 loc) · 55.5 KB
/
GHComponents.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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using System.Windows.Forms;
using Grasshopper.Kernel.Parameters;
using System.Drawing;
using static SuperDelaunay.Utils;
using Grasshopper.GUI.Canvas;
using Grasshopper.GUI;
namespace SuperDelaunay.GH
{
public class Comp_Delaunay2D : GH_Component, IGH_VariableParameterComponent
{
#region Fields
internal Delaunay2d DelaunayInstance;
#endregion
#region Properties
public override Guid ComponentGuid => new Guid("84e042eb-39e3-42e8-8cbf-119b6f203e9d");
protected override Bitmap Icon => Properties.Resources.delaunay2_24x24;
public UI.DebuggerForm Controller { get; internal set; }
public bool IsControllerAlive { get { return Controller != null; } }
#endregion
#region Constructors
public Comp_Delaunay2D() : base("Delaunay", "Delaunay", "Perform the 2D weighted delaunay triangulation", "Mesh", "Triangulation")
{
ValuesChanged();
}
#endregion
#region Parameters
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
UpdateInputParameters(GetInputMode(), GetOutputMode());
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
UpdateOutputParameters(GetInputMode(), GetOutputMode());
}
private void UpdateInputParameters(DelaunayInputMode inputMode, DelaunayOutputMode outputMode)
{
IGH_Param CreateDelaunayInput()
{
return new Param_GenericObject()
{
Name = "Preexisting Delaunay",
NickName = "D",
Description = "Preexisting delaunay where to include new points",
Access = GH_ParamAccess.item
};
}
IGH_Param CreatePointsInput(bool optional = false)
{
return new Param_Point()
{
Name = "Points",
NickName = "P",
Description = "Points to triangulate",
Access = GH_ParamAccess.list,
Hidden = true,
Optional = optional
};
}
IGH_Param CreateWeightsInput()
{
return new Param_Number()
{
Name = "Weights",
NickName = "W",
Description = "Optional positive weights per point",
Access = GH_ParamAccess.list,
Optional = true
};
}
IGH_Param CreatePlaneInput()
{
var pln = new Param_Plane()
{
Name = "Plane",
NickName = "Pl",
Description = "Optional plane, if none then the best fit plane will be used",
Access = GH_ParamAccess.item,
Optional = true
};
//pln.SetPersistentData(new GH_Plane(Plane.WorldXY));
return pln;
}
IGH_Param CreateVoronoiInput()
{
var v = new Param_Curve()
{
Name = "Bounds",
NickName = "B",
Description = "Planar polyline to intersect the cells",
Access = GH_ParamAccess.item,
Optional = true
};
return v;
}
IGH_Param CreateBetaInput()
{
var bk = new Param_Number()
{
Name = "Beta",
NickName = "B",
Description = "Beta factor.\r\n" +
"If beta < 1, it uses the intersection of the two beta circles with radius edge length / beta.\r\n" +
"If beta == 1, the diameter of the beta circle is the edge length (Gabriel Graph).\r\n" +
"If beta > 1, it uses the union of the two beta circles with radius edge length * beta.",
Access = GH_ParamAccess.item
};
bk.SetPersistentData(new GH_Number(1.0));
return bk;
}
IGH_Param CreateAlphaInput()
{
var ash = new Param_Number()
{
Name = "Alpha",
NickName = "A",
Description = "Radius of the alpha shape ball",
Access = GH_ParamAccess.item
};
ash.SetPersistentData(new GH_Number(1.0));
return ash;
}
IGH_Param CreateRadiusInput()
{
var ash = new Param_Number()
{
Name = "Radius",
NickName = "R",
Description = "Radius of the skeleton",
Access = GH_ParamAccess.item
};
ash.SetPersistentData(new GH_Number(1.0));
return ash;
}
IGH_Param CreateDivisionsInput()
{
var ash = new Param_Number()
{
Name = "Division Factor",
NickName = "D",
Description = "The number of divisions along the edge is the length of the edge multiplied by this value. ",
Access = GH_ParamAccess.item
};
ash.SetPersistentData(new GH_Number(1.0));
return ash;
}
var delaunayInput = Params.Input.Find(p => p.Name == "Preexisting Delaunay");
var pointsInput = Params.Input.Find(p => p.Name == "Points") as Param_Point;
var weightsInput = Params.Input.Find(p => p.Name == "Weights") as Param_Number;
var planeInput = Params.Input.Find(p => p.Name == "Plane") as Param_Plane;
var voronoiInput = Params.Input.Find(p => p.Name == "Bounds") as Param_Curve;
var betaInput = Params.Input.Find(p => p.Name == "Beta") as Param_Number;
var alphaInput = Params.Input.Find(p => p.Name == "Alpha") as Param_Number;
var radiusInput = Params.Input.Find(p => p.Name == "Radius") as Param_Number;
var divisionsInput = Params.Input.Find(p => p.Name == "Division Factor") as Param_Number;
switch (inputMode)
{
case DelaunayInputMode.Create:
if (delaunayInput != null)
Params.UnregisterInputParameter(delaunayInput);
if (pointsInput != null)
pointsInput.Optional = false;
else
Params.RegisterInputParam(CreatePointsInput(), 0);
if (weightsInput == null)
Params.RegisterInputParam(CreateWeightsInput(), 1);
if (planeInput == null)
Params.RegisterInputParam(CreatePlaneInput(), 2);
break;
case DelaunayInputMode.Insert:
if (delaunayInput == null)
Params.RegisterInputParam(CreateDelaunayInput(), 0);
if (pointsInput == null)
Params.RegisterInputParam(CreatePointsInput(true), 1);
if (weightsInput == null)
Params.RegisterInputParam(CreateWeightsInput(), 2);
if (planeInput != null)
Params.UnregisterInputParameter(planeInput);
break;
case DelaunayInputMode.Extract:
if (delaunayInput == null)
Params.RegisterInputParam(CreateDelaunayInput(), 0);
if (pointsInput != null)
Params.UnregisterInputParameter(pointsInput);
if (weightsInput != null)
Params.UnregisterInputParameter(weightsInput);
if (planeInput != null)
Params.UnregisterInputParameter(planeInput);
break;
}
switch (outputMode)
{
case DelaunayOutputMode.Voronoi:
if (voronoiInput == null)
Params.RegisterInputParam(CreateVoronoiInput(), 3);
if (betaInput != null)
Params.UnregisterInputParameter(betaInput);
if (alphaInput != null)
Params.UnregisterInputParameter(alphaInput);
if (radiusInput != null)
Params.UnregisterInputParameter(radiusInput);
if (divisionsInput != null)
Params.UnregisterInputParameter(divisionsInput);
break;
case DelaunayOutputMode.Beta_Skeleton:
if (betaInput == null)
Params.RegisterInputParam(CreateBetaInput(), 3);
if (voronoiInput != null)
Params.UnregisterInputParameter(voronoiInput);
if (alphaInput != null)
Params.UnregisterInputParameter(alphaInput);
if (radiusInput != null)
Params.UnregisterInputParameter(radiusInput);
if (divisionsInput != null)
Params.UnregisterInputParameter(divisionsInput);
break;
case DelaunayOutputMode.Alpha_Shape:
if (alphaInput == null)
Params.RegisterInputParam(CreateAlphaInput(), 3);
if (betaInput != null)
Params.UnregisterInputParameter(betaInput);
if (voronoiInput != null)
Params.UnregisterInputParameter(voronoiInput);
if (radiusInput != null)
Params.UnregisterInputParameter(radiusInput);
if (divisionsInput != null)
Params.UnregisterInputParameter(divisionsInput);
break;
case DelaunayOutputMode.Thickener_Primal:
if (voronoiInput != null)
Params.UnregisterInputParameter(voronoiInput);
if (betaInput != null)
Params.UnregisterInputParameter(betaInput);
if (alphaInput != null)
Params.UnregisterInputParameter(alphaInput);
if (radiusInput == null)
Params.RegisterInputParam(CreateRadiusInput());
if (divisionsInput == null)
Params.RegisterInputParam(CreateDivisionsInput());
break;
case DelaunayOutputMode.Thickener_Dual:
if (voronoiInput == null)
Params.RegisterInputParam(CreateVoronoiInput());
if (betaInput != null)
Params.UnregisterInputParameter(betaInput);
if (alphaInput != null)
Params.UnregisterInputParameter(alphaInput);
if (radiusInput == null)
Params.RegisterInputParam(CreateRadiusInput());
if (divisionsInput == null)
Params.RegisterInputParam(CreateDivisionsInput());
break;
default:
if (voronoiInput != null)
Params.UnregisterInputParameter(voronoiInput);
if (betaInput != null)
Params.UnregisterInputParameter(betaInput);
if (alphaInput != null)
Params.UnregisterInputParameter(alphaInput);
if (radiusInput != null)
Params.UnregisterInputParameter(radiusInput);
if (divisionsInput != null)
Params.UnregisterInputParameter(divisionsInput);
break;
}
Params.OnParametersChanged();
}
private void UpdateOutputParameters(DelaunayInputMode inputMode, DelaunayOutputMode outputMode)
{
IGH_Param CreateDelaunayOutput()
{
return new Param_GenericObject()
{
Name = "Delaunay",
NickName = "D",
Description = "Resulting delaunay object",
Access = GH_ParamAccess.item,
Hidden = true
};
}
IGH_Param CreateGraphOutput()
{
IGH_Param param = null;
switch (outputMode)
{
case DelaunayOutputMode.Vertices:
param = new Param_Point() { Access = GH_ParamAccess.list };
break;
case DelaunayOutputMode.Edges:
case DelaunayOutputMode.Beta_Skeleton:
case DelaunayOutputMode.Relative_Neighborhood_Graph:
case DelaunayOutputMode.Nearest_Neighbor_Graph:
case DelaunayOutputMode.Urquhart_Graph:
case DelaunayOutputMode.Minimum_Spanning_Tree:
param = new Param_Line() { Access = GH_ParamAccess.list };
break;
case DelaunayOutputMode.Voronoi:
case DelaunayOutputMode.Alpha_Shape:
param = new Param_Curve() { Access = GH_ParamAccess.list };
break;
case DelaunayOutputMode.Convex_Hull:
param = new Param_Curve() { Access = GH_ParamAccess.item };
break;
case DelaunayOutputMode.Mesh:
case DelaunayOutputMode.Thickener_Primal:
case DelaunayOutputMode.Thickener_Dual:
param = new Param_Mesh() { Access = GH_ParamAccess.item };
break;
default:
throw new Exception();
}
param.Name = GetModeName(outputMode);
param.NickName = param.Name[0].ToString();
param.Description = $"Resulting {param.Name.ToLower()}, {GetModeDescription(outputMode).ToLower()}.";
return param;
}
IGH_Param CreateTopologyOutput()
{
IGH_Param param = new Param_Integer();
param.Name = $"{GetModeName(outputMode)} topology";
param.NickName = "T";
param.Access = GH_ParamAccess.tree;
switch (outputMode)
{
case DelaunayOutputMode.Edges:
case DelaunayOutputMode.Beta_Skeleton:
case DelaunayOutputMode.Relative_Neighborhood_Graph:
case DelaunayOutputMode.Nearest_Neighbor_Graph:
case DelaunayOutputMode.Urquhart_Graph:
case DelaunayOutputMode.Minimum_Spanning_Tree:
case DelaunayOutputMode.Thickener_Primal:
param.Description = "For each edge, indices of its points.";
break;
case DelaunayOutputMode.Vertices:
case DelaunayOutputMode.Voronoi:
param.Description = "For each cell/point, indices of its connected cells/points.";
break;
case DelaunayOutputMode.Alpha_Shape:
param.Description = "For each polygon, indices of its points.";
break;
case DelaunayOutputMode.Convex_Hull:
param.Description = "Indices of its points.";
break;
case DelaunayOutputMode.Mesh:
param.Description = "For each triangle, indices of its points.";
break;
case DelaunayOutputMode.Thickener_Dual:
param.Description = "For each cell/point, indices of its connected cells/points.";
break;
default:
throw new Exception();
}
return param;
}
IGH_Param CreateAdditionalOutput()
{
switch (outputMode)
{
case DelaunayOutputMode.Vertices:
return new Param_Boolean()
{
Name = "Connected",
NickName = "C",
Description = "For each point, true if is connected to other vertex.\r\nSome points may not be connected because of nearby points with larger weights.",
Access = GH_ParamAccess.list
};
default:
return null;
}
}
var delaunayOutput = Params.Output.Find(p => p.Name == "Delaunay") as Param_GenericObject;
var graphOutput = Params.Output.Count > 1 ? Params.Output[1] : null;
var topologyOutput = Params.Output.Count > 2 ? Params.Output[2] : null;
var additionalOutput = Params.Output.Count > 3 ? Params.Output[3] : null;
if (delaunayOutput == null)
Params.RegisterOutputParam(CreateDelaunayOutput(), 0);
else
delaunayOutput.Hidden = true;
if (graphOutput == null)
{
Params.RegisterOutputParam(CreateGraphOutput(), 1);
}
else
{
if (graphOutput.Name != GetModeName(outputMode))
{
var output = CreateGraphOutput();
foreach (var recipient in graphOutput.Recipients)
recipient.AddSource(output);
Params.UnregisterOutputParameter(graphOutput);
Params.RegisterOutputParam(output, 1);
}
}
if (topologyOutput == null)
{
Params.RegisterOutputParam(CreateTopologyOutput(), 2);
}
else
{
if (topologyOutput.Name != $"{GetModeName(outputMode)} topology")
{
var output = CreateTopologyOutput();
foreach (var recipient in topologyOutput.Recipients)
recipient.AddSource(output);
Params.UnregisterOutputParameter(topologyOutput);
Params.RegisterOutputParam(output, 2);
}
}
if (additionalOutput == null)
{
var aoutput = CreateAdditionalOutput();
if (aoutput != null)
Params.RegisterOutputParam(aoutput);
}
else
{
if (outputMode != DelaunayOutputMode.Vertices && additionalOutput.Name == "Is External")
{
Params.UnregisterOutputParameter(additionalOutput);
}
}
Params.OnParametersChanged();
}
#region InputMode
public enum DelaunayInputMode { Create, Insert, Extract }
private DelaunayInputMode GetInputMode()
{
return (DelaunayInputMode)GetValue(nameof(DelaunayInputMode), 0);
}
private void SetInputMode(DelaunayInputMode inputMode)
{
SetValue(nameof(DelaunayInputMode), (int)inputMode);
}
public static string GetModeName(DelaunayInputMode inputMode)
{
switch (inputMode)
{
case DelaunayInputMode.Create:
return "Create new delaunay";
case DelaunayInputMode.Insert:
return "Insert weighted points";
case DelaunayInputMode.Extract:
return "Extract only";
default:
throw new Exception();
}
}
private static string GetModeDescription(DelaunayInputMode inputMode)
{
switch (inputMode)
{
case DelaunayInputMode.Create:
return "Create a new delaunay from weighted points";
case DelaunayInputMode.Insert:
return "Insert weighted points in a existing delaunay";
case DelaunayInputMode.Extract:
return "Extract a subgraph or some property in a existing delaunay";
default:
throw new Exception();
}
}
#endregion
#region OutputMode
public enum DelaunayOutputMode { Vertices, Edges, Mesh, Voronoi, Thickener_Primal, Thickener_Dual, Convex_Hull, Alpha_Shape, Beta_Skeleton, Relative_Neighborhood_Graph, Nearest_Neighbor_Graph, Urquhart_Graph, Minimum_Spanning_Tree }
private DelaunayOutputMode GetOutputMode()
{
return (DelaunayOutputMode)GetValue(nameof(DelaunayOutputMode), 1);
}
private void SetOutputMode(DelaunayOutputMode outputMode)
{
SetValue(nameof(DelaunayOutputMode), (int)outputMode);
}
private static string GetModeName(DelaunayOutputMode outputMode)
{
return outputMode.ToString().Replace("_", " ");
}
private static string GetModeDescription(DelaunayOutputMode outputMode)
{
switch (outputMode)
{
case DelaunayOutputMode.Vertices:
return "The triangulation vertices";
case DelaunayOutputMode.Edges:
return "The triangulation lines";
case DelaunayOutputMode.Mesh:
return "The triangulation mesh";
case DelaunayOutputMode.Voronoi:
return "The delaunay dual";
case DelaunayOutputMode.Thickener_Primal:
return "The delaunay edges with thickness";
case DelaunayOutputMode.Thickener_Dual:
return "The delaunay dual with thickness";
case DelaunayOutputMode.Convex_Hull:
return "The convex bounding polygon";
case DelaunayOutputMode.Alpha_Shape:
return "The bounding polygons where for each edge there is no point within the circle formed by its ends and a given radius";
case DelaunayOutputMode.Beta_Skeleton:
return "The graph where for each edge there is no point within the intersection of the circles formed by its ends and a given factor";
case DelaunayOutputMode.Relative_Neighborhood_Graph:
return "The graph where for each edge there is no other point closer to both than they are to each other";
case DelaunayOutputMode.Nearest_Neighbor_Graph:
return "The graph where for each edge there is no other point closer to some of its ends than they are to each other";
case DelaunayOutputMode.Urquhart_Graph:
return "The graph obtained by removing the longest edge from each triangle";
case DelaunayOutputMode.Minimum_Spanning_Tree:
return "The graph obtained by connecting all the edges with the minimum total length";
default:
throw new Exception();
}
}
protected override void ValuesChanged()
{
Message = GetModeName(GetOutputMode());
if (Message.Length > 15)
Message = Message.Replace(" ", Environment.NewLine);
ClearRuntimeMessages();
foreach (var input in Params.Input)
input.ClearRuntimeMessages();
}
#endregion
#region Metric
private DelaunayMetric GetMetric()
{
return (DelaunayMetric)GetValue(nameof(DelaunayMetric), 0);
}
private void SetMetric(DelaunayMetric metric)
{
SetValue(nameof(DelaunayMetric), (int)metric);
}
#endregion
#region IGH_VariableParameterComponent
public bool CanInsertParameter(GH_ParameterSide side, int index)
{
return false;
}
public bool CanRemoveParameter(GH_ParameterSide side, int index)
{
return false;
}
public IGH_Param CreateParameter(GH_ParameterSide side, int index)
{
return null;
}
public bool DestroyParameter(GH_ParameterSide side, int index)
{
return true;
}
public void VariableParameterMaintenance()
{
}
#endregion
#endregion
#region Preview
private enum DelaunayPreview { Points, Edges, Weights, Orthoballs, InfiniteTriangles }
private Dictionary<DelaunayPreview, bool> GetPreviewMode()
{
var dic = new Dictionary<DelaunayPreview, bool>();
foreach (var mode in GetEnumValues<DelaunayPreview>())
dic.Add(mode, GetPreviewMode(mode));
return dic;
}
private bool GetPreviewMode(DelaunayPreview mode)
{
return GetValue($"{nameof(DelaunayPreview)}.{mode}", false);
}
private void SetPreviewMode(DelaunayPreview mode, bool value)
{
SetValue($"{nameof(DelaunayPreview)}.{mode}", value);
}
private Color GetPreviewColor(DelaunayPreview mode)
{
var name = $"{nameof(DelaunayPreview)}.{mode}.Color";
switch (mode)
{
case DelaunayPreview.Points:
return GetValue(name, Color.DarkGray);
case DelaunayPreview.Edges:
return GetValue(name, Color.Gray);
case DelaunayPreview.Weights:
return GetValue(name, Color.Orange);
case DelaunayPreview.Orthoballs:
return GetValue(name, Color.Red);
case DelaunayPreview.InfiniteTriangles:
return GetValue(name, Color.Magenta);
default:
throw new NotImplementedException();
}
}
private void SetPreviewColor(DelaunayPreview mode, Color color)
{
SetValue($"{nameof(DelaunayPreview)}.{mode}.Color", color);
}
private static string GetModeName(DelaunayPreview mode)
{
switch (mode)
{
case DelaunayPreview.Points:
return "Draw points";
case DelaunayPreview.Edges:
return "Draw edges";
case DelaunayPreview.Weights:
return "Draw weights";
case DelaunayPreview.Orthoballs:
return "Draw orthoballs";
case DelaunayPreview.InfiniteTriangles:
return "Draw infinite triangles";
default:
throw new NotImplementedException();
}
}
private static string GetModeDescription(DelaunayPreview mode)
{
switch (mode)
{
case DelaunayPreview.Points:
return "Display the delaunay vertices";
case DelaunayPreview.Edges:
return "Display the delaunay edges";
case DelaunayPreview.Weights:
return "Display the weights as circles";
case DelaunayPreview.Orthoballs:
return "Display the weighted circumcenters as circles";
case DelaunayPreview.InfiniteTriangles:
return "Display the edges of the auxiliar triangles used to build the delaunay";
default:
throw new NotImplementedException();
}
}
Line[] edgesCache;
Circle[] circleWeightsCache;
Circle[] orthoballsCache;
Line[] infiniteTriangles;
public void Draw(Rhino.Display.DisplayPipeline pipeline, Color mainColor)
{
if (DelaunayInstance == null)
return;
if (IsControllerAlive)
{
if (!Controller.HasInstance)
return;
var style = Grasshopper.CentralSettings.PreviewPointStyle;
if (Controller.ShowTriangulationEdges)
{
var colorEdges = GetPreviewColor(DelaunayPreview.Edges);
edgesCache = DelaunayInstance.GetAllEdges().Select(e => e.ToLine()).ToArray();
pipeline.DrawLines(edgesCache, colorEdges);
}
if (Controller.ShowTriangulationVertices)
{
var colorVertices = GetPreviewColor(DelaunayPreview.Points);
foreach (var v in DelaunayInstance.GetAllVertices())
pipeline.DrawPoint(v.Position3d, style, 3, colorVertices);
}
var visibleTriangles = Controller.GetSelectedTriangles();
if (visibleTriangles.Any())
{
if (Controller.ShowTriangulationWeights)
{
var visibleVertices = visibleTriangles.SelectMany(t => t.Vertices).Distinct();
circleWeightsCache = visibleVertices.Select(v => v.ToCircle()).ToArray();
var color = GetPreviewColor(DelaunayPreview.Weights);
foreach (var c in circleWeightsCache)
pipeline.DrawCircle(c, color);
}
if (Controller.ShowTriangulationOrthoballs)
{
orthoballsCache = visibleTriangles.Select(t => t.ToCircle()).ToArray();
var color = GetPreviewColor(DelaunayPreview.Orthoballs);
foreach (var c in orthoballsCache)
pipeline.DrawCircle(c, color);
foreach (var c in orthoballsCache)
pipeline.DrawPoint(c.Center, style, 3, color);
}
}
var currentVertex = Controller.CurrentVertex();
if (currentVertex != null)
{
pipeline.DrawPoint(currentVertex.Position3d, style, 7, mainColor);
if (Controller.ShowNextVertexWeight)
{
pipeline.DrawCircle(currentVertex.ToCircle(), Color.Orange);
}
IEnumerable<Triangle> badTriangles = null;
if (Controller.ShowNextVertexBadTriangles)
{
badTriangles = DelaunayInstance.FindBadTriangles(currentVertex);
foreach (var t in badTriangles)
{
pipeline.DrawPolyline(t.ToPolyline(), Color.Magenta, 5);
}
}
if (Controller.ShowNextVertexOrthoballs)
{
if(badTriangles == null)
badTriangles = DelaunayInstance.FindBadTriangles(currentVertex);
foreach (var t in badTriangles)
{
pipeline.DrawCircle(t.ToCircle(), Color.DarkRed, 2);
}
}
if (Controller.ShowNextVertexNewTriangles)
{
if (badTriangles == null)
badTriangles = DelaunayInstance.FindBadTriangles(currentVertex);
var newTriangles = DelaunayInstance.Polygonize(badTriangles, currentVertex);
foreach (var t in newTriangles)
{
pipeline.DrawPolyline(t.ToPolyline(), Color.Green, 5);
}
}
}
}
else
{
if (GetPreviewMode(DelaunayPreview.Points))
{
var color = Attributes.Selected ? mainColor : GetPreviewColor(DelaunayPreview.Points);
var style = Grasshopper.CentralSettings.PreviewPointStyle;
foreach (var v in DelaunayInstance.Vertices)
{
pipeline.DrawPoint(v.Position3d, style, 5, color);
}
}
if (GetPreviewMode(DelaunayPreview.Edges))
{
if (edgesCache == null)
edgesCache = DelaunayInstance.GetEdges().Select(e => e.ToLine()).ToArray();
var color = Attributes.Selected ? mainColor : GetPreviewColor(DelaunayPreview.Edges);
pipeline.DrawLines(edgesCache, color);
}
if (GetPreviewMode(DelaunayPreview.Weights))
{
if (circleWeightsCache == null)
{
circleWeightsCache = DelaunayInstance.Vertices.Select(v =>v.ToCircle()).ToArray();
}
var color = Attributes.Selected ? mainColor : GetPreviewColor(DelaunayPreview.Weights);
foreach (var c in circleWeightsCache)
{
pipeline.DrawCircle(c, color);
}
}
if (GetPreviewMode(DelaunayPreview.Orthoballs))
{
if (orthoballsCache == null)
{
orthoballsCache = DelaunayInstance.Triangles.Select(t => t.ToCircle()).ToArray();
}
var color = Attributes.Selected ? mainColor : GetPreviewColor(DelaunayPreview.Orthoballs);
foreach (var c in orthoballsCache)
{
pipeline.DrawCircle(c, color);
}
}
if (GetPreviewMode(DelaunayPreview.InfiniteTriangles))
{
if (infiniteTriangles == null)
{
infiniteTriangles = DelaunayInstance.GetAllEdges().Where(e => e.A.IsInfinite || e.B.IsInfinite).Select(e => e.ToLine()).ToArray();
}
var color = Attributes.Selected ? mainColor : GetPreviewColor(DelaunayPreview.InfiniteTriangles);
pipeline.DrawLines(infiniteTriangles, color);
}
}
}
public override void DrawViewportWires(IGH_PreviewArgs args)
{
base.DrawViewportWires(args);
if(!IsControllerAlive)
Draw(args.Display, Attributes.Selected ? args.WireColour_Selected : args.WireColour);
}
public override void ClearData()
{
base.ClearData();
if(!IsControllerAlive)
DelaunayInstance = null;
edgesCache = null;
circleWeightsCache = null;
orthoballsCache = null;
infiniteTriangles = null;
}
#endregion
#region Menu
protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
{
var inputMode = GetInputMode();
foreach (var mode in GetEnumValues<DelaunayInputMode>())
{
var tsmi = Menu_AppendItem(menu, GetModeName(mode), Menu_ChangeInputMode, true, inputMode == mode);
tsmi.ToolTipText = $"When checked, {GetModeDescription(mode).ToLower()}.";
tsmi.Tag = mode;
}
Menu_AppendSeparator(menu);
var outputMode = GetOutputMode();
foreach (var mode in GetEnumValues<DelaunayOutputMode>())
{
var tsmi = Menu_AppendItem(menu, GetModeName(mode), Menu_ChangeOutputMode, true, outputMode == mode);
tsmi.ToolTipText = $"When checked, returns {GetModeDescription(mode).ToLower()}.";
tsmi.Tag = mode;
}
Menu_AppendSeparator(menu);
var previewMode = GetPreviewMode();
foreach (var mode in GetEnumValues<DelaunayPreview>())
{
var tsmi = Menu_AppendItem(menu, GetModeName(mode), Menu_ChangePreview, true, previewMode[mode]);
tsmi.ToolTipText = $"When checked, {GetModeDescription(mode).ToLower()}.";
tsmi.Tag = mode;
Menu_AppendColourPicker(tsmi.DropDown, GetPreviewColor(mode), Menu_ChangePreviewColor);
}
#if DEBUG
Menu_AppendSeparator(menu);
if(DelaunayInstance != null)
{
var currentMetric = GetMetric();
var mem = Menu_AppendItem(menu, "Metric");
foreach (var mode in GetEnumValues<DelaunayMetric>())
{
Menu_AppendItem(mem.DropDown, mode.ToString(), Menu_ChangeMetric, true, currentMetric == mode).Tag = mode;
}
}
#endif
}
private void Menu_ChangeInputMode(object sender, EventArgs e)
{
var currentMode = GetInputMode();
var newMode = (DelaunayInputMode)((ToolStripMenuItem)sender).Tag;
if (currentMode != newMode)
{
RecordUndoEvent("Change delaunay mode");
UpdateInputParameters(newMode, GetOutputMode());
SetInputMode(newMode);
ExpireSolution(true);
}
}
private void Menu_ChangeOutputMode(object sender, EventArgs e)
{
var currentGraph = GetOutputMode();
var newGraph = (DelaunayOutputMode)((ToolStripMenuItem)sender).Tag;
if (currentGraph != newGraph)
{
RecordUndoEvent("Change delaunay graph");
var mode = GetInputMode();
UpdateInputParameters(mode, newGraph);
UpdateOutputParameters(mode, newGraph);
SetOutputMode(newGraph);
ExpireSolution(true);
}
}
private void Menu_ChangePreview(object sender, EventArgs e)
{
RecordUndoEvent("Change delaunay preview");
var menu = (ToolStripMenuItem)sender;
var mode = (DelaunayPreview)menu.Tag;
SetPreviewMode(mode, !menu.Checked);
var parent = menu.GetCurrentParent();
if (parent != null)
parent.Hide();
menu.HideDropDown();
ExpirePreview(true);
}
private void Menu_ChangePreviewColor(Grasshopper.GUI.GH_ColourPicker picker, Grasshopper.GUI.Base.GH_ColourPickerEventArgs e)
{
RecordUndoEvent("Change delaunay preview");
var mode = (DelaunayPreview)((ToolStripDropDownMenu)picker.Parent).OwnerItem.Tag;
SetPreviewColor(mode, e.Colour);
ExpirePreview(true);
}
private void Menu_ChangeMetric(object sender, EventArgs e)
{
if (DelaunayInstance != null)
{
RecordUndoEvent("Change delaunay metric");
SetMetric((DelaunayMetric)((ToolStripMenuItem)sender).Tag);
ExpireSolution(true);
}
}
#endregion
#region Solution
protected override void SolveInstance(IGH_DataAccess DA)
{
var points = new List<Point3d>();
var weights = new List<double>();
var plane = Plane.WorldXY;
bool ValidateWeightedPoints(DelaunayInputMode mode)
{
List<GH_Point> pts = new List<GH_Point>();
List<GH_Number> wgs = new List<GH_Number>();
if (!DA.GetDataList("Points", pts))
return false;
for (int i = 0; i < pts.Count; i++)
{
var g = pts[i];
if(g == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Null point at {i}.");
return false;
}
if (!g.IsValid)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Invalid point at {i}: {g.IsValidWhyNot}");
return false;
}
points.Add(g.Value);
}
if (!DA.GetDataList("Weights", wgs))
{
weights = Repeat(0.0, points.Count).ToList();
}
else
{
if (points.Count != wgs.Count)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "The number of points and weights must be the same");
return false;
}
for (int i = 0; i < wgs.Count; i++)
{
var g = wgs[i];
if (g == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Null weight at {i}.");
return false;
}
if (!g.IsValid)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Invalid weight at {i}: {g.IsValidWhyNot}");
return false;
}
weights.Add(g.Value);
}
}
if(mode == DelaunayInputMode.Create)