-
Notifications
You must be signed in to change notification settings - Fork 0
/
X_Ray_Projection_test.m
358 lines (277 loc) · 15.7 KB
/
X_Ray_Projection_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
% Muhammed Saadeddin Kocak
% Lalith Boggaram Naveen
% Aleksandr Udalov
classdef X_Ray_Projection_test < matlab.uitest.TestCase
methods (Test)
%Testing the properties of class Cube
function test_Cube(testCase)
cube_shape = Cube('Cube', 'r', 1, 2);
testCase.verifyEqual(cube_shape.name,'Cube');
testCase.verifyEqual(cube_shape.color,'r');
testCase.verifyEqual(cube_shape.opacity,1);
testCase.verifyEqual(cube_shape.edgeSize,2) ;
end
% Testing Cube shape - doesnt' work
function test_Cube_Shape(testCase)
% Create object Cube
CubeObject = Cube('Cube', 'b', 1, 20);
edgeSize = 20;
x = [-1 1 1 -1 -1; -1 1 1 -1 -1] .*edgeSize/2;
y = [-1 -1 1 1 -1; -1 -1 1 1 -1] .*edgeSize/2;
z = [-1 -1 -1 -1 -1; 1 1 1 1 1] .*edgeSize/2;
cubeStruct = surf2patch(x, y, z, 'triangles');
% Close the cube top and bottom faces
lowerFace = [1, 3, 5; 1, 7, 5];
upperFace = [2, 4, 6; 2, 8, 6];
Example_Cube_Faces = [cubeStruct.faces; lowerFace; upperFace];
Example_Cube_Vertices = cubeStruct.vertices;
% Compare vertices and faces
testCase.verifyEqual(CubeObject.faces,Example_Cube_Faces);
testCase.verifyEqual(CubeObject.vertices,Example_Cube_Vertices);
end
%Testing the properties of class Sphere
function test_Sphere(testCase)
cube_shape = Sphere('Sphere', 'r', 1, 3, 40);
testCase.verifyEqual(cube_shape.name,'Sphere');
testCase.verifyEqual(cube_shape.color,'r');
testCase.verifyEqual(cube_shape.opacity,1);
testCase.verifyEqual(cube_shape.radius,3);
testCase.verifyEqual(cube_shape.numberOfFaces,40);
end
% Testing Sphere shape
function test_Sphere_Shape(testCase)
% Creating object Sphere
SphereObject = Sphere('Sphere', 'b', 1, 4, 50);
% Create sphere manually
radius = 4;
[X,Y,Z] = sphere(50);
X = X*radius;
Y = Y*radius;
Z = Z*radius;
[Example_Sphere_Faces, Example_Sphere_Vertices] = surf2patch(X, Y, Z, 'triangles');
% Compare vertices and faces
testCase.verifyEqual(SphereObject.faces,Example_Sphere_Faces);
testCase.verifyEqual(SphereObject.vertices,Example_Sphere_Vertices);
end
%Testing the properties of class Ellipsoid
function test_Ellipsoid(testCase)
cube_shape = Ellipsoid('Ellipsoid', 'r', 0.3, 6, 7, 13);
testCase.verifyEqual(cube_shape.name,'Ellipsoid');
testCase.verifyEqual(cube_shape.color,'r');
testCase.verifyEqual(cube_shape.opacity,0.3);
testCase.verifyEqual(cube_shape.semiAxesX,6);
testCase.verifyEqual(cube_shape.semiAxesY,7);
testCase.verifyEqual(cube_shape.semiAxesZ,13);
end
% Testing Ellipsoid shape
function test_Ellipsoid_Shape(testCase)
% Create object Ellipsoid
EllipsoidObject = Ellipsoid('Ellipsoid', 'g', 0.3, 6, 7, 13);
% Create Ellipsoid manually
semiAxesX = 6;
semiAxesY = 7;
semiAxesZ = 13;
[X,Y,Z] = ellipsoid(0, 0, 0, semiAxesX, semiAxesY, semiAxesZ);
[Example_Ellipsoid_Faces, Example_Ellipsoid_Vertices] = surf2patch(X, Y, Z, 'triangles');
% Compare vertices and faces
testCase.verifyEqual(EllipsoidObject.faces,Example_Ellipsoid_Faces);
testCase.verifyEqual(EllipsoidObject.vertices,Example_Ellipsoid_Vertices);
end
% Testing the properties of class Cylinder
function test_Cylinder(testCase)
cylinder_shape = Cylinder('Cylinder', 'r', 0.1, 9, 24, 32);
testCase.verifyEqual(cylinder_shape.name,'Cylinder');
testCase.verifyEqual(cylinder_shape.opacity,0.1);
testCase.verifyEqual(cylinder_shape.radius,9);
testCase.verifyEqual(cylinder_shape.height,24);
testCase.verifyEqual(cylinder_shape.numberOfCurvePoints,32);
testCase.verifyEqual(cylinder_shape.color,'r');
end
% Testing Cylinder shape
function test_Cylinder_Shape(testCase)
% Create object Cylinder
CylinderObject = Cylinder('chestCylinder', 'r', 0.1, 9, 24, 32);
% Create Cylinder manually
radius = 9;
height = 24;
numberCurvePoints = 32;
[X,Y,Z] = cylinder(radius,numberCurvePoints);
Z = Z*height - height/2;
[Faces, Vertices] = surf2patch(X, Y, Z, 'triangles');
% Create a lower and upper face using circles (close the shape)
circle_faces = zeros(floor(size(Vertices,1)/2)-1, 3);
circle_faces(:,1) = 1:2:size(Vertices,1)-2;
circle_faces(:,2) = 3:2:size(Vertices,1)-1;
% Adjust height coordinate of lower and upper face
Upper_faces = circle_faces + 1;
Upper_faces(:,3) = size(Vertices,1) + 2;
Lower_faces = circle_faces;
Lower_faces(:,3) = size(Vertices, 1) + 1;
% Positioning the created cylinder
Example_Faces = [Faces; Lower_faces; Upper_faces];
Example_Vertices = [Vertices; [0 0 -height/2]; [0 0 height/2]]; % center is [0 0 0]
% Compare vertices and faces
testCase.verifyEqual(CylinderObject.faces,Example_Faces);
testCase.verifyEqual(CylinderObject.vertices,Example_Vertices);
end
%Testing the properties of class Cone
function test_Cone(testCase)
cube_shape = Cone('Cone', 'r', 1, 3, 4, 30);
testCase.verifyEqual(cube_shape.name,'Cone');
testCase.verifyEqual(cube_shape.color,'r');
testCase.verifyEqual(cube_shape.opacity,1);
testCase.verifyEqual(cube_shape.radius,3) ;
testCase.verifyEqual(cube_shape.height,4) ;
testCase.verifyEqual(cube_shape.numberOfCurvePoints,30) ;
end
% Testing Cone shape - doesn't work
function test_Cone_Shape(testCase)
% Create object Ellipsoid
ConeObject = Cone('tumorCone', 'b', 1, 3, 4, 30);
% Create Cone manually
radius = 3;
height = 4;
numberOfCurvePoints = 30;
% creating a cone
[x, y, z] = cylinder(radius, numberOfCurvePoints);
cylinderStruct = surf2patch(x, y, z.*height);
% Using the lower vertices of the cylinder to create a cone
verticesCone = [cylinderStruct.vertices(1:2:end, :); [0, 0, 0]; [0, 0, height]];
% Create lower face and upper hat
facesCircle = nan(size(cylinderStruct.vertices, 1)/2 - 1, 3);
facesCircle(:,1) = 1 : size(verticesCone, 1) - 3;
facesCircle(:,2) = 2 : size(verticesCone, 1) - 2;
% Adjust height coordinate of lower face and upper hat
facesLowerCircle = facesCircle;
facesLowerCircle(:,3) = size(verticesCone, 1) - 1;
facesUpperHat = facesCircle;
facesUpperHat(:,3) = size(verticesCone, 1);
% Positioning the created cone onto the display
facesCone = [facesLowerCircle; facesUpperHat];
verticesCone(:,3) = verticesCone(:,3) - 0.5*height;
% Assign the values of vertices and faces back to the obj
Example_Cone_Vertices = verticesCone;
Example_Cone_Faces = facesCone;
% Compare vertices and faces
testCase.verifyEqual(ConeObject.faces,Example_Cone_Faces);
testCase.verifyEqual(ConeObject.vertices,Example_Cone_Vertices);
end
% Testing the properties of class Material
function test_Material(testCase)
% PET
pet_material = Material('pet', 0.95);
testCase.verifyEqual(pet_material.name,'pet');
testCase.verifyEqual(pet_material.density,0.95);
% WATER
water_material = Material('water', 1);
testCase.verifyEqual(water_material.name,'water');
testCase.verifyEqual(water_material.density,1);
% AIR
air_material = Material('air', 0.0012);
testCase.verifyEqual(air_material.name,'air')
testCase.verifyEqual(air_material.density,0.0012);
end
% Testing interpolation
function test_interpolation(testCase)
% Calculated value for density = 1 and energy = 7 MeV
test_material = Material('water', 1);
actual_coeff = getAttenuationCoefficient(test_material, 7);
% Load example data
data = load('water.mat');
energy = data.data(:,1);
mass_att_coeff = data.data(:,2);
energy_sample = 7;
density = 1;
% Calculate interpolation manually
expected_coeff = interp1(energy, mass_att_coeff, energy_sample, 'linear', 'extrap')*density;
% Compare automated and manual calculations
testCase.verifyEqual(actual_coeff,expected_coeff);
end
% Testing class Objects (if it can contain many inputs (subobjects))
function test_Object(testCase)
% Creating main object
MainObjectShape = Ellipsoid('lungEllipsoid', 'g', 0.35, 6, 4, 12);
MainObjectMaterial = Material('air', 0.00012);
MainObject = Objects('MainObject', [0, 0, 0], MainObjectShape, MainObjectMaterial);
% Creating subobjects
SubObject1_Shape = Sphere('Sphere', 'b', 0.9, 2.7, 31);
SubObject2_Shape = Cube('Cube', 'b', 0.7, 3);
SubObject_Material = Material('pet', 0.95);
SubObject1 = Objects('SubObject1', [1, -1, -10], SubObject1_Shape, SubObject_Material);
SubObject2 = Objects('SubObject2', [2.5, 0.24, -8.6], SubObject2_Shape, SubObject_Material);
% Merging main- and subobjects
MainObject = addSubObject(MainObject, SubObject1);
MainObject = addSubObject(MainObject, SubObject2);
% Check if subobjects is equal as they were defined
testCase.verifyNotEqual(MainObject.subObjects,[]);
testCase.verifyEqual(MainObject.subObjects(1),SubObject1);
testCase.verifyEqual(MainObject.subObjects(2),SubObject2);
end
% Testing the positions of created objects after merging
function test_Object_Position(testCase)
% Create chest
chestShape = Cylinder('chestCylinder', 'r', 0.1, 12, 35, 30);
chestMaterial = Material('water', 1);
chestObject = Objects('Chest', [0, 0, 0], chestShape, chestMaterial);
% Create lung
lungShape = Ellipsoid('lungEllipsoid', 'g', 0.35, 6, 4, 12);
lungMaterial = Material('air', 0.00012);
lungObject = Objects('Lung', [0, 0, 0], lungShape, lungMaterial);
% Create tumors
tumorShape1 = Sphere('tumorSphere', 'b', 0.9, 2.7, 31);
tumorShape2 = Cube('tumorCube', 'b', 0.7, 3);
tumorShape3 = Cone('tumorCone', 'b', 1, 2, 3.5, 30);
tumorMaterial = Material('pet', 0.95);
tumorObject1 = Objects('Tumor1', [0, 0, -10], tumorShape1, tumorMaterial);
tumorObject2 = Objects('Tumor2', [1, 1, 0], tumorShape2, tumorMaterial);
tumorObject3 = Objects('Tumor3', [-1, -1, 10], tumorShape3, tumorMaterial);
% Add subobjects: tumors to lung and lung to chest
lungObject = addSubObject(lungObject, tumorObject1);
lungObject = addSubObject(lungObject, tumorObject2);
lungObject = addSubObject(lungObject, tumorObject3);
chestObject = addSubObject(chestObject, lungObject);
% Test if objects are at the same position
testCase.verifyEqual(lungObject.shape.vertices,chestObject.subObjects(1).shape.vertices);
testCase.verifyEqual(tumorObject1.shape.vertices,chestObject.subObjects.subObjects(1).shape.vertices);
testCase.verifyEqual(tumorObject2.shape.vertices,chestObject.subObjects.subObjects(2).shape.vertices);
testCase.verifyEqual(tumorObject3.shape.vertices,chestObject.subObjects.subObjects(3).shape.vertices);
end
% Testing class Scanner (properties and phantom object)
function test_Scanner(testCase)
% Creating object Chest
chestShape = Cylinder('chestCylinder', 'r', 0.1, 12, 35, 30);
chestMaterial = Material('water', 1);
chestObject = Objects('Chest', [0, 0, 0], chestShape, chestMaterial);
% Creating object Scanner
ScannerObject = Scanner('X-Ray Scanner', [20, 10, -15], 0.06, ...
[-30, 0, 0], [60, 60], [128, 128], chestObject);
% Check the properties of Scanner
testCase.verifyEqual(ScannerObject.sourcePosition, [20, 10, -15]);
testCase.verifyEqual(ScannerObject.sourceEnergy, 0.06);
testCase.verifyEqual(ScannerObject.detectorPosition, [-30, 0, 0]);
testCase.verifyEqual(ScannerObject.detectorPhysicalSize, [60, 60]);
testCase.verifyEqual(ScannerObject.detectorMatrixSize, [128, 128]);
% Check if phantom is empty
testCase.verifyNotEqual(ScannerObject.phantom,[]);
testCase.verifyEqual(ScannerObject.phantom,chestObject);
end
% Testing the attenuated image generation with 1 example
function test_Atten_Image(testCase)
% Creating object Chest
chestShape = Cylinder('chestCylinder', 'r', 0.1, 12, 35, 30);
chestMaterial = Material('water', 1);
chestObject = Objects('Chest', [0, 0, 0], chestShape, chestMaterial);
% Creating object Scanner
ScannerObject = Scanner('X-Ray Scanner', [20, 10, -15], 0.06, ...
[-30, 0, 0], [60, 60], [128, 128], chestObject);
% Load actual data
data = load('water.mat');
% Apply Lambert Beer Law (values from data folder for water and energy = 0.06 MeV)
Example_intensity = data.data(7,1)*exp(-data.data(7,2)*12);
% Get image on detector
attenuationImage = ScannerObject.run();
% Compare calculated and actual values of Intensity with 5% inaccuracy
testCase.verifyEqual(mean(attenuationImage, 'all'),Example_intensity,'AbsTol',0.05);
end
end
end