-
Notifications
You must be signed in to change notification settings - Fork 0
/
printimage_modify_beam.m
224 lines (185 loc) · 9.57 KB
/
printimage_modify_beam.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
function [ao_volts_out] = printimage_modify_beam(ao_volts_raw);
global STL;
% Vignetting compensation can be done here, since it's independent of
% zoom and thus doesn't require re-voxelising. Sinusoidal-velocity
% compensation is currently in voxelise(); this is awkward for testing
% and readability, but it's okay, since any change that affects it
% (besides tweaking parameters) depends on zoom and thus requires
% re-voxelising anyway.
POWER_COMPENSATION = {};
POWER_COMPENSATION = {'fit'} %{'speed', 'fit'};
BEAM_SPEED_POWER_COMPENSATION_FACTOR = 1;
FIT_COMPENSATION_FACTOR = 1.5;
SHOW_COMPENSATION = 34;
hSI = evalin('base', 'hSI');
%hSI.hChannels.loggingEnable = false;
if STL.print.voxelise_needed
voxelise([], 'print');
end
if STL.print.voxelise_needed
error('Tried re-voxelising, but was unsuccessful.');
end
% Pull down which metavoxel we're working on:
mvx = STL.print.mvx_now;
mvy = STL.print.mvy_now;
mvz = STL.print.mvz_now;
voxelpower = STL.print.metavoxels{mvx, mvy, mvz} * STL.print.power;
v_i = find(voxelpower(:)); % indices into voxels to be printed
disp(sprintf('~ Voxel power is on [%g, %g]', ...
min(voxelpower(v_i)), ...
max(voxelpower(v_i))));
% Flyback blanking workaround KLUDGE!!! This means that
% metavoxel_overlap will need to be bigger than it would otherwise need
% to be, by one voxel.
workspace_size = size(voxelpower);
voxelpower(end,:,:) = zeros(workspace_size(2:3));
adj = ones(workspace_size);
xc = STL.print.voxelpos_wrt_fov{mvx, mvy, mvz}.x;
yc = STL.print.voxelpos_wrt_fov{mvx, mvy, mvz}.y;
if SHOW_COMPENSATION
figure(SHOW_COMPENSATION);
subplot(1,2,1);
cla;
legend_entries = {};
end
for powercomp = 1:length(POWER_COMPENSATION)
% Vignetting power compensation lives here.
switch POWER_COMPENSATION{powercomp}
case 'speed'
% Compensate proportionally--generalise Christos's ad-hoc
% compensation due to a nonlinearity in polymerisation vs speed
% e.g., ((v - 1) * 0.5) + 1
% Normalisation: as we zoom in, absolute speed decreasees,
% so no normalisation is necessary.
beamspeed = diff(xc) * STL.calibration.pockelsFrequency;
beamspeed(end+1) = beamspeed(1);
beam_power_comp_x = ((beamspeed - STL.calibration.beam_speed_max_um) * BEAM_SPEED_POWER_COMPENSATION_FACTOR ...
+ STL.calibration.beam_speed_max_um) ...
/ STL.calibration.beam_speed_max_um;
adj = repmat(beam_power_comp_x', [1, workspace_size(2), workspace_size(3)]);
voxelpower = voxelpower .* adj;
disp(sprintf('~ Beam speed power compensation (factor %g) applied. Adjusted power is on [%g, %g]', ...
BEAM_SPEED_POWER_COMPENSATION_FACTOR, ...
min(voxelpower(v_i)), ...
max(voxelpower(v_i))));
if SHOW_COMPENSATION
figure(SHOW_COMPENSATION);
subplot(1,2,1);
hold on;
plot(xc, beam_power_comp_x);
legend_entries{end+1} = 'speed';
hold off;
end
case 'cos'
[vig_x, vig_y] = meshgrid(xc, yc);
vignetting_falloff = cos(atan(((vig_x.^2 + vig_y.^2).^(1/2))/STL.calibration.lens_optical_working_distance));
vignetting_falloff = repmat(vignetting_falloff', [1, 1, size(voxelpower, 3)]);
voxelpower = voxelpower ./ vignetting_falloff;
adj = adj ./ vignetting_falloff;
disp(sprintf('~ Vignetting power compensation (cos(theta)) applied. Adjusted power is on [%g, %g]', ...
min(voxelpower(v_i)), ...
max(voxelpower(v_i))));
case 'fit'
if isfield(STL, 'calibration') & isfield(STL.calibration, 'vignetting_fit') & length(STL.calibration.vignetting_fit) > 0
[vig_x, vig_y] = meshgrid(xc, yc);
for fit_function = 1:length(STL.calibration.vignetting_fit)
vignetting_falloff = STL.calibration.vignetting_fit{fit_function}(vig_x, -vig_y);
% So far, vignetting_falloff is still in arbitrary
% units of TIFF brightness! Set power change in centre
% of FOV to 1.
centreY = round(size(vignetting_falloff, 1)/2);
centreX = round(size(vignetting_falloff, 2)/2);
vignetting_falloff = vignetting_falloff / vignetting_falloff(centreY, centreX);
% Rescale compensation (like a learning rate and a
% photoresist responsiveness factor rolled into
% one)
%vignetting_falloff = ((vignetting_falloff-1) * FIT_COMPENSATION_FACTOR) + 1;
vignetting_falloff = vignetting_falloff .^ FIT_COMPENSATION_FACTOR;
% Higher luminance (e.g. edges) indicates higher
% falloff, so it needs to be inverted.
vignetting_falloff = 1 ./ vignetting_falloff;
% Transpose: xc is the first index of the matrix (row #)
vignetting_falloff = repmat(vignetting_falloff', [1, 1, size(voxelpower, 3)]);
adj = adj ./ vignetting_falloff;
voxelpower = voxelpower ./ vignetting_falloff;
disp(sprintf('~ Vignetting power compensation (current fit %d, power factor %g) applied. Adjusted power is on [%g, %g]', ...
fit_function, ...
FIT_COMPENSATION_FACTOR, ...
min(voxelpower(v_i)), ...
max(voxelpower(v_i))));
if SHOW_COMPENSATION
figure(SHOW_COMPENSATION);
subplot(1,2,1);
hold on;
middle = round(size(vignetting_falloff, 2));
plot(vig_x, adj(:, middle, end));
legend_entries{end+1} = sprintf('Iter %d', fit_function);
%plot(viog_x, STL.print.power ./ voxelpower(:, middle, end));
hold off;
subplot(1,2,2);
surfc(vig_x, vig_y, 1./real(adj(:,:,end)'));
colorbar;
end
end
else
warning('~ No vignetting power compensation fit functions available.');
end
case 'none'
disp('~ Vignetting power compensation NOT applied.');
otherwise
warning('~ Illegal value specified. Vignetting power compensation NOT applied.');
end
end
adj = real(adj);
voxelpower = real(voxelpower);
% Do not ask for more than 100% power:
if max(voxelpower(:)) > 1
warning(sprintf('~ Vignetting compensation is requesting power %g%%! Squashing to 100%%.', 100*max(voxelpower(:))));
voxelpower = min(voxelpower, 1);
end
if min(voxelpower(:)) < 0
voxelpower = max(voxelpower, 0);
%error('~ Someone requested power < 0. You''ll want to fix that.');
end
disp(sprintf('~ Final adjusted power is on [%g, %g]', ...
min(voxelpower(v_i)), ...
max(voxelpower(v_i))));
if SHOW_COMPENSATION
figure(SHOW_COMPENSATION);
if exist('vignetting_falloff', 'var')
subplot(1,2,1);
title('Compensation factors @ Y=0');
xlabel('X (\mu{}m)');
ylabel('Factor');
legend(legend_entries, 'Location', 'South');
subplot(1,2,2);
else
subplot(1,1,1);
end
adj = adj(:,:,1); % Don't need all the repeats!
imagesc(STL.print.voxelpos_wrt_fov{1,1,1}.x, ...
STL.print.voxelpos_wrt_fov{1,1,1}.y, ...
adj');
axis square;
colorbar;
colormap(jet);
title('Power compensation');
xlabel('X (\mu{}m)');
ylabel('Y (\mu{}m)');
end
% Save for analysis
STL.print.voxelpower_adjustment = adj;
STL.print.voxelpower_actual = voxelpower;
STL.print.ao_volts_out = ao_volts_raw;
if STL.logistics.simulated
STL.print.ao_volts_out.B(:, STL.print.whichBeam) = voxelpower(:);
else
STL.print.ao_volts_out.B(:, STL.print.whichBeam) = hSI.hBeams.zprpBeamsPowerFractionToVoltage(STL.print.whichBeam, voxelpower(:));
end
% Decrease power as appropriate for current zoom level. Empirically, this
% seems to go sublinearly! Not sure why. Perhaps overscanning on Y doesn't
% happen fast enough to count as more power? Perhaps SUBlinear because I
% have not calibrated aspect ratio yet? FIXME
%STL.print.ao_volts_raw.B = STL.print.ao_volts_raw.B / hSI.hRoiManager.scanZoomFactor;
ao_volts_out = STL.print.ao_volts_out;
end