-
Notifications
You must be signed in to change notification settings - Fork 14
/
Example18_STLimport.m
185 lines (156 loc) · 7.47 KB
/
Example18_STLimport.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
%% Description
% This example shows how to import a shape from an STL file (sample.stl)
% and optionally apply scaling (unit conversion), rotation and mirroring on
% the mesh before using a function to voxelise the mesh.
%
% Importing is done inside the geometry function at the end of the model
% file (this file). The function findInsideVoxels returns a logical 3D
% array which is true in the indices of those voxels that are inside the
% (watertight) shape stored in the STL file. The function takes six inputs.
% The first three are always just the X, Y, and Z arrays exactly as they
% are passed into the geometry function. The fourth is the file name of the
% STL file. The fifth is a 3x3 matrix A that will be multiplied onto the
% STL mesh [x;y;z] coordinates. The sixth is a 1D array v of length 3 that
% defines the translation that will be applied to the mesh points after
% multiplication and before voxelisation. In other words, the mesh points
% are transformed according to [x';y';z'] = A*[x;y;z] + v and the
% voxelisation is then performed based on the mesh with positions
% [x';y';z'].
%
% The multiplication matrix A can contain a scaling factor, e.g., for
% converting from the units of the STL file to centimeters, which is the
% unit used in MCmatlab. It can also contain various rotation matrices, see
% https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions
% You may also mirror the coordinates by inverting the sign of the
% appropriate elements in the matrix.
%
% The example runs four separate sub-examples showing various
% transformations. Press enter to advance from one sub-example to the next.
% The MC simulation is not run in this example.
%
% Remember that since the z-axis in MCmatlab points down, the coordinate
% system is a left-handed coordinate system. Therefore a rotation that is
% clockwise in a right-handed coordinate system will be counter-clockwise
% in MCmatlab's coordinate system.
%
% The STL import feature uses the mesh_voxelization code by William Warriner:
% https://github.com/wwarriner/mesh_voxelization
% although the function has been lightly modified by Anders K. Hansen for
% use in MCmatlab.
%% MCmatlab abbreviations
% G: Geometry, MC: Monte Carlo, FMC: Fluorescence Monte Carlo, HS: Heat
% simulation, M: Media array, FR: Fluence rate, FD: Fractional damage.
%
% There are also some optional abbreviations you can use when referencing
% object/variable names: LS = lightSource, LC = lightCollector, FPID =
% focalPlaneIntensityDistribution, AID = angularIntensityDistribution, NI =
% normalizedIrradiance, NFR = normalizedFluenceRate.
%
% For example, "model.MC.LS.FPID.radialDistr" is the same as
% "model.MC.lightSource.focalPlaneIntensityDistribution.radialDistr"
%% Geometry definition
MCmatlab.closeMCmatlabFigures();
model = MCmatlab.model;
model.G.nx = 100; % Number of bins in the x direction
model.G.ny = 100; % Number of bins in the y direction
model.G.nz = 100; % Number of bins in the z direction
model.G.Lx = 4; % [cm] x size of simulation cuboid
model.G.Ly = 4; % [cm] y size of simulation cuboid
model.G.Lz = 4; % [cm] z size of simulation cuboid
model.G.mediaPropertiesFunc = @mediaPropertiesFunc; % Media properties defined as a function at the end of this file
% No rotation or mirroring, only scaling to convert from mm to cm and a
% translation along +z:
model.G.geomFunc = @geometryDefinition_1;
model = plot(model,'G');
figure(31); % Make the STL import illustration the active figure so we can see it
fprintf('Press enter to continue...\n');pause;
% Mirror along the z direction, scale and translate:
model.G.geomFunc = @geometryDefinition_2;
model = plot(model,'G');
figure(31); % Make the STL import illustration the active figure so we can see it
fprintf('Press enter to continue...\n');pause;
% Rotate by pi/2 (90 degrees) around the x axis, then scale and translate:
model.G.geomFunc = @geometryDefinition_3;
model = plot(model,'G');
figure(31); % Make the STL import illustration the active figure so we can see it
fprintf('Press enter to continue...\n');pause;
% Rotate by pi/2 (90 degrees) around the x axis, then -pi/2 (-90 degrees)
% around the z axis, then scale and translate:
model.G.geomFunc = @geometryDefinition_4;
model = plot(model,'G');
figure(31); % Make the STL import illustration the active figure so we can see it
%% Geometry function(s) (see readme for details)
function M = geometryDefinition_1(X,Y,Z,parameters)
% Don't rotate or mirror the STL mesh points, but convert the coordinates from mm to cm by multiplying by 0.1:
A = 0.1*eye(3);
% Then translate the points by 1.5 in the +z direction:
v = [0 0 1.5];
% Find out which voxels are located inside this mesh:
insideVoxels = findInsideVoxels(X,Y,Z,'sample.stl',A,v);
% Set the background to air and the inside voxels to standard tissue:
M = ones(size(X)); % Air
M(insideVoxels) = 2;
end
function M = geometryDefinition_2(X,Y,Z,parameters)
% Invert the z-coordinates of the STL mesh points, and scale the result by 0.1:
A = 0.1*[1 0 0;
0 1 0;
0 0 -1];
% Then translate the points by 2.5 in the +z direction:
v = [0 0 2.5];
% Find out which voxels are located inside this mesh:
insideVoxels = findInsideVoxels(X,Y,Z,'sample.stl',A,v);
% Set the background to air and the inside voxels to standard tissue:
M = ones(size(X)); % Air
M(insideVoxels) = 2;
end
function M = geometryDefinition_3(X,Y,Z,parameters)
% First rotate the STL file mesh points by theta around the x axis, then scale by 0.1:
theta = pi/2;
A = 0.1*[1 0 0 ;
0 cos(theta) -sin(theta);
0 sin(theta) cos(theta)]; % Rotation around the x axis
% Then translate the mesh by 2.5 in the +z direction:
v = [0 0 2.5];
% Find out which voxels are located inside this mesh:
insideVoxels = findInsideVoxels(X,Y,Z,'sample.stl',A,v);
% Set the background to air and the inside voxels to standard tissue:
M = ones(size(X)); % Air
M(insideVoxels) = 2;
end
function M = geometryDefinition_4(X,Y,Z,parameters)
% Construct some rotation matrices for rotation around the x and z axes:
theta = pi/2;
Rx = [ 1 0 0 ;
0 cos(theta) -sin(theta);
0 sin(theta) cos(theta)]; % Rotation around the x axis
phi = -pi/2;
Rz = [cos(phi) -sin(phi) 0 ;
sin(phi) cos(phi) 0 ;
0 0 1 ]; % Rotation around the z axis
% First rotate the STL file mesh points by theta around the x axis, then
% phi around the z axis (multiplication onto [x;y;z] happens from right to
% left), then scale by 0.1:
A = 0.1*Rz*Rx;
% Then translate the mesh by 2.5 in the +z direction:
v = [0 0 2.5];
% Find out which voxels are located inside this mesh:
insideVoxels = findInsideVoxels(X,Y,Z,'sample.stl',A,v);
% Set the background to air and the inside voxels to standard tissue:
M = ones(size(X)); % Air
M(insideVoxels) = 2;
end
%% Media Properties function (see readme for details)
function mediaProperties = mediaPropertiesFunc(parameters)
mediaProperties = MCmatlab.mediumProperties;
j=1;
mediaProperties(j).name = 'air';
mediaProperties(j).mua = 1e-8; % [cm^-1]
mediaProperties(j).mus = 1e-8; % [cm^-1]
mediaProperties(j).g = 1;
j=2;
mediaProperties(j).name = 'standard tissue';
mediaProperties(j).mua = 1; % [cm^-1]
mediaProperties(j).mus = 100; % [cm^-1]
mediaProperties(j).g = 0.9;
end