annotate Rotate.py @ 17:27af4a7b1e1d draft

Uploaded
author mb2013
date Tue, 20 May 2014 03:29:26 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
1 # Rotate ply file to normalized position
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
2 # based at points on symmetry axis
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
3 # MB
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
4
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
5 import math
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
6 from math import *
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
7 import sys
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
8 import numpy
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
9
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
10 # Main function
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
11 def main():
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
12 # input user .ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
13 name_file_ply = sys.argv[1]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
14 # input user symmetry points file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
15 sym_points_file = sys.argv[2]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
16
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
17 # to function 'extracting header', return header features
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
18 var_header,var_vertex_nm,var_face_nm, var_col = extracting_header(name_file_ply)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
19
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
20 # to function 'extracting symmetry points', return symmetry points and list with new vertex
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
21 listsym,listtotal_vertex = extracting_symmetry_points(sym_points_file)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
22
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
23 # to function 'extracting coordiantes', return list vertex and colors
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
24 listtotal_vertex, listtotal_colors = extracting_coordinates(name_file_ply,
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
25 var_header,var_vertex_nm,var_face_nm,
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
26 listtotal_vertex, var_col)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
27 # to function 'shift to zero'
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
28 shift_to_zero(listsym,listtotal_vertex)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
29
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
30 # to function 'write ply file'
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
31 write_ply_file(name_file_ply, var_header,var_vertex_nm,var_face_nm,
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
32 listtotal_colors, listtotal_vertex)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
33
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
34
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
35 # Function to extract all the values of the header of the ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
36 def extracting_header(name_file_ply):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
37 file_ply = open(name_file_ply) # open ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
38 var_col = 0 # color variable
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
39 count = 0 # counter
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
40
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
41 # for the first lines of the ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
42 for line in range(0, 40):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
43 readheader = file_ply.readline().strip().split()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
44 if len(readheader) != 0:
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
45 if readheader[0] == 'end_header':
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
46 var_header = count
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
47
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
48 # If there are colors in the ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
49 if 'red' in readheader:
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
50 var_col += 1
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
51 if 'blue' in readheader:
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
52 var_col += 1
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
53 if 'green' in readheader:
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
54 var_col += 1
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
55
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
56 # extracting number of vertexen
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
57 if readheader[0] == "element" and readheader[1] == "vertex":
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
58 var_vertex_nm = readheader[2]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
59
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
60 # extracting number of faces
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
61 if readheader[0] == "element" and readheader[1] == "face":
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
62 var_face_nm = readheader[2]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
63 count += 1
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
64 else:
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
65 continue
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
66
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
67 file_ply.close() # closing ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
68 return var_header, var_vertex_nm,var_face_nm, var_col # return values to main
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
69
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
70 # Function for extracting the values of the symmetry points file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
71 def extracting_symmetry_points(sym_points_file):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
72 sympoints = open(sym_points_file) # open symmetry file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
73 symlines = sympoints.readlines() # read all the lines of file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
74 listsym = [] # symmetry points
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
75 listtotal_vertex = [] # vertexen
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
76 # every line in the sym file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
77 for sym in range(0,len(symlines)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
78 symline_1 = symlines[sym].strip().split()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
79 listsym.append(symlines[sym].strip().split()) #add symmetry points
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
80 listtotal_vertex.append(symlines[sym].strip().split()) # add symmetry points
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
81
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
82 sympoints.close() # close symmetry file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
83 return listsym,listtotal_vertex # return symmetry points list and vertex list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
84
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
85
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
86 # Function to extract the coordinates of the ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
87 def extracting_coordinates(name_file_ply, var_header,var_vertex_nm,var_face_nm, listtotal_vertex, var_col):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
88 file1 = open(name_file_ply) # open ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
89
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
90 sub_list = [] #sublist coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
91 listtotal_colors = [] # color list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
92 sub_colors = [] # sublist color
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
93
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
94 # coordinates of ply file to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
95 for coordinates_ln in range(0, (int(var_header) + int(var_vertex_nm) + int(var_face_nm) + 1)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
96 line = file1.readline().strip().split()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
97 if (int(var_header) < coordinates_ln < (int(var_vertex_nm)+ int(var_header)+1)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
98 rayx = sub_list.append(line[0])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
99 rayy = sub_list.append(line[1])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
100 rayz = sub_list.append(line[2])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
101 listtotal_vertex.append(sub_list) #x y z coordinates to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
102 sub_list = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
103 if var_col != 0: #if color code is not 0
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
104 color_x = sub_colors.append(line[3])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
105 color_y = sub_colors.append(line[4])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
106 color_z = sub_colors.append(line[5])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
107 listtotal_colors.append(sub_colors) #colors to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
108 sub_colors = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
109
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
110 return listtotal_vertex, listtotal_colors # return coordinates (vertexen) and color lists
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
111
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
112
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
113 # Function to calculate the values in the Z rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
114 def Rz_matrix(z_angle): # Rz rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
115 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]]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
116
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
117 # Function to calculate the new coordinates rotated with Z rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
118 def Z_rotation(point2, z_angle): # multiplication rotation matrix and coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
119 r_z = Rz_matrix(z_angle)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
120 rotated_z = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
121 for i in range(3):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
122 rotated_z.append((sum([r_z[i][j] * point2[j] for j in range(3)])))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
123 return rotated_z
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
124
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
125 # Function to calculate the values in the X rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
126 def Rx_matrix(x_angle): #rotation matrix x-axis
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
127 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))]]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
128
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
129 # Function to calculate the new coordinates rotated with X rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
130 def X_rotation(point3, x_angle): #multiplication rotation matrix and coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
131 r_x = Rx_matrix(x_angle)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
132 rotated_x = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
133 for i in range(3):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
134 rotated_x.append((sum([r_x[i][j] * point3[j] for j in range(3)])))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
135 return rotated_x
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
136
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
137 # Function to calculate the values in the Y rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
138 def Ry_matrix(y_angle): # Ry rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
139 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))]]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
140
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
141 # Function to calculate the new coordinates rotated with Y rotation matrix
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
142 def Y_rotation(point4, y_angle): #multiplication rotation matrix and coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
143 r_y = Ry_matrix(y_angle)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
144 rotated_y = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
145 for i in range(3):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
146 rotated_y.append((sum([r_y[i][j] * point4[j] for j in range(3)])))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
147 return rotated_y
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
148
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
149 #Function to shift the object to the zeropoint
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
150 def shift_to_zero(listsym, listtotal_vertex):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
151 sym3 = listsym[2]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
152 zeropoint = sym3
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
153 list2 = [] # sublist coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
154 listdata3 = [] # new coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
155 # every coordinate minus the sym3 coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
156 for vertex in range(0,len(listtotal_vertex)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
157 list2.append(float(listtotal_vertex[vertex][0]) - float(zeropoint[0]))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
158 list2.append(float(listtotal_vertex[vertex][1]) - float(zeropoint[1]))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
159 list2.append(float(listtotal_vertex[vertex][2]) - float(zeropoint[2]))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
160 listdata3.append(list2) # add new coordinates to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
161 list2 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
162
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
163 # to function 'rotate z axis'
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
164 rotate_z_axis(listdata3)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
165
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
166 # Function for rotating the object around z axis
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
167 def rotate_z_axis(listdata3):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
168 # If object is upside down, 180 degrees rotation around the Z axis is needed.
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
169 listdatatemp = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
170 # check if object is upside down
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
171 if listdata3[0][1] < listdata3[2][1]:
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
172 angle = 180
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
173 # rotate 180 degrees.
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
174 for coordinates in range(0,len(listdata3)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
175 listdatatemp.append(Z_rotation(listdata3[coordinates], angle))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
176 listdata3 = listdatatemp # new coordinates
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
177
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
178
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
179 # calculate angle rotation z
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
180 len_z_a = listdata3[0][0] - listdata3[2][0]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
181 len_z_b = listdata3[0][1] - listdata3[2][1]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
182 z_angle = (math.degrees(math.atan(len_z_a/len_z_b)))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
183
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
184 # calculate new coordinates with rotation matrix of Z
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
185 listdata4 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
186 for coordinates in range(0, len(listdata3)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
187 listdata4.append(Z_rotation(listdata3[coordinates], z_angle)) # add new coordinates to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
188 listdata3 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
189
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
190 # to function 'rotate x axis'
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
191 rotate_x_axis(listdata4)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
192
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
193 # Function for rotating the object around x axis
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
194 def rotate_x_axis(listdata4):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
195 #calculate angle rotation x
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
196 len_x_a = listdata4[0][2] - listdata4[2][0]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
197 len_x_b = listdata4[0][1] - listdata4[2][0]
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
198 x_angle = -(math.degrees(math.atan(len_x_a/len_x_b)))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
199
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
200 # calculate new coordinates with rotation matrix of X
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
201 listdata5 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
202 for coordinates in range(0, len(listdata4)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
203 listdata5.append(X_rotation(listdata4[coordinates], x_angle)) # add new coordinates to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
204 listdata4 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
205
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
206 # to function 'rotate y axis'
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
207 rotate_y_axis(listdata5)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
208
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
209 # Function for rotating the object around y axis
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
210 def rotate_y_axis(listdata5):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
211 #calculate angle rotation y
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
212 len_y_a = (listdata5[1][0] - listdata5[2][0])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
213 len_y_b = (listdata5[1][2] - listdata5[2][2])
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
214 y_angle = -(math.degrees(math.atan(len_y_a/len_y_b)))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
215
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
216 # calculate new coordinates with rotation matrix of Y
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
217 listdata6 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
218 for coordinates in range(0, len(listdata5)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
219 listdata6.append(Y_rotation(listdata5[coordinates], y_angle))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
220
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
221 listdata5 = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
222 #Rotate 180 degrees around y axis when object is backwards.#
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
223 listdatatemp = []
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
224 if listdata6[1][0] < listdata6[3][0]: #point sym2_x < point sym 3
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
225 angle = 180
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
226 for coordinates in range(0,len(listdata6)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
227 listdatatemp.append(Y_rotation(listdata6[coordinates], angle)) # add new coordinates to list
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
228 listdata6 = listdatatemp
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
229
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
230 # to function 'write new coordinates'
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
231 write_new_coordinates(listdata6)
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
232
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
233 # Function write the new coordinates to outputfile.
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
234 def write_new_coordinates(listdata6):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
235 file_outputname4 = 'outputrotate_points.ply' # sub outputfile
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
236 output4 = open(file_outputname4, 'w')
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
237
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
238 # write every coordinate to output file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
239 for line in range(0,len(listdata6)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
240 output4.write("%.7f %.7f %.7f\n"%(listdata6[line][0], listdata6[line][1], listdata6[line][2]))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
241
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
242 # Function to write the new ply file with
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
243 def write_ply_file(name_file_ply, var_header,var_vertex_nm,var_face_nm, listtotal_colors, listtotal_vertex):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
244 outfile_rotating2 = open(sys.argv[3], 'w') #create new file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
245 pointsfile = open('outputrotate_points.ply', 'r') #new points
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
246 file2= open(name_file_ply) #original ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
247 counter = 0 # counter for color
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
248
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
249 # writing all the parts of the new ply file
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
250 for line in range(0,(int(var_header) + int(var_vertex_nm) + int(var_face_nm) + 1)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
251 line2 = file2.readline()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
252 readline2 = line2.strip().split()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
253 if line <= (int(var_header)): #header writing
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
254 outfile_rotating2.write('%s'%(line2))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
255 if line == int(var_header): #new coordinates writing
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
256 line2 = file2.readline()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
257 readline2 = line2.strip().split()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
258 for vertex in range(0,len(listtotal_vertex)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
259 line3 = pointsfile.readline().strip()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
260 if vertex >= 4: #from point 4, because they were extra edit to the list in the beginning
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
261 outfile_rotating2.write('%s %s %s %s\n'%
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
262 (line3, listtotal_colors[counter][0],
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
263 listtotal_colors[counter][1], listtotal_colors[counter][2]))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
264 counter += 1
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
265 if line == (int(var_vertex_nm) + int(var_header)-1): #write the faces
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
266 for face in range(0, int(var_face_nm)):
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
267 line2 = file2.readline()
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
268 outfile_rotating2.write('%s'%(line2))
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
269 counter = 0
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
270
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
271 outfile_rotating2.close() # outputfile close
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
272
27af4a7b1e1d Uploaded
mb2013
parents:
diff changeset
273 main()