-
Notifications
You must be signed in to change notification settings - Fork 0
/
extrusionBehaviors.wl
2099 lines (1698 loc) · 69.4 KB
/
extrusionBehaviors.wl
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
(* ::Package:: *)
(* ::Title:: *)
(*Functions for measuring extrusion behaviors*)
dir = NotebookDirectory[];
file = FileNameJoin[{dir, "fileManagement.wl"}];
If[FileExistsQ[file], Import[file]];
Clear[dir, file];
(* ::Section:: *)
(*color management*)
getColors[n_]:=Module[{f, ni}, Catch[
If[Length[n]>1
,
ni = n[[1]];
If[OddQ[ni]
, f = n[[2]][#, 40]&;
, f = n[[2]][#, 20]&;
];
,
f = COLORrainbow[#, 0]&;
ni = n;
If[ni==4, Throw[RGBColor[#/256]&/@{{190,40,60}, {203,154,97}, {102,181,120},{32,97,136}}]]
];
If[ni>2
, f/@(Range[0,ni-1]/(ni-1))
, If[ni==2
, {Black, (*Darker[Red, 0.2]*)Gray}
, Black
]
]
]];
COLORtealorange[f_, cg_]:=Module[{teal, orange, gray},
teal = {30, 163, 201};
orange = {220, 90, 58};
gray = COLORgray[cg];
RGBColor[If[f<=0.5, (f*2*gray+(1-f*2)*teal)/256, (2*(f-0.5)*orange+(1-2*(f-0.5))*gray)/256]]
]
COLORrainbow[f_, cg_]:=ColorData["DarkRainbow"][f]
COLORredblue[f_, cg_]:=Module[{red, blue, gray},
red = {202,0,32};
blue = {5,113,176};
gray = COLORgray[cg];
RGBColor[If[f<=0.5, (f*2*gray+(1-f*2)*blue)/256, (2*(f-0.5)*red+(1-2*(f-0.5))*gray)/256]]
]
COLORgray[s_]:=ConstantArray[256-s,3];
COLORredblack[f_, cg_]:=Module[{red, blue, gray},
red = {165, 18, 53};
blue = {0,0,0};
gray = COLORgray[cg];
RGBColor[If[f<=0.5, (f*2*gray+(1-f*2)*blue)/256, (2*(f-0.5)*red+(1-2*(f-0.5))*gray)/256]]
]
(* ::Section:: *)
(*Constants*)
(*nozzle detection*)
EXPECTEDNOZWIDTH = 225;
(*expected width of nozzle in pixels*)
EXPECTEDNOZVARIATION = 5;
(*allowable variation in nozzle width, in pixels*)
NOZFRAMES = 2;
(*number of frames to use for nozzle detection*)
(*substrate detection*)
EXPECTEDSUBSTRATEFRACTION = 0.6;
(*distance from the top of the frame, as a ratio*)
ALLOWABLEREFLECTIONRADIUS = 5;
(*pixels from the nozzle edges allowable for inclusion as a nozzle edge*)
CRITMIRRORPOINTS = 100;
(*critical number of points in common between image and reflection for a mirror plane to be considered accurate*)
BASELINERATIO = 0.2;
(*fraction of image height to use on top and bottom for baseline intensity measurement*)
SEARCHRADIUS = 6;
(*number of points to use on each search step of mirror plane detection*)
SUBSTRATEFRAMES = 3;
CRITREFLECTIONPOINTS = 10;
(*bottom detection*)
BOTFRAMES = 3;
(*real dimensions*)
NOZZLEWIDTH = 1.025;
(*actual nozzle width in mm*)
NOZEXITF = (0.525 - 0.15/2)/(0.525 + 0.5);
(*middle of nozzle, expressed as a fraction of the nozzle depth, measured from the bottom*)
INFINITERADIUS = 10^20;
ERRORCURVEPOINT =<|"x"->"", "y"-> "", "r"-> "", "dn"-> "", "ds"-> "", "dSi"-> ""|>;
Needs["ErrorBarPlots`"] (*need to run this line to make plots with error bars*)
SetOptions[ErrorListPlot, Frame->True, FrameStyle->Black, LabelStyle->Directive[12, Black], Axes->None, AspectRatio->1];
SetOptions[ListPlot, Frame->True, FrameStyle->Black, LabelStyle->Directive[12, Black], Axes->None, AspectRatio->1];
SetOptions[ListLinePlot, Frame->True, FrameStyle->Black, LabelStyle->Directive[12, Black], Axes->None, AspectRatio->1];
SetOptions[Plot, Frame->True, FrameStyle->Black, LabelStyle->Directive[12, Black], Axes->None, AspectRatio->1];
totalrange[list_]:=Module[{d}, d = MinMax[list]; d[[2]] - d[[1]]];
(* ::Subsubsection:: *)
(*SMHEADER2*)
SMHEADER2 = Join[
{"frame #", "time (s)", "h (mm)"}
, Flatten[Table[i<>" "<>j<>" "<>k<>" dist (mm)", {i, {"up", "down"}}, {j, {"right", "left", "mid"}}, {k, {"noz", "sub", "Si"}}]]
, Flatten[Table[i<>" "<>j<>" "<>k, {i, {"up", "down"}}, {j, {"right", "left", "mid"}}, {k, {"outer radius (mm)", "inner radius (mm)", "Laplace pressure (Pa)"}}]]
, Flatten[Table[i<>" "<>j[[1]]<>" to "<>j[[2]]<>" pressure (Pa)", {i, {"up", "down"}}, {j, Subsets[{"right", "left", "mid"}, {2}]}]]
, {"SL contact angle (deg)", "BL contact angle (deg)", "SiL contact angle (deg)"}
, {"SL contact point channel dist (mm)", "SiL contact point nozzle dist (mm)", "Laplace pressure upstream to downstream"}
];
(* ::Section:: *)
(*measuring distances to reference geometries and getting lists of points*)
(* ::Subsection:: *)
(*distances*)
(* ::Subsubsection:: *)
(*pldist[x_, y_, m_, b_]*)
(* ::Text:: *)
(*pldist is the distance from a point to a line*)
(*(x,y) = point*)
(*y = mx+b = line*)
(*OUTPUT: distance in px*)
(*NEEDS:*)
pldist[x_, y_, m_, b_]:=Abs[b + m*x - y]/Sqrt[1+m^2];
(* ::Subsubsection:: *)
(*dpbe[x_, y_, be_]*)
(* ::Text:: *)
(*distance between point and bottom edge*)
(*{x,y} = point*)
(*be = bottom edge*)
(*OUTPUT: distance in px*)
(*NEEDS: pldist*)
dpbe[x_, y_, be_]:=Module[{m, b},
m = be[[2]];
b = be[[1,2]] - m*be[[1,1]];
pldist[x,y,m,b]
]
(* ::Subsection:: *)
(*selecting points relative to nozzle*)
(* ::Subsubsection:: *)
(*leftofnozzle[pts_, noz_, range_]*)
(* ::Text:: *)
(*list of points left of the nozzle*)
(*pts = list of points (x,y)*)
(*noz = left and right nozzle edges {{x,y}, {dx/dy}}*)
(*range = pixel range within which to exclude points*)
(*OUTPUT: list of points*)
(*NEEDS:*)
leftofnozzle[pts_, noz_, range_]:=Module[{lnoz},
lnoz = noz[[1]];
Select[pts, #[[1]]< lnoz[[1,1]] - lnoz[[2]]*(lnoz[[1,2]] - #[[2]])-range&]
]
(* ::Subsubsection:: *)
(*belownozzle[pts_, be_, range_, midplane_]*)
(* ::Text:: *)
(*belownozzle selects points between the nozzle and the point halfway between the midplane and its reflection*)
(*pts = list of points (x,y)*)
(*be = bottom edge {{x,y}, dy/dx}*)
(*midplane = location of midplane FROM THE BOTTOM (px)*)
(*OUTPUT: list of points (x,y)*)
(*NEEDS:*)
belownozzle[pts_, be_, midplane_]:=Select[pts, midplane - 0.5*(be[[1,2]] + be[[2]]*(#[[1]] - be[[1,1]]) - midplane) < #[[2]]<be[[1,2]] + be[[2]]*(#[[1]] - be[[1,1]])&]
(* ::Subsection:: *)
(*selecting points relative to frame*)
(* ::Subsubsection:: *)
(*topedgelist[list_]*)
(* ::Text:: *)
(*points on the top edge of the list*)
(*list: list of xy points*)
(*OUTPUT: list of xy points*)
(*NEEDS:*)
topedgelist[list_]:=MaximalBy[#, #[[2]]&][[1]]&/@GatherBy[list, #[[1]]&]
(* ::Subsubsection:: *)
(*leftedgelist[list_]*)
(* ::Text:: *)
(*points on the left edge of the list*)
(*list: list of xy points*)
(*OUTPUT: list of xy points*)
(*NEEDS:*)
leftedgelist[list_]:=MinimalBy[#, #[[1]]&][[1]]&/@GatherBy[list, #[[2]]&]
(* ::Section:: *)
(*getting points*)
(* ::Subsection:: *)
(*downstream points*)
(* ::Subsubsection:: *)
(*dspdiagplot[dsp_, lon_]*)
(* ::Text:: *)
(*diagnostic plot for downstream point collection*)
(*dsp = list of collected points*)
(*lon = list of original points*)
(*OUTPUT: plot*)
(*NEEDS: *)
dspdiagplot[dsp_, lon_]:=Module[{pr, pts},
pr = (({-10, 10}+MinMax[#])&/@Transpose[lon]);
(*ListPlot[dsp, ImageSize->300, AspectRatio->(pr[[2]]/pr[[1]]), PlotRange->pr, PlotStyle->Black, Frame->None]*)
pts = dsp;
pts[[;;,1]] = pts[[;;,1]] - pr[[1,1]];
pts[[;;,2]] = pr[[2,2]] - pts[[;;,2]];
pts = Reverse/@pts;
Show[Image[SparseArray[(#->0)&/@pts,{pr[[2,2]] - pr[[2,1]], pr[[1,2]] - pr[[1,1]]},1]], ImageSize->300]
]
(* ::Subsubsection:: *)
(*shortDownstream[stats_, lon_, mp_, pts_, superdiag_]*)
(* ::Text:: *)
(*gets a list of points if the leftmost point hits the substrate or the x-span is short*)
(*stats = assoc*)
(*lon = list of points left of nozzle*)
(*mp = midplane in px FROM THE BOTTOM*)
(*pts = original list of all edge points*)
(*superdiag = bool true to print*)
(*OUTPUT: list of points*)
(*NEEDS: leftedgelist, topedgelist, dspdiagplot*)
shortDownstream[stats_, lon_, mp_, pts_, superdiag_]:=Module[{nozbot, ma, mi, dsp, final},
(*if the leftmost point hits the substrate or the x-span is short*)
nozbot = stats["criticalPoints"][[1,2]];
If[Min[lon[[;;,2]]]> mp + 10 ||
Length[Select[DeleteDuplicates[lon[[;;,2]]], #<nozbot&]] < 0.8 * (nozbot - mp)
,
ma = Max[lon[[;;,1]]];
mi = Min[MaximalBy[Split[SortBy[lon, #[[2]]&], Abs[#1[[2]] - #2[[2]]]<5&], Length][[1,;;,2]]];
dsp = Join[lon, Select[pts, ma<#[[1]] && mp<#[[2]]<mi&]];
(*if the points don't reach the substrate, add more points between here and the substrate, to the right
this is for extreme dropletting behaviors where the filament retracts all the way to the nozzle*)
,
dsp = lon;
];
dsp = Select[dsp, #[[2]]>=mp&];
(*points above the substrate*)
final = DeleteDuplicates[Sort[Join[leftedgelist[dsp], Select[topedgelist[dsp], #[[1]]<MaximalBy[dsp, #[[2]]&][[1, 1]]&]]]];
(*left and top edges, but only include top edges for points to the left of the contact point*)
final = MinimalBy[Split[SortBy[final, #[[2]]&], Abs[#1[[2]] - #2[[2]]]<10&], #[[1,2]]&][[1]];
(*remove any stray y-clusters up on the nozzle*)
If[superdiag,
Print[Grid[{{"left of nozzle", "expanded", "edges"}
,{dspdiagplot[lon, pts], dspdiagplot[dsp, pts], dspdiagplot[final, pts]}}]]
];
If[Max[final[[;;,2]]]<stats["criticalPoints"][[1,2]] ||
Max[final[[;;,1]]]>stats["criticalPoints"][[1,1]]+(stats["criticalPoints"][[1,2]] - mp)
, final = 1
(*if the topmost point is below the nozzle or the rightmost point
is far in front of the back edge, throw error*)
];
Throw[final]
]
(* ::Subsubsection:: *)
(*downstreampoints[pts_, noz_, range_]*)
(* ::Text:: *)
(*downstreampoints finds points on the downstream edge of the nozzle*)
(*pts = list of points (x,y)*)
(*noz = left and right nozzle edges {{x,y}, {dx/dy}}*)
(*range = pixel range within which to exclude points*)
(*OUTPUT: list of points (x,y)*)
(*NEEDS: leftofnozzle, totalrange, shortDownstream, dspdiagplot, pldist*)
downstreampoints[pts_, stats_, range_, mp_, superdiag_]:=Module[{lon, dsp, i, lastpoint, extrapoints, critspacing, line, dspnear, print1, print2, print3, print4, rightmost, print5, drl, splitos, noz, toadd, ma, mi, final, nozbot, SD},
Catch[
SD = True; (*False to suppress output*)
noz = stats["noz"];
lon = leftofnozzle[pts, noz, range];
(*all points left of the nozzle*)
lon = Select[lon, #[[2]]>mp-10&];
(*points left of the nozzle and above the midplane, with a little wiggle*)
lon = MaximalBy[Split[Sort[lon], Abs[#1[[1]] - #2[[1]]]<15&], #[[1,1]]&][[1]];
(*rightmost x-cluster of points*)
If[MinimalBy[lon, #[[1]]&][[1,2]]<mp+10 && totalrange[lon[[;;,1]]] < 0.5*totalrange[pts[[;;,1]]],
shortDownstream[stats, lon, mp, pts, superdiag];
];
If[superdiag, print1 = {"left of nozzle ", dspdiagplot[lon, lon]}];
dsp = topedgelist[lon];
(*top edge*)
i = 1;
critspacing = 10;
If[superdiag, print2 = {"top edge ", dspdiagplot[dsp, lon]}; DSP1 = dsp;];
splitos = Select[Split[dsp, EuclideanDistance[#1, #2]<critspacing &], Length[#]>10&];
(*group points using gaps*)
If[Length[splitos]<1, Throw[1]];
dsp = MaximalBy[splitos, #[[1,1]]&][[1]];
(*take the rightmost group*)
toadd = Select[splitos, EuclideanDistance[#[[-1]], dsp[[1]]]<critspacing&];
While[Length[toadd]>0,
dsp = Join[toadd[[1]], dsp];
toadd = Select[Complement[splitos, toadd[[1]]], EuclideanDistance[#[[-1]], dsp[[1]]]<critspacing&];
];
(*add other nearby groups to the left*)
If[superdiag,
print4 = {"longest stretch ", dspdiagplot[dsp, lon]};
If[Abs[Length[dsp] - Length[DSP1]]>10, SD = True;];
DSP1 = dsp;
];
rightmost = MaximalBy[dsp, #[[1]]&][[1]];
(*rightmost point*)
drl = pldist[rightmost[[1]],rightmost[[2]], -1/noz[[1,2]], noz[[1,1,2]]+1/noz[[1,2]]*noz[[1,1,1]]];
(*distance between rightmost point and nozzle*)
dsp = Join[dsp, Select[
Select[lon
, #[[1]]>rightmost[[1]] && #[[2]]>rightmost[[2]]&]
, EuclideanDistance[#, rightmost]<drl*2&]
];
(*add points between the rightmostpoint and the nozzle*)
dsp = DeleteDuplicates[Join[topedgelist[dsp], leftedgelist[dsp]]];
(*just take the top and left edge*)
dsp = MaximalBy[Split[Sort[dsp], EuclideanDistance[#1, #2]<10&], Length][[1]];
(*select the largest continuous segment*)
rightmost = MaximalBy[dsp, #[[1]]&][[1]];
drl = pldist[rightmost[[1]],rightmost[[2]], -1/noz[[1,2]], noz[[1,1,2]]+1/noz[[1,2]]*noz[[1,1,1]]];
If[Abs[Length[dsp] - Length[DSP1]]>10, SD = True;];
If[superdiag && SD,
print5 = {"grown ", dspdiagplot[dsp, lon]};
Print[Grid[Transpose[{print1, print2, (*print3, *)print4, print5, {"distance", drl}}]]];
];
If[drl>8 || Max[dsp[[;;,2]]]<stats["criticalPoints"][[1,2]], dsp = 1];
(*if the points don't reach the nozzle or the contact point is below the nozzle, throw an error*)
dsp]]
(* ::Subsection:: *)
(*upstream points*)
(* ::Subsubsection:: *)
(*upstreampoints[pts_, be_, midplane_, printeverything_]*)
(* ::Text:: *)
(*upstreampoints selects a list of points on the upstream edge*)
(*pts = list of points (x,y)*)
(*be = bottom edge {{x,y}, dy/dx}*)
(*midplane = location of midplane FROM THE BOTTOM (px)*)
(*OUTPUT: list of points*)
(*NEEDS: belownozzle, dspdiagplot*)
upstreampoints[pts_, stats_, midplane_, printeverything_]:=Module[{errorRet, pts1, pts2, leftmost, critspacing, i, be
, bottomloc, print1, print2, print3, print4, print5, print6
, split, nozbottom, itspans,f, substratepoint
, rightmost, frontpoint, pts2sort, count, minx, xvals, SD},
Catch[
SD = True; (*false to suppress output*)
errorRet = 1;
be = stats["be"];
pts1 = belownozzle[pts, be, midplane];
(*points between the nozzle and the midplane*)
pts2 = pts1;
If[printeverything, print1 = {"below nozzle ", dspdiagplot[pts2, pts1]}];
pts2 = MaximalBy[#, #[[1]]][[1]]&/@GatherBy[pts1, #[[2]]&];
(*rightmost points*)
If[printeverything, print2 = {"rightmost ", dspdiagplot[pts2, pts1]}];
print2 = {"", ""};
pts2sort = Sort[pts2];(*sort by x*)
leftmost = pts2sort[[1]]; (*leftmost point*)
count = 2;
While[count<10 && (Abs[leftmost[[2]] - midplane]>10 || leftmost[[2]]==Min[pts2sort[[;;,2]]] || leftmost[[2]]==Max[pts2sort[[;;,2]]]),
(*while the leftmost point is the lowest or highest point or it is well above the substrate, drop the leftmost point*)
pts2sort = pts2sort[[2;;]];
leftmost = pts2sort[[1]];
count = count+1;
];
If[count>=10,
(*if the leftmost point is well above the substrate or if it's the lowest point, throw an error*)
If[printeverything, Print[Grid[Transpose[{print1, print2}]]]];
Throw[errorRet];
];
pts2 = Select[pts1, #[[1]]>leftmost[[1]] && #[[2]]>leftmost[[2]]&];
(*all points to the right and above the leftmost point*)
If[printeverything, print3 = {"above left edge ", dspdiagplot[pts2, pts1]}];
pts2 = GatherBy[pts2, #[[1]]&];
pts2 = (MinimalBy[#, #[[2]]&][[1]])&/@pts2;
(*lowest point on each x*)
If[Length[pts2]<10,
If[printeverything, Print[Grid[Transpose[{print1, print2, print3}]]]];
Throw[errorRet]
];
If[printeverything, print4 = {"lowest per x ", dspdiagplot[pts2, pts1]}];
pts2 = Sort[pts2];
(*sort by x*)
split = Select[Split[pts2, EuclideanDistance[#1, #2]<10&], Length[#]>10&];
(*longest continuous segments*)
nozbottom = stats["criticalPoints"][[3,2]];
itspans = Select[split, Min[#[[;;,2]]] - midplane < 10 && nozbottom - Max[#[[;;,2]]] < 10&];
(*segments that span the whole gap*)
If[Length[itspans]>0
, pts2 = MaximalBy[itspans, #[[1,1]]&][[1]]
(*if there is a segment that spans the whole gap, keep it*)
, pts2 = Flatten[split,1]
(*otherwise, keep all the segments*)
];
substratepoint = MinimalBy[pts2, #[[2]]&][[1]];
pts2 = Select[pts2, #[[1]]>=substratepoint[[1]]&];
(*take the contact point to be the lowest point, and only take points to the right of it*)
rightmost = MaximalBy[pts2, #[[1]]&][[1]];
frontpoint = stats["criticalPoints"][[2]];
leftmost = MinimalBy[pts2, #[[1]]&][[1]];
pts2 = DeleteDuplicates[Sort[Join[pts2
,Select[pts1, rightmost[[1]]<=#[[1]]<=frontpoint[[1]] && rightmost[[2]]<=#[[2]]<=frontpoint[[2]]&] (*points at the nozzle surface*)
,Select[Table[expansionPoint[pts1, z, leftmost[[1]] - (leftmost[[2]]-midplane), leftmost[[1]], leftmost]
, {z, Round[midplane], leftmost[[2]]}]
, Length[#]>0&] (*points at the midplane*)
]]];
(*expand pts2 to include points between existing points and the substrate and the front nozzle point*)
If[printeverything && SD
, print5 = {"expanded", dspdiagplot[pts2, pts1]};
Print[Grid[Transpose[{print1, print2, print3, print4, print5}]]]
];
(*xvals = Sort[DeleteDuplicates[pts2[[;;,1]]]];
minx = Position[Differences[xvals], z_/;z>5][[;;,1]];
If[Length[minx]>0 && minx<0.2*Length[xvals]
, pts2 = Select[pts2, #[[1]]>xvals[[Max[minx]]]&]
];
(*eliminate large jumps in x*)
pts2 = Select[pts2, #[[2]]>=midplane&];
If[printeverything,
If[Abs[Length[pts2]-expandedlength]>5,
PRINTIT = True;
print6 = {"cleaned ", dspdiagplot[pts2, pts1]};
Print[Grid[Transpose[{print1, print2, print3, print4, print5, print6}]]];
];
];*)
If[Min[pts2[[;;,2]]]>midplane+10 || Mean[pts2[[;;10,2]]]>midplane+20, pts2 = {};];
(*if the bottom doesn't reach the substrate, try again*)
If[Max[pts2[[;;,2]]]<nozbottom - 10 || Mean[pts2[[-10;;,2]]]<nozbottom-20, pts2 = {};];
(*if the top doesn't reach the nozzle, try again*)
pts2
]]
expansionPoint[pts1_, z_, left_, right_, point_]:=Module[{p},
p = Select[pts1, #[[2]]==z && left<=#[[1]]<=right&];
If[Length[p]>0,
If[Length[p]>1,
MinimalBy[p, EuclideanDistance[#, point]&][[1]]
,
p[[1]]
]
,
{}
]]
(* ::Subsection:: *)
(*getting midplane*)
(* ::Subsubsection:: *)
(*ptslope2ptpt[line_, height_]*)
(* ::Text:: *)
(*ptslope2ptpt converts a point and slope to a point and point*)
(*line = {{x,y}, dy/dx}*)
(*height = height of image (int)*)
(*OUTPUT: {{x1,y1},{x2,y2}}*)
(*NEEDS: *)
ptslope2ptpt[line_, height_]:={line[[1]], {line[[1,1]] - line[[2]]*height, 0}}
xptslope2ptpt[line_, width_]:={line[[1]], {width, line[[1,2]] + line[[2]]*width}}
(* ::Subsubsection:: *)
(*drawNozzleOnImage[image_, noz_, firstmid_, be_, ppmm_, criticalPoints_]*)
(* ::Text:: *)
(*ptslope2ptpt converts a point and slope to a point and point*)
(*image = any image to be annotated (image)*)
(*noz = two nozzle edges {{x,y},dx/dy}*)
(*firstmid = mid position MEASURED FROM BOTTOM (int)*)
(*be = bot edge line {{x,y}, {dy/dx}}*)
(*ppmm = pixels per mm (int)*)
(*criticalPoints = list of {x,y} points on bottom of nozzle {left, right, mid}*)
(*OUTPUT: annotated image (graphics object)*)
(*NEEDS: ptslope2ptpt, xptslope2ptpt*)
drawNozzleOnImage[image_, noz_, firstmid_, be_, ppmm_, criticalPoints_]:=Module[{id, width, height, nozzle, midline, cps, edges},
id = ImageDimensions[image];
width = id[[1]];
height = id[[2]];
nozzle = If[Length[noz]==2 && Length[noz[[1]]]==2 && Length[noz[[2]]]==2
, If[Length[criticalPoints]==3
, {Line[{noz[[1,1]], criticalPoints[[1]]}], Line[{noz[[2,1]], criticalPoints[[2]]}], Line[{criticalPoints[[1]], criticalPoints[[2]]}]}
, edges = {Line[ptslope2ptpt[noz[[1]], height]], Line[ptslope2ptpt[noz[[2]], height]]};
If[Length[be]>0, {edges, Line[xptslope2ptpt[be, width]]}, edges]
]
, Graphics[]
];
midline = If[firstmid>0
, Line[{{0, firstmid}, {width, firstmid}}]
, Graphics[]
];
cps = If[Length[criticalPoints]>0
, If[Length[#]==2
, Point[#]
, Graphics[]
]&/@criticalPoints[[{3}]]
, Graphics[]
];
Show[HighlightImage[image
, {White, nozzle
, midline
(*, Red
, Style[Text[ToString[ppmm]<>" px/mm", Scaled[{0.1, 0.9}]], Medium]*)
, White
, PointSize[0.01]
, cps
}]
, ImageSize->600]
]
dnoi[video_, frame_]:=Module[{stats, fr, mp},
stats = grasGeoAssoc[video];
fr = importFrame[video, frame];
mp = framenum2midplane[video, frame, stats];
drawNozzleOnImage[fr, stats["noz"], mp, stats["be"], stats["ppmm"], stats["criticalPoints"]]
]
(* ::Subsubsection:: *)
(*framenum2h[video_, frame_]*)
(* ::Text:: *)
(*converts a frame number to a standoff distance in micron*)
(*video = file name (string) or list of parameters {substrate, beginnings, speeds, fr, ends}*)
(*frame = frame number (int)*)
(*OUTPUT = {time (s), x (mm), y (mm), h (um)}*)
(*NEEDS: grasGeoAssoc*)
framenum2h[video_, frame_]:=Module[{plane, beginnings, speeds, fr, time, line, yy, xx, h, ends},
Catch[
If[Length[video]==0 && FileExistsQ[video]
,{plane, beginnings, speeds, fr, ends} = grasGeoAssoc[video]/@{"substrate", "beginnings", "speeds", "fr", "ends"};
,If[Length[video]==5
,{plane, beginnings, speeds, fr, ends} = video
,Return[{"2nd input must be video name or {plane, beginnings, speeds, fr, ends}", 0,0,0}]
];
];
time = frame/fr;
If[time>ends[[-1]], Return[{time, 0,0,0}]];
line = FirstPosition[beginnings, n_/;n>time, {10}][[1]]-1;
If[line>0,
yy = line*-5;
xx = speeds[[line]]*(time - beginnings[[line]]);
h = -(plane/.{x->xx, y->yy});
{time, xx, yy, h}
,
{time, 0,0,0}
]
]]
(* ::Subsubsection:: *)
(*framenum2midplane[video_, frame_, stats_]*)
(* ::Text:: *)
(*gets the midplane location for a frame*)
(*video = file name (string)*)
(*frame = frame number (int)*)
(*statsinput = grasGeoAssoc (association) or literally anything else*)
(*OUTPUT = position in px from the bottom*)
(*NEEDS: framenum2h, grasGeoAssoc*)
framenum2midplane[video_, frame_, statsinput_]:=Module[{stats, nozheight, ppmm},
If[AssociationQ[statsinput]
,stats = statsinput;
,stats = grasGeoAssoc[video];
];
nozheight = stats["criticalPoints"][[3,2]];
ppmm = stats["ppmm"];
nozheight - framenum2h[stats/@{"substrate", "beginnings", "speeds", "fr", "ends"}, frame][[4]]*0.001*ppmm
]
(* ::Subsubsection:: *)
(*plotSubstrate[video_, frame_, stats_]*)
(* ::Text:: *)
(*plot substrate on image*)
(*video = file name (string)*)
(*frame = frame number (int)*)
(*stats = grasGeoAssociation (association) or literally anything else*)
(*OUTPUT: plot*)
(*NEEDS: grasGeoAssoc, framenum2midplane, drawNozzleOnImage*)
plotSubstrate[video_, frame_, statsinput_]:=Module[{stats, mp, im, width},
If[!AssociationQ[statsinput]
,stats = grasGeoAssoc[video];
,stats = statsinput
];
mp = framenum2midplane[video, frame, stats];
im = importFrame[video, frame];
width = ImageDimensions[im][[1]];
drawNozzleOnImage[im, stats["noz"], mp, stats["be"], stats["ppmm"], stats["criticalPoints"]]
]
(* ::Subsection:: *)
(*points together*)
(* ::Subsubsection:: *)
(*streampointsPlot*)
streampointsPlot[pts_, upstreampts_, downstreampts_, origframe_, stats_, mp_]:=Module[{id, plot1, showthepoints, pts2plot, ptstyles, markers},
showthepoints = 0; (*0 for no points, 1 for surfaces, 2 for surfaces and edge points*)
Switch[showthepoints
, 0,
pts2plot = {0,0}; ptstyles = White; markers = {\[FilledCircle], 2};
, 1,
If[Length[upstreampts]>0
, If[Length[downstreampts]>0
, pts2plot = {upstreampts, downstreampts}; ptstyles = {White, White}; markers = {{\[FilledCircle], 4},{\[FilledCircle], 4}}
, pts2plot = upstreampts; ptstyles = White; markers = {\[FilledCircle], 4};
];
, If[Length[downstreampts]>0
, pts2plot = downstreampts; ptstyles = White; markers = {\[FilledCircle], 4};
, pts2plot = {0,0}; ptstyles = White; markers = {\[FilledCircle], 2};
];
];
, 2,
If[Length[pts]>0
, If[Length[upstreampts]>0
, If[Length[downstreampts]>0
, pts2plot = {pts, upstreampts, downstreampts}; ptstyles = {White, Red, Red}; markers = {{\[FilledCircle], 2},{\[FilledCircle], 4},{\[FilledCircle], 4}};
, pts2plot = {pts, upstreampts}; ptstyles = {White, Red}; markers = {{\[FilledCircle], 2},{\[FilledCircle], 4}};
];
, If[Length[downstreampts]>0
, pts2plot = {pts, downstreampts}; ptstyles = {White, Red}; markers = {{\[FilledCircle], 2},{\[FilledCircle], 4}};
, pts2plot = pts; ptstyles = White; markers = {\[FilledCircle], 2};
];
];
, If[Length[upstreampts]>0
, If[Length[downstreampts]>0
, pts2plot = {upstreampts, downstreampts}; ptstyles = {White, White}; markers = {{\[FilledCircle], 4},{\[FilledCircle], 4}}
, pts2plot = upstreampts; ptstyles = White; markers = {\[FilledCircle], 4};
];
, If[Length[downstreampts]>0
, pts2plot = downstreampts; ptstyles = White; markers = {\[FilledCircle], 4};
, pts2plot = {0,0}; ptstyles = White; markers = {\[FilledCircle], 4};
];
];
];
];
id = ImageDimensions[origframe];
plot1 = ListPlot[pts2plot
, PlotRange->{{0, id[[1]]}, {0, id[[2]]}}
, AspectRatio->id[[2]]/id[[1]]
, ImageSize->id[[1]]
, PlotStyle->ptstyles
, PlotMarkers->markers
, Frame->None
, Prolog->{
Texture[SetAlphaChannel[drawNozzleOnImage[origframe, stats["noz"], mp, stats["be"]
, stats["ppmm"], stats["criticalPoints"]]
, 1]]
,
Polygon[{Scaled[{0, 0}], Scaled[{1, 0}], Scaled[{1, 1}], Scaled[{0, 1}]},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]
}];
plot1
]
(* ::Subsubsection:: *)
(*streampoints[video_, frame_, statsinput_, diag_, superdiag_]*)
(* ::Text:: *)
(*list of upstream and downstream points from frame*)
(*video = file name (string)*)
(*frame = frame number (int)*)
(*statsinput = grasGeoAssociation (association) or literally anything else*)
(*diag = bool true to print*)
(*superdiag = bool true to print tons of shit*)
(*OUTPUT: upstream points, downstream points, plot, midplane location*)
(*NEEDS: grasGeoAssoc, importFrame, upstreampoints, downstreampoints, getPointsFromBW, framenum2midplane, streamPointsPlot*)
streampoints[video_, frame_, statsinput_, diag_, superdiag_]:=Module[{mp, stats, edgeim, pts, downstreampts, upstreampts, id, origframe, blurradius, plot1, time, ends, beginnings, errorRet, critpoints, adjustbadup, SD},
Catch[
SD = True;
errorRet = {};
If[AssociationQ[statsinput], stats = statsinput, stats = grasGeoAssoc[video]];
(*set up meta stats*)
time = frame/stats["fr"];
beginnings = stats["beginnings"];
ends = stats["ends"];
Do[If[ends[[i]]<time<beginnings[[i+1]],
Throw["Time = "<>ToString[time]<>", after line "<>ToString[i]<>"("<>ToString[ends[[i]]]<>","<>ToString[beginnings[[i+1]]]<>")"]
]
, {i, 8}];
If[time>ends[[9]], Throw["Time = "<>ToString[time]<>", after line 9, ("<>ToString[ends[[9]]]<>")"]];
(*check that this frame is in a print line*)
origframe = importFrame[video, frame];
If[superdiag && SD, Print[origframe]];
blurradius = 3;
upstreampts = {};
downstreampts = {};
mp = framenum2midplane[video, frame, stats];
critpoints = 100;
While[(Length[upstreampts]<1 || Length[downstreampts]<1) && blurradius>=0 && critpoints >=10,
adjustbadup = True;
edgeim = DeleteSmallComponents[EdgeDetect[Blur[origframe,blurradius]], critpoints];
(*get edge image, throw out junk*)
If[superdiag && SD, Print[ColorNegate@edgeim]];
pts = getPointsFromBW[edgeim];
(*get list of points from edge image*)
If[Length[downstreampts]<1,
downstreampts = downstreampoints[pts, stats, 2, mp, superdiag];
If[Length[downstreampts]==0 && downstreampts == 1,
blurradius = blurradius + 1;
critpoints = Round[critpoints/2];
downstreampts = {};
adjustbadup = False;
];
];
(*collect downstream points*)
If[Length[upstreampts]<1,
upstreampts = upstreampoints[pts, stats, mp, superdiag];
If[Length[upstreampts]==0 && upstreampts == 1 && adjustbadup,
blurradius = blurradius + 1;
critpoints = Round[critpoints/2];
upstreampts = {};
];
If[Max[upstreampts[[;;,1]]]<Max[downstreampts[[;;,1]]],
upstreampts = {}
];
];
(*collect upstream points, zero out if they don't span the midplane to the nozzle*)
blurradius = blurradius - 1;
(*loop by blurring the original image more if collection of either set of points failed*)
];
If[diag || superdiag, plot1 = streampointsPlot[pts, upstreampts, downstreampts, origframe, stats, mp], plot1 = {}];
(*plot points on image*)
If[superdiag && SD, Print[plot1]];
{upstreampts, downstreampts, plot1, mp}
]]
(* ::Section:: *)
(*fitting points*)
(* ::Subsection:: *)
(*interpolating*)
(* ::Subsubsection:: *)
(*pixelGaussian[list_]*)
(* ::Text:: *)
(*uses a moving gaussian to smooth pixels in a list of points*)
(*list = list of (x,y) points (px)*)
(*OUTPUT = list of (x,y) points (px)*)
(*NEEDS: pixelGaussian1D*)
pixelGaussian[list_]:=Module[{minusx, plusx, xnew, minusy, plusy, ynew},
Table[pixelGaussian1D[list, i, xy], {i, Length[list]},{xy, {1,2}} ]
]
(* ::Subsubsection:: *)
(*pixelGaussian1D[list_, i_, xy_]*)
(* ::Text:: *)
(*uses a moving gaussian to smooth pixels in a list of points*)
(*list = list of (x,y) points (px)*)
(*i = index of point to smooth*)
(*xy = smoothing in x direction (1) or y direction (2)*)
(*OUTPUT = list of (x,y) points (px)*)
(*NEEDS: *)
pixelGaussian1D[list_, i_, xy_]:=Module[{listtemp, minus, plus, new, widest, g, gminus, gplus, radius},
radius = 3;
listtemp = Reverse[Abs[list[[i,xy]] - #]&/@list[[;;i, xy]]];
minus = Min[FirstPosition[listtemp, z_/;z>radius, {i}][[1]]-1, Min[i-1, 50]];
If[Abs[list[[i-minus, xy]] - list[[i, xy]]]>radius, minus = Max[minus - 1, 1]];
listtemp = Abs[list[[i,xy]] - #]&/@list[[i;;, xy]];
plus = Min[FirstPosition[listtemp, z_/;z>radius, {(Length[list]-i+2)}][[1]]-1, Min[Length[list]-i, 50]];
If[Abs[list[[i+plus, xy]] - list[[i, xy]]]>radius, plus = Max[plus - 1, 1]];
gminus = GaussianMatrix[minus][[minus+1, 1;;minus+1]];
gplus = GaussianMatrix[plus][[plus+1, plus+1;;2*plus+1]];
new =(gminus.list[[i-minus;;i, xy]] + gplus.list[[i;;i+plus, xy]])/(Total[gminus] + Total[gplus])
]
(* ::Subsubsection:: *)
(*ybackground[data_]*)
ybackground[dinterpdata_]:=Transpose[{dinterpdata[[;;,1]],EstimatedBackground[dinterpdata[[;;,2]]]}]
(* ::Subsubsection:: *)
(*ygaussianfilter[data_, range_]*)
(* ::Text:: *)
(*apply a gaussian filter across points*)
(*data = list of points (x,y) (px)*)
(*range = range across which to blur (int px)*)
(*OUTPUT: list of points (x,y) (px)*)
(*NEEDS:*)
ygaussianfilter[data_, range_]:=Transpose[{GaussianFilter[data[[;;,1]], range], GaussianFilter[data[[;;,2]], range]}]
(* ::Subsubsection:: *)
(*derivs[usp_, pg1_, range_, neg_, diag_]*)
(* ::Text:: *)
(*gets derivatives of a list of pixels*)
(*usp = original list of pixels (x,y) (px)*)
(*pg1 = smoothed list of pixels (x,y) (px) or {} to ask the function to smooth it for you*)
(*range = smoothing range to use on 1st and 2nd derivatives*)
(*neg = 1 to take the background from the bottom, -1 to take the background from the top*)
(*diag = bool true to print diagnostics*)
(*OUTPUT: four functions and a plot*)
(*NEEDS: ybackground, ygaussianfilter, pixelGaussian*)
derivs[usp_, pg1_, range_, neg_, diag_]:=Module[{pg, interp, dinterpdata, dinterp, ddinterp
, xmin, xmax, fmm, dfmm, ddfmm, ddinterpdata
, plot, curvaturedata, curvatureinterp, cmm, shave, errorRet, showranges},
Catch[
errorRet = {0,0,0,0,ListPlot[usp, ImageSize->800, AxesStyle->Black, LabelStyle->Directive[12, Black]]};
If[Length[pg1]<30
,If[Length[usp]<30
,Throw[errorRet];
,(*pg = pixelGaussian[usp];*)
pg = ygaussianfilter[({1, neg}*#)&/@ybackground[({1, neg}*#)&/@usp], 10];
];
,pg = pg1
];
shave = 2;
pg = GatherBy[pg, #[[1]]&][[;;,1]];
(*make sure there are no duplicate x points*)
interp = Interpolation[pg];
(*interpolate function*)
{xmin, xmax} = interp["Domain"][[1]];
dinterpdata = Table[{x,interp'[x]}, {x, xmin+shave, xmax-shave, 0.1}];
(*first derivative, cut off boundary pixels b/c less reliable*)
If[Length[dinterpdata]<30, Throw[errorRet]];
dinterp = Interpolation[ygaussianfilter[dinterpdata, range]];
(*smooth first derivative*)
{xmin, xmax} = dinterp["Domain"][[1]];
ddinterpdata = Table[{x,dinterp'[x]}, {x, xmin+2*shave, xmax-2*shave, 0.1}];
(*2nd derivative, cut off boundary pixels b/c less reliable*)
If[Length[ddinterpdata]<30, Throw[errorRet]];
ddinterp = Interpolation[ygaussianfilter[ddinterpdata, range/2]];
(*smooth 2nd derivative*)
{xmin, xmax} = ddinterp["Domain"][[1]];
curvaturedata = Table[{x,ddinterp[x]/(1 + dinterp[x]^2)^(3/2)}, {x, xmin+(*2**)shave, xmax-(*2**)shave, 0.1}];
If[Length[curvaturedata]<30, Throw[errorRet]];
curvatureinterp = Interpolation[ygaussianfilter[curvaturedata, range/2]];
{xmin, xmax} = curvatureinterp["Domain"][[1]];
fmm = MinMax[(*Select[*)pg(*, xmin<=#[[1]]<=xmax&]*)[[;;,2]]];
dfmm = MinMax[(*Select[*)dinterpdata(*, xmin<=#[[1]]<=xmax&]*)[[;;,2]]];
ddfmm = MinMax[(*Select[*)ddinterpdata(*, xmin<=#[[1]]<=xmax&]*)[[;;,2]]];
cmm = MinMax[(*Select[*)curvaturedata(*, xmin<=#[[1]]<=xmax&]*)[[;;,2]]];
(*normalize for plotting*)
If[diag,
showranges = False;
plot = Show[ListPlot[{{#[[1]], (#[[2]] - fmm[[1]])/(fmm[[2]]-fmm[[1]])}&/@usp
, {#[[1]], (#[[2]] - dfmm[[1]])/(dfmm[[2]]-dfmm[[1]])}&/@dinterpdata
, {#[[1]], (#[[2]]-ddfmm[[1]])/(ddfmm[[2]]-ddfmm[[1]])}&/@ddinterpdata
, {#[[1]], (#[[2]]-cmm[[1]])/(cmm[[2]]-cmm[[1]])}&/@curvaturedata
}
(*, Joined->True*)
, PlotStyle->(Append[Table[{GrayLevel[i], Dashed}, {i,{0, 0.25, 0.5}}], {Red, Dashed}])
, PlotLabels->{"y "<>If[showranges, ToString[FortranForm/@SetAccuracy[fmm,4]], ""]
, Style["y' "<>If[showranges, ToString[FortranForm/@SetAccuracy[dfmm,4]], ""], GrayLevel[0.25]]
, Style["y'' "<>If[showranges, ToString[FortranForm/@SetAccuracy[ddfmm,4]], ""], GrayLevel[0.5]]
, Style["\[Kappa] "<>If[showranges, ToString[FortranForm/@SetAccuracy[cmm,4]], ""], Red]}
, PlotRange->All]
, Plot[{(interp[x] - fmm[[1]])/(fmm[[2]]-fmm[[1]])
, (dinterp[x]-dfmm[[1]])/(dfmm[[2]]-dfmm[[1]])
, (ddinterp[x]-ddfmm[[1]])/(ddfmm[[2]]-ddfmm[[1]])
, (curvatureinterp[x]-cmm[[1]])/(cmm[[2]]-cmm[[1]])
}
, {x, xmin, xmax}
, PlotStyle->Append[Table[GrayLevel[i], {i,{0, 0.25, 0.5}}], Red]
, PlotRange->All]
, ImageSize->150*100/72
, ImagePadding->{{22, 22}, {20, 5}}
, PlotRange->{All, {-0.15, 1.15}}
, AxesStyle->Black
, LabelStyle->Directive[8*100/72, Black]
, FrameLabel->{"x (px)", None}
, AspectRatio->1/2