-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_cli.cpp
183 lines (159 loc) · 4.91 KB
/
main_cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* @author Alessandro Muntoni ([email protected])
* @copyright Alessandro Muntoni 2016.
*/
#include <iostream>
#include <filesystem>
#include <cg3/utilities/command_line_argument_manager.h>
#include <cg3/utilities/string.h>
#include <methods/faf/faf_data.h>
#include "faf_pipeline.h"
//strings used for intro and help
const std::string intro =
"This is the command line tool for the paper 'Automatic surface segmentation \n"
"for seamless fabrication using 4-axis milling machines'.";
const std::string help =
"Usage: \n"
"./fourAxisMilling --input=file.obj [--output=outDirectory] [parameters]";
FourAxisFabrication::Data getDataFromArguments(
const cg3::CommandLineArgumentManager& clArguments,
FAFParameters& params);
int main(int argc, char *argv[]) {
cg3::CommandLineArgumentManager clArguments(argc, argv);
FourAxisFabrication::Data data;
FAFParameters params;
try {
data = getDataFromArguments(clArguments, params);
}
catch (const std::runtime_error& e) {
if (std::string(e.what()) == std::string("ok")) {
return 0;
}
else {
std::cerr << e.what();
return -1;
}
}
std::cout << "\n--- Parameter values: ---\n\n";
params.print();
//get the output dir and save
std::string outputDir; //meant to be left empty if no outpurDir argument was given
if (clArguments.exists("o")){
outputDir = clArguments["o"];
}
if (clArguments.exists("output")){
outputDir = clArguments["output"];
}
params.outputDir = outputDir;
if (outputDir != ""){
if (!std::filesystem::exists(outputDir)) {
bool res = std::filesystem::create_directory(outputDir);
if (!res)
throw std::runtime_error("Cannot create " + outputDir);
}
}
std::cout << "\n################ BEGIN ################\n\n\n";
std::cout << "Starting algorithm for " <<
cg3::filenameWithExtension(params.filename) << "\n";
//run the algorithm...
FAFPipeline::pipeline(data, params);
std::cout << "\nAlgorithm terminated. Saving results for " <<
cg3::filenameWithExtension(params.filename) << "\n";
std::cout << "\n################ END ################\n\n\n";
data.restoredMesh.saveOnPly(outputDir + "/segmentation.ply");
if (!params.justSegmentation) {
unsigned int i = 0;
for (const cg3::EigenMesh& s : data.stocks){
s.saveOnPly(outputDir + "/stock_" + std::to_string(i++) + ".ply");
}
i = 0;
for (const cg3::EigenMesh& s : data.results){
s.saveOnPly(outputDir + "/result_" + std::to_string(i++) + ".ply");
}
}
return 0;
}
/**
* Fills all the data with the input parameters or default values.
* Throws a std::runtime_error if some data is missing or invalid.
*/
FourAxisFabrication::Data getDataFromArguments(
const cg3::CommandLineArgumentManager& clArguments,
FAFParameters& params)
{
FourAxisFabrication::Data data;
if (clArguments.size() == 0 || clArguments.exists("h") || clArguments.exists("help")) {
std::cout << intro << help;
throw std::runtime_error("ok");
}
std::string inputFile;
if (clArguments.exists("i")){
inputFile = clArguments["i"];
}
if (clArguments.exists("input")){
inputFile = clArguments["input"];
}
if (inputFile.empty()){
throw std::runtime_error("Error: Input file not specified.\n" + help);
}
data.isMeshLoaded = data.originalMesh.loadFromFile(inputFile);
params.filename = inputFile;
if (!data.isMeshLoaded){
throw std::runtime_error(
"Error: impossible to load input file.\n"
"Known input formats: OBJ, PLY.");
}
data.mesh = data.originalMesh;
//manage other parameters
const std::array<std::string, 12> strParams = {
"model_height",
"stock_length",
"stock_diameter",
"dont_scale_model",
"prefiltering_smooth_iters",
"n_best_axis_dirs",
"n_visibility_dirs",
"saliency_factor",
"compactness_term",
"wall_angle",
"max_first",
"just_segmentation"
};
if (clArguments.exists(strParams[0])){
params.modelLength = std::stod(clArguments[strParams[0]]);
}
if (clArguments.exists(strParams[1])){
params.stockLength = std::stod(clArguments[strParams[1]]);
}
if (clArguments.exists(strParams[2])){
params.stockDiameter = std::stod(clArguments[strParams[2]]);
}
if (clArguments.exists(strParams[3])){
params.scaleModel = false;
}
if (clArguments.exists(strParams[4])){
params.smoothIterations = std::stoi(clArguments[strParams[4]]);
}
if (clArguments.exists(strParams[5])){
params.nOrientations = std::stoi(clArguments[strParams[5]]);
}
if (clArguments.exists(strParams[6])){
params.nVisibilityDirections = std::stoi(clArguments[strParams[6]]);
}
if (clArguments.exists(strParams[7])){
params.detailMultiplier = std::stod(clArguments[strParams[7]]);
}
if (clArguments.exists(strParams[8])){
params.compactness = std::stod(clArguments[strParams[8]]);
}
if (clArguments.exists(strParams[9])){
params.firstLayerAngle = std::stod(clArguments[strParams[9]]);
}
if (clArguments.exists(strParams[10])){
params.minFirst = false;
}
if (clArguments.exists(strParams[11])){
params.justSegmentation = true;
}
return data;
}