-
Notifications
You must be signed in to change notification settings - Fork 2
/
polyOffset_V2.vfl
1010 lines (904 loc) · 34.5 KB
/
polyOffset_V2.vfl
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
//HEADER: CurveExpand
//customFunctions
float clampAngle(float inputAngle){
float x = inputAngle/3.14159265;
x=x%2;
if(x>1){
x=-1+(x%1);
}
if(x<-1){
x=1+(x%1);
}
x = x*3.14159265;
return x;
}
void mergeArray(int array1[];int array2[]){
foreach(int a2; array2){
if(find(array1,a2)<0){
push(array1, a2);
}
}
}
float angleBetweenVectors2(vector a; vector b){
a = normalize(a*{1,0,1});
b = normalize(b*{1,0,1});
vector c = normalize(a+b);
float result = atan2(c[2],c[0]);
return(result);
}
float sideCheckXZ(vector s; vector e; vector p){ //+=left
float result = (e[0]-s[0])*(p[2]-s[2]) - (e[2]-s[2])*(p[0]-s[0]);
return result;
}
void Specialdecode1(int i; string S_input; int output[]){
output = {};
string L[] = split(S_input,"L");
if(i>len(L)-1){output = {-1};}
string splits[] = split(L[i],",");
//encode to integer array
foreach(string s; splits){
push(output, atoi(s));
}
}
string ftoa(float in){
return sprintf("%.10f", in);
}
void addIntegerToStringSet(int i; string Set){
int intarray[];
string splits[] = split(Set, ",");
foreach(string s; splits){
int convert = atoi(s);
push(intarray,convert);
}
if(find(intarray,i)<0){
if(Set[-1]!=","){Set+=",";}
Set+=itoa(i);
}
}
void drawAngle(vector position;float scale; float radians; vector color){
//draw a circle with 10 points.
int circle = addprim(0,"polyline");
float total = 36;
float pi = 3.141592;
for(int points=0;points<total;points++){
float fraction = points/total;
int p = addpoint(0, set(position[0]+cos(fraction*2*pi)*scale ,position[1],position[2]+sin(fraction*2*pi)*scale));
int pass = setpointattrib(0, "Cd", p, color/5, "set");
addvertex(0, circle, p);
}
int p = addpoint(0, set(position[0]+1*scale ,position[1],position[2]+0*scale));
int pass = setpointattrib(0, "Cd", p, color/5, "set");
addvertex(0, circle, p);
//draw angle
int direction = addprim(0,"polyline");
int p0 = addpoint(0,position);
int p1 = addpoint(0,set(position[0]+cos(radians)*scale ,position[1],position[2]+sin(radians)*scale));
pass = setpointattrib(0, "Cd", p0, color*2, "set");
pass = setpointattrib(0, "Cd", p1, color*2, "set");
addvertex(0,direction,p0);
addvertex(0,direction,p1);
int reference = addprim(0,"polyline");
int p00 = addpoint(0, set(position[0]+(1*scale),position[1],position[2]+(0*scale)));
int p01 = addpoint(0, set(position[0]+(1.2*scale),position[1],position[2]+(0*scale)));
pass = setpointattrib(0, "Cd", p00, color, "set");
pass = setpointattrib(0, "Cd", p01, color, "set");
addvertex(0,reference,p00);
addvertex(0,reference,p01);
}
//constants
float diameter = 5;
float radius = diameter/2;
float pi = 3.14159264;
float tau = 3.14159264*2;
//create array that stores all point pairs, based on location.
string originalOverlaps = ","; // "N,x,y,z,N,i,j,k,N,a,b,c"
int Overlap_ends[];
int Overlap_processedEnds[];
vector Overlap_endLocations[];
int npoints = npoints(0);
int nprims = nprimitives(0);
for(int primindex=0;primindex<nprims;primindex++){
int pointArray[] = primpoints(0,primindex);
int fp = pointArray[0];
int lp = pointArray[-1];
vector floc = point(0,"P",fp);
vector lloc = point(0,"P",lp);
push(Overlap_ends, fp);
push(Overlap_ends, lp);
push(Overlap_endLocations, floc);
push(Overlap_endLocations, lloc);
}
for(int match=0; match<len(Overlap_ends)-1;match++){ //this code assumes that prims only meet at the ends.
int thisEnd = Overlap_ends[match];
if(find(Overlap_processedEnds,thisEnd)<0){
push(Overlap_processedEnds, thisEnd);
vector thisLocation = Overlap_endLocations[match];
int thisGroup[];
for(int match2=match+1;match2<len(Overlap_ends);match2++){
int otherEnd = Overlap_ends[match2];
vector otherLocation = Overlap_endLocations[match2];
float distance = distance(thisLocation, otherLocation);
if(distance<0.01){
push(thisGroup, otherEnd);
push(Overlap_processedEnds, otherEnd);
}
}
//construct the string.
if(len(thisGroup)!=0){
drawAngle(thisLocation,5, 0.0, {1,1,0});
string output = "N,"+itoa(thisEnd)+",";
foreach(int g; thisGroup){
output+=itoa(g)+",";
}
originalOverlaps+=output;
}
}
}
//printf("oj_l: %f\\n",originalOverlaps);
//define the mechanisms for accessing data from this string.
int PNTOVRLAP_RESULT[];
void Pntoverlap(string source;int address; int outArray[]){
string S_addresss = ","+itoa(address)+",";
if(find(source, S_addresss)>-1){//IF ADDRESS EXISTS.
string LocalSets[] = split(source,"N");
foreach(string local; LocalSets){
if(find(local, S_addresss)>-1){//IF ADDRESS IS IN CURRENT SET.
int results[];
string S_nums[] = split(local, ",");
foreach(string S_point; S_nums){
int stoi = atoi(S_point);
if(stoi!=address){
push(results,stoi);
}
}
outArray = results;
return;
}
}
}
outArray = {};
return;
}
//Pntoverlap(originalOverlaps, 908, PNTOVRLAP_RESULT);
//printf("npr: %f\\n",PNTOVRLAP_RESULT);
//printf("len(npr): %f\\n",len(PNTOVRLAP_RESULT));
//10102019: edited lines 184>185,186; 246>147,248
//neightbourcount modified on 199 and 226
int OVERLAP_PRIM_RESULTS[];
void PRMoverlap(string source; int address; int subresult[]; int outArray[]){
Pntoverlap(source, address, subresult);
outArray = pointprims(0,address);
foreach(int entry; subresult){
int pntprims[] = pointprims(0,entry);
foreach(int pp; pntprims){
push(outArray,pp);
}
}
}
//example:
PRMoverlap(originalOverlaps, 1508, PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS);
printf("OVERLAP_PRIM_RESULTS: %f\\n", OVERLAP_PRIM_RESULTS);
int loops[];
for(int loopcandidate=0;loopcandidate<nprimitives(0);loopcandidate++){//find all cyclic curves.
int loopPoints[] = primpoints(0,loopcandidate);//get all points
//int firstNs[] = neighbours(0,loopPoints[0]);
Pntoverlap(originalOverlaps, loopPoints[0], PNTOVRLAP_RESULT);
int firstNs[] = PNTOVRLAP_RESULT;
if(find(firstNs,loopPoints[0] && len(firstNs)<3 )){
push(loops,loopcandidate);
}
}
int originalJunctions[];
foreach(string x; split(originalOverlaps,",")){
if(x!="N"){
push(originalJunctions, atoi(x));
}
}
int pointsTooClose[]; //AKA DONOTSWEEP
//mark points based on proximity to all 3+ intersections
for(int pProxCheck=0; pProxCheck<npoints(0);pProxCheck++){
vector locA = point(0,"P",pProxCheck);
for(int junc=0;junc<len(originalJunctions);junc++){
vector locB =point(0,"P",originalJunctions[junc]);
float dist = distance(locA,locB);
if(dist<radius){
push(pointsTooClose,pProxCheck);
break;//only needs to be marked once.
}
}
}
int pointlessPrims[];
//mark primitives active or inactve
for(int pPrimCheck=0;pPrimCheck<nprimitives(0);pPrimCheck++){
int numberOfActivePoints = 0;
int pPoints[] = primpoints(0,pPrimCheck);
for(int pp=0;pp<len(pPoints);pp++){
int focusedPoint = pPoints[pp];
if(find(pointsTooClose,focusedPoint)<1){
//int nCount = neighbourcount(0,focusedPoint);
Pntoverlap(originalOverlaps, focusedPoint, PNTOVRLAP_RESULT);
int nCount = len(PNTOVRLAP_RESULT);
if(nCount<2){//Check to see if is a junction.
numberOfActivePoints++;//if point is not found, then it is active
break;//stop searching after first one is found.
}
}
}
if(numberOfActivePoints==0){
push(pointlessPrims,pPrimCheck);//if prim has any active points, then it is active.
}
}
//limit scope
int specialIntersections[];
string reg[];//regular //primitive indice that needs new meta.
string rem[];//remove //primitives that will no longer exist.
for(int j=0;j<len(originalJunctions);j++){//each junction
push(reg,",");
push(rem,",");
int thisJunction = originalJunctions[j];
//int theseNeighbours[] = neighbours(0,thisJunction);//e gach point connected to junction.
Pntoverlap(originalOverlaps, thisJunction, PNTOVRLAP_RESULT);
int theseNeighbours[] = PNTOVRLAP_RESULT;
for(int jn=0;jn<len(theseNeighbours);jn++){
int thisNeighbour = theseNeighbours[jn];
PRMoverlap(originalOverlaps, thisNeighbour, PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS); //int connectedPrims[] = pointprims(0,thisNeighbour);//only junctions have multiple prims.
int connectedPrims[] = OVERLAP_PRIM_RESULTS;
for(int withP=0;withP<len(connectedPrims);withP++){
int thisConnectedPrim = connectedPrims[withP];
if(find(pointlessPrims,thisConnectedPrim)>-1){//if branch is found in pointless
if(find(specialIntersections,thisJunction)<0){
push(specialIntersections,thisJunction);
}
rem[j] += itoa(thisConnectedPrim)+","; //possible error, correct array index?
}
else{//branch is not pointless
reg[j] += itoa(thisConnectedPrim)+",";
}
}
}
}
// sharesSimple is an array of all pointless prim sets.
//2 questions.
// 1.does this pointless prim exist in a share?
// yes: break. (optimization)
// no : next question...
// 2. BURN PROCESS. Find all adjacent pointless siblings. and create+assign to new share.
string sharesSimple[]; //Pointless primitive sets. EG: "{1,2,8,54","101,102,103"} < pointlessprims that share starts or ends with or more of each other.
for(int i=0;i<len(pointlessPrims);i++){
int edge = pointlessPrims[i];
string s_edge = ","+itoa(edge)+",";
int findResultAllIndices = 0;
foreach(string oneStringInSharesSimple; sharesSimple){
if(find(oneStringInSharesSimple,s_edge)>-1){
//if found
findResultAllIndices=1;
break;
}
}
if(findResultAllIndices==0){//if not found, new entry needed
//We will be creating a new share, because all adjacents will be gathered in one go!
int newPrims[]; push(newPrims,edge); // < initially only has this edge, records edges/prims that are connected to current.
int processedPrims[]; //flag. do not process primitives
int processedPoints[]; //flag. do not process points
//THIS IS A BURN. We continue until no adjacent (POINTLESS)edges are found
int NewEdgesFound =1;
while(NewEdgesFound>0){
for(int cp = 0; cp<len(newPrims); cp++){
int checkPrim = newPrims[cp];
if(find(processedPrims,checkPrim)<0){//only process if not found.
push(processedPrims, checkPrim);//do not process this prim again.
int rawPoints[] = primpoints(0,checkPrim);//get first and last point.
int initialEdges[]={};
int endEdges[]={};
if(find(processedPoints,rawPoints[0])<0){ //if not processed
//initialEdges = pointprims(0,rawPoints[0]); //get connecting edges.
PRMoverlap(originalOverlaps, rawPoints[0], PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS);
initialEdges = OVERLAP_PRIM_RESULTS;
push(processedPoints,rawPoints[0]);
}
if(find(processedPoints,rawPoints[-1])<0){ //if not processed
//endEdges = pointprims(0,rawPoints[-1]); //get connecting edges.
PRMoverlap(originalOverlaps, rawPoints[-1], PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS);
endEdges = OVERLAP_PRIM_RESULTS;
push(processedPoints,rawPoints[-1]);
}
//cocatinate pointless.
int connectedPointlessEdges[];
NewEdgesFound = 0;
foreach(int iEdge; initialEdges){
if(find(pointlessPrims, iEdge)>-1&&find(processedPrims, iEdge)<0&&find(newPrims, iEdge)<0){ //if not processed, if not in newprim, and pointless.
push(newPrims,iEdge);
NewEdgesFound+=1;
}
}
foreach(int eEdge; endEdges){
if(find(pointlessPrims, eEdge)>-1&&find(processedPrims, eEdge)<0&&find(newPrims, eEdge)<0){ //if not processed, if not in newprim, and pointless.
push(newPrims,eEdge);
NewEdgesFound+=1;
}
}
}
}
}
//convert newPrim integer array to string for storage.
string S_newShare = ",";
foreach(int p; newPrims){
S_newShare+=itoa(p)+",";
}
push(sharesSimple,S_newShare);
}
}
//TODO: get sharepoints
string sharePrims[]; //for each group of edges, there will be a group of *non removed* edges connected (PRIMS!).
string shareDirection[]; //1=out, 0=in // maps to the above.
string firstValidPoint[]; //walk edge until first usable point. (POINTS!)
foreach(string oneSimpleShare; sharesSimple){ //for each collection of edges/primitives
int I_output[];
string output = ",";
string directionoutput = ",";
string firstValid = ",";
int sharePointProcessedPrims[]; //keep track of prims that have been processed.
string thisShareSplit[] = split(oneSimpleShare,","); //",4,5," pointless prims.
int I_removablePrimitiveInShare[];
foreach(string s; thisShareSplit){
push(I_removablePrimitiveInShare, atoi(s));
}
//Get end and start of each split
for(int rpis=0;rpis<len(I_removablePrimitiveInShare);rpis++){ //rpis = abbriviation for "removable prim in share"
int i_removablePrm = I_removablePrimitiveInShare[rpis];
int rawpoints[] = primpoints(0,i_removablePrm); //all points in prim.
int end1 = rawpoints[0];
int end2 = rawpoints[-1];
int ends[];push(ends,end1);push(ends,end2);
//int allConnectedPrims[] = pointprims(0,end1);
//int otherPrims[] = pointprims(0,end2);
PRMoverlap(originalOverlaps, end1, PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS);
int allConnectedPrims[] = OVERLAP_PRIM_RESULTS;
PRMoverlap(originalOverlaps, end2, PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS);
int otherPrims[] = OVERLAP_PRIM_RESULTS;
foreach(int oneOtherPrim; otherPrims){
push(allConnectedPrims,oneOtherPrim);
} //ALLCONNECTEDPRIMS = all edges attached to this prim.
for(int sp=0;sp<len(allConnectedPrims);sp++){// sp = abbriviation for "selected prim"
int thisprim= allConnectedPrims[sp];
//Is this prim regular? (not to be removed?) && does this prim exist in processedPrims?
if(find(sharePointProcessedPrims,thisprim)<0&&find(pointlessPrims,thisprim)<0){
push(sharePointProcessedPrims,thisprim);//don't process again.
output= output + itoa(thisprim) +",";
push(I_output,thisprim);
}
}
if(rpis==len(I_removablePrimitiveInShare)-1){ //special checks on last loop, before submitting final answer.
int extraOutput[];
int extraPoints[];
int extraDirections[];
//for each of I_output, determine if endpoints exist in set.
int I_output_cache[] = I_output;//cache output, because it will change.
for(int i=0;i<len(I_output_cache);i++){
int thisoutput = I_output_cache[i]; //one connected prim
int thisoutputPoints[] = primpoints(0,thisoutput);
int fpoint= thisoutputPoints[0];
int tpoint= thisoutputPoints[-1];
//all pointless ends.
int pointlessEnds[];
foreach(int pointlessEdge;I_removablePrimitiveInShare){
int plends[] = primpoints(0,pointlessEdge);
push(pointlessEnds,plends[0]);
push(pointlessEnds,plends[-1]);
}
// original location // int extraOutput[];
// original location // int extraPoints[];
// original location // int extraDirections[];
if(find(pointlessEnds,fpoint)>-1){//check if first point exists in junction.
foreach(int walkForwardPoint;thisoutputPoints){//walk to first valid point..
if(find(pointsTooClose,walkForwardPoint)<0){//point is not too close.
firstValid = firstValid + itoa(walkForwardPoint)+",";
directionoutput = directionoutput + "1,";
//now we check to see if the end also belongs to this intersection.
if(find(pointlessEnds,tpoint)>-1){//check to see if last point ALSO exists in junction. (loop detection)
int reversedPoints[] = reverse(thisoutputPoints);
foreach(int walkBackwardsPoint;reversedPoints){//walk reverse to first terminal that is valid
if(find(pointsTooClose,walkBackwardsPoint)<0){//if not too close
push(extraOutput, thisoutput);
push(extraPoints, walkBackwardsPoint);
push(extraDirections, 0);
break;
}
}
}
break;
}
}
}
else if(find(pointlessEnds,tpoint)>-1){//else check if last point exists in junction.
foreach(int walkBackwardsPoint;reverse(thisoutputPoints)){//walk backwards to first point that is valid.
if(find(pointsTooClose,walkBackwardsPoint)<0){//point is not too close.
firstValid = firstValid + itoa(walkBackwardsPoint)+",";
directionoutput = directionoutput + "0,";
//no need to also check first, we already did.
break;
}
}
}
//APPEND EXTRAS!
}
//append extras.
foreach(int eO; extraOutput){
output = output + itoa(eO)+",";
}
foreach(int eP; extraPoints){
firstValid = firstValid + itoa(eP)+ ",";
}
foreach(int eD; extraDirections){
directionoutput = directionoutput + itoa(eD)+ ",";
}
push(sharePrims,output);
push(firstValidPoint, firstValid);
push(shareDirection, directionoutput);
}
}
}
//calc new location.
vector V_shareLoc[];
foreach(string oneSimpleShare; sharesSimple){
// HAVING A POINT RECORDED MANY TIMES IS ***GOOD***. EMERGENT WEIGHTS.
string S_edges[] = split(oneSimpleShare, ",");
int I_edges[];
int JunctionPoints[];
foreach(string item; S_edges){
push(I_edges, atoi(item));
}
foreach(int edgeindex; I_edges){
int rawpoints[] = primpoints(0,edgeindex);
int e0 = rawpoints[0];
int e1 = rawpoints[-1];
push(JunctionPoints,e0);
push(JunctionPoints,e1);
}
vector newLoc = {0,0,0};
//sum position for all points
foreach(int point; JunctionPoints){
vector loc = point(0,"P",point);
newLoc+=loc;
}
newLoc = newLoc/len(JunctionPoints); //AVERAGE
push(V_shareLoc,newLoc);
}
//Calculate UV Offset. Will need flow direction for +/- multiplier.
//get valid point
//get direction
//get prim
//get beginning or end based on direction
//calculate location difference as float offset.
//store floats in a string array.
//offset source uv happens when we sweep the curve.
//offset junction happens when we expand the junction.
string offsets[]; // uv offset for first valid point.
for(int k=0; k<len(sharePrims);k++){
string offsetCollection = ",";
string S_thisPrimSet = sharePrims[k];
string S_thisDirections = shareDirection[k];
string S_theseClosePoints = firstValidPoint[k];
//convert these strings to integer arrays.
int I_thesePrims[]; foreach(string x;split(S_thisPrimSet,",")) {push(I_thesePrims, atoi(x) );}
int I_theseDirections[]; foreach(string x;split(S_thisDirections,",")) {push(I_theseDirections, atoi(x) );}
int I_theseClosePoints[]; foreach(string x;split(S_theseClosePoints,",")){push(I_theseClosePoints, atoi(x) );}
//all of these arrays should be 1:1
for(int x = 0; x<len(I_thesePrims);x++){
int edg = I_thesePrims[x];
int edgpnts[] = primpoints(0,edg);
int dir = I_theseDirections[x];
int pnt = I_theseClosePoints[x];
vector thisLoc = point(0,"P",pnt);
vector newLoc = V_shareLoc[k];
vector originalLoc;
if(dir==1){originalLoc = point(0,"P",edgpnts[0]);}else{originalLoc = point(0,"P",edgpnts[-1]);}
float od = distance(thisLoc,originalLoc);
float nd = distance(thisLoc,newLoc);
//float dd = nd-od;
float dd = nd; //all that matters is new distance!
string dd_stringified = ftoa(dd);
offsetCollection += dd_stringified+",";
}
push(offsets,offsetCollection);
}
//add simple junctions. updates: sharePrims, shareDirection, FirstValidPoint
foreach(int original; originalJunctions){
if(find(specialIntersections,original)<0){ //if not part of a share, we need to append info.
string S_PRM = ",";
string S_DIR = ",";
string S_FVP = ",";
string S_OFF = ",";
vector V_avgloc;
int adj[];
//point prims
//int edges[] = pointprims(0, original);
PRMoverlap(originalOverlaps, original, PNTOVRLAP_RESULT, OVERLAP_PRIM_RESULTS);
int edges[] = OVERLAP_PRIM_RESULTS;
foreach(int e; edges){
int edgepoints[] = primpoints(0,e);
if(edgepoints[0]==original){//is original first?
S_DIR+="1,";
//walk from beginning
for(int a=0;a<len(edgepoints);a++){
if(find(pointsTooClose,edgepoints[a])<0){//if not too close record.
S_FVP+= itoa(edgepoints[a])+",";
push(adj,edgepoints[a]);
break;
}
}
}
else{//was last
S_DIR+="0,";
//walk from last
for(int a=len(edgepoints)-1;a>0;a--){
if(find(pointsTooClose,edgepoints[a])<0){//if not too close record.
S_FVP+= itoa(edgepoints[a])+",";
push(adj,edgepoints[a]);
break;
}
}
}
}
//average adjacencies (adj) points to obtain center. store that center location, and calculate offsets.
vector v_adjloc[];
vector v_averageLocation={0,0,0};
foreach(int adjpnt; adj){
vector ploc = point(0,"P",adjpnt);
push(v_adjloc, ploc);
v_averageLocation+=ploc;
}
v_averageLocation=v_averageLocation/len(adj);
push(V_shareLoc, v_averageLocation);
//calculate distances
foreach(vector pointlocation; v_adjloc){
S_OFF+= ftoa(distance(v_averageLocation, pointlocation))+",";
}
push(sharePrims,S_PRM);
push(shareDirection,S_DIR);
push(firstValidPoint,S_FVP);
push(offsets, S_OFF);
}
}
//////////////////////
//SWEEP --LETS DO THIS.
int pointleafleaf[]; //each prim contains 6 entries. start,leaf,leaf,end,leaf,leaf.
vector V_pUV[]; //each prim contains 2 entries: uvstart,uvend
int startingPrimCount = nprimitives(0);
for(int sweep=0; sweep<startingPrimCount;sweep++){
if(find(pointlessPrims,sweep)<0){//if prim is not pointless.
//for each prim get a list of points that are not too close.
//mach list with angles.
int allpoints[] = primpoints(0,sweep);
int allverts[] = primvertices(0,sweep);
//create list of points that are not too close.
int notremoved[];
int vert_notremoved[];
foreach(int index; int unfilteredpoint; allpoints){
if(find(pointsTooClose,unfilteredpoint)<0){
push(notremoved,unfilteredpoint);
push(vert_notremoved,allverts[index]);
}
}
int looping = 0;
if(find(loops,sweep)>-1){ //add beginning index to end of aray?
looping = 1;
//push(notremoved,notremoved[0]);
//push(vert_notremoved,vert_notremoved[0]);
}
float notRemovedAngles[];
//now we calculate the angles to accompany points that were not removed.
for(int angleCalc=0; angleCalc<len(notremoved);angleCalc++){
int thisPoint = notremoved[angleCalc];
int vert_thisPoint = vert_notremoved[angleCalc];
//Operation Summary:
//if unfiltered point is not last unfiltered:
// angle is this unfiltered to next unfiltered
//if unfiltered point is last unfiltered:
// if unfiltered point is also last general point :
// angle is this unfiltered to previous general point +180°
// else:
// angle is this unfiltered point to last general point
if(thisPoint!=notremoved[-1]){//if not last unfiltered.
vector locationA = point(0,"P",thisPoint);//from
vector locationB = point(0,"P",notremoved[angleCalc+1]);//to
float deltax = locationB[0]-locationA[0];
float deltaz = locationB[2]-locationA[2];
float atan2result = atan2(deltaz,deltax);
push(notRemovedAngles,atan2result);
}else{//is last unfiltered.
int lastPointInPrim = allpoints[-1];
if(thisPoint==lastPointInPrim){//this last unfiltered is also last point in prim.
//angle from second from last to last.
vector locationA = point(0,"P",allpoints[-2]);//from last-1
vector locationB = point(0,"P",thisPoint);//to last
float deltax = locationB[0]-locationA[0];
float deltaz = locationB[2]-locationA[2];
float atan2result = atan2(deltaz,deltax);
push(notRemovedAngles,atan2result);
}else{
//angle from last general point.
int indexOf = find(allpoints,thisPoint);
int previousGeneral = allpoints[indexOf-1];
vector locationA = point(0,"P",previousGeneral);//from last-1
vector locationB = point(0,"P",thisPoint);//to last
float deltax = locationB[0]-locationA[0];
float deltaz = locationB[2]-locationA[2];
float atan2result = atan2(deltaz,deltax);
push(notRemovedAngles,atan2result);
}
}
}
//CREATE NEW POINTS USING THESE ANGLES.
int lastCenter = -1;
int lastRight = -1;
int lastLeft = -1;
vector UV_lastCenter;
vector UV_lastRight;
vector UV_lastLeft;
for(int pointIterator = 0; pointIterator<len(notremoved);pointIterator++){
int thispoint = notremoved[pointIterator];
float thisangle = notRemovedAngles[pointIterator];
vector thisUV = vertex(0,"uv",vert_notremoved[pointIterator]);
////////////////////////////////////////////////////////////////////////////////////////////////////////
//if(looping==1){/////////////////////////////////////////////////////////////////////////////////////////
// if(pointIterator==len(notremoved)-1){ //if last link
// break;
// int vert_prev =pointvertex(0,notremoved[1]);
// int vert_next =pointvertex(0,notremoved[pointIterator-1]);
// vector UV_NextLink = vertex(0,"P",vert_next);
// vector UV_PreviousLink = vertex(0,"P",vert_prev);
// // this,next,.....,previous,
// // solution: previous + (next-this)
// printf("prev:%f,this:%f,next:%f\\n",UV_PreviousLink,thisUV,UV_NextLink);
// printf("V_prev:%f,V_this:%f,V_next:%f\\n",vert_notremoved[pointIterator-1],vert_notremoved[pointIterator],vert_notremoved[1]);
// thisUV= set(UV_PreviousLink[0]+(UV_NextLink[0]-thisUV[0]),thisUV[1]);
// thisUV= {500,500,0};
// }
//}
if(looping==1){
if(pointIterator==len(notremoved)-1){
//distance between this point and previous point.
vector lastpointloc = point(0,"P",lastCenter);
vector thispointloc = point(0,"P",thispoint);
float lastU = UV_lastCenter[0];
thisUV = set(lastU+distance(lastpointloc,thispointloc),0.5);
}
}
vector thisLocation = point(0,"P",thispoint);
int thisID = point(0,"id",thispoint);
float m90 = clampAngle(thisangle-pi/2); //m90 and p90 are for calculating the position of the left and right points.
float p90 = clampAngle(thisangle+pi/2);
vector locL= set(
thisLocation[0]+radius*cos(m90),
0,
thisLocation[2]+radius*sin(m90));
vector locR= set(
thisLocation[0]+radius*cos(p90),
0,
thisLocation[2]+radius*sin(p90));
int pl = addpoint(0,locL);
//int sc1=setpointattrib(0,"Cd",pl,{0.1,0,0.1},"set");
int sc1=setpointattrib(0,"id",pl,thisID,"set");
sc1=setpointattrib(0,"leaf",pl,1,"set");
int pr = addpoint(0,locR);
//int sc2=setpointattrib(0,"Cd",pr,{0.1,0,0.1},"set");
int sc2=setpointattrib(0,"id",pr,thisID,"set");
sc2=setpointattrib(0,"leaf",pr,1,"set");
vector upUV = set(
thisUV[0],
1,
0);
vector downUV = set(
thisUV[0],
0,
0);
//int pass = setpointattrib(0, "uv", pl, upUV, "set");
//int pass1 = setpointattrib(0, "uv", pr, downUV, "set");
if(pointIterator==0){//is first
push(pointleafleaf,thispoint); //pointleafleaf defined on line 349.
push(pointleafleaf,pl);
push(pointleafleaf,pr);
push(V_pUV,thisUV);//store first uv
}
if(pointIterator==len(notremoved)-1){//is last
push(pointleafleaf,thispoint);
push(pointleafleaf,pl);
push(pointleafleaf,pr);
push(V_pUV,thisUV); //store last uv
}
if(lastCenter!=-1){//if we have processed a set already, now we can begin connecting trios.
int pass;
int leftface = addprim(0,'poly');
int v1 = addvertex(0,leftface, lastLeft);
int v2 = addvertex(0,leftface, lastCenter);
int v3 = addvertex(0,leftface, thispoint);
int v4 = addvertex(0,leftface, pl);
pass= setvertexattrib(0,"uv",-1,v1,UV_lastLeft,"set");
pass= setvertexattrib(0,"uv",-1,v2,UV_lastCenter,"set");
pass= setvertexattrib(0,"uv",-1,v3,thisUV,"set");
pass= setvertexattrib(0,"uv",-1,v4,upUV,"set");
int rightface = addprim(0,'poly');
int v5 = addvertex(0,rightface, lastCenter);
int v6 = addvertex(0,rightface, lastRight);
int v7 = addvertex(0,rightface, pr);
int v8 = addvertex(0,rightface, thispoint);
pass= setvertexattrib(0,"uv",-1,v5,UV_lastCenter,"set");
pass= setvertexattrib(0,"uv",-1,v6,UV_lastRight,"set");
pass= setvertexattrib(0,"uv",-1,v7,downUV,"set");
pass= setvertexattrib(0,"uv",-1,v8,thisUV,"set");
}
//update last.
lastCenter = thispoint;
lastRight = pr;
lastLeft = pl;
UV_lastCenter= thisUV;
UV_lastLeft = upUV;
UV_lastRight = downUV;
}
}//end bracket if not pointless.
}//end bracket for all primitives.
//DEBUG SECTION:
//printf("originalJunctions: %f\\n", originalJunctions);
//printf("pointsTooClose: %f\\n", pointsTooClose);
//printf("pointlessPrims: %f\\n", pointlessPrims);
//printf("specialIntersections: %f\\n", specialIntersections);
//printf("sharesSimple: %f\\n", sharesSimple);
//printf("sharePrims: %f\\n", sharePrims);
//printf("shareDirection: %f\\n", shareDirection);
//printf("firstValidPoint: %f\\n", firstValidPoint);
//printf("V_shareLoc: %f\\n", V_shareLoc);
//printf("offsets: %f\\n", offsets);
//printf("pointleafleaf: %f\\n", pointleafleaf);
//printf("V_pUV: %f\\n", V_pUV);
//INGREDIENTS:
//originalJunctions[];
//pointsTooClose[];
//pointlessPrims[];
//specialIntersections[];
//sharesSimple[]
//sharePrims[];
//shareDirection[];
//firstValidPoint[];
//V_shareLoc[];
//offsets[];
//pointleafleaf[];
//remove pointless
for(int index=0; index<npoints(0);index++){
if(find(pointsTooClose,index)>-1){
removepoint(0,index);
}
}
//Create the Junctions.
foreach(int index; string junction; firstValidPoint){
vector centroid = V_shareLoc[index];
string S_connectionPoints[] = split(junction,",");
int I_connectionPoints[]; foreach(string j; S_connectionPoints){push(I_connectionPoints,atoi(j));}
vector V_connectionLocation[]; foreach (int cp;I_connectionPoints){vector loc = point(0,"P",cp); push(V_connectionLocation,loc);}
//create integer arrays of direction and offset.
string S_dir = shareDirection[index];
int I_dir[]; foreach(string singleDir;split(S_dir,",")){push(I_dir,atoi(singleDir));}
string S_off = offsets[index];
float F_offsets[]; foreach(string singleOffset; split(S_off,",") ){push(F_offsets, atof(singleOffset));}
//draw an angle to each connection.
//drawAngle(centroid+{0,-.01,0},radius, 0.0, {1.0,2.0,0});
float allAngles[];
foreach(vector v; V_connectionLocation){
float dx = v[0]-centroid[0];
float dz = v[2]-centroid[2];
float thisAngle = atan2(dz,dx);
//drawAngle(v+{0,-.01,0},0.15,thisAngle,{0,0.2,0.5});
push(allAngles,thisAngle);
}
//sort the angles.
int angsort[] = argsort(allAngles);
float sortall[] = reorder(allAngles,angsort);
float sortang[] = reorder(allAngles,angsort);
int sortcon[] = reorder(I_connectionPoints,angsort);
int sortdir[] = reorder(I_dir,angsort);
float sortoff[] = reorder(F_offsets,angsort);
//printf("angsort: %f\\n",angsort);
//printf("sortang: %f\\n",sortang);
//printf("sortcon: %f\\n",sortcon);
//printf("sortcon+1: %f",sortcon[-1]);//first item in sortcon?
float ccbisectors[];
for(int b=0;b<len(sortang);b++){ //bisctor time!
//FIND COUNTER-CLOCKWISE ANGULAR DISTANCE
vector angcolor = hsvtorgb((b*0.1)%1.0, 1.0, 1.0);
float thisang = sortang[b];
float lastang = sortang[b-1];
if(thisang<lastang){ //make sure angle space wraps for comparison.
thisang+=(2*pi);
}
// P + (C-P/2) <<< P"previous" C"current"
float middle = lastang + (thisang-lastang)/2;
push(ccbisectors,middle);
//drawAngle(centroid+{0,-.01,0},0.85,middle,angcolor);
}
//now let's create something.
//for each connection we have two bisectors, two leaf, and a centroid to skin.
foreach (int branch;int con; sortcon){
float centang = sortall[branch];//sortall is all storted angles (to original (con)nection)
float thisoff = sortoff[branch];
int thisdir = sortdir[branch];
float f_bisectorleftangle= ccbisectors[branch];
float f_bisectorrightangle= ccbisectors[(branch+1)%len(ccbisectors)];
int pbisectLeft= addpoint(0,set(centroid[0]+radius*cos(f_bisectorleftangle),centroid[1],centroid[2]+radius*sin(f_bisectorleftangle)));
int pbisectRight= addpoint(0,set(centroid[0]+radius*cos(f_bisectorrightangle),centroid[1],centroid[2]+radius*sin(f_bisectorrightangle)));
int pcenter = addpoint(0,centroid);
int pcenter2 = addpoint(0,set(centroid[0]+radius*cos(centang),centroid[1],centroid[2]+radius*sin(centang)));
int connectionIndex = find(pointleafleaf,con);
int leafa;
int leafb;
if(thisdir==0){
leafa = pointleafleaf[connectionIndex+1];
leafb = pointleafleaf[connectionIndex+2];
}else{
leafa = pointleafleaf[connectionIndex+2];
leafb = pointleafleaf[connectionIndex+1];
}
vector originalUV = V_pUV[int(connectionIndex/3)];
float u = originalUV[0];
vector offsetUV;
vector offsetUV2;
thisoff=thisoff*1;
if(thisdir==0){//this is a source and offset is negative
offsetUV= set(u+thisoff,0.5,0);
offsetUV2= set(u+thisoff-radius,0.5,0);
}else{//this is outflow and offset is positive
offsetUV= set(u-thisoff,0.5,0);
offsetUV2= set(u-thisoff+radius,0.5,0);
}
vector uv_center = set(offsetUV[0],0.5);
vector uv_center2 = set(offsetUV2[0],0.5);
vector uv_con = set(originalUV[0],0.5);
vector uv_leftbisect;
vector uv_rightbisect;
vector uv_leafL;
vector uv_leafR;
if(thisdir==0){
uv_leftbisect = set(offsetUV2[0],0.0);
uv_rightbisect = set(offsetUV2[0],1.0);
uv_leafL = set(originalUV[0],0.0);
uv_leafR = set(originalUV[0],1.0);
}else{
uv_leftbisect = set(offsetUV2[0],1.0);
uv_rightbisect = set(offsetUV2[0],0.0);
uv_leafL = set(originalUV[0],1.0);
uv_leafR = set(originalUV[0],0.0);
}
int uvpass;
int ltri = addprim(0,"poly");
int lt1 = addvertex(0,ltri,pcenter);
int lt2 = addvertex(0,ltri,pcenter2);
int lt3= addvertex(0,ltri,pbisectLeft);
uvpass= setvertexattrib(0,"uv",-1,lt1,uv_center,"set");
uvpass= setvertexattrib(0,"uv",-1,lt2,uv_center2,"set");
uvpass= setvertexattrib(0,"uv",-1,lt3,uv_leftbisect,"set");
int rtri = addprim(0,"poly");
int rt1 = addvertex(0,rtri,pcenter2);
int rt2 = addvertex(0,rtri,pcenter);
int rt3= addvertex(0,rtri,pbisectRight);
uvpass= setvertexattrib(0,"uv",-1,rt1,uv_center2,"set");
uvpass= setvertexattrib(0,"uv",-1,rt2,uv_center,"set");
uvpass= setvertexattrib(0,"uv",-1,rt3,uv_rightbisect,"set");
int lquad = addprim(0,"poly");
int lq1 = addvertex(0,lquad,pcenter2);
int lq2 = addvertex(0,lquad,con);
int lq3 = addvertex(0,lquad,leafb);
int lq4 = addvertex(0,lquad,pbisectLeft);
uvpass= setvertexattrib(0,"uv",-1,lq1,uv_center2,"set");
uvpass= setvertexattrib(0,"uv",-1,lq2,uv_con,"set");
uvpass= setvertexattrib(0,"uv",-1,lq3,uv_leafL,"set");
uvpass= setvertexattrib(0,"uv",-1,lq4,uv_leftbisect,"set");
int rquad = addprim(0,"poly");