Commit 02ab304b authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

add command-line options and other improvements to cgal-view-surface

parent 53890937
Loading
Loading
Loading
Loading
+45 −9
Original line number Diff line number Diff line
#include <filesystem>

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
#include <CGAL/draw_surface_mesh.h>
#include <fstream>

#include "lyra/lyra.hpp"

using Kernel = CGAL::Simple_cartesian<double>;
using Point = Kernel::Point_3;
@@ -9,14 +13,46 @@ using Mesh = CGAL::Surface_mesh<Point>;

int main(int argc, char* argv[])
{
    const char* filename = (argc>1) ? argv[1] : "";
	bool show_help = false;
	bool verbose = false;
	std::filesystem::path input_file;

	auto cli = lyra::cli();
	cli |= lyra::help(show_help).description("Polygonal mesh viewer based on the CGAL library");
	cli |= lyra::opt(verbose)["-v"]["--verbose"]("Instruct CGAL to produce verbose output.");
	cli |= lyra::arg(input_file, "input_file")("Input file (supported formats: OFF, STL, VTP, OBJ, GOCAD).").required();

	auto result = cli.parse({ argc, argv });

	// check that the arguments where valid
	if (!result) {
		std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
		std::cerr << cli << std::endl;
		return EXIT_FAILURE;
	}

	// show the help when asked for.
	if (show_help) {
		std::cout << cli << std::endl;
		return EXIT_SUCCESS;
	}

	// check if the input file exists
	if (!std::filesystem::exists(input_file)) {
		std::cerr << "Error: input_file \"" + input_file.string() + "\" does not exist." << std::endl;
		return EXIT_FAILURE;
	}

	Mesh mesh;

    Mesh sm;
    if (!CGAL::IO::read_polygon_mesh(filename, sm)) {
	// the function from the Polygon_mesh_processing does some repairs and orientations
	// to obtain a valid polygon mesh
	// https://doc.cgal.org/latest/Polygon_mesh_processing/group__PMP__IO__grp.html
	if (!CGAL::Polygon_mesh_processing::IO::read_polygon_mesh(input_file, mesh, CGAL::parameters::verbose(verbose))) {
		std::cerr << "Invalid input file." << std::endl;
		return EXIT_FAILURE;
	}

    CGAL::draw(sm);
	CGAL::draw(mesh);
	return EXIT_SUCCESS;
}