# ----------------------------------------------------------------------------
# -                        Open3D: www.open3d.org                            -
# ----------------------------------------------------------------------------
# The MIT License (MIT)
#
# Copyright (c) 2018-2021 www.open3d.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# ----------------------------------------------------------------------------

# examples/python/reconstruction_system/refine_registration.py

import numpy as np
import open3d as o3d
import sys
sys.path.append("../utility")
from visualization import draw_registration_result_original_color
import argparse


def multiscale_icp(source,
                   target,
                   voxel_size,
                   max_iter,
                   init_transformation=np.identity(4)):
    current_transformation = init_transformation
    for i, scale in enumerate(range(len(max_iter))):  # multi-scale approach
        iter = max_iter[scale]
        distance_threshold = 0.07
        print("voxel_size {}".format(voxel_size[scale]))
        source_down = source.voxel_down_sample(voxel_size[scale])
        target_down = target.voxel_down_sample(voxel_size[scale])
        source_down.estimate_normals(
            o3d.geometry.KDTreeSearchParamHybrid(radius=voxel_size[scale] * 2.0,
                                                 max_nn=30))
        target_down.estimate_normals(
            o3d.geometry.KDTreeSearchParamHybrid(radius=voxel_size[scale] * 2.0,
                                                 max_nn=30))
        result_icp = o3d.pipelines.registration.registration_colored_icp(
            source_down, target_down, distance_threshold,
            current_transformation,
            o3d.pipelines.registration.TransformationEstimationForColoredICP(),
            o3d.pipelines.registration.ICPConvergenceCriteria(
                relative_fitness=1e-6, relative_rmse=1e-6, max_iteration=iter))

        current_transformation = result_icp.transformation
        draw_registration_result_original_color(source, target,
                                                current_transformation)
        print(current_transformation)

    return result_icp.transformation


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('src')
    parser.add_argument('dst')
    parser.add_argument('--voxel_size', default=0.05, type=float)
    args = parser.parse_args()

    o3d.utility.set_verbosity_level(o3d.utility.Debug)
    source = o3d.io.read_point_cloud(args.src)
    target = o3d.io.read_point_cloud(args.dst)
    voxel_size = args.voxel_size

    trans = np.array([
        0.60949996, 0.49802465, -0.61683162, 0.15081973, -0.57239243,
        0.81477382, 0.09225254, -0.21454357, 0.5485223, 0.29684183, 0.78167015,
        -0.05018587, 0, 0, 0, 1
    ]).reshape((4, 4))

    trans = multiscale_icp(source, target,
                           [voxel_size, voxel_size / 2.0, voxel_size / 4.0],
                           [50, 30, 14], trans)
