| Previous changeset 12:f69fc89bb392 (2014-05-20) Next changeset 14:70fc3c02ca14 (2014-05-20) |
|
Commit message:
Uploaded |
|
added:
ReConstructor.py |
| b |
| diff -r f69fc89bb392 -r fc4bc4ab91b7 ReConstructor.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ReConstructor.py Tue May 20 03:28:13 2014 -0400 |
| [ |
| b"@@ -0,0 +1,597 @@\n+# Reconstruction of faces of a .ply file,\r\n+# based on the symmetry plane\r\n+# MB\r\n+import math\r\n+from math import *\r\n+import sys\r\n+import numpy\r\n+from time import gmtime, strftime\r\n+\r\n+# Function main, \r\n+def main():\r\n+ var_col = 0\r\n+ name_file_ply = sys.argv[1] \r\n+ output = open('new_coordinates2.ply', 'w')\r\n+ file_ply = open(name_file_ply)\r\n+ out_log = open(str(sys.argv[15]), 'w')\r\n+ \r\n+ out_log.write('Start time: %s\\n\\nInput user:\\n'%(time()))\r\n+ x_1 = sys.argv[9]\r\n+ x_2 = sys.argv[10]\r\n+ y_1 = sys.argv[11]\r\n+ y_2 = sys.argv[12]\r\n+ z_1 = sys.argv[13]\r\n+ z_2 = sys.argv[14]\r\n+ # to function 'user input side'\r\n+ side = user_input_side(out_log)\r\n+ # to function 'user input sections'\r\n+ number_of_boxes_hor,number_of_boxes_ver,number_of_boxes_z = user_input_sections(out_log)\r\n+ # to function 'user input factor stdev'\r\n+ factor = user_input_factor_stdev(out_log)\r\n+ # to function user input colors\r\n+ col_r, col_g, col_b = user_input_colors(out_log)\r\n+ # to function 'extracting header' \r\n+ var_header, var_vertex_nm,var_face_nm = extracting_header(file_ply)\r\n+ # to funtion 'array coordinates'\r\n+ matrix, matrix2,matrix3 = array_coordinates(name_file_ply, var_vertex_nm, var_header, var_face_nm)\r\n+ # to function 'calc min max'\r\n+ amin,amax = calc_min_max(out_log, matrix, x_1,x_2,y_1,y_2,z_1,z_2)\r\n+\r\n+ # to function 'range sections'\r\n+ steps_x,steps_y,steps_z,x_window_min_x,x_window_max_x = range_sections(amin,amax,\r\n+ number_of_boxes_hor, number_of_boxes_ver,\r\n+ number_of_boxes_z)\r\n+ # to function 'create list ranges'\r\n+ newlist = create_list_ranges(amin,amax,number_of_boxes_hor, number_of_boxes_ver,\r\n+ number_of_boxes_z, x_window_min_x, x_window_max_x,steps_x,steps_y,steps_z)\r\n+ # to function 'create list coordinates'\r\n+ newlist2, indexlist = create_list_coordinates(newlist)\r\n+ # to function 'fill sections coordinates'\r\n+ newlist, indexlist = fill_sections_coordinates(newlist, newlist2, matrix, indexlist)\r\n+ # to function 'calc sections'\r\n+ difference, differencemean = calc_sections(number_of_boxes_ver,number_of_boxes_hor,number_of_boxes_z,newlist)\r\n+ # to function 'calc mean stdev'\r\n+ mean_percentage, std_percentage = calc_mean_stdev(differencemean,factor)\r\n+ # to function 'calc range'\r\n+ left_range, right_range = calc_range(mean_percentage,std_percentage)\r\n+ # to function 'collect mirror values'\r\n+ total, listindexcount, listindex, listindextotalcount, totalcount = collect_mirror_values(out_log, side, output,\r\n+ right_range, left_range,\r\n+ mean_percentage, std_percentage,\r\n+ newlist, indexlist, difference,\r\n+ number_of_boxes_hor,number_of_boxes_ver,number_of_boxes_z,\r\n+ col_r, col_g, col_b)\r\n+\r\n+ # to function 'find original faces'\r\n+ m_face,m_face_square = find_original_faces(matrix2,matrix3, listindex)\r\n+ \r\n+ # to function 'replace index values'\r\n+ out_faces = replace_index_values(m_face,listindex,listindextotalcount)\r\n+ out_faces_2 = []\r\n+ # if there are square faces, do function replace index values again\r\n+ if len(m_face_square)!= 0:\r\n+ out_faces_2 = replace_index_values(m_face_square,listindex,listindextotalcount)\r\n+ output.close()\r\n+ \r\n+ # to function 'write output'\r\n+ write_output(name_file_ply, out_log,"..b' rows with those faces\r\n+ return m_face, m_face_square\r\n+\r\n+# Function replacing index values\r\n+def replace_index_values(m_face,listindex,listindextotalcount):\r\n+ c = []\r\n+ array_oi = numpy.array(m_face)\r\n+ maxn = numpy.amax(array_oi)\r\n+ palette = list(range(int(maxn)))\r\n+ palette = numpy.array(palette)\r\n+ key = numpy.array(listindex)\r\n+ listindextotalcount = numpy.array(listindextotalcount)\r\n+\r\n+ # sorting the lists which have to be replaced\r\n+ order= numpy.argsort(key) # sorting of the array key \r\n+ litc_sorted = listindextotalcount[order] # sorting the palette\r\n+ key_sorted = key[order] # sorting the key\r\n+\r\n+ palette2 = list(range(int(maxn))) # creating list with all possible values in it with max the highest number in the faces\r\n+ key2 = []\r\n+\r\n+ counter = 0\r\n+ for item in range(0,int(maxn)): # make array containing what have to be changed\r\n+ if (counter < len(key_sorted)) and item == (key_sorted[counter]):\r\n+ key2.append(litc_sorted[counter])\r\n+ counter += 1\r\n+ else:\r\n+ key2.append(numpy.nan)\r\n+ \r\n+ key2 = numpy.array(key2) # converten of list to array\r\n+ index_f = numpy.digitize(array_oi.reshape(-1,), palette2)-1 # indexing which numbers has to be changed\r\n+ out_sub =(key2[index_f].reshape(array_oi.shape)) # new array creating with changed values\r\n+ out_faces = out_sub[~numpy.isnan(out_sub).any(axis=1)] # extracting the faces with not changed values, (nan)\r\n+\r\n+ return out_faces\r\n+\r\n+# Function write output\r\n+def write_output(name_file_ply, out_log, totalcount, var_header, var_vertex_nm, var_face_nm,out_faces,out_faces_2): # write output \r\n+ outfile2 = open(str(sys.argv[2]), \'w\')#writing the output file\r\n+ file1 = open(name_file_ply)\r\n+ g = 0\r\n+ for d in range(0,(int(var_header) + int(var_vertex_nm) + int(var_face_nm) + 1)):\r\n+ line2 = file1.readline().strip()\r\n+ readline2 = line2.strip().split()\r\n+ if readline2[0] == "element" and readline2[1] == "vertex":\r\n+ outfile2.write("element vertex %s\\n"%(int(var_vertex_nm) + totalcount)) #number of vertexen changing\r\n+ \r\n+ elif readline2[0] == "element" and readline2[1] == "face":\r\n+ outfile2.write("element face %s\\n"%(int(var_face_nm) + len(out_faces) + len(out_faces_2)))#number of faces changing \r\n+\r\n+ elif (int(var_header) < d < (int(var_header) + int(var_vertex_nm))): #rotated vertexen, with original color code\r\n+ outfile2.write(\'%s\\n\'%(line2))\r\n+ g += 1\r\n+ \r\n+ elif (int(var_header) + int(var_vertex_nm)) == d: #for new vertexen, with color code 0,0,0 \r\n+ outfile2.write(\'%s\\n\'%(line2)) #the last original vertex\r\n+ g += 1\r\n+ with open(\'new_coordinates2.ply\') as infile:\r\n+ for line in infile:\r\n+ outfile2.write(line)\r\n+\r\n+ else: #everything left\r\n+ outfile2.write(\'%s\\n\'%(line2))\r\n+\r\n+ #writing new faces to output\r\n+ for z in range(0,len(out_faces)):\r\n+ outfile2.write("3 %s %s %s\\n"%((int(out_faces[z][0])+ int(var_vertex_nm)),\r\n+ (int(out_faces[z][1])+ int(var_vertex_nm)), (int(out_faces[z][2])+ int(var_vertex_nm))))\r\n+ #writing new square faces to output\r\n+ if len(out_faces_2) != 0:\r\n+ for x in range(0,len(out_faces_2)):\r\n+ outfile2.write("4 %s %s %s %s\\n"%((int(out_faces_2[z][0])+ int(var_vertex_nm)),\r\n+ (int(out_faces_2[z][1])+ int(var_vertex_nm)), (int(out_faces_2[z][2])+ int(var_vertex_nm)),\r\n+ (int(out_faces_2[z][3] + int(var_vertex_nm)))))\r\n+\r\n+ out_log.write(\'number of reconstructed faces:\\t%s\\n\\n\'%(len(out_faces) + len(out_faces_2))) \r\n+ outfile2.close()\r\n+\r\n+ \r\n+main()\r\n+\r\n+\r\n+\r\n' |