#! /bin/bash

source release_config.sh

mesh=$1
scale=$2

if [ "${mesh}" == "" ] ; then
    echo "Please input mesh ..."
    exit
fi

mesh_in_dir=${in_dir}
mesh_out_dir=${out_dir}/${mesh}

echo "############################"
echo "MESH   : $mesh"
echo "IN-DIR : ${mesh_in_dir}"
echo "OUT_DIR: ${mesh_out_dir}"
echo "############################"

tri=${mesh_in_dir}/${mesh}.obj
fl=${mesh_in_dir}/${mesh}.fl
ff=${mesh_in_dir}/${mesh}.ff
scale_file=${mesh_in_dir}/${mesh}_scale.txt

if [ -f ${tri} ] ; then echo "Exist triangular mesh."
else echo "Please input the triangular mesh rightly. No SUCH FILE: ${tri}"; exit
fi

if [ -f ${ff} ] ; then echo "Exist frame."
else echo "Please set the input frame rightly. NO SUCH FILE: ${ff}"; exit
fi

######################################################
mkdir -p ${mesh_out_dir}
######################################################

if [ -f ${fl} ] ; then
    echo "Exist feature file."
else
    fl=${mesh_out_dir}/null.fl
    echo "0" > ${fl}
fi
echo "FL: ${fl}"

#############################################################################
exe=${bin_dir}/morse2d
verify=${bin_dir}/verify
#############################################################################

if [ "${scale}" == "" ] ; then
    if [ -f ${scale_file} ] ; then
	scale=`sed -n 1p ${scale_file}`
    else
	scale=1
    fi
elif [ "${scale}" == "calculate" ] ; then
    scale_file=${mesh_out_dir}/${mesh}_scale.txt
    ${verify} prog=cal_scale_with_ff in_mesh=${tri} in_ff=${ff} \
	      ratio=2e-2 out_txt=${scale_file}
    scale=`sed -n 1p ${scale_file}`
fi
echo "scale: ${scale}"

######################################################
is_curl_correct=0
if [ "${is_curl_correct}" == "1" ] ; then
    corr_ff=${mesh_out_dir}/${mesh}_corr.ff
    ${exe} prog=curl_correct in_mesh=${tri} in_fl=${fl} in_ff=${ff} \
	   out_ff=${corr_ff} curl_reg_wei=0.1 curl_aniso_wei=0.1 curl_lap_wei=1
    ff=${corr_ff}
fi
echo "FF: ${ff}"
######################################################

check_log=${mesh_out_dir}/LOG_CHECK_${mesh}.txt
echo "check input ..."
${verify} prog=check_input tri=${tri} ff=${ff} fl=${fl} \
	  length_eps=1e-13 area_eps=1e-13 > ${check_log}

check_res=`sed -n '$p' ${check_log}`
if [ "${check_res}" == "# [ CHECK RESULT ] YES" ] ; then
    echo "INPUT IS OK."
else
    echo "THE INPUT HAS SOME PROBLEM, Please input a valid mesh, ff and fl."
    exit
fi


######################################################
# if is_sub_mesh_with_fl=1 and is_output_sub=1,
# sub info will be written out.
is_sub=1
output_sub=0
if [ ${output_sub} == 1 ]; then
    mkdir -p ${mesh_out_dir}/sub
    sub_mesh=${mesh_out_dir}/sub/${mesh}_${scale}_sub.obj
    sub_ff=${mesh_out_dir}/sub/${mesh}_${scale}_sub.ff
    sub_fl=${mesh_out_dir}/sub/${mesh}_${scale}_sub.fl
fi
######################################################

out_quad=${mesh_out_dir}/${mesh}_${scale}_quad.obj
HQ_log=${mesh_out_dir}/LOG_${mesh}_${scale}.txt

echo "hybrid quad ..."
${exe} prog=pipeline in_mesh=${tri} in_ff=${ff} in_fl=${fl} \
     refine_thres=0.5 out_quad=${out_quad} scale=${scale} smooth_iter_num=5 \
     fl_control_wei=20 is_sub_mesh_with_fl=${is_sub} is_output_sub=${output_sub} \
     sub_mesh=${sub_mesh} sub_ff=${sub_ff} sub_fl=${sub_fl}

# info=${mesh_out_dir}/INFO_${mesh}_${scale}
# ${exe} prog=quad_info tri=${tri} mesh_name=${mesh} quad=${out_quad} \
#        quad_fl=${out_quad}.fl ff=${ff} fl=${fl} scale=${scale} out_report=${info}
