Mercurial > repos > mb2013 > nepenthes_3dpca
comparison ReConstructor.py @ 13:fc4bc4ab91b7 draft
Uploaded
author | mb2013 |
---|---|
date | Tue, 20 May 2014 03:28:13 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
12:f69fc89bb392 | 13:fc4bc4ab91b7 |
---|---|
1 # Reconstruction of faces of a .ply file, | |
2 # based on the symmetry plane | |
3 # MB | |
4 import math | |
5 from math import * | |
6 import sys | |
7 import numpy | |
8 from time import gmtime, strftime | |
9 | |
10 # Function main, | |
11 def main(): | |
12 var_col = 0 | |
13 name_file_ply = sys.argv[1] | |
14 output = open('new_coordinates2.ply', 'w') | |
15 file_ply = open(name_file_ply) | |
16 out_log = open(str(sys.argv[15]), 'w') | |
17 | |
18 out_log.write('Start time: %s\n\nInput user:\n'%(time())) | |
19 x_1 = sys.argv[9] | |
20 x_2 = sys.argv[10] | |
21 y_1 = sys.argv[11] | |
22 y_2 = sys.argv[12] | |
23 z_1 = sys.argv[13] | |
24 z_2 = sys.argv[14] | |
25 # to function 'user input side' | |
26 side = user_input_side(out_log) | |
27 # to function 'user input sections' | |
28 number_of_boxes_hor,number_of_boxes_ver,number_of_boxes_z = user_input_sections(out_log) | |
29 # to function 'user input factor stdev' | |
30 factor = user_input_factor_stdev(out_log) | |
31 # to function user input colors | |
32 col_r, col_g, col_b = user_input_colors(out_log) | |
33 # to function 'extracting header' | |
34 var_header, var_vertex_nm,var_face_nm = extracting_header(file_ply) | |
35 # to funtion 'array coordinates' | |
36 matrix, matrix2,matrix3 = array_coordinates(name_file_ply, var_vertex_nm, var_header, var_face_nm) | |
37 # to function 'calc min max' | |
38 amin,amax = calc_min_max(out_log, matrix, x_1,x_2,y_1,y_2,z_1,z_2) | |
39 | |
40 # to function 'range sections' | |
41 steps_x,steps_y,steps_z,x_window_min_x,x_window_max_x = range_sections(amin,amax, | |
42 number_of_boxes_hor, number_of_boxes_ver, | |
43 number_of_boxes_z) | |
44 # to function 'create list ranges' | |
45 newlist = create_list_ranges(amin,amax,number_of_boxes_hor, number_of_boxes_ver, | |
46 number_of_boxes_z, x_window_min_x, x_window_max_x,steps_x,steps_y,steps_z) | |
47 # to function 'create list coordinates' | |
48 newlist2, indexlist = create_list_coordinates(newlist) | |
49 # to function 'fill sections coordinates' | |
50 newlist, indexlist = fill_sections_coordinates(newlist, newlist2, matrix, indexlist) | |
51 # to function 'calc sections' | |
52 difference, differencemean = calc_sections(number_of_boxes_ver,number_of_boxes_hor,number_of_boxes_z,newlist) | |
53 # to function 'calc mean stdev' | |
54 mean_percentage, std_percentage = calc_mean_stdev(differencemean,factor) | |
55 # to function 'calc range' | |
56 left_range, right_range = calc_range(mean_percentage,std_percentage) | |
57 # to function 'collect mirror values' | |
58 total, listindexcount, listindex, listindextotalcount, totalcount = collect_mirror_values(out_log, side, output, | |
59 right_range, left_range, | |
60 mean_percentage, std_percentage, | |
61 newlist, indexlist, difference, | |
62 number_of_boxes_hor,number_of_boxes_ver,number_of_boxes_z, | |
63 col_r, col_g, col_b) | |
64 | |
65 # to function 'find original faces' | |
66 m_face,m_face_square = find_original_faces(matrix2,matrix3, listindex) | |
67 | |
68 # to function 'replace index values' | |
69 out_faces = replace_index_values(m_face,listindex,listindextotalcount) | |
70 out_faces_2 = [] | |
71 # if there are square faces, do function replace index values again | |
72 if len(m_face_square)!= 0: | |
73 out_faces_2 = replace_index_values(m_face_square,listindex,listindextotalcount) | |
74 output.close() | |
75 | |
76 # to function 'write output' | |
77 write_output(name_file_ply, out_log, totalcount, var_header, var_vertex_nm, var_face_nm,out_faces, out_faces_2) | |
78 #a = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) | |
79 | |
80 out_log.write('End time: %s'%(time())) | |
81 out_log.close() # close log | |
82 | |
83 # Function time | |
84 def time(): | |
85 a = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) | |
86 return a | |
87 | |
88 # Function user input side | |
89 def user_input_side(out_log): # user input side | |
90 side = int(sys.argv[3]) | |
91 if side == 0: | |
92 out_log.write('Left side of object is correct') | |
93 else: | |
94 out_log.write('Right side of object is correct') | |
95 return side | |
96 | |
97 # Function user input sections | |
98 def user_input_sections(out_log): | |
99 # number of boxes on x axis, this will be multiplied. | |
100 number_of_boxes_hor = int(sys.argv[4]) | |
101 # number of boxes on y axis | |
102 number_of_boxes_ver = int(sys.argv[5]) | |
103 # number of boxes on z axis | |
104 number_of_boxes_z = int(sys.argv[6]) | |
105 out_log.write("number of boxes x axis:\t%s\nnumber of boxes y axis:\t%s\nnumber of boxes z axis:\t%s\n\n"% | |
106 ((number_of_boxes_hor *2),(number_of_boxes_ver), (number_of_boxes_z))) | |
107 | |
108 return number_of_boxes_hor, number_of_boxes_ver, number_of_boxes_z | |
109 | |
110 # Function user input colors of reconstructed surface | |
111 def user_input_colors(out_log): | |
112 col = sys.argv[8] | |
113 # four colors can be chosen | |
114 if int(col) == 0: | |
115 out_log.write('color is pink\n') | |
116 col_r = 236 | |
117 col_g = 149 | |
118 col_b = 221 | |
119 elif int(col) == 1: | |
120 out_log.write('color is blue\n') | |
121 col_r = 192 | |
122 col_g = 245 | |
123 col_b = 250 | |
124 elif int(col) == 2: | |
125 out_log.write('color is green\n') | |
126 col_r = 0 | |
127 col_g = 255 | |
128 col_b = 0 | |
129 else: | |
130 out_log.write('color is red\n') | |
131 col_r = 255 | |
132 col_g = 0 | |
133 col_b = 0 | |
134 | |
135 return col_r, col_g, col_b | |
136 | |
137 # Function user input factor standard deviation | |
138 def user_input_factor_stdev(out_log): | |
139 factor = int(sys.argv[7]) | |
140 out_log.write("Factor standard deviation:\t%s\n\n"%(factor)) | |
141 | |
142 return factor | |
143 | |
144 # Function extracting values of header ply file | |
145 def extracting_header(file_ply): | |
146 for x in range(0, 20): | |
147 readheader = file_ply.readline().strip().split() | |
148 if readheader[0] == 'end_header': # when the words 'end_header' are found | |
149 var_header = x # var_header is line number | |
150 if readheader[0] == "element" and readheader[1] == "vertex": # when 'element vertex' found | |
151 var_vertex_nm = readheader[2] # amount of vertexen | |
152 if readheader[0] == "element" and readheader[1] == "face": # when 'element face' found | |
153 var_face_nm = readheader[2] # amount of faces | |
154 file_ply.close() | |
155 | |
156 return var_header, var_vertex_nm,var_face_nm | |
157 | |
158 # Function extracting coordintates of ply file | |
159 def array_coordinates(name_file_ply, var_vertex_nm, var_header,var_face_nm): | |
160 file1 = open(name_file_ply) | |
161 matrix = numpy.zeros((int(var_vertex_nm),3)) # creating empty numpy array of amount of vertexen | |
162 matrix2 = numpy.empty((int(var_face_nm),3)) # creating empty numpy array of amount of triangle faces | |
163 matrix3 = numpy.empty((int(var_face_nm),4)) # creating empty numpy array of amounf of square faces 4 | |
164 count = 0 # counter for vertexen | |
165 count2 = 0 # counter for faces | |
166 # Putting all the vertexen and faces in a matrix | |
167 for a in range(0, (int(var_header) + int(var_vertex_nm) + int(var_face_nm) + 1)): | |
168 line = file1.readline().strip().split() # reading every line | |
169 if int(var_header) < a < (int(var_vertex_nm)+ int(var_header)+1): #vertexen | |
170 matrix[count][0] = float(line[0]) # x coordinate | |
171 matrix[count][1] = float(line[1]) # y coordinate | |
172 matrix[count][2] = float(line[2]) # z coordinate | |
173 count += 1 # counter + 1 for next line in matrix | |
174 if a > (int(var_vertex_nm)+ int(var_header)): # faces | |
175 if len(line) == 4: # if triangle faces exists | |
176 matrix2[count2][0] = int(line[1]) # first coordinate for face | |
177 matrix2[count2][1] = int(line[2]) # second coordinate for face | |
178 matrix2[count2][2] = int(line[3]) # third coordinate for face | |
179 #check1 = True | |
180 | |
181 | |
182 if len(line) == 5: # if square faces exists | |
183 matrix3[count2][0] = int(line[1]) # first coordinate for face | |
184 matrix3[count2][1] = int(line[2]) # second coordinate for face | |
185 matrix3[count2][2] = int(line[3]) # third coordinate for face | |
186 matrix3[count2][3] = int(line[4]) # fourth coordinate for face | |
187 #check2 = True | |
188 count2 += 1 # counter + 1 for next line in matrix | |
189 file1.close() | |
190 | |
191 # to function calc_histogram | |
192 calc_histogram(matrix) | |
193 | |
194 return matrix,matrix2,matrix3 | |
195 | |
196 # Function histogram | |
197 def calc_histogram(matrix): # histogram of x,y, z coordinates | |
198 out_histo = open(str(sys.argv[16]), 'w') # open output | |
199 x_co = numpy.array(matrix[:,0]) | |
200 histo_x = numpy.histogram(x_co, bins = 50) # calculate x coordinate distribution | |
201 y_co = numpy.array(matrix[:,1]) | |
202 histo_y = numpy.histogram(y_co, bins = 50) # calculate y coordinate distribution | |
203 z_co = numpy.array(matrix[:,2]) | |
204 histo_z = numpy.histogram(z_co, bins = 50) # calculate z coordinate distribution | |
205 | |
206 # write histogram output | |
207 out_histo.write("x nm point\tx coordinate\ty nm point\ty coordinate\tz nm point\tz coordinate\n") | |
208 for item in range(0,len(histo_x[0])): | |
209 out_histo.write("%s\t%s\t\t%s\t%s\t\t%s\t%s\n"%(histo_x[0][item], histo_x[1][item], | |
210 histo_y[0][item], histo_y[1][item], | |
211 histo_z[0][item], histo_z[1][item], )) | |
212 | |
213 | |
214 out_histo.close() #close histogram output | |
215 | |
216 # Function calculation minimum and maximum of x,y and z | |
217 def calc_min_max(out_log, matrix, x_1,x_2,y_1,y_2,z_1,z_2): | |
218 amin = numpy.amin(matrix, axis = 0) #minima of x y and z | |
219 amax = numpy.amax(matrix, axis = 0) #maxima of x y and z | |
220 # if user input max and min values, change the min and max | |
221 if x_1 != 'standard': | |
222 amin[0] = int(x_1) | |
223 if x_2 != 'standard': | |
224 amax[0] = int(x_2) | |
225 if y_1 != 'standard': | |
226 amin[1] = int(y_1) | |
227 if y_2 != 'standard': | |
228 amax[1] = int(y_2) | |
229 if z_1 != 'standard': | |
230 amin[2] = int(z_1) | |
231 if z_2 != 'standard': | |
232 amax[2] = int(z_2) | |
233 | |
234 | |
235 # write to log | |
236 out_log.write("lowest x:\t%s\nhighest x:\t%s\nlowest y:\t%s\nhighest y:\t%s\nlowest z:\t%s\nhighest z:\t%s\n\n" | |
237 %(amin[0],amax[0],amin[1],amax[1],amin[2],amax[2])) | |
238 | |
239 return amin, amax | |
240 | |
241 # Function calculating range for sections | |
242 def range_sections(amin,amax,number_of_boxes_hor, number_of_boxes_ver,number_of_boxes_z): | |
243 # Find the highest absolute x because the boxes on the x axis has to be mirrored in the symmetry plane | |
244 if abs(amax[0]) < abs(amin[0]): # if the highest x is absolute smaller than the lowest x: | |
245 x_window_min_x = amin[0] # lowest x is lowest windows x | |
246 x_window_max_x= amin[0] *-1 # lowest x * -1 is highest windows x | |
247 steps_x = abs(amin[0]) / number_of_boxes_hor # length of x boxes | |
248 else: | |
249 x_window_min_x = amax[0] *-1 # highest x *-1 is lowest windows x | |
250 x_window_max_x = amin[0] # highest x is highest window x | |
251 steps_x = abs(amax[0]) / number_of_boxes_hor # length of x boxes | |
252 | |
253 #Calculating range of y boxes | |
254 steps_y = (abs(amax[1]) + abs(amin[1])) / number_of_boxes_ver | |
255 | |
256 #Calculating range of z boxes | |
257 steps_z = (abs(amax[2]) + abs(amin[2])) / number_of_boxes_z | |
258 | |
259 return steps_x, steps_y, steps_z, x_window_min_x,x_window_max_x | |
260 | |
261 # Function create list ranges of each section | |
262 def create_list_ranges(amin,amax,number_of_boxes_hor, number_of_boxes_ver,number_of_boxes_z, | |
263 x_window_min_x, x_window_max_x,steps_x,steps_y,steps_z): # creat list with ranges of each section | |
264 #creating the list with the ranges for every section | |
265 y_window_min = amin[1] | |
266 y_window_max = amin[1] | |
267 z_window_min = amin[2] | |
268 newlist = [] | |
269 sublist = [] | |
270 y_window_min2 = y_window_min | |
271 z_window_min2 = z_window_min | |
272 | |
273 # From front to back, from floor to ceiling | |
274 for a in range(0,int(number_of_boxes_ver)): #number of rows | |
275 x_window_min = x_window_min_x | |
276 x_window_min2 = x_window_min_x + steps_x | |
277 y_window_min = y_window_min2 | |
278 y_window_min2+= steps_y | |
279 z_window_min = amin[2] | |
280 z_window_min2 = z_window_min | |
281 for c in range(0,int(number_of_boxes_z)): # in z direction | |
282 z_window_min = z_window_min2 | |
283 z_window_min2 += steps_z | |
284 x_window_min = x_window_min_x | |
285 x_window_min2 = x_window_min_x + steps_x | |
286 for b in range(0, int((2*number_of_boxes_hor))): #number of columns | |
287 sublist.append(x_window_min) | |
288 sublist.append(x_window_min2) | |
289 sublist.append(y_window_min) | |
290 sublist.append(y_window_min2) | |
291 sublist.append(z_window_min) | |
292 sublist.append(z_window_min2) | |
293 newlist.append(sublist) | |
294 sublist = [] | |
295 x_window_min = x_window_min2 | |
296 x_window_min2 += steps_x | |
297 | |
298 return newlist | |
299 | |
300 # Function create list coordinates in sections | |
301 def create_list_coordinates(newlist): # create list coordinates in sections | |
302 #empty the list but maintaining the structure | |
303 newlist2=[] | |
304 newlist2sub = [] | |
305 newlistsub = [] | |
306 for x in range(0,len(newlist)): | |
307 for y in range(5,-1,-1): | |
308 newlist2sub.append(newlist[x].pop(y)) | |
309 newlist2.append(newlist2sub) | |
310 newlist2sub = [] | |
311 | |
312 # creating empty index list for storing line index of coordinates | |
313 indexlist = [] | |
314 for x in range(0,len(newlist)): | |
315 indexlist.append([]) | |
316 | |
317 return newlist2, indexlist | |
318 | |
319 # Function fill list sections with coordinates | |
320 def fill_sections_coordinates(newlist,newlist2,matrix,indexlist): # fill list sections with coordinates | |
321 #Filling the sections with the coordinates | |
322 for y in range(0,len(newlist2)): #notice the ranges are in reverse order of each section | |
323 rows_vertex = (numpy.where((matrix[:,0]>= float(newlist2[y][5])) | |
324 & (matrix[:,0] < float(newlist2[y][4])) | |
325 & (matrix[:,1] >= float(newlist2[y][3])) | |
326 & (matrix[:,1] < float(newlist2[y][2])) | |
327 & (matrix[:,2] >= float(newlist2[y][1])) | |
328 & (matrix[:,2] < float(newlist2[y][0])))) | |
329 | |
330 vertex_new = matrix[list(rows_vertex)] | |
331 newlist[y].append((vertex_new)) | |
332 indexlist[y].append((rows_vertex)) | |
333 | |
334 return newlist, indexlist | |
335 | |
336 # Function calculate mean and number of coordinates sections | |
337 def calc_sections(number_of_boxes_ver,number_of_boxes_hor,number_of_boxes_z,newlist): | |
338 #Selecting the sections on the symmetry axis and calculating the mean of every section | |
339 counter = 0 | |
340 counter2 = 0 | |
341 counter3 = 0 | |
342 row_count = 0 | |
343 difference = [] #whit sections which are empty | |
344 differencemean = [] #without sections which are empty | |
345 total = 0 | |
346 for x in range(0, (int(number_of_boxes_ver)*int(number_of_boxes_hor) * int(number_of_boxes_z))): # through all the lists/sections | |
347 if counter % (int (number_of_boxes_hor)*2) == 0: | |
348 counter = 0 | |
349 if counter3 == (int(number_of_boxes_hor)): | |
350 counter2 += (int(number_of_boxes_hor)) | |
351 counter3 = 0 | |
352 | |
353 first_pos = (newlist[counter2][0]) # coordinates of left section | |
354 last_pos = (newlist[counter2 + (int(number_of_boxes_hor)*2 -1)- counter][0]) # coordinates of right section | |
355 if len(first_pos) == 0: # If there are no coordinates in a section | |
356 number_coordinates_box1 = len(first_pos) | |
357 mean1 = 2000 | |
358 mean3 = '' | |
359 | |
360 else: | |
361 number_coordinates_box1 = len(first_pos) | |
362 for e in range(0,len(first_pos)): # If there are coordinates in a section | |
363 total += float(first_pos[e][0]) | |
364 mean1 = total/len(first_pos) | |
365 mean3 = mean1 | |
366 total = 0 | |
367 | |
368 if len(last_pos) == 0: # If there are no coordinates in a section | |
369 number_coordinates_box2 = len(last_pos) | |
370 mean2 = 1000 | |
371 mean4 = '' | |
372 | |
373 else: | |
374 number_coordinates_box2 = len(last_pos) | |
375 | |
376 for e in range(0,len(last_pos)): # If there are coordinates in a section | |
377 total += float(last_pos[e][0]) | |
378 mean2 = total/len(last_pos) | |
379 mean4 = mean2 | |
380 total = 0 | |
381 number_coordinates_box1 = len(first_pos) | |
382 number_coordinates_box2 = len(last_pos) | |
383 | |
384 # if number of coordinates in sections deviate to much, | |
385 if (abs(number_coordinates_box1 - number_coordinates_box2) > (number_coordinates_box1 / 2.0) | |
386 ) and(abs(number_coordinates_box1 - number_coordinates_box2) > (number_coordinates_box2 / 2.0)): | |
387 mean1 = 3000 | |
388 mean2 = 4000 | |
389 | |
390 difference.append(float(abs(mean2 + mean1))) #the means of every sections | |
391 counter += 2 | |
392 counter2 += 1 | |
393 counter3 += 1 | |
394 | |
395 try: | |
396 differencemean.append(abs(mean4 + mean3)) # the mean without empty sections | |
397 except: | |
398 continue | |
399 | |
400 return difference, differencemean | |
401 | |
402 # Function calculating the mean and the standard deviation | |
403 def calc_mean_stdev(differencemean, factor): | |
404 mean_percentage = numpy.mean(differencemean) #calculating the mean without empty sections | |
405 std_percentage = numpy.std(differencemean) # calculating the standard deviation of the list without the empty sections | |
406 std_percentage = std_percentage * factor | |
407 | |
408 return mean_percentage, std_percentage | |
409 | |
410 # Function calculating range of accepted differences | |
411 def calc_range(mean_percentage,std_percentage): | |
412 left_range = float(mean_percentage) - float(std_percentage) #left range mean minus one standard deviation | |
413 right_range = float(mean_percentage) + (float(std_percentage) )# right range mean plus one standard deviation | |
414 if left_range > 0: | |
415 left_range = 0 | |
416 return left_range, right_range | |
417 | |
418 # Function collecting incorrect coordinates | |
419 def collect_mirror_values(out_log, side, output, right_range, left_range,mean_percentage, | |
420 std_percentage, newlist, indexlist,difference, | |
421 number_of_boxes_hor,number_of_boxes_ver,number_of_boxes_z, | |
422 col_r, col_g, col_b): | |
423 | |
424 # Collecting the values of the sections which have to be mirrored | |
425 out_log.write('mean x difference:\t%s\nstandarddeviation mean x difference:\t%s\nrange:\t%s - %s\n\n'% | |
426 ((mean_percentage),(std_percentage),(left_range),(right_range))) | |
427 y = 0 | |
428 counter = 0 | |
429 counter2 = 0 | |
430 sub = [] | |
431 total = [] | |
432 left_range = "%.10f"%(left_range) | |
433 indexcount = 0 | |
434 facecount = 0 | |
435 totalcount = 0 | |
436 listindex = [] | |
437 listindexsub = [] | |
438 listindexcount = [] | |
439 listindextotalcount = [] | |
440 | |
441 for x in range(0, len(difference)): | |
442 difference_x = "%.10f"%(difference[x]) | |
443 if counter %(int(number_of_boxes_hor)) == 0 and x != 0: | |
444 counter = 0 | |
445 counter2 += (int(number_of_boxes_hor)) | |
446 counter += 1 | |
447 counter2 += 1 #left side counter | |
448 | |
449 # if left side of the object is correct | |
450 if side == 0: | |
451 if (float(difference_x) < float(left_range)) or (float(difference_x) > float(right_range)): | |
452 if len(newlist[counter2 -1][0]) != 0: | |
453 sub = (newlist[counter2 -1][0]) #coordinates in list | |
454 for y in range(0, len(sub)): | |
455 i = sub[y] | |
456 index_a = indexlist[counter2 -1][0][0][y] | |
457 try: | |
458 listindex.append(int(index_a)) # index number | |
459 listindextotalcount.append(int(totalcount)) | |
460 listindexsub.append(int(index_a)) # index number | |
461 listindexsub.append(totalcount) # index number of new coordinate | |
462 | |
463 except: | |
464 continue | |
465 listindexcount.append(listindexsub) # index number original | |
466 listindexsub = [] | |
467 number= float(sub[y][0]) * -1 # x coordinate * -1 for mirroring at symmetry plane | |
468 sub[y][0] = number | |
469 total.append(sub[y]) | |
470 totalcount += 1 | |
471 # writing new coordinates to output | |
472 output.write('%s %s %s %s %s %s\n'%(sub[y][0], float(sub[y][1]), float(sub[y][2]),(col_r), (col_g), (col_b))) | |
473 | |
474 | |
475 # if right side of the object is correct | |
476 if side == 1: | |
477 dif = (int(number_of_boxes_hor)) - counter | |
478 counter3 = counter2 + (dif*2) + 1 # right side counter | |
479 | |
480 if (float(difference[x] )> float(right_range)) or (float(difference[x]) < float(left_range)): | |
481 if len(newlist[counter3 -1][0]) != 0: | |
482 sub = (newlist[counter3 -1][0]) # coordinates in list | |
483 for y in range(0, len(sub)): | |
484 i = sub[y] | |
485 index_a = indexlist[counter3 -1][0][0][y] | |
486 try: | |
487 listindex.append(int(index_a))# index number | |
488 listindextotalcount.append(int(totalcount)) | |
489 listindexsub.append(int(index_a)) # index number | |
490 listindexsub.append(totalcount)# index number of new coordinate | |
491 except: | |
492 continue | |
493 listindexcount.append(listindexsub)# index number original | |
494 listindexsub = [] | |
495 number= float(sub[y][0]) * -1 # x coordinate * -1 for mirroring at symmetry plane | |
496 sub[y][0] = number | |
497 total.append(sub[y]) | |
498 totalcount += 1 | |
499 # writing new coordinates to output | |
500 output.write('%s %s %s %s %s %s\n'%(sub[y][0], float(sub[y][1]), float(sub[y][2]),(col_r), (col_g), (col_b))) | |
501 | |
502 return total, listindexcount, listindex, listindextotalcount,totalcount | |
503 | |
504 # Function extract original faces for corrected coordinates | |
505 def find_original_faces(matrix2, matrix3, listindex): | |
506 # finding the original faces of kopied points | |
507 ix = numpy.in1d(matrix2.ravel(),listindex).reshape(matrix2.shape) # finding where the listindex is the same as the matrix | |
508 rows, cols = numpy.where(ix) # finding index of the faces | |
509 m_face = matrix2[list(set(rows))] # extracting the rows with those faces | |
510 m_face_square = [] | |
511 # if square faces present | |
512 if matrix3[-1][0] != 0: | |
513 ix_square = numpy.in1d(matrix3.ravel(),listindex).reshape(matrix3.shape) # finding where the listindex is the same as the matrix | |
514 rows_square, cols3 = numpy.where(ix_square) # finding index of the faces | |
515 m_face_square = matrix3[list(set(rows_square))] # extracting the rows with those faces | |
516 return m_face, m_face_square | |
517 | |
518 # Function replacing index values | |
519 def replace_index_values(m_face,listindex,listindextotalcount): | |
520 c = [] | |
521 array_oi = numpy.array(m_face) | |
522 maxn = numpy.amax(array_oi) | |
523 palette = list(range(int(maxn))) | |
524 palette = numpy.array(palette) | |
525 key = numpy.array(listindex) | |
526 listindextotalcount = numpy.array(listindextotalcount) | |
527 | |
528 # sorting the lists which have to be replaced | |
529 order= numpy.argsort(key) # sorting of the array key | |
530 litc_sorted = listindextotalcount[order] # sorting the palette | |
531 key_sorted = key[order] # sorting the key | |
532 | |
533 palette2 = list(range(int(maxn))) # creating list with all possible values in it with max the highest number in the faces | |
534 key2 = [] | |
535 | |
536 counter = 0 | |
537 for item in range(0,int(maxn)): # make array containing what have to be changed | |
538 if (counter < len(key_sorted)) and item == (key_sorted[counter]): | |
539 key2.append(litc_sorted[counter]) | |
540 counter += 1 | |
541 else: | |
542 key2.append(numpy.nan) | |
543 | |
544 key2 = numpy.array(key2) # converten of list to array | |
545 index_f = numpy.digitize(array_oi.reshape(-1,), palette2)-1 # indexing which numbers has to be changed | |
546 out_sub =(key2[index_f].reshape(array_oi.shape)) # new array creating with changed values | |
547 out_faces = out_sub[~numpy.isnan(out_sub).any(axis=1)] # extracting the faces with not changed values, (nan) | |
548 | |
549 return out_faces | |
550 | |
551 # Function write output | |
552 def write_output(name_file_ply, out_log, totalcount, var_header, var_vertex_nm, var_face_nm,out_faces,out_faces_2): # write output | |
553 outfile2 = open(str(sys.argv[2]), 'w')#writing the output file | |
554 file1 = open(name_file_ply) | |
555 g = 0 | |
556 for d in range(0,(int(var_header) + int(var_vertex_nm) + int(var_face_nm) + 1)): | |
557 line2 = file1.readline().strip() | |
558 readline2 = line2.strip().split() | |
559 if readline2[0] == "element" and readline2[1] == "vertex": | |
560 outfile2.write("element vertex %s\n"%(int(var_vertex_nm) + totalcount)) #number of vertexen changing | |
561 | |
562 elif readline2[0] == "element" and readline2[1] == "face": | |
563 outfile2.write("element face %s\n"%(int(var_face_nm) + len(out_faces) + len(out_faces_2)))#number of faces changing | |
564 | |
565 elif (int(var_header) < d < (int(var_header) + int(var_vertex_nm))): #rotated vertexen, with original color code | |
566 outfile2.write('%s\n'%(line2)) | |
567 g += 1 | |
568 | |
569 elif (int(var_header) + int(var_vertex_nm)) == d: #for new vertexen, with color code 0,0,0 | |
570 outfile2.write('%s\n'%(line2)) #the last original vertex | |
571 g += 1 | |
572 with open('new_coordinates2.ply') as infile: | |
573 for line in infile: | |
574 outfile2.write(line) | |
575 | |
576 else: #everything left | |
577 outfile2.write('%s\n'%(line2)) | |
578 | |
579 #writing new faces to output | |
580 for z in range(0,len(out_faces)): | |
581 outfile2.write("3 %s %s %s\n"%((int(out_faces[z][0])+ int(var_vertex_nm)), | |
582 (int(out_faces[z][1])+ int(var_vertex_nm)), (int(out_faces[z][2])+ int(var_vertex_nm)))) | |
583 #writing new square faces to output | |
584 if len(out_faces_2) != 0: | |
585 for x in range(0,len(out_faces_2)): | |
586 outfile2.write("4 %s %s %s %s\n"%((int(out_faces_2[z][0])+ int(var_vertex_nm)), | |
587 (int(out_faces_2[z][1])+ int(var_vertex_nm)), (int(out_faces_2[z][2])+ int(var_vertex_nm)), | |
588 (int(out_faces_2[z][3] + int(var_vertex_nm))))) | |
589 | |
590 out_log.write('number of reconstructed faces:\t%s\n\n'%(len(out_faces) + len(out_faces_2))) | |
591 outfile2.close() | |
592 | |
593 | |
594 main() | |
595 | |
596 | |
597 |