# Geo_Compare
# Written by Connor Lough
# Parts of MlxWriter and MeshlabServerCaller were written by Will Zaylor


#########LOADING LIBRARIES#########
import os

# import Tkinter library, use dialog box when selecting files
from Tkinter import Tk
from tkFileDialog import askopenfilename

from DetermineValues import DetermineValues  # import the DetermineValues function from DetermineValues.py
from MlxCompareWriter import MlxCompareWriter, PercCrop, MaxHaus  # import these functions from MlxCompareWriter.py
from MeshlabServerCaller import SurfaceCompare  # import this function from MeshLabServerCaller.py


#########IMPORTING STL FILE 1#########
# Allow user to select the first of two .stl files to be used. User has option to type filename or to select.
print ''
print 'Select .stl file 1.'
print ''

answer1 = raw_input('Enter filename path or enter "dialog" for a dialog box to select the file: ')
if answer1 == 'dialog':
    # To show dialog box and select filename
    Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
    stl1filename = askopenfilename()  # show an "Open" dialog box and return the path to the selected file
else:
    stl1filename = answer1

print ''
author1 = raw_input('Enter the initials of the author of the first selected .stl file: ')
print ''


#########IMPORTING STL FILE 2#########
# Allow user to select the second of two .stl files to be used. User has option to type filename or to select.
print ''
print 'Select .stl file 2.'
print ''

answer2 = raw_input('Enter filename path or enter "dialog" for a dialog box to select the file: ')
if answer2 == 'dialog':
    # To show dialog box and select filename
    Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
    stl2filename = askopenfilename()  # show an "Open" dialog box and return the path to the selected file
else:
    stl2filename = answer2

print ''
author2 = raw_input('Enter the initials of the author of the second selected .stl file: ')
print''
structure_compared = raw_input('Now enter the abbreviation for the type of geometry structures (ie., for fibula bone,'
                               'enter FBB) that are being compared: ')
print ''


#########DETERMINE VALUES FROM FILE#########
# Using the DetermineValues function imported earlier, the user can extract the maximum voxel size/ maximum distance
# and the number of vertices, which is the number of samples to be used. This will be done for both .stl files.
(side_len1, samples1) = DetermineValues(stl1filename)
print ''
(side_len2, samples2) = DetermineValues(stl2filename)
print ''


#########MLX WRITER ROUND 1#########
# Next, these values will be incorporated into the .mlx filter script file, allowing the filter script to be specific to
# the geometries used.
mlxFile1 = 'MeshLab_Comparison_1.mlx' # probably don't want user input here, leave as default
MlxCompareWriter(mlxFile1, side_len1, samples1) # side_len1 and samples1 come from DetermineValues


#########JUST HAUSDORF DIST, ROUND 1#########
# Here, the Hausdorf Distance will be taken between the two geometries, using the specifications from the .mlx file.
source1SurfaceFileName = stl1filename
source2SurfaceFileName = stl2filename
outputSurfaceFileName = 'test1.ply'  # this file is just temporary and will eventually be deleted
logFile = 'testlog1.txt'  # this file is just temporary and will eventually be deleted
# Directory='/home/loughc/openknee/oks/oks001/dat/Geometry' # Can specify the directory, otherwise will use current directory
SurfaceCompare(source1SurfaceFileName, source2SurfaceFileName, outputSurfaceFileName, mlxFileName=mlxFile1, logFileName=logFile, callDirectory=None)


#########FINE TUNING 1#########
# Here, a value will be extracted from the testlog1.txt and inserted into a second .mlx file, allowing for the full
# comparison to take place.
lines = []
with open ('testlog1.txt', 'rt') as in_file:
    for line in in_file: # For each line of text, store it in a string variable named "line", and
        lines.append(line)
x = lines[2]
z = x.split()
val = z[4]
mlxFile2 = 'MeshLab_Comparison_2.mlx'
MaxHaus (mlxFile2, val, side_len1, samples1)


########DELETE UNNECESSARY FILES#########
# The two temporary files will be deleted here, if they exist.
delete_filename1 = 'testlog1.txt'
delete_filename2 = 'test1.ply'
if os.path.exists(delete_filename1):
    os.remove(delete_filename1)
if os.path.exists(delete_filename2):
    os.remove(delete_filename2)


#########COMPARE GEOMETRIES, ROUND 1#########
# Now with the missing piece of the second .mlx file, the full comparison can be run.
source1SurfaceFileName = stl1filename
source2SurfaceFileName = stl2filename
outputName1 = structure_compared+'_'+author1+'_'+author2
logFile1 = outputName1
# Directory='/home/loughc/openknee/oks/oks001/dat/Geometry'
percents = [0, 5, 10, 15, 20]
for x in percents:
    PercCrop(mlxFile2,Perc=x) # we establish a loop so that the comparison will be done for percentile crop values of
                              # 0, 5, 10, 15, and 20, and then saved accordingly
    if x<10:
        outputSurfaceFileName=outputName1+'_0'+str(x)+'.ply'
        logFile=logFile1+'_0'+str(x)+'.txt'
    else:
        outputSurfaceFileName=outputName1+'_'+str(x)+'.ply'
        logFile=logFile1+'_'+str(x)+'.txt'
    SurfaceCompare(source1SurfaceFileName, source2SurfaceFileName, outputSurfaceFileName, mlxFileName=mlxFile2, logFileName=logFile, callDirectory=None)


# #########MLX WRITER ROUND 2#########
# print ''
# MlxCompareWriter(mlxFile1, side_len2, samples2) # side_len2 and samples2 come from determine values
#
#
# #########JUST HAUSDORF DIST, ROUND 2#########
# outputSurfaceFileName = 'test2.ply' # insert user raw input later
# logFile = 'testlog2.txt' #insert user raw input later
# SurfaceCompare(source2SurfaceFileName, source1SurfaceFileName, outputSurfaceFileName, mlxFileName=mlxFile1, logFileName=logFile, callDirectory=Directory)
#
#
# #########FINE TUNING 1#########
# lines = []
# with open ('testlog2.txt', 'rt') as in_file:
#     for line in in_file: # For each line of text, store it in a string variable named "line", and
#         lines.append(line)
# x = lines[2]
# z = x.split()
# val = z[4]
# mlxFile2 = 'MeshLab_Comparison_2.mlx'
# MaxHaus (mlxFile2, val, side_len2, samples2)
#
#
# ########DELETE UNNECESSARY FILES#########
# delete_filename1 = '/home/loughc/openknee/oks/oks001/dat/Geometry/testlog2.txt'
# delete_filename2 = '/home/loughc/openknee/oks/oks001/dat/Geometry/test2.ply'
# if os.path.exists(delete_filename1):
#     os.remove(delete_filename1)
# if os.path.exists(delete_filename2):
#     os.remove(delete_filename2)
#
#
# #########CHANGE PERC CROP#########
# PercCrop(mlxFile2,Perc=5.00)
#
#
# #########COMPARE GEOMETRIES, ROUND 2#########
# # order switched
# outputSurfaceFileName = 'test2.ply' # insert user raw input later
# logFile = 'testlog2.txt' # insert user raw input later
# SurfaceCompare(source2SurfaceFileName, source1SurfaceFileName, outputSurfaceFileName, mlxFileName=mlxFile2, logFileName=logFile, callDirectory=Directory)
#
#
# #########END OF SCRIPT#########
# print ''
# print 'Comparison process completed.'
# print ''