Commit 2104a4cf authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

added basic validation of input arguments

parent a80b8d3e
Loading
Loading
Loading
Loading
Loading
+28 −14
Original line number Diff line number Diff line
@@ -138,10 +138,9 @@ struct Parameters
		// pipeline options
		cli |= lyra::opt(detect_features)
				["-f"]["--detect-features"]
				("Before mesh generation, detect sharp features and boundaries "
				 "of the internal bounding polyhedron (and the potential "
				 "internal polyhedron) and inserts them as features of the "
				 "domain." + fmt_default(detect_features));
				("Before mesh generation, detect sharp features at polygonal "
				 "surface and insert them as features of the domain."
				 + fmt_default(detect_features));
		cli |= lyra::opt(features_angle_bound, "SCALAR")
				["--features-angle-bound"]
				("The maximum angle (in degrees) between the two normal vectors "
@@ -332,17 +331,16 @@ struct Parameters
};

template< typename Polyhedron >
void readPolyhedron(Polyhedron & polyhedron, const std::string & filePath, bool verbose = true)
void readPolyhedron(Polyhedron & polyhedron, const std::filesystem::path & input_file, bool verbose = true)
{
	// 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(filePath, polyhedron, CGAL::parameters::verbose(verbose))) {
		std::cerr << "Error: Could not read file '" <<  filePath << "'" << std::endl;
		std::exit(EXIT_FAILURE);
	}
	if (!CGAL::Polygon_mesh_processing::IO::read_polygon_mesh(input_file, polyhedron, CGAL::parameters::verbose(verbose)))
		throw std::runtime_error("Could not read file " + input_file.string());

	if (!CGAL::is_triangle_mesh(polyhedron)) {
		if (verbose)
			std::cout << "Input surface mesh is not triangular, calling triangulate_faces..." << std::endl;
		CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron);
	}
@@ -367,12 +365,16 @@ void polyDomain(const std::filesystem::path & input_file,
	using Tr = CGAL::Mesh_triangulation_3<Mesh_domain, CGAL::Default, ConcurrencyTag>::type;
	using C3T3 = CGAL::Mesh_complex_3_in_triangulation_3<Tr, Mesh_domain::Corner_index, Mesh_domain::Curve_index>;

	// Criteria
	using MeshCriteria = CGAL::Mesh_criteria_3<Tr>;

	// To avoid verbose function and named parameters call
	using namespace CGAL::parameters;

	// Validate input arguments
	if (args.edge_size < 0) throw std::invalid_argument("edge_size cannot be negative");
	if (args.facet_size < 0) throw std::invalid_argument("facet_size cannot be negative");
	if (args.cell_size < 0) throw std::invalid_argument("cell_size cannot be negative");
	if (args.detect_features && args.edge_size == 0)
		throw std::invalid_argument("edge_size must be positive when detection of sharp features is enabled");

	Polyhedron casePoly;
	readPolyhedron(casePoly, input_file);

@@ -383,6 +385,8 @@ void polyDomain(const std::filesystem::path & input_file,
	if (args.detect_features)
		domain.detect_features(args.features_angle_bound);

	// Criteria
	using MeshCriteria = CGAL::Mesh_criteria_3<Tr>;
	MeshCriteria criteria(
			edge_size = args.edge_size,
			facet_angle = args.facet_angle,
@@ -487,7 +491,17 @@ int main(int argc, char * argv[])
	std::cout << "Output VTU file: " << output_file << "\n";
	args.print_options(std::cout);

	try {
		polyDomain(input_file, output_file, args);
	}
	catch (std::logic_error & e) {
		std::cerr << "Logic error: " << e.what() << std::endl;
		return EXIT_FAILURE;
	}
	catch (std::runtime_error & e) {
		std::cerr << "Runtime error: " << e.what() << std::endl;
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}