From e9e3a356dc12ab29747195c210b2e85653b66c5b Mon Sep 17 00:00:00 2001 From: Patrik Huber Date: Sun, 15 Dec 2024 11:52:46 +0000 Subject: [PATCH] Replace boost program_options with cxxopts in generate-obj.cpp The command-line argument syntax of generate-obj slightly changed: Coefficient values now need to be separated by a comma instead of spaces, e.g. `generate.obj.exe --model ... --shape-coefficients 1.0,-1.5`. If cxxopts merge https://github.com/jarro2783/cxxopts/pull/444, we could potentially use their multitoken option - but I think the syntax is fine now as it is. --- examples/CMakeLists.txt | 2 +- examples/generate-obj.cpp | 65 ++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 55d86f85..b43e80e9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -26,7 +26,7 @@ target_compile_options(fit-model-multi PRIVATE "$<$:/bigob # Generate random samples from the model: add_executable(generate-obj generate-obj.cpp) -target_link_libraries(generate-obj PRIVATE eos opencv_core opencv_imgproc opencv_imgcodecs Boost::program_options) +target_link_libraries(generate-obj PRIVATE cxxopts::cxxopts eos opencv_core opencv_imgcodecs) # Install these targets: install(TARGETS fit-model-simple DESTINATION bin) diff --git a/examples/generate-obj.cpp b/examples/generate-obj.cpp index b9c65356..9dc014c5 100644 --- a/examples/generate-obj.cpp +++ b/examples/generate-obj.cpp @@ -25,7 +25,7 @@ #include "eos/render/render.hpp" #include "eos/render/matrix_projection.hpp" -#include "boost/program_options.hpp" +#include #include "opencv2/core.hpp" #include "opencv2/imgcodecs.hpp" @@ -34,7 +34,6 @@ #include using namespace eos; -namespace po = boost::program_options; using std::cout; using std::endl; using std::string; @@ -49,43 +48,51 @@ using std::vector; */ int main(int argc, char* argv[]) { + cxxopts::Options options("generate-obj", "Generates samples from the model and stores them as obj file " + "as well as outputs a frontal rendering of the sample."); + // clang-format off + options.add_options() + ("h,help", "display the help message") + ("m,model", "an eos .bin Morphable Model file", cxxopts::value(), "filename") + ("shape-coeffs", "optional comma-separated list of shape coefficients. Do not use spaces between values. " + "E.g.: '--shape-coeffs 0.0,1.5,-1.0'. All coefficients not specified will be set to zero. " + "If omitted, the mean is used.", + cxxopts::value>()) + ("color-coeffs", "optional comma-separated list of colour coefficients. Do not use spaces between values. " + "E.g.: '--colour-coeffs 0.0,1.0,-0.5'. All coefficients not specified will be set to zero. " + "If omitted, the mean is used.", + cxxopts::value>()) + ("o,output", "name of the output obj file (including .obj). Can be a full path.", + cxxopts::value()->default_value("output.obj"), "filename"); + // clang-format on + using std::filesystem::path; path model_file, output_file; vector shape_coefficients, color_coefficients; try { - po::options_description desc("Allowed options"); - // clang-format off - desc.add_options() - ("help", "produce help message") - ("model", po::value(&model_file)->required(), "an eos .bin Morphable Model file") - ("shape-coeffs", po::value>(&shape_coefficients)->multitoken(), - "optional parameter list of shape coefficients. All not specified will be set to zero. E.g.: " - "'--shape-coeffs 0.0 1.5'. If omitted, the mean is used.") - ("color-coeffs", po::value>(&color_coefficients)->multitoken(), - "optional parameter list of colour coefficients. All not specified will be set to zero. E.g.: " - "'--colour-coeffs 0.0 1.5'. If omitted, the mean is used.") - ("output", po::value(&output_file)->default_value("output.obj"), - "name of the output obj file (including .obj). Can be a full path."); - // clang-format on - po::variables_map vm; - // disabling short options to allow negative values for the coefficients, e.g. '--shape-coeffs 0.0 -1.5' - po::store( - po::parse_command_line(argc, argv, desc, - po::command_line_style::unix_style ^ po::command_line_style::allow_short), - vm); - if (vm.count("help")) + const auto result = options.parse(argc, argv); + if (result.count("help")) { - cout << "Usage: generate-obj [options]" << endl; - cout << desc; + std::cout << options.help() << std::endl; return EXIT_SUCCESS; } - po::notify(vm); - } catch (const po::error& e) + + model_file = result["model"].as(); // required (with default) + if (result.count("shape-coeffs")) // optional + { + shape_coefficients = result["shape-coeffs"].as>(); + } + if (result.count("color-coeffs")) // optional + { + color_coefficients = result["color-coeffs"].as>(); + } + output_file = result["output"].as(); // required (with default) + } catch (const std::exception& e) { - cout << "Error while parsing command-line arguments: " << e.what() << endl; - cout << "Use --help to display a list of options." << endl; + std::cout << "Error while parsing command-line arguments: " << e.what() << std::endl; + std::cout << "Use --help to display a list of options." << std::endl; return EXIT_FAILURE; }