-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoordinateSystem.m
294 lines (241 loc) · 10.8 KB
/
CoordinateSystem.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
function [Temp_Coordinates, Temp_Nodes] = CoordinateSystem(aligned_nodes,bone_indx,bone_coord,side_indx)
% This function produces the coordinate system for the users bone in the
% temporarily aligned orientation.
%% TT CS for Talus
if bone_indx == 1 && bone_coord >= 2
nodes_aligned_original = aligned_nodes;
aligned_nodes = [aligned_nodes(aligned_nodes(:,2)<10,1) aligned_nodes(aligned_nodes(:,2)<10,2) aligned_nodes(aligned_nodes(:,2)<10,3)];
end
%% Tibial Realignment for Medial Malleolus
if bone_indx == 13
nodes_aligned_original = aligned_nodes;
cutting_plane = min(aligned_nodes(:,3)) + 14; % Temporarily removes the tibial plafond
cutting_plane2 = min(aligned_nodes(:,3)) + 100; % Temporarily shortens the tibia
aligned_nodes = [aligned_nodes(aligned_nodes(:,3)>cutting_plane,1) aligned_nodes(aligned_nodes(:,3)>cutting_plane,2) aligned_nodes(aligned_nodes(:,3)>cutting_plane,3)];
aligned_nodes = [aligned_nodes(aligned_nodes(:,3)<cutting_plane2,1) aligned_nodes(aligned_nodes(:,3)<cutting_plane2,2) aligned_nodes(aligned_nodes(:,3)<cutting_plane2,3)];
end
%% Split up the bone into nth sections in all three planes
x_min = min(aligned_nodes(:,1));
y_min = min(aligned_nodes(:,2));
z_min = min(aligned_nodes(:,3));
x_max = max(aligned_nodes(:,1));
y_max = max(aligned_nodes(:,2));
z_max = max(aligned_nodes(:,3));
range_x = x_max - x_min;
range_y = y_max - y_min;
range_z = z_max - z_min;
% Splits bone up in n sections
if bone_indx == 1 % Talus
n = 3;
elseif bone_indx == 2 % Calcaneus
n = 10;
elseif bone_indx == 3 % Navicular
n = 5;
elseif bone_indx == 4 % Cuboid
n = 5;
elseif bone_indx >= 5 && bone_indx <= 7 % Cuneiforms
n = 3;
elseif bone_indx >= 8 && bone_indx <= 12 % Metatarsals
n = 3;
elseif bone_indx == 13 || bone_indx == 14 % Tibia or Fibula
n = 3;
end
nth_x = range_x/n;
nth_y = range_y/n;
nth_z = range_z/n;
%% Positive Y Nth ROI
positive_y_nth = y_max - nth_y;
positive_y_nth_ROI = aligned_nodes(:,2) >= positive_y_nth;
positive_y_nth_x = nonzeros(aligned_nodes(:,1).*positive_y_nth_ROI);
positive_y_nth_y = nonzeros(aligned_nodes(:,2).*positive_y_nth_ROI);
positive_y_nth_z = nonzeros(aligned_nodes(:,3).*positive_y_nth_ROI);
av_positive_y_nth_x = mean(positive_y_nth_x);
av_positive_y_nth_y = mean(positive_y_nth_y);
av_positive_y_nth_z = mean(positive_y_nth_z);
av_positive_y_nth = [av_positive_y_nth_x,av_positive_y_nth_y,av_positive_y_nth_z];
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% hold on
% plot3(positive_y_nth_x,positive_y_nth_y,positive_y_nth_z,'ys')
% plot3(av_positive_y_nth_x,av_positive_y_nth_y,av_positive_y_nth_z,'r.','MarkerSize',50)
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Negative Y nth ROI
negative_y_nth = y_min + nth_y;
negative_y_nth_ROI = aligned_nodes(:,2) <= negative_y_nth;
negative_y_nth_x = nonzeros(aligned_nodes(:,1).*negative_y_nth_ROI);
negative_y_nth_y = nonzeros(aligned_nodes(:,2).*negative_y_nth_ROI);
negative_y_nth_z = nonzeros(aligned_nodes(:,3).*negative_y_nth_ROI);
av_negative_y_nth_x = mean(negative_y_nth_x);
av_negative_y_nth_y = mean(negative_y_nth_y);
av_negative_y_nth_z = mean(negative_y_nth_z);
av_negative_y_nth = [av_negative_y_nth_x,av_negative_y_nth_y,av_negative_y_nth_z];
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% hold on
% plot3(negative_y_nth_x,negative_y_nth_y,negative_y_nth_z,'ys')
% plot3(av_negative_y_nth_x,av_negative_y_nth_y,av_negative_y_nth_z,'r.','MarkerSize',50)
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Positive Z nth ROI
positive_z_nth = z_max - nth_z;
positive_z_nth_ROI = aligned_nodes(:,3) >= positive_z_nth;
positive_z_nth_x = nonzeros(aligned_nodes(:,1).*positive_z_nth_ROI);
positive_z_nth_y = nonzeros(aligned_nodes(:,2).*positive_z_nth_ROI);
positive_z_nth_z = nonzeros(aligned_nodes(:,3).*positive_z_nth_ROI);
av_positive_z_nth_x = mean(positive_z_nth_x);
av_positive_z_nth_y = mean(positive_z_nth_y);
av_positive_z_nth_z = mean(positive_z_nth_z);
av_positive_z_nth = [av_positive_z_nth_x,av_positive_z_nth_y,av_positive_z_nth_z];
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% hold on
% plot3(positive_z_nth_x,positive_z_nth_y,positive_z_nth_z,'ys')
% plot3(av_positive_z_nth_x,av_positive_z_nth_y,av_positive_z_nth_z,'r.','MarkerSize',50)
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Negative Z nth ROI
negative_z_nth = z_min + nth_z;
negative_z_nth_ROI = aligned_nodes(:,3) <= negative_z_nth;
negative_z_nth_x = nonzeros(aligned_nodes(:,1).*negative_z_nth_ROI);
negative_z_nth_y = nonzeros(aligned_nodes(:,2).*negative_z_nth_ROI);
negative_z_nth_z = nonzeros(aligned_nodes(:,3).*negative_z_nth_ROI);
av_negative_z_nth_x = mean(negative_z_nth_x);
av_negative_z_nth_y = mean(negative_z_nth_y);
av_negative_z_nth_z = mean(negative_z_nth_z);
av_negative_z_nth = [av_negative_z_nth_x,av_negative_z_nth_y,av_negative_z_nth_z];
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% hold on
% plot3(negative_z_nth_x,negative_z_nth_y,negative_z_nth_z,'ys')
% plot3(av_negative_z_nth_x,av_negative_z_nth_y,av_negative_z_nth_z,'r.','MarkerSize',50)
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Negative X nth ROI
negative_x_nth = x_min + nth_x;
negative_x_nth_ROI = aligned_nodes(:,1) <= negative_x_nth;
negative_x_nth_x = nonzeros(aligned_nodes(:,1).*negative_x_nth_ROI);
negative_x_nth_y = nonzeros(aligned_nodes(:,2).*negative_x_nth_ROI);
negative_x_nth_z = nonzeros(aligned_nodes(:,3).*negative_x_nth_ROI);
av_negative_x_nth_x = mean(negative_x_nth_x);
av_negative_x_nth_y = mean(negative_x_nth_y);
av_negative_x_nth_z = mean(negative_x_nth_z);
av_negative_x_nth = [av_negative_x_nth_x,av_negative_x_nth_y,av_negative_x_nth_z];
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% hold on
% plot3(negative_x_nth_x,negative_x_nth_y,negative_x_nth_z,'ys')
% plot3(av_negative_x_nth_x,av_negative_x_nth_y,av_negative_x_nth_z,'r.','MarkerSize',50)
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Positive X nth ROI
positive_x_nth = x_max - nth_x;
positive_x_nth_ROI = aligned_nodes(:,1) >= positive_x_nth;
positive_x_nth_x = nonzeros(aligned_nodes(:,1).*positive_x_nth_ROI);
positive_x_nth_y = nonzeros(aligned_nodes(:,2).*positive_x_nth_ROI);
positive_x_nth_z = nonzeros(aligned_nodes(:,3).*positive_x_nth_ROI);
av_positive_x_nth_x = mean(positive_x_nth_x);
av_positive_x_nth_y = mean(positive_x_nth_y);
av_positive_x_nth_z = mean(positive_x_nth_z);
av_positive_x_nth = [av_positive_x_nth_x,av_positive_x_nth_y,av_positive_x_nth_z];
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% hold on
% plot3(positive_x_nth_x,positive_x_nth_y,positive_x_nth_z,'ys')
% plot3(av_positive_x_nth_x,av_positive_x_nth_y,av_positive_x_nth_z,'r.','MarkerSize',50)
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Raw Axis Calculation
if bone_indx == 3 % Navicular
first_point = av_positive_x_nth;
second_point = av_negative_x_nth;
third_point = av_positive_z_nth;
elseif bone_indx >= 13 % Tibia, Fibula
first_point = av_positive_z_nth;
second_point = av_negative_z_nth;
if av_negative_z_nth(3) > av_negative_x_nth(3)
third_point = [av_negative_x_nth(1) av_negative_x_nth(2) 0];
else
third_point = av_negative_x_nth;
end
else % Cuneiforms, Metatarsals, Calcaneus, Cuboid, Talus
first_point = av_positive_y_nth;
second_point = av_negative_y_nth;
third_point = av_positive_z_nth;
end
origin = [0,0,0];
% Define the primary axis based on first_point and second_point
primary_axis_vector = first_point - second_point;
primary_axis_unit = primary_axis_vector / norm(primary_axis_vector); % Normalize
% Project the third_point onto the primary axis to find the closest point
projection_length = dot(third_point - second_point, primary_axis_unit);
closest_point_on_primary = second_point + projection_length * primary_axis_unit;
% Define the secondary axis
secondary_axis_vector = third_point - closest_point_on_primary;
secondary_axis_unit = secondary_axis_vector / norm(secondary_axis_vector); % Normalize
% Define the tertiary axis as cross product of primary and secondary axes
tertiary_axis_vector = cross(primary_axis_unit, secondary_axis_unit);
tertiary_axis_unit = tertiary_axis_vector / norm(tertiary_axis_vector); % Normalize
if side_indx == 1
ml = -1;
else
ml = 1;
end
% Adjust axes based on bone index and side index
if bone_indx == 3 % Navicular
ML_vector_points = ml*[origin; origin + 50 * primary_axis_unit];
SI_vector_points = [origin; origin + 50 * secondary_axis_unit];
AP_vector_points = -[origin; origin + 50 * tertiary_axis_unit];
elseif bone_indx == 13 || bone_indx == 14 % Tibia, Fibula
SI_vector_points = [origin; origin + 50 * primary_axis_unit];
ML_vector_points = -ml*[origin; origin + 50 * secondary_axis_unit];
AP_vector_points = -[origin; origin + 50 * tertiary_axis_unit];
else % Cuneiforms, Metatarsals, Calcaneus, Cuboid, Talus
AP_vector_points = [origin; origin + 50 * primary_axis_unit];
SI_vector_points = [origin; origin + 50 * secondary_axis_unit];
ML_vector_points = ml*[origin; origin + 50 * tertiary_axis_unit];
end
% figure()
% plot3(aligned_nodes(:,1),aligned_nodes(:,2),aligned_nodes(:,3),'k.')
% % plot3(nodes_aligned_original(:,1),nodes_aligned_original(:,2),nodes_aligned_original(:,3),'.k')
% hold on
% plot3(AP_vector_points(:,1),AP_vector_points(:,2),AP_vector_points(:,3),'r')
% plot3(SI_vector_points(:,1),SI_vector_points(:,2),SI_vector_points(:,3),'g')
% plot3(ML_vector_points(:,1),ML_vector_points(:,2),ML_vector_points(:,3),'b')
% plot3(0,0,0,'ys')
% plot3(first_point(:,1),first_point(:,2),first_point(:,3),'rs','MarkerSize',20)
% plot3(second_point(:,1),second_point(:,2),second_point(:,3),'bs','MarkerSize',20)
% plot3(third_point(:,1),third_point(:,2),third_point(:,3),'gs','MarkerSize',20)
% legend('Nodal Points','AP Axis','SI Axis','ML Axis')
% text(AP_vector_points(2,1),AP_vector_points(2,2),AP_vector_points(2,3),'Anterior','HorizontalAlignment','left','FontSize',10,'Color','r');
% text(SI_vector_points(2,1),SI_vector_points(2,2),SI_vector_points(2,3),'Superior','HorizontalAlignment','left','FontSize',10,'Color','g');
% if side_indx == 1
% text(ML_vector_points(2,1),ML_vector_points(2,2),ML_vector_points(2,3),'Lateral','HorizontalAlignment','left','FontSize',10,'Color','b');
% else
% text(ML_vector_points(2,1),ML_vector_points(2,2),ML_vector_points(2,3),'Medial','HorizontalAlignment','left','FontSize',10,'Color','b');
% end
% xlabel('X')
% ylabel('Y')
% zlabel('Z')
% axis equal
%% Output Axes and Rotation Index
Temp_Coordinates = [AP_vector_points([1,2],:)
SI_vector_points([1,2],:)
ML_vector_points([1,2],:)];
if (bone_indx == 1 && bone_coord >= 2) || bone_indx == 13
Temp_Nodes = nodes_aligned_original;
else
Temp_Nodes = aligned_nodes;
end