// ========================================================================== //
// Local libraries
#include "Options/options.hpp"
#include "StreamSurfaces/StreamSurfaces.hpp"
#include "Timer/Timer.hpp"

// Standard libraries
#include <string>

// Parallelisation
#if USE_MPI == 1
    #include <mpi.h>
#endif

// Macros
#ifndef MPI_VERSION
    #define MPI_VERSION 0
#endif

// ========================================================================== //
// Main driver

int main(int argc, char **argv) {
    // This function will read the computed results from the optimisation and
    // recreate the surfaces selected as the optimal ones for the system. These
    // will then be exported in a .ply format, ready to be run through
    // visualisation.

#if MPI_VERSION > 0
    // Startup the mpi instancing
    MPI_Init(&argc, &argv);
#endif

    // Setup variables and inputs
    Options opt = argc > 1 ? Options(argv[1]) : Options();

    // Initialise timing
    Timer times;

    // Modify input related options
    opt.set("super_sampling", true);

    // Modify surface related options
    times.add("options");

    // Setup the stream surface generator
    StreamSurfaces streamsurf(opt);
    times.add("init");

    // Select the surfaces specified from optimisation
    streamsurf.import_selected();
    times.add("selection");

    // Recreate the surfaces
    streamsurf.create_surfaces();
    times.add("super_sampling");

    streamsurf.triangle_converter();
    times.add("triangle_mesh");

    // Export the surfaces
    streamsurf.export_surf("surfaces_optimal");
    times.add("export");

    streamsurf.print_surfaces();
    times.print();

#if MPI_VERSION > 0
    // Conclude MPI execution
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();
#endif
    return EXIT_SUCCESS;
}

// End of File
// ========================================================================== //