-
Notifications
You must be signed in to change notification settings - Fork 11
/
Parameters.cpp
307 lines (266 loc) · 10.2 KB
/
Parameters.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// BSD 3-Clause License
//
// Copyright (c) 2019, Jiong Tao, Bailin Deng, Yue Peng
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Parameters.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
class OptionInterpreter {
public:
OptionInterpreter(const std::string &option_str, const std::string &value_str)
: option_str_(option_str),
value_str_(value_str) {
}
// Load a single value matching the option name
template<typename T>
bool load_value(const std::string &target_option_name,
T &target_option_value) const {
if (option_str_ == target_option_name) {
if (!load_value_impl(value_str_, target_option_value)) {
std::cerr << "Error loading option: " << target_option_name
<< std::endl;
return false;
}
return true;
} else {
return false;
}
}
// Load a sequence of values matching the option name
template<typename T>
bool load_values(const std::string &target_option_name,
std::vector<T> &target_option_values) const {
if (option_str_ == target_option_name) {
target_option_values.clear();
std::istringstream istr(value_str_);
T value;
while (istr >> value) {
target_option_values.push_back(value);
}
if (target_option_values.empty()) {
std::cerr << "Error: no value loaded for option: " << target_option_name
<< std::endl;
return false;
}
return true;
} else {
return false;
}
}
// Load an enum matching the option name
template<typename EnumT>
bool load_enum(const std::string &target_option_name, int enum_value_count,
EnumT &value) const {
if (option_str_ == target_option_name) {
int enum_int = 0;
if (load_value_impl(value_str_, enum_int)) {
if (enum_int >= 0 && enum_int < enum_value_count) {
value = static_cast<EnumT>(enum_int);
return true;
}
}
std::cerr << "Error loading option: " << target_option_name << std::endl;
return false;
} else {
return false;
}
}
private:
std::string option_str_, value_str_;
bool load_value_impl(const std::string &str, double &value) const {
try {
value = std::stod(str);
} catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << std::endl;
return false;
} catch (const std::out_of_range &oor) {
std::cerr << "Out of Range error: " << oor.what() << std::endl;
return false;
}
return true;
}
bool load_value_impl(const std::string &str, int &value) const {
try {
value = std::stoi(str);
} catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << std::endl;
return false;
} catch (const std::out_of_range &oor) {
std::cerr << "Out of Range error: " << oor.what() << std::endl;
return false;
}
return true;
}
bool load_value_impl(const std::string &str, bool &value) const {
int bool_value = 0;
if (load_value_impl(str, bool_value)) {
value = (bool_value != 0);
return true;
} else {
return false;
}
}
};
// Load options from file
bool Parameters::load(const char* filename) {
std::ifstream ifile(filename);
if (!ifile.is_open()) {
std::cerr << "Error while opening file " << filename << std::endl;
return false;
}
std::string line;
while (std::getline(ifile, line)) {
std::string::size_type pos = line.find_first_not_of(' ');
if (pos == std::string::npos) {
continue;
}
// Check for comment line
else if (line.at(pos) == '#') {
continue;
}
std::string::size_type first_pos = line.find_first_not_of(' ');
std::string trimmed_line = line.substr(first_pos, std::string::npos);
std::string::size_type end_pos = trimmed_line.find_first_of(' ');
std::string option_str = trimmed_line.substr(pos, end_pos - pos);
std::string value_str = trimmed_line.substr(end_pos + 1, std::string::npos);
OptionInterpreter opt(option_str, value_str);
if (!(opt.load_value("HeatSolverMaxIter", heat_solver_max_iter)
|| opt.load_value("HeatSolverEps", heat_solver_eps)
|| opt.load_value("HeatSolverConvergeCheckFrequency",
heat_solver_convergence_check_frequency)
|| opt.load_value("GradSolverMaxIter", grad_solver_max_iter)
|| opt.load_value("GradSolverEps", grad_solver_eps)
|| opt.load_value("Penalty", penalty)
|| opt.load_value("GradSolverOutputFrequency",
grad_solver_output_frequency)
|| opt.load_value("GradSolverConvergeCheckFrequency",
grad_solver_convergence_check_frequency)
|| opt.load_values("SourceVertices", source_vertices)
|| opt.load_value("SolverType", solver_type))) {
std::cerr << "Unable to parse option " << option_str << std::endl;
return false;
}
}
if (!valid_parameters()) {
std::cerr << "Error: invalid parameter value" << std::endl;
return false;
}
std::sort(source_vertices.begin(), source_vertices.end());
std::cout << "Successfully loaded options from file " << filename
<< std::endl;
return true;
}
template<typename T>
bool check_lower_bound(const std::string &name, T val, T lower_bound,
bool allow_equal) {
bool valid = allow_equal ? (val >= lower_bound) : (val > lower_bound);
if (!valid) {
std::cerr << "Error: " << name << " must be "
<< (allow_equal ? ("at least") : ("larger than")) << " " << lower_bound
<< std::endl;
}
return valid;
}
template<typename T>
bool check_upper_bound(const std::string &name, T val, T upper_bound,
bool allow_equal) {
bool valid = allow_equal ? (val <= upper_bound) : (val < upper_bound);
if (!valid) {
std::cerr << "Error: " << name << " must be "
<< (allow_equal ? ("at most") : ("smaller than")) << " " << upper_bound
<< std::endl;
}
return valid;
}
bool check_nonempty_index_sequence(const std::string &name,
const std::vector<int> &seq) {
bool valid = (!seq.empty());
if (!valid) {
std::cerr << "Error: " << name << " is empty" << std::endl;
}
for (int i = 0; i < static_cast<int>(seq.size()); ++i) {
if (seq[i] < 0) {
std::cerr << "Error: invalid index " << seq[i] << std::endl;
valid = false;
break;
}
}
return valid;
}
bool check_solvertype(const std::string& name, int type) {
bool valid = (type == 0) || (type == 1);
if (!valid) {
std::cerr << "Error:" << name << " must be " << "0 or 1" << std::endl;
std::cout << "0 for Face Based Geodesic Solver \n"
<< "1 for Edge Based Geodesic Solver \n";
}
return valid;
}
// Check whether the parameter values are valid
bool Parameters::valid_parameters() const {
return check_lower_bound("HeatSolverMaxIter", heat_solver_max_iter, 0, false)
&& check_lower_bound("HeatSolverEps", heat_solver_eps, 0.0, false)
&& check_lower_bound("HeatSolverConvergeCheckFrequency",
heat_solver_convergence_check_frequency, 0, false)
&& check_lower_bound("GradSolverMaxIter", grad_solver_max_iter, 0, false)
&& check_lower_bound("GradSolverEps", grad_solver_eps, 0.0, false)
&& check_lower_bound("Penalty", penalty, 0.0, false)
&& check_lower_bound("GradSolverOutputFrequency",
grad_solver_output_frequency, 0, false)
&& check_lower_bound("GradSolverConvergeCheckFrequency",
grad_solver_convergence_check_frequency, 0, false)
&& check_nonempty_index_sequence("SourceVertices", source_vertices)
&& check_solvertype("SolverType", solver_type);
}
template<typename T>
void print_value(const std::string &name, T value) {
std::cout << name << ": " << value << std::endl;
}
void Parameters::output_options() {
std::cout << "======== Solver Parameters ========" << std::endl;
print_value("HeatSolverMaxIter", heat_solver_max_iter);
print_value("HeatSolverEps", heat_solver_eps);
print_value("GradSolverMaxIter", grad_solver_max_iter);
print_value("GradSolverEps", grad_solver_eps);
print_value("Penalty", penalty);
std::cout << "Source vertices: ";
for (int i = 0; i < static_cast<int>(this->source_vertices.size()); ++i) {
std::cout << source_vertices[i] << " ";
}
std::cout << std::endl;
std::cout << "====================================" << std::endl;
std::cout << "======== Solver Algorithm ========" << std::endl;
if (solver_type == 0) {
std::cout << "Face Based Geodesic Distance Solver" << std::endl;
} else {
std::cout << "Edge Based Geodesic Distance Solver" << std::endl;
}
std::cout << "====================================" << std::endl;
}