-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathload_stl_shape.m
87 lines (65 loc) · 2.1 KB
/
load_stl_shape.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
% Load a STL file, generate a voxel grid and scatter a plane wave off the shape
% Add ott to the path
addpath('../');
% Setup the figure for output
figure();
%% Load the STL object
% STL reader only supports binary STL files for now
% Change the filename to an STL file on your computer or download
% an STL file from online.
shape = ott.shapes.StlLoader('mesh.stl');
% Visualise the shape
h = subplot(1, 3, 1);
shape.surf('surfoptions', {'EdgeColor', 'none'});
axis equal;
view([-30, 25]);
title('Mesh');
xlabel('X');
ylabel('Y');
zlabel('Z');
%% Convert the shape to a voxel grid
spacing = 1.0; % Adjust this for your mesh
subplot(1, 3, 2);
xyz = shape.voxels(spacing, 'visualise', true);
axis equal;
view([-30, 25]);
title('Voxels');
xlabel('X');
ylabel('Y');
zlabel('Z');
%% Calculate the T-matrix for this shape
scale = 0.1; % Convert from mesh units to simulation units
Tmatrix = ott.TmatrixDda(scale*xyz, 'index_particle', 1.4, 'index_medium', 1.33, ...
'wavelength0', 1.0);
%% Calculate torque as a function of axial angle
angles = linspace(0, 2*pi, 100);
rotation = ott.utils.roty(angles*180/pi);
beam = ott.BscPmGauss('NA', 1.02, 'power', 1.0, 'index_medium', 1.33, ...
'polarisation', [1, 1i], 'wavelength0', 1.0);
[~, torque] = ott.forcetorque(beam, Tmatrix, 'rotation', rotation);
subplot(1, 3, 3);
plot(angles, torque);
xlabel('Y-Angle [rad]');
ylabel('Torque [a.u.]');
legend({'X', 'Y', 'Z'}, 'Location', 'SouthEast');
title('Torque');
%% Animate the mesh rotating (no translation)
beam = ott.BscPmGauss('NA', 1.02, 'power', 1.0, 'index_medium', 1.33, ...
'polarisation', [1, 1i], 'wavelength0', 1.0);
x = [0;0;0];
Rx = eye(3);
dtheta = 2*pi/100;
dx = 0.0; % no translation
figure();
shape.surf('rotation', Rx, 'surfoptions', {'EdgeColor', 'none'});
axis equal;
axis([-10, 10, -10, 10, -2, 10]);
for ii = 1:100
[f, t] = ott.forcetorque(beam, Tmatrix, 'rotation', Rx, 'position', x);
x = x + dx*f./vecnorm(f);
Rx = ott.utils.rotation_matrix(dtheta*t./vecnorm(t))*Rx;
shape.surf('rotation', Rx, 'surfoptions', {'EdgeColor', 'none'});
axis equal;
axis([-10, 10, -10, 10, -2, 10]);
drawnow;
end