forked from gavinscode/crab_cone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraded_RI_trace_and_test.m
2733 lines (1963 loc) · 117 KB
/
graded_RI_trace_and_test.m
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
% Set up to do both tracing on data and have testing mode on either
% lundaberg lens or graded fibre, as in nishdate 2011 papers
% clear;
clearvars -except dataFolder metaFile interConeValueToUse externalPigmentValue
clc; close all
%% Set parameters
useRealData = 1;
%%% Change back
incidenceAngle = 0:2.5:20; [0 5 10]; % deg, in XZ - plane
% interConeValueToUse = 1.40;
%%% Change back
xSpacing = 5; % in voxels...
plotSpacing = 2; % Integer mutiple of xSpaing to plot
if useRealData
analysisFolder = '/Users/gavintaylor/Documents/Company/Client Projects/Cones MPI/AnalysisResults/';
saveData = 1; %%%
saveFigures = 1; %%%
%%% For 2 micron data
% Varying RI
% dataFolder = '/Users/gavintaylor/Documents/Company/Client Projects/Cones MPI/AnalysisVolumes/2 micron/Varying RI Profile/';
%
% metaFile = 'Cone_1000_nm_Cone_0_SD_GRIN_both.mat';
% metaFile = 'Cone_1000_nm_Cone_0_SD_GRIN_cylinder.mat';
% metaFile = 'Cone_1000_nm_Cone_0_SD_GRIN_linear.mat';
% metaFile = 'Cone_1000_nm_Cone_0_SD_GRIN_radialTop.mat';
% metaFile = 'Cone_1000_nm_Cone_0_SD_GRIN_radialTop_TipCorrection.mat';
%%% For 5 micron testing
% dataFolder = '/Users/gavintaylor/Documents/Company/Client Projects/Cones MPI/AnalysisVolumes/5 micron/';
% metaFile = 'Cylinder_1000_nm_Cone_0_SD_GRIN_cylinder.mat';
% metaFile = 'Cone_1000_nm_Cone_0_SD_GRIN_both.mat';
% metaFile = 'Cone_1000_nm_Cone_0_SD_Uniform_1.52.mat';
% metaFile = 'Cone_CinC_EC_1000_nm_Cone_0_SD_GRIN_both.mat';
dataFile = sprintf('Volume_%s', metaFile);
% Need to set so these don't have holes - note they are in voxels
% A low value also often causes planes across middle of cone
% For 5 micron test, 10, 50 and 10 were ok
% Going to 2 micron doubling seems ok. Could also be influenced by angle step on profiles (2 for all)
alphaForIntercone = 20;
alphaForCone = 100;
alphaForCinC = 20;
dilateBorderRadius = 3; %1 - was ok for 5 micron
% Usually saved pointing down
flipVolume = 1;
flipSurfaces = 1;
createGradedFiber = 0;
createLunebergLens = 0;
plotReferenceFiber = 0;
plotRIImageOnRayDiagram = 0;
useTestData = 0;
% Note this is actually the aperture radius... from Chamberlain and Barlow 1987
receptorRadius = 30; % micron - night basically 0 out, 5 at 70 out for for day
receptorAcceptance = 90; % degrees
exposedHeight = 15; % micron - night, about the same for day (maybe a bit closer to 20)
blockExposedRentry = 1;
testOrigin = []; [5 6 7 8];
else
useTestData = 1;
radius = 1; %mm - used for both lens and fiber
voxelSize = 0.065; % mm
exteriorRI = 1;
createLunebergLens = 1;
createGradedFiber = 0;
%%% Add flag to create gaussian profile then update solution and period
fiberLength = 10;
n0 = sqrt(2.5);
alpha = 1/sqrt(2.5); 0.7;
beta = alpha*n0;
plotReferenceFiber = 0; % will plot data from Nishidate and only trace 0.5
blockExposedRentry = 0;
end
trace3D = 1; % Otherwise just line across X
% Options, 1, 4S, 4RKN, 5RKN, 45RKN
% Difference 4 to 5 is quite small
interpType = '4S'; %'4S';
%This doesn't have big effect on error < 10^-9, ok even at 10^-6 but then collapses
% May effect peripheral rays more
tolerance = 10^-9; % Nishidate uses 10^-12, seems a bit too tight
% Seems good to start a bit lower than the general deltaS
% in fixed step, 10^-3 vs. 10^-4 gives rough order of magnitude error
% initialDeltaS = 0.5*10^-3;
% Need for uniform
initialDeltaS = 0.5*10^-3;
% This keeps spot size surpsingly small, even on loose tolerance
% (I guess it ends up stepping further at loose tolerance...)
iterativeFinal = 1;
%error term, sqrt of machine precision, from Nishidate paper sqrt(1.49e-8)
% Take geometric mean of single and double precision, double takes ages to evaluate
epsilon = sqrt(geomean([1.19e-7 2.22e-16])); sqrt(1.49e-8); %sqrt(geomean([1.19e-7 2.22e-16]));
% Should be on, but can switch off to test
interfaceRefraction = 1;
% Ray that has exited will error on re-entry
blockMultipleExits = 0;
% Rays won't continue if it hits intercone base - effectively makes base of cone the aperture
limitToConeBase = 1;
% extend on final plots - only added for real data
%%% Now done as extension of zsteps
% extendRayLength = 1; % mm
% Best to do this as I haven't really delt with them yet
clearReverseRays = 1;
plotLineCols = 0;
colorTIR = 1; % Cant use both but not tested...
plotColorsOnSummary = 0;
justPlotCenterRays = 0;
scaleBarsOnRayDiagram = 1;
acceptanceUsingReceptor = 1;
testPlot = 0;
testPlotSurface = 0;
loopLim = 10000;
%% Load or create test data
if useRealData
temp = load(sprintf('%s%s',dataFolder, dataFile));
lensRIVolume = temp.volume;
clear temp
metaData = load(sprintf('%s%s',dataFolder, metaFile));
voxelSize = metaData.voxSize3D/1000; %um to mm
% Flip to change z direction
if flipVolume
lensRIVolume = permute(flipud(permute(lensRIVolume,[3 1 2])), [2 3 1]);
end
volumeSize = size(lensRIVolume);
% Using negative coding for GRIN
if metaData.interconeValue > 0
RIFlagVolume = lensRIVolume < 0;
interConeValueUsed = metaData.interconeValue;
else
RIFlagVolume = lensRIVolume < -1;
lensRIVolume(lensRIVolume == metaData.interconeValue) = interConeValueToUse;
interConeValueUsed = interConeValueToUse;
end
if isnumeric(metaData.coneValue)
fixedConeRI = 1;
else
fixedConeRI = 0;
end
lensRIVolume(RIFlagVolume) = -lensRIVolume(RIFlagVolume);
% make meshes for each layer
meshAngles = (0:2:360)/180*pi;
%%% Note, border cones won't be defined using this process
% Add an error if they are present (rotated volume won't work anyway)
% For cornea - flat
% single Z at end
corneaZ = (metaData.coneLengthToUse + metaData.outerCorneaLengthToUse + metaData.epicorneaLengthToUse)* ...
metaData.voxSize/metaData.voxSize3D;
[xGrid, yGrid, zGrid] = meshgrid(1:volumeSize(1), 1:volumeSize(2), corneaZ);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
corneaVertices = [xGrid, yGrid, zGrid];
corneaVertices(:,3) = corneaVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Get 2D triangulation
tempTriangulation = delaunayTriangulation(corneaVertices(:,1:2));
corneaSurface.faces = tempTriangulation.ConnectivityList;
corneaSurface.vertices = corneaVertices;
corneaZ = corneaZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
if flipSurfaces
corneaSurface.vertices(:,3) = -(corneaSurface.vertices(:,3)-volumeSize(3)/2)+volumeSize(3)/2;
corneaZ = -(corneaZ-volumeSize(3)/2)+volumeSize(3)/2;
end
corneaBorderVolume = polygon2voxel(corneaSurface, volumeSize, 'none');
% for top of outer cornea - can be curved
if metaData.displayProfiles.EpicorneaCone
% has a curve
epicorneaProfileR = metaData.epicorneaProfileToUse*metaData.voxSize/metaData.voxSize3D;
epicorneaProfileZ = metaData.corneaXRef*metaData.voxSize/metaData.voxSize3D;
epicorneaProfileZ(isnan(epicorneaProfileR)) = [];
epicorneaProfileR(isnan(epicorneaProfileR)) = [];
% Adjust to actual length
epicorneaProfileZ(1) = 0;
epicorneaProfileZ = epicorneaProfileZ + (metaData.coneLengthToUse + metaData.outerCorneaLengthToUse)*metaData.voxSize/metaData.voxSize3D;
epicorneaVertices = zeros(length(epicorneaProfileR)*length(meshAngles),3);
profileMax = max(epicorneaProfileR);
for i = 1:length(epicorneaProfileR)
for j = 1:length(meshAngles)
epicorneaVertices((i-1)*length(meshAngles)+j,:) = [epicorneaProfileR(i)*sin(meshAngles(j)), ...
epicorneaProfileR(i)*cos(meshAngles(j)), epicorneaProfileZ(i)];
end
end
% Get cap to pad front
[xGrid, yGrid, zGrid] = meshgrid(-floor(profileMax):ceil(profileMax), -floor(profileMax):ceil(profileMax), epicorneaProfileZ(end));
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
rGrid = sqrt(xGrid.^2 + yGrid.^2);
frontInds = find(rGrid < epicorneaProfileR(end));
epicorneaVertices = [epicorneaVertices', [xGrid(frontInds), yGrid(frontInds), zGrid(frontInds)]']';
epicorneaVertices(:,1) = epicorneaVertices(:,1) + volumeSize(1)/2;
epicorneaVertices(:,2) = epicorneaVertices(:,2) + volumeSize(2)/2;
epicorneaVertices(:,3) = epicorneaVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Get ring to pad back
[xGrid, yGrid, zGrid] = meshgrid(1:volumeSize(1), 1:volumeSize(2), epicorneaProfileZ(1) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
rGrid = sqrt((xGrid-volumeSize(1)/2).^2 + (yGrid-volumeSize(2)/2).^2);
endInds = find(rGrid > epicorneaProfileR(1));
epicorneaVertices = [[xGrid(endInds), yGrid(endInds), zGrid(endInds)]', epicorneaVertices']';
% Make another layer at base to force 3D
[xGrid, yGrid, zGrid] = meshgrid(1:volumeSize(1), 1:volumeSize(2), epicorneaProfileZ(1)-1 + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
epicorneaVertices = [epicorneaVertices' [xGrid, yGrid, zGrid]' ]';
forcedInds = find(epicorneaVertices(:,3) == epicorneaProfileZ(1)-1 + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D);
tempShape = alphaShape(epicorneaVertices, 10);
[tempFaces, tempVertices] = boundaryFacets(tempShape);
if ~isempty(forcedInds)
% remove forced inds at base
% not all of the original forced inds are used in the final trinagulation
usedForcedInds = find(tempVertices(:,3) == epicorneaVertices(forcedInds(1),3));
facesToRemove = zeros(length(tempFaces),1, 'logical');
for i = 1:length(usedForcedInds)
% Flag for each column
facesToRemove(tempFaces(:,1) == usedForcedInds(i)) = 1;
facesToRemove(tempFaces(:,2) == usedForcedInds(i)) = 1;
facesToRemove(tempFaces(:,3) == usedForcedInds(i)) = 1;
end
tempFaces(facesToRemove,:) = [];
% Cant remove from vertices or it will screw up face referencing
% tempVertices(usedForcedInds,:) = [];
epicorneaVertices(forcedInds,:) = [];
end
epicorneaSurface.faces = tempFaces;
epicorneaSurface.vertices = tempVertices;
epicorneaProfileZ = epicorneaProfileZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
if flipSurfaces
epicorneaProfileZ = -(epicorneaProfileZ-volumeSize(3)/2)+volumeSize(3)/2;
end
else
% just flat, can copy cornea with adjusted Z
epicorneaVertices = corneaVertices;
epicorneaVertices(:,3) = epicorneaVertices(:,3) - metaData.epicorneaLengthToUse* metaData.voxSize/metaData.voxSize3D;
epicorneaSurface = corneaSurface;
epicorneaSurface.vertices = epicorneaVertices;
epicorneaProfileR = [];
epicorneaProfileZ = [];
end
epicorneaZ = (metaData.coneLengthToUse + metaData.outerCorneaLengthToUse)* metaData.voxSize/metaData.voxSize3D;
epicorneaZ = epicorneaZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
if flipSurfaces
epicorneaSurface.vertices(:,3) = -(epicorneaSurface.vertices(:,3)-volumeSize(3)/2)+volumeSize(3)/2;
epicorneaZ = -(epicorneaZ-volumeSize(3)/2)+volumeSize(3)/2;
end
epicorneaBorderVolume = polygon2voxel(epicorneaSurface, volumeSize, 'none');
% for base outer cornea/intercone around cone - flat
% Make flat grid as for cornea
coneBaseZ = (metaData.coneLengthToUse)* metaData.voxSize/metaData.voxSize3D;
[xGrid, yGrid, zGrid] = meshgrid((1:volumeSize(1))-volumeSize(1)/2, (1:volumeSize(2))-volumeSize(2)/2, coneBaseZ);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
% Shrink vertices that are inside radius at base of cone
rGrid = sqrt(xGrid.^2 + yGrid.^2);
if ~isnan(metaData.coneProfileToUse(end)) & ~metaData.createCylinder
coneBaseRadius = metaData.coneProfileToUse(end)*metaData.voxSize/metaData.voxSize3D;
elseif isnan(metaData.coneProfileToUse(end)) & ~metaData.createCylinder
error('Need to get last good value in cone profile')
elseif metaData.createCylinder
coneBaseRadius = max(metaData.coneProfileToUse)*metaData.voxSize/metaData.voxSize3D;
end
verticesToRemove = find(rGrid < coneBaseRadius);
interconeBaseVertices = [xGrid, yGrid, zGrid];
% Step in slightly so these don't get incorperated into faces along base of cone
interconeBaseVertices(verticesToRemove, 1:2) = interconeBaseVertices(verticesToRemove, 1:2)*0.9;
% Add ring of vertices at cone base
interconeBaseVertices = [interconeBaseVertices' [coneBaseRadius*sin(meshAngles)' coneBaseRadius*cos(meshAngles)' coneBaseZ*ones(length(meshAngles),1)]']';
interconeBaseVertices(:,1) = interconeBaseVertices(:,1) + volumeSize(1)/2;
interconeBaseVertices(:,2) = interconeBaseVertices(:,2) + volumeSize(2)/2;
interconeBaseVertices(:,3) = interconeBaseVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Get 2D triangulation
tempTriangulation = delaunayTriangulation(interconeBaseVertices(:,1:2));
interconeBaseSurface.faces = tempTriangulation.ConnectivityList;
interconeBaseSurface.vertices = interconeBaseVertices;
facesToRemove = zeros(length(interconeBaseSurface.faces),1,'logical');
for i = 1:length(verticesToRemove)
% Flag for each column
facesToRemove(interconeBaseSurface.faces(:,1) == verticesToRemove(i)) = 1;
facesToRemove(interconeBaseSurface.faces(:,2) == verticesToRemove(i)) = 1;
facesToRemove(interconeBaseSurface.faces(:,3) == verticesToRemove(i)) = 1;
end
interconeBaseSurface.faces(facesToRemove,:) = [];
coneBaseZ = coneBaseZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
if flipSurfaces
interconeBaseSurface.vertices(:,3) = -(interconeBaseSurface.vertices(:,3)-volumeSize(3)/2)+volumeSize(3)/2;
coneBaseZ = -(coneBaseZ-volumeSize(3)/2)+volumeSize(3)/2;
end
interconeBaseBorderVolume = polygon2voxel(interconeBaseSurface, volumeSize, 'none');
if metaData.displayProfiles.CinC
if metaData.createCylinder
error('Not coded')
end
% Make CinC profile at base of cone
cInCProfileR = metaData.CinCProfileToUse*metaData.voxSize/metaData.voxSize3D;
cInCProfileZ = metaData.coneXRef*metaData.voxSize/metaData.voxSize3D;
cInCProfileZ(isnan(cInCProfileR)) = [];
cInCProfileR(isnan(cInCProfileR)) = [];
% Adjust to actual length
cInCProfileZ(end) = metaData.coneLengthToUse*metaData.voxSize/metaData.voxSize3D;
cInCVertices = zeros(length(cInCProfileR)*length(meshAngles),3);
for i = 1:length(cInCProfileR)
for j = 1:length(meshAngles)
cInCVertices((i-1)*length(meshAngles)+j,:) = [cInCProfileR(i)*sin(meshAngles(j)), ...
cInCProfileR(i)*cos(meshAngles(j)), cInCProfileZ(i)];
end
end
% Cap to pad front and suround sides of cinc (within cone base)
[xGrid, yGrid, zGrid] = meshgrid(-coneBaseRadius:coneBaseRadius, -coneBaseRadius:coneBaseRadius, 1);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
rGrid = sqrt(xGrid.^2 + yGrid.^2);
firstInds = find(rGrid < cInCProfileR(1));
cInCVertices = [[xGrid(firstInds), yGrid(firstInds), (cInCProfileZ(1))*zGrid(firstInds)]', cInCVertices']';
sideInds = find(rGrid > cInCProfileR(end) & rGrid < coneBaseRadius);
cInCVertices = [[xGrid(sideInds), yGrid(sideInds), (cInCProfileZ(end))*zGrid(sideInds)]', cInCVertices']';
% Add ring of vertices at cone base
cInCVertices = [cInCVertices' [coneBaseRadius*sin(meshAngles)' coneBaseRadius*cos(meshAngles)' cInCProfileZ(end)*ones(length(meshAngles),1)]']';
% Add ring and base stepped down
baseInds = find(rGrid < coneBaseRadius);
cInCVertices = [[xGrid(baseInds), yGrid(baseInds), (cInCProfileZ(end)+1)*zGrid(baseInds)]', cInCVertices']';
cInCVertices = [cInCVertices' [coneBaseRadius*sin(meshAngles)' coneBaseRadius*cos(meshAngles)' (cInCProfileZ(end)+1)*ones(length(meshAngles),1)]']';
forcedInds = find(cInCVertices(:,3) == (cInCProfileZ(end)+1));
cInCVertices(:,1) = cInCVertices(:,1) + volumeSize(1)/2;
cInCVertices(:,2) = cInCVertices(:,2) + volumeSize(2)/2;
cInCVertices(:,3) = cInCVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
tempShape = alphaShape(cInCVertices, alphaForCinC);
[tempFaces, tempVertices] = boundaryFacets(tempShape);
if ~isempty(forcedInds)
% remove forced inds at base
% not all of the original forced inds are used in the final trinagulation
usedForcedInds = find(tempVertices(:,3) == cInCVertices(forcedInds(1),3));
facesToRemove = zeros(length(tempFaces),1, 'logical');
for i = 1:length(usedForcedInds)
% Flag for each column
facesToRemove(tempFaces(:,1) == usedForcedInds(i)) = 1;
facesToRemove(tempFaces(:,2) == usedForcedInds(i)) = 1;
facesToRemove(tempFaces(:,3) == usedForcedInds(i)) = 1;
end
tempFaces(facesToRemove,:) = [];
cInCVertices(forcedInds,:) = [];
end
cInCSurface.faces = tempFaces;
cInCSurface.vertices = tempVertices;
cInCProfileZ = cInCProfileZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
if flipSurfaces
cInCProfileZ = -(cInCProfileZ-volumeSize(3)/2)+volumeSize(3)/2;
end
else
% use a flat cInC - basically as for intercone base
tempZ = (metaData.coneLengthToUse)* metaData.voxSize/metaData.voxSize3D;
[xGrid, yGrid, zGrid] = meshgrid((1:volumeSize(1))-volumeSize(1)/2, (1:volumeSize(2))-volumeSize(2)/2, tempZ);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
% Shrink vertices that are inside radius at base of cone
rGrid = sqrt(xGrid.^2 + yGrid.^2);
% Remove some at start so this doesn't spread to wide
cInCVertices = [xGrid, yGrid, zGrid];
cInCVertices(rGrid > coneBaseRadius*1.25, :) = []; rGrid(rGrid > coneBaseRadius*1.25) = [];
verticesToRemove = find(rGrid > coneBaseRadius);
% Step out slightly so these don't get incorperated into faces along base of cone
cInCVertices(verticesToRemove, 1:2) = cInCVertices(verticesToRemove, 1:2)*1.1;
% Add ring of vertices at cone base
cInCVertices = [cInCVertices' [coneBaseRadius*sin(meshAngles)' coneBaseRadius*cos(meshAngles)' tempZ*ones(length(meshAngles),1)]']';
cInCVertices(:,1) = cInCVertices(:,1) + volumeSize(1)/2;
cInCVertices(:,2) = cInCVertices(:,2) + volumeSize(2)/2;
cInCVertices(:,3) = cInCVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Get 2D triangulation
tempTriangulation = delaunayTriangulation(cInCVertices(:,1:2));
cInCSurface.faces = tempTriangulation.ConnectivityList;
cInCSurface.vertices = cInCVertices;
facesToRemove = zeros(length(cInCSurface.faces),1,'logical');
for i = 1:length(verticesToRemove)
% Flag for each column
facesToRemove(cInCSurface.faces(:,1) == verticesToRemove(i)) = 1;
facesToRemove(cInCSurface.faces(:,2) == verticesToRemove(i)) = 1;
facesToRemove(cInCSurface.faces(:,3) == verticesToRemove(i)) = 1;
end
cInCSurface.faces(facesToRemove,:) = [];
cInCProfileR = [];
cInCProfileZ = [];
end
if flipSurfaces
cInCSurface.vertices(:,3) = -(cInCSurface.vertices(:,3)-volumeSize(3)/2)+volumeSize(3)/2;
end
%%% Would be good to make a funciton for doing the rotation in these...
% for cone - is curved note voxSize is actually 2D pixel size
coneProfileR = metaData.coneProfileToUse*metaData.voxSize/metaData.voxSize3D;
coneProfileZ = metaData.coneXRef*metaData.voxSize/metaData.voxSize3D;
coneProfileZ(isnan(coneProfileR)) = [];
coneProfileR(isnan(coneProfileR)) = [];
% Adjust to actual length
coneProfileZ(end) = metaData.coneLengthToUse*metaData.voxSize/metaData.voxSize3D;
profileMax = max(coneProfileR);
coneVertices = zeros(length(coneProfileR)*length(meshAngles),3);
for i = 1:length(coneProfileR)
for j = 1:length(meshAngles)
if ~metaData.createCylinder
coneVertices((i-1)*length(meshAngles)+j,:) = [coneProfileR(i)*sin(meshAngles(j)), ...
coneProfileR(i)*cos(meshAngles(j)), coneProfileZ(i)];
else
coneVertices((i-1)*length(meshAngles)+j,:) = [profileMax*sin(meshAngles(j)), ...
profileMax*cos(meshAngles(j)), coneProfileZ(i)];
end
end
end
% Get caps to pad front and back, otherwise there are degenerate faces
[xGrid, yGrid, zGrid] = meshgrid(-floor(profileMax):ceil(profileMax), -floor(profileMax):ceil(profileMax), 1);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
rGrid = sqrt(xGrid.^2 + yGrid.^2);
if ~metaData.createCylinder
firstInds = find(rGrid < coneProfileR(1));
else
firstInds = find(rGrid < profileMax);
end
coneVertices = [ [xGrid(firstInds), yGrid(firstInds), coneProfileZ(1)*zGrid(firstInds)]', coneVertices']';
if ~metaData.createCylinder
endInds = find(rGrid < coneProfileR(end));
else
endInds = find(rGrid < profileMax);
end
coneVertices = [ [xGrid(endInds), yGrid(endInds), (coneProfileZ(end)+1)*zGrid(endInds)]', coneVertices']';
forcedInds = find(coneVertices(:,3) == (coneProfileZ(end)+1));
coneVertices(:,1) = coneVertices(:,1) + volumeSize(1)/2;
coneVertices(:,2) = coneVertices(:,2) + volumeSize(2)/2;
coneVertices(:,3) = coneVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Switch to alpha shape to prevent degenerate triangles
tempShape = alphaShape(coneVertices, alphaForCone);
[tempFaces, tempVertices] = boundaryFacets(tempShape);
% tempTriangulation = delaunayTriangulation(coneVertices);
% [tempFaces, tempVertices] = freeBoundary(tempTriangulation); % this seems to flip vertices in z
if ~isempty(forcedInds)
% remove forced inds at base
% not all of the original forced inds are used in the final trinagulation
usedForcedInds = find(tempVertices(:,3) == coneVertices(forcedInds(1),3));
facesToRemove = zeros(length(tempFaces),1, 'logical');
for i = 1:length(usedForcedInds)
% Flag for each column
facesToRemove(tempFaces(:,1) == usedForcedInds(i)) = 1;
facesToRemove(tempFaces(:,2) == usedForcedInds(i)) = 1;
facesToRemove(tempFaces(:,3) == usedForcedInds(i)) = 1;
end
tempFaces(facesToRemove,:) = [];
coneVertices(forcedInds,:) = [];
end
grinSurface.faces = tempFaces;
grinSurface.vertices = tempVertices;
coneProfileZ = coneProfileZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Get cone border volume
if flipSurfaces
grinSurface.vertices(:,3) = -(grinSurface.vertices(:,3)-volumeSize(3)/2)+volumeSize(3)/2;
coneProfileZ = -(coneProfileZ-volumeSize(3)/2)+volumeSize(3)/2;
end
% Combine cone surfaces and really hope they are water tight...
grinSurface.vertices = vertcat(grinSurface.vertices, cInCSurface.vertices);
% Reverse order of CinC faces so they point in same polarity (at base so will point inward)
if metaData.displayProfiles.CinC
cInCSurface.faces = cInCSurface.faces(:,[3 2 1]);
end
% update the face list
faceUpdate = cInCSurface.faces+max(max(grinSurface.faces));
grinSurface.faces = vertcat(grinSurface.faces,faceUpdate);
coneBorderVolume = polygon2voxel(grinSurface, volumeSize, 'none');
% for inner intercone - is curved
if any(metaData.exposedInterconeProfileToUseLeft - metaData.exposedInterconeProfileToUseRight)
error('Right and left intercone profiles arent equal - rotated volume also wont work')
end
profileToUse = find(~isnan(metaData.exposedInterconeProfileToUseLeft));
if ~metaData.createCylinder
interconeProfileR = (metaData.exposedInterconeProfileToUseLeft(profileToUse)+metaData.coneProfileToUse(profileToUse))*metaData.voxSize/metaData.voxSize3D;
% Shrink 1st in profile to cone
% Tried adding a step before hand, but always caused problems somewhere
interconeProfileR(1) = metaData.coneProfileToUse(profileToUse(1))*metaData.voxSize/metaData.voxSize3D;
else
interconeProfileR = (metaData.exposedInterconeProfileToUseLeft(profileToUse)+max(metaData.coneProfileToUse))*metaData.voxSize/metaData.voxSize3D;
interconeProfileR(1) = max(metaData.coneProfileToUse)*metaData.voxSize/metaData.voxSize3D;
end
interconeProfileZ = metaData.coneXRef(profileToUse)*metaData.voxSize/metaData.voxSize3D;
interconeVertices = zeros(length(interconeProfileR)*length(meshAngles),3);
profileMax = max(interconeProfileR);
for i = 1:length(interconeProfileR)
for j = 1:length(meshAngles)
interconeVertices((i-1)*length(meshAngles)+j,:) = [interconeProfileR(i)*sin(meshAngles(j)), ...
interconeProfileR(i)*cos(meshAngles(j)), interconeProfileZ(i)];
end
end
% Get fake cap to pad top
[xGrid, yGrid, zGrid] = meshgrid(-floor(profileMax):ceil(profileMax), -floor(profileMax):ceil(profileMax), (interconeProfileZ(1)-1));
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
rGrid = sqrt(xGrid.^2 + yGrid.^2);
startInds = find(rGrid < interconeProfileR(1));
interconeVertices = [ [xGrid(startInds), yGrid(startInds), zGrid(startInds)]', interconeVertices']';
forcedCapInds = find(interconeVertices(:,3) == interconeProfileZ(1)-1);
% Need a lower multiple for alpha shape
interconeVertices(forcedCapInds, 1:2) = interconeVertices(forcedCapInds, 1:2)*0.75;
interconeVertices(:,1) = interconeVertices(:,1) + volumeSize(1)/2;
interconeVertices(:,2) = interconeVertices(:,2) + volumeSize(2)/2;
interconeVertices(:,3) = interconeVertices(:,3) + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
% Now get a ring to force base
[xGrid, yGrid, zGrid] = meshgrid(1:volumeSize(1), 1:volumeSize(2), interconeProfileZ(end)+metaData.tipOffset*metaData.voxSize/metaData.voxSize3D);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
rGrid = sqrt((xGrid-volumeSize(1)/2).^2 + (yGrid-volumeSize(2)/2).^2);
endInds = find(rGrid > interconeProfileR(end));
interconeVertices = [interconeVertices', [xGrid(endInds), yGrid(endInds), zGrid(endInds)]']';
% Make another layer at base to force 3D
[xGrid, yGrid, zGrid] = meshgrid(1:volumeSize(1), 1:volumeSize(2), interconeProfileZ(end)+1+metaData.tipOffset*metaData.voxSize/metaData.voxSize3D);
xGrid = xGrid(:); yGrid = yGrid(:); zGrid = zGrid(:);
interconeVertices = [interconeVertices', [xGrid, yGrid, zGrid]']';
forcedGridInds = find(interconeVertices(:,3) == interconeProfileZ(end)+1+metaData.tipOffset*metaData.voxSize/metaData.voxSize3D);
% tempTriangulation = delaunayTriangulation(interconeVertices);
% [tempFaces, tempVertices] = freeBoundary(tempTriangulation);
% as exposed cone is concave has to be done with alpha - but leads to some flattening around border
tempShape = alphaShape(interconeVertices, alphaForIntercone);
[tempFaces, tempVertices] = boundaryFacets(tempShape);
% remove forced inds at cap and base
usedForcedCapInds = find(tempVertices(:,3) == interconeVertices(forcedCapInds(1),3));
usedForcedBaseInds = find(tempVertices(:,3) == interconeVertices(forcedGridInds(1),3));
facesToRemove = zeros(length(tempFaces),1,'logical');
for i = 1:length(usedForcedBaseInds)
% Flag for each column
facesToRemove(tempFaces(:,1) == usedForcedBaseInds(i)) = 1;
facesToRemove(tempFaces(:,2) == usedForcedBaseInds(i)) = 1;
facesToRemove(tempFaces(:,3) == usedForcedBaseInds(i)) = 1;
end
for i = 1:length(usedForcedCapInds)
% Flag for each column
facesToRemove(tempFaces(:,1) == usedForcedCapInds(i)) = 1;
facesToRemove(tempFaces(:,2) == usedForcedCapInds(i)) = 1;
facesToRemove(tempFaces(:,3) == usedForcedCapInds(i)) = 1;
end
tempFaces(facesToRemove,:) = [];
interconeVertices([forcedCapInds' forcedGridInds']',:) = [];
interconeSurface.faces = tempFaces;
interconeSurface.vertices = tempVertices;
interconeProfileZ = interconeProfileZ + metaData.tipOffset*metaData.voxSize/metaData.voxSize3D;
if flipSurfaces
interconeSurface.vertices(:,3) = -(interconeSurface.vertices(:,3)-volumeSize(3)/2)+volumeSize(3)/2;
interconeProfileZ = -(interconeProfileZ-volumeSize(3)/2)+volumeSize(3)/2;
end
interconeBorderVolume = polygon2voxel(interconeSurface, volumeSize, 'none');
% plots
if testPlotSurface
figure;
% plot3(coneVertices(:,1), coneVertices(:,2), coneVertices(:,3), '.')
hold on; axis equal
trisurf(grinSurface.faces, grinSurface.vertices(:,1), grinSurface.vertices(:,2), grinSurface.vertices(:,3), ...
'FaceColor','g','FaceAlpha',0.1);
% plot3(corneaVertices(:,1), corneaVertices(:,2), corneaVertices(:,3), '.')
trisurf(corneaSurface.faces, corneaSurface.vertices(:,1), corneaSurface.vertices(:,2), corneaSurface.vertices(:,3), ...
'FaceColor','m','FaceAlpha',0.8);
% plot3(epicorneaVertices(:,1), epicorneaVertices(:,2), epicorneaVertices(:,3), '.')
trisurf(epicorneaSurface.faces, epicorneaSurface.vertices(:,1), epicorneaSurface.vertices(:,2), epicorneaSurface.vertices(:,3), ...
'FaceColor','c','FaceAlpha',0.8);
% plot3(interconeBaseVertices(:,1), interconeBaseVertices(:,2), interconeBaseVertices(:,3), '.')
trisurf(interconeBaseSurface.faces, interconeBaseSurface.vertices(:,1), interconeBaseSurface.vertices(:,2), interconeBaseSurface.vertices(:,3), ...
'FaceColor','b','FaceAlpha',0.8);
% plot3(cInCVertices(:,1), cInCVertices(:,2), cInCVertices(:,3), '.')
% trisurf(cInCSurface.faces, cInCSurface.vertices(:,1), cInCSurface.vertices(:,2), cInCSurface.vertices(:,3), ...
% 'FaceColor','r','FaceAlpha',0.8);
% plot3(interconeVertices(:,1), interconeVertices(:,2), interconeVertices(:,3), '.')
trisurf(interconeSurface.faces, interconeSurface.vertices(:,1), interconeSurface.vertices(:,2), interconeSurface.vertices(:,3), ...
'FaceColor','b','FaceAlpha',0.8);
end
figure;
subplot(1,2,1)
tempVolume = coneBorderVolume + corneaBorderVolume + epicorneaBorderVolume + interconeBaseBorderVolume + interconeBorderVolume;
imshow(permute(tempVolume(round(volumeSize(1)/2),:,:), [2 3 1])');
subplot(1,2,2)
imshow(permute((lensRIVolume(round(volumeSize(1)/2),:,:)-1.35)/(1.54-1.35), [2 3 1])');
elseif useTestData
if ~xor(createLunebergLens, createGradedFiber)
error('Both or neither lens/fiber flags set. Check.')
end
if createLunebergLens
volumeSize = ceil(radius*2*1.5/voxelSize*[1 1 1]);
lensRIVolume = createluneberglens(radius/voxelSize, volumeSize);
elseif createGradedFiber
volumeSize = ceil([radius*2 radius*2 fiberLength]*1.5/voxelSize);
lensRIVolume = creategradedfiber(radius/voxelSize, fiberLength/voxelSize, volumeSize, ...
n0, alpha, voxelSize);
end
RIFlagVolume = ~isnan(lensRIVolume);
lensRIVolume(isnan(lensRIVolume)) = exteriorRI;
coneBorderVolume = imdilate(~RIFlagVolume, strel('sphere', 1)) & RIFlagVolume;
% Note that meshes are used in test cases, refraction is based on an analytical solution
end
%% Do general set up
if useRealData
coneTipZ = coneProfileZ(1)*voxelSize;
% Connect GRIN vertexes to an exterior RI
tempRIVolume = lensRIVolume;
tempRIVolume(RIFlagVolume) = 0;
tempRIVolume = imdilate(tempRIVolume, strel('Sphere',1));
vertexIndexes = sub2ind(volumeSize, round(grinSurface.vertices(:,1)), round(grinSurface.vertices(:,2)), round(grinSurface.vertices(:,3)));
vertexExteriorRI = tempRIVolume(vertexIndexes);
% If any are missing loop until they are caught
while any(vertexExteriorRI == 0)
tempRIVolume2 = imdilate(tempRIVolume, strel('Sphere',1));
vertexExteriorRI(vertexExteriorRI == 0) = tempRIVolume2(vertexIndexes(vertexExteriorRI == 0));
end
exposedConeFlag = vertexExteriorRI == metaData.innerValue;
% Change RI of exterior at border of exposed cone
if ~isempty(externalPigmentValue)
vertexExteriorRI( grinSurface.vertices(:,3)*voxelSize < coneTipZ - exposedHeight/1000 & vertexExteriorRI == metaData.innerValue) = externalPigmentValue;
end
% Adjust vertices
grinSurface.vertices = grinSurface.vertices*voxelSize;
corneaSurface.vertices = corneaSurface.vertices*voxelSize;
epicorneaSurface.vertices = epicorneaSurface.vertices*voxelSize;
interconeBaseSurface.vertices = interconeBaseSurface.vertices*voxelSize;
interconeSurface.vertices = interconeSurface.vertices*voxelSize;
% Get normals for all
grinNormals = meshFaceNormals(grinSurface.vertices, grinSurface.faces);
corneaNormals = meshFaceNormals(corneaSurface.vertices, corneaSurface.faces);
epicorneaNormals = meshFaceNormals(epicorneaSurface.vertices, epicorneaSurface.faces);
interconeBaseNormals = meshFaceNormals(interconeBaseSurface.vertices, interconeBaseSurface.faces);
interconeNormals = meshFaceNormals(interconeSurface.vertices, interconeSurface.faces);
if testPlotSurface
%%% Direction of open surfaces does not seem to matter, but cone normals all need to point in
figure; hold on; axis equal
trisurf(grinSurface.faces, grinSurface.vertices(:,1), grinSurface.vertices(:,2), grinSurface.vertices(:,3), ...
'FaceColor','g','FaceAlpha',0.8);
quiver3(grinSurface.vertices(grinSurface.faces(:,1),1), grinSurface.vertices(grinSurface.faces(:,1),2), grinSurface.vertices(grinSurface.faces(:,1),3), ...
grinNormals(:,1)*5, grinNormals(:,2)*5, grinNormals(:,3)*5);
trisurf(corneaSurface.faces, corneaSurface.vertices(:,1), corneaSurface.vertices(:,2), corneaSurface.vertices(:,3), ...
'FaceColor','m','FaceAlpha',0.8);
quiver3(corneaSurface.vertices(corneaSurface.faces(:,1),1), corneaSurface.vertices(corneaSurface.faces(:,1),2), corneaSurface.vertices(corneaSurface.faces(:,1),3), ...
corneaNormals(:,1)*5, corneaNormals(:,2)*5, corneaNormals(:,3)*5);
trisurf(epicorneaSurface.faces, epicorneaSurface.vertices(:,1), epicorneaSurface.vertices(:,2), epicorneaSurface.vertices(:,3), ...
'FaceColor','c','FaceAlpha',0.8);
quiver3(epicorneaSurface.vertices(epicorneaSurface.faces(:,1),1), epicorneaSurface.vertices(epicorneaSurface.faces(:,1),2), epicorneaSurface.vertices(epicorneaSurface.faces(:,1),3), ...
epicorneaNormals(:,1)*5, epicorneaNormals(:,2)*5, epicorneaNormals(:,3)*5);
trisurf(interconeBaseSurface.faces, interconeBaseSurface.vertices(:,1), interconeBaseSurface.vertices(:,2), interconeBaseSurface.vertices(:,3), ...
'FaceColor','b','FaceAlpha',0.8);
quiver3(interconeBaseSurface.vertices(interconeBaseSurface.faces(:,1),1), interconeBaseSurface.vertices(interconeBaseSurface.faces(:,1),2), interconeBaseSurface.vertices(interconeBaseSurface.faces(:,1),3), ...
interconeBaseNormals(:,1)*5, interconeBaseNormals(:,2)*5, interconeBaseNormals(:,3)*5);
trisurf(interconeSurface.faces, interconeSurface.vertices(:,1), interconeSurface.vertices(:,2), interconeSurface.vertices(:,3), ...
'FaceColor','b','FaceAlpha',0.8);
quiver3(interconeSurface.vertices(interconeSurface.faces(:,1),1), interconeSurface.vertices(interconeSurface.faces(:,1),2), interconeSurface.vertices(interconeSurface.faces(:,1),3), ...
interconeNormals(:,1)*5, interconeNormals(:,2)*5, interconeNormals(:,3)*5);
end
% Correct normal direction - should point out...
% It seems that some vertices can end up outside of the border volume..
% Could try add border subscripts as well?
coneBorderVolume = imdilate(coneBorderVolume, strel('sphere',dilateBorderRadius));
% tempV = RIFlagVolume*2 + coneBorderVolume;
% figure; imshow(permute(tempV(round(volumeSize(1)/2),:,:)/3, [2 3 1])');
end
% Do plotting - for test volume
if useTestData
% Get surface voxels of RI volume
surfaceInds = find(coneBorderVolume);
[surfaceX, surfaceY, surfaceZ] = ind2sub(volumeSize, surfaceInds);
bottomZ = min(surfaceZ);
topZ = max(surfaceZ);
figure;
subplot(1,3,1);
imshow((lensRIVolume(:,:,round(volumeSize(3)/2)))/(max(lensRIVolume(:))))
subplot(1,3,2);
imshow(permute((lensRIVolume(:,round(volumeSize(2)/2),:))/...
(max(lensRIVolume(:))), [1 3 2]))
subplot(1,3,3); hold on
plot(lensRIVolume(:,round(volumeSize(2)/2),bottomZ),'b');
centreLine = find(RIFlagVolume(:,round(volumeSize(2)/2),bottomZ));
tempRadius = sqrt((centreLine - volumeSize(1)/2).^2 + ...
(round(volumeSize(2)/2) - volumeSize(2)/2)^2 );
% Nishidate eqn
if createGradedFiber
plot(centreLine, sqrt(2.5-(tempRadius*voxelSize/radius).^2), 'rx');
plot(centreLine, sqrt(n0^2*(1-alpha^2*(tempRadius*voxelSize).^2)), 'g');
end
end
% Search is limited to good in points
RIFlagInds = find(RIFlagVolume);
if useRealData
plotInds = 1:50:size(grinSurface.vertices,1);
elseif useTestData
if createLunebergLens
plotInds = 1:length(surfaceX);
elseif createGradedFiber
plotInds = 1:50:length(surfaceX);
end
end
% Get voxel coordinates
[tempX, tempY, tempZ] = meshgrid(1:volumeSize(1), 1:volumeSize(2), 1:volumeSize(3));
volInds = sub2ind(volumeSize, tempX(:), tempY(:), tempZ(:));
volCoords = ([tempX(:), tempY(:), tempZ(:)])*voxelSize;
zSteps = (1:volumeSize(3)*1.5)*voxelSize;
if useRealData
xStartPoints = volumeSize(1)/2:xSpacing:volumeSize(1)*1.5;
xStartPointsOrig = xStartPoints;
xStartPoints = [volumeSize(1)-fliplr(xStartPoints(2:end)) xStartPoints];
if trace3D
[xGrid, yGrid, zGrid] = meshgrid(xStartPoints, xStartPoints, 1);
rayOrigins = [xGrid(:), yGrid(:), zGrid(:)]*voxelSize;
else
rayOrigins = [xStartPoints(:), ones(numel(xStartPoints),1)*volumeSize(2)/2, ...
ones(numel(xStartPoints),1)]*voxelSize;
end
if ~isempty(testOrigin)
rayOrigins = rayOrigins(testOrigin,:);
end
else
%%% Does in 2D, could add trace3D as well (need to update solver)
if ~plotReferenceFiber | createLunebergLens
xStartPoints = volumeSize(1)/2:xSpacing:volumeSize(1);
xStartPoints = [(xStartPoints(1:end-1)-volumeSize(1)/2+1) xStartPoints];
else
xStartPoints = volumeSize(1)/2+radius/voxelSize*0.5;
end
rayOrigins = [xStartPoints(:), ones(numel(xStartPoints),1)*volumeSize(2)/2, ...
ones(numel(xStartPoints),1)]*voxelSize;
end
raysOnXZPlane = rayOrigins(:,2)/voxelSize == volumeSize(2)/2;
raysOnYZPlane = rayOrigins(:,1)/voxelSize == volumeSize(1)/2;
nOrigins = size(rayOrigins,1);
rayCols = lines(nOrigins);
%% Run ray tracer
volumeCenter = volumeSize/2*voxelSize;
% Get border test values
if useRealData