Skip to content

Commit e9e3a35

Browse files
committed
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 jarro2783/cxxopts#444, we could potentially use their multitoken option - but I think the syntax is fine now as it is.
1 parent e74d18f commit e9e3a35

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ target_compile_options(fit-model-multi PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/bigob
2626

2727
# Generate random samples from the model:
2828
add_executable(generate-obj generate-obj.cpp)
29-
target_link_libraries(generate-obj PRIVATE eos opencv_core opencv_imgproc opencv_imgcodecs Boost::program_options)
29+
target_link_libraries(generate-obj PRIVATE cxxopts::cxxopts eos opencv_core opencv_imgcodecs)
3030

3131
# Install these targets:
3232
install(TARGETS fit-model-simple DESTINATION bin)

examples/generate-obj.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "eos/render/render.hpp"
2626
#include "eos/render/matrix_projection.hpp"
2727

28-
#include "boost/program_options.hpp"
28+
#include <cxxopts.hpp>
2929

3030
#include "opencv2/core.hpp"
3131
#include "opencv2/imgcodecs.hpp"
@@ -34,7 +34,6 @@
3434
#include <filesystem>
3535

3636
using namespace eos;
37-
namespace po = boost::program_options;
3837
using std::cout;
3938
using std::endl;
4039
using std::string;
@@ -49,43 +48,51 @@ using std::vector;
4948
*/
5049
int main(int argc, char* argv[])
5150
{
51+
cxxopts::Options options("generate-obj", "Generates samples from the model and stores them as obj file "
52+
"as well as outputs a frontal rendering of the sample.");
53+
// clang-format off
54+
options.add_options()
55+
("h,help", "display the help message")
56+
("m,model", "an eos .bin Morphable Model file", cxxopts::value<std::string>(), "filename")
57+
("shape-coeffs", "optional comma-separated list of shape coefficients. Do not use spaces between values. "
58+
"E.g.: '--shape-coeffs 0.0,1.5,-1.0'. All coefficients not specified will be set to zero. "
59+
"If omitted, the mean is used.",
60+
cxxopts::value<std::vector<float>>())
61+
("color-coeffs", "optional comma-separated list of colour coefficients. Do not use spaces between values. "
62+
"E.g.: '--colour-coeffs 0.0,1.0,-0.5'. All coefficients not specified will be set to zero. "
63+
"If omitted, the mean is used.",
64+
cxxopts::value<std::vector<float>>())
65+
("o,output", "name of the output obj file (including .obj). Can be a full path.",
66+
cxxopts::value<std::string>()->default_value("output.obj"), "filename");
67+
// clang-format on
68+
5269
using std::filesystem::path;
5370
path model_file, output_file;
5471
vector<float> shape_coefficients, color_coefficients;
5572

5673
try
5774
{
58-
po::options_description desc("Allowed options");
59-
// clang-format off
60-
desc.add_options()
61-
("help", "produce help message")
62-
("model", po::value<path>(&model_file)->required(), "an eos .bin Morphable Model file")
63-
("shape-coeffs", po::value<vector<float>>(&shape_coefficients)->multitoken(),
64-
"optional parameter list of shape coefficients. All not specified will be set to zero. E.g.: "
65-
"'--shape-coeffs 0.0 1.5'. If omitted, the mean is used.")
66-
("color-coeffs", po::value<vector<float>>(&color_coefficients)->multitoken(),
67-
"optional parameter list of colour coefficients. All not specified will be set to zero. E.g.: "
68-
"'--colour-coeffs 0.0 1.5'. If omitted, the mean is used.")
69-
("output", po::value<path>(&output_file)->default_value("output.obj"),
70-
"name of the output obj file (including .obj). Can be a full path.");
71-
// clang-format on
72-
po::variables_map vm;
73-
// disabling short options to allow negative values for the coefficients, e.g. '--shape-coeffs 0.0 -1.5'
74-
po::store(
75-
po::parse_command_line(argc, argv, desc,
76-
po::command_line_style::unix_style ^ po::command_line_style::allow_short),
77-
vm);
78-
if (vm.count("help"))
75+
const auto result = options.parse(argc, argv);
76+
if (result.count("help"))
7977
{
80-
cout << "Usage: generate-obj [options]" << endl;
81-
cout << desc;
78+
std::cout << options.help() << std::endl;
8279
return EXIT_SUCCESS;
8380
}
84-
po::notify(vm);
85-
} catch (const po::error& e)
81+
82+
model_file = result["model"].as<std::string>(); // required (with default)
83+
if (result.count("shape-coeffs")) // optional
84+
{
85+
shape_coefficients = result["shape-coeffs"].as<std::vector<float>>();
86+
}
87+
if (result.count("color-coeffs")) // optional
88+
{
89+
color_coefficients = result["color-coeffs"].as<std::vector<float>>();
90+
}
91+
output_file = result["output"].as<std::string>(); // required (with default)
92+
} catch (const std::exception& e)
8693
{
87-
cout << "Error while parsing command-line arguments: " << e.what() << endl;
88-
cout << "Use --help to display a list of options." << endl;
94+
std::cout << "Error while parsing command-line arguments: " << e.what() << std::endl;
95+
std::cout << "Use --help to display a list of options." << std::endl;
8996
return EXIT_FAILURE;
9097
}
9198

0 commit comments

Comments
 (0)