#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# Copyright (C) 2024 Correlated Solutions, Inc.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
# -----------------------------------------------------------------------------
# Applies a transformation to a series of data files
# -----------------------------------------------------------------------------

from VicPy import *
import sys
import glob
import argparse
import os


__desc__ = """This program applies a transformation to a serier of data files.
The user needs to provide the path to the data and can provide any of the
transformation parameters.
"""

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__desc__)
    parser.add_argument(
        "directory", help="Path to directory that contains the data files."
    )
    parser.add_argument(
        "--alpha",
        "-a",
        default=0.0,
        type=float,
        help="Rotation around x",
    )
    parser.add_argument(
        "--beta",
        "-b",
        default=0.0,
        type=float,
        help="Rotation around y",
    )
    parser.add_argument(
        "--gamma",
        "-g",
        default=0.0,
        type=float,
        help="Rotation around z",
    )
    parser.add_argument(
        "--tx",
        "-x",
        default=0.0,
        type=float,
        help="Translation in x",
    )
    parser.add_argument(
        "--ty",
        "-y",
        default=0.0,
        type=float,
        help="Translation in y",
    )
    parser.add_argument(
        "--tz",
        "-z",
        default=0.0,
        type=float,
        help="Translation in z",
    )

    args, unknown = parser.parse_known_args()

    input_files = glob.glob(args.directory + "*_0.out")
    input_files.sort()

    trafo = RigidTransformation(
        alpha=args.alpha,
        beta=args.beta,
        gamma=args.gamma,
        Tx=args.tx,
        Ty=args.ty,
        Tz=args.tz,
    )

    data = VicDataSet()

    n_files = len(input_files)

    for i in range(n_files):
        print("Processing {}".format(input_files[i]))
        data.load(input_files[i])
        data.transform(trafo)

        if data.save(input_files[i]) == False:
            print("Could not save the dataset.")
            os._exit(-1)
