annotate ReMarker.py @ 15:60ed96f5706e draft

Uploaded
author mb2013
date Tue, 20 May 2014 03:28:59 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
1 #Program for correcting landmarks.
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
2 #The object will be rotated, based on 3 symmetry points.
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
3 #The differences between the mirror points will be calculated and
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
4 # the coordinates will be corrected where needed.
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
5
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
6 #MB
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
7
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
8 import math
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
9 from math import *
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
10 import sys
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
11 import numpy
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
12 import os
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
13
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
14 # Main function
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
15 def main():
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
16 file_name = sys.argv[1] # dta file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
17 file_connect = sys.argv[2] # connect file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
18 side = sys.argv[3] # correct side of object
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
19
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
20 file_outputname5 = sys.argv[5] # output dta file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
21 output5 = open(file_outputname5, 'w')
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
22
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
23 # to function 'open connect file'
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
24 sym1,sym2,sym3, listmirror, listspec = open_connect_file(file_connect)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
25
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
26 # to function 'open dta file'
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
27 listdata = open_dta_file(file_name, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
28
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
29 # to function 'shift to zero'
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
30 shift_to_zero(listdata,sym1,sym2,sym3,listmirror,listspec, side, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
31
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
32 # Function for extracting values of connect file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
33 def open_connect_file(file_connect):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
34 mirrorpoints = open(file_connect)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
35 lines= mirrorpoints.readlines()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
36 listmirror = [] #with mirror points
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
37 listsym = [] #with symmetry points
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
38 listspec = [] #with spec points
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
39
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
40 #extracting symmetry landmarks and mirror landmarks of connect file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
41 for x in range(0, len(lines)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
42 line = lines[x].strip()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
43 if line == 'sym': # extract points on symmetry plane
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
44 for a in range(1,4):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
45 listsym.append(lines[a].strip().split())
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
46 if line == 'mirror': # extract mirror points
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
47 for y in range(x + 1,len(lines)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
48 try:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
49 if lines[y].strip() != 'spec':
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
50 if lines[y].strip() == '':
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
51 break
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
52 else:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
53 listmirror.append(lines[y].strip().split()) # add mirror points
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
54
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
55 else:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
56 break
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
57
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
58 except:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
59 break
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
60 if line == 'spec':# if specs are present
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
61 for b in range(x+1, len(lines)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
62 listspec.append(lines[b].strip().split()) # add points in specs
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
63 #Symmetry points
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
64 sym1 = int(listsym[0][0]) -1
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
65 sym2 = int(listsym[1][0]) -1
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
66 sym3 = int(listsym[2][0]) -1
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
67
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
68 mirrorpoints.close()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
69 return sym1, sym2, sym3, listmirror, listspec
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
70
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
71 # Function open .dta file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
72 def open_dta_file(file_name, output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
73 datapoints = open(file_name)#open file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
74 datalines = datapoints.readlines()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
75 listdata = [] # landmarklist
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
76 listdta = [] # coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
77 sublist = [] # sublist
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
78 for a in range(0,len(datalines)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
79 aline = datalines[a].strip()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
80 listdta.append(datalines[a].strip().split(' ')) # notice space
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
81
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
82 #numbers in dta file to list
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
83 if len(listdta[a]) == 3:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
84 sublist.append(float(listdta[a][0]))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
85 sublist.append(float(listdta[a][1]))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
86 sublist.append(float(listdta[a][2]))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
87 listdata.append(sublist)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
88 sublist = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
89
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
90 #write header of dta file to outputfile
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
91 else:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
92 output5.write("%s\n"%(aline))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
93
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
94 datapoints.close()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
95 return listdata
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
96
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
97 # Function to calculate the values in the Z rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
98 def Rz_matrix(z_angle): # Rz rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
99 return [[cos(math.radians(z_angle)), -sin(math.radians(z_angle)), 0.0],[sin(math.radians(z_angle)),cos(math.radians(z_angle)),0.0],[0.0, 0.0, 1.0]]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
100
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
101 # Function to calculate the new coordinates rotated with Z rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
102 def Z_rotation(point2, z_angle): # multiplication rotation matrix and coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
103 r_z = Rz_matrix(z_angle)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
104 rotated_z = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
105 for i in range(3):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
106 rotated_z.append((sum([r_z[i][j] * point2[j] for j in range(3)])))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
107 return rotated_z
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
108
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
109 # Function to calculate the values in the X rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
110 def Rx_matrix(x_angle): #rotation matrix x-axis
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
111 return [[1, 0, 0],[0,cos(math.radians(x_angle)),-sin(math.radians(x_angle))],[0,sin(math.radians(x_angle)),cos(math.radians(x_angle))]]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
112
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
113 # Function to calculate the new coordinates rotated with X rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
114 def X_rotation(point3, x_angle): #multiplication rotation matrix and coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
115 r_x = Rx_matrix(x_angle)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
116 rotated_x = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
117 for i in range(3):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
118 rotated_x.append((sum([r_x[i][j] * point3[j] for j in range(3)])))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
119 return rotated_x
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
120
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
121 # Function to calculate the values in the Y rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
122 def Ry_matrix(y_angle): # Ry rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
123 return [[cos(math.radians(y_angle)), 0.0, sin(math.radians(y_angle))],[0.0, 1.0, 0.0],[-sin(math.radians(y_angle)),0.0, cos(math.radians(y_angle))]]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
124
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
125 # Function to calculate the new coordinates rotated with Y rotation matrix
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
126 def Y_rotation(point4, y_angle): #multiplication rotation matrix and coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
127 r_y = Ry_matrix(y_angle)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
128 rotated_y = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
129 for i in range(3):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
130 rotated_y.append((sum([r_y[i][j] * point4[j] for j in range(3)])))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
131 return rotated_y
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
132
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
133 # Function shift object to zeropoint
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
134 def shift_to_zero(listdata, sym1,sym2,sym3, listmirror, listspec, side,output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
135 # shifting object that sym3 point will be the zeropoint #
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
136 zeropoint = int(sym3)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
137 co_zeropoint = listdata[zeropoint] #coordinates of sym3
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
138
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
139 listdata2 = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
140 listdata3 = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
141
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
142 # minus x,y,z of point shift to zeropoint of every point
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
143 for x in range(0,len(listdata)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
144 listdata2.append(listdata[x][0] - co_zeropoint[0])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
145 listdata2.append(listdata[x][1] - co_zeropoint[1])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
146 listdata2.append(listdata[x][2] - co_zeropoint[2])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
147 listdata3.append(listdata2)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
148 listdata2 = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
149 rotate_z_axis(listdata3,sym1,sym2,sym3, listmirror, listspec, side, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
150
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
151
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
152 # Function for rotating the object around z axis
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
153 def rotate_z_axis(listdata3, sym1,sym2,sym3, listmirror, listspec, side, output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
154 # If object is upside down, 180 degrees rotation around the Z axis is needed.#
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
155 listdatatemp = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
156
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
157 if listdata3[sym1][1] < listdata3[sym3][1]:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
158 angle = 180
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
159 for coordinates in range(0,len(listdata3)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
160 listdatatemp.append(Z_rotation(listdata3[coordinates], angle))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
161 listdata3 = listdatatemp
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
162
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
163
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
164 # calculate angle rotation z
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
165 len_z_a = listdata3[int(sym1)][0] - listdata3[int(sym3)][0]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
166 len_z_b = listdata3[int(sym1)][1] - listdata3[int(sym3)][1]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
167 z_angle = (math.degrees(math.atan(len_z_a/len_z_b)))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
168
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
169 # calculate new coordinates with rotation matrix of Z
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
170 listdata4 = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
171 for coordinates in range(0, len(listdata3)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
172 listdata4.append(Z_rotation(listdata3[coordinates], z_angle))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
173
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
174 rotate_x_axis(listdata4,sym1,sym2,sym3, listmirror, listspec, side, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
175
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
176 # Function for rotating the object around x axis
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
177 def rotate_x_axis(listdata4,sym1,sym2,sym3, listmirror, listspec, side, output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
178 #calculate angle rotation x
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
179 len_x_a = listdata4[int(sym1)][2] - listdata4[int(sym3)][0]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
180 len_x_b = listdata4[int(sym1)][1] - listdata4[int(sym3)][0]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
181 x_angle = -(math.degrees(math.atan(len_x_a/len_x_b)))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
182
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
183 # calculate new coordinates with rotation matrix of X
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
184 listdata5 = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
185 for d in range(0, len(listdata4)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
186 listdata5.append(X_rotation(listdata4[d], x_angle))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
187
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
188 rotate_y_axis(listdata5,sym1,sym2,sym3, listmirror, listspec, side, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
189
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
190 # Function for rotating the object around y axis
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
191 def rotate_y_axis(listdata5,sym1,sym2,sym3,listmirror, listspec, side, output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
192 #calculate angle rotation y
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
193 len_y_a = (listdata5[int(sym2)][0] - listdata5[int(sym3)][0])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
194 len_y_b = (listdata5[int(sym2)][2] - listdata5[int(sym3)][2])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
195 y_angle = -(math.degrees(math.atan(len_y_a/len_y_b)))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
196
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
197 # calculate new coordinates with rotation matrix of Y
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
198 listdata6 = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
199 for d in range(0, len(listdata5)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
200 listdata6.append(Y_rotation(listdata5[d], y_angle))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
201
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
202
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
203 #Rotate 180 degrees around y axis when object is backwards.#
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
204 listdatatemp = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
205 if listdata6[sym2][0] < listdata6[(int(listmirror[0][0]))][0]: #point sym2_x < point mirror 1_x
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
206 angle = 180
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
207 for coordinates in range(0,len(listdata6)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
208 listdatatemp.append(Y_rotation(listdata6[coordinates], angle))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
209 listdata6 = listdatatemp
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
210 write_rotate_co(listdata6)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
211
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
212 # to correcting the landmarks
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
213 if len(listspec) == 0:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
214 correct_landmarks(listdata6, listmirror, side, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
215 else:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
216 correct_specs(listdata6, listmirror, listspec, output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
217
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
218 # Function writing rotated coordinates to file
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
219 def write_rotate_co(listdata6):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
220 file_outputname4 = sys.argv[4]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
221 output4 = open(file_outputname4, 'w')
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
222 #writing new coordinates to outputfile
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
223 for x in range(0,len(listdata6)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
224 output4.write("%.7f\t%.7f\t%.7f\n"%(listdata6[x][0], listdata6[x][1], listdata6[x][2]))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
225
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
226 #Function for correcting the landmarks
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
227 def correct_landmarks(listdata6, listmirror, side,output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
228
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
229 percentage_distance = []
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
230 #calculate marge of distance
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
231 for x in range(0,len(listmirror)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
232 left = listmirror[x][0] #left landmark
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
233 right = listmirror[x][1] #right landmark
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
234 co_left = listdata6[int(left)-1] #left landmark coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
235 co_left_x = co_left[0] # left landmark coordinate x
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
236 co_right = listdata6[int(right)-1]#right landmark coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
237 co_right_x = co_right[0] #right landmark coordinate x
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
238 distance_x = float(co_left[0]) + float(co_right[0])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
239
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
240 # if left is correct
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
241 if int(side) == 0:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
242 percentage_distance.append(abs(distance_x) / abs(co_left_x)) #percentage
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
243
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
244 # if right is correct
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
245 elif int(side) == 1:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
246 percentage_distance.append(abs(distance_x) / abs(co_right_x)) #percentage
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
247 else:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
248 print 'something wrong'
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
249
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
250
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
251 mean_percentage = numpy.mean(percentage_distance) #mean of percentages
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
252 std_percentage = numpy.std(percentage_distance)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
253 #range of correct values
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
254 left_range = mean_percentage - std_percentage #left range mean minus one standard deviation
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
255 right_range = mean_percentage + std_percentage # rigth range mean plus one standard deviation
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
256
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
257 #correcting the landmarks coordinates#
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
258 for x in range(0,len(listmirror)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
259 left = listmirror[x][0]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
260 right = listmirror[x][1]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
261 co_left = listdata6[int(left)-1]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
262 co_right = listdata6[int(right)-1]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
263 #if there is to much deviation take correct coordinate and project it to the other side.
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
264 if (percentage_distance[x] > right_range) or (percentage_distance[x] < left_range):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
265
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
266 #if left side is correct
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
267 if int(side) == 0:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
268 listdata6[int(right)-1][0] = float(co_left[0]) * -1
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
269 listdata6[int(right)-1][1] = float(co_left[1])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
270 listdata6[int(right)-1][2] = float(co_left[2])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
271 #if right side is correct
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
272 if int(side) == 1:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
273 listdata6[int(left)-1][0] = float(co_right[0]) * -1
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
274 listdata6[int(left)-1][1] = float(co_right[1])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
275 listdata6[int(left)-1][2] = float(co_right[2])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
276 write_output(listdata6,output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
277
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
278 #Function for correcting landmarks, defined in specs
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
279 def correct_specs(listdata6, listmirror, listspec,output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
280 for x in range(0,len(listspec)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
281 number = listspec[x][0]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
282 for spec in range(0, len(listmirror)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
283 try:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
284 pos = listmirror[spec].index(number) #find number in mirror list
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
285 #extracting the opposite landmark number
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
286 if pos == 0:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
287 number2 = listmirror[spec][1]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
288 elif pos == 1 :
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
289 number2 = listmirror[spec][0]
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
290 else:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
291 print 'wrong'
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
292 #replace the wrong coordinates
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
293 listdata6[int(number)-1][0] = float(listdata6[int(number2)-1][0])*-1
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
294 listdata6[int(number)-1][1] = float(listdata6[int(number2)-1][1])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
295 listdata6[int(number)-1][2] = float(listdata6[int(number2)-1][2])
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
296 except:
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
297 continue
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
298 write_output(listdata6,output5)
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
299
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
300 #Function for writing the corrected landmarks to outputfile
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
301 def write_output(listdata6,output5):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
302
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
303 for x in range(0,len(listdata6)):
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
304 output5.write("%.7f %.7f %.7f\n"%(listdata6[x][0], listdata6[x][1], listdata6[x][2]))
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
305 output5.close()
60ed96f5706e Uploaded
mb2013
parents:
diff changeset
306 main()