-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharguments.cpp
More file actions
197 lines (184 loc) · 5.9 KB
/
arguments.cpp
File metadata and controls
197 lines (184 loc) · 5.9 KB
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
#include <stdexcept>
#include "opencv2/highgui/highgui.hpp" //CV_FOURCC
#include "arguments.hpp"
/**
* Constructor. Sets a default output filename, then resets all other parameters to their defaults.
*/
Arguments::Arguments() {
output_filename_default = "output.avi";
reset();
}
/**
* Destructor.
*/
Arguments::~Arguments() {
}
/**
* Sets all argument parameters to our defaults.
*/
void Arguments::reset() {
g_args_mutex.lock(); // or, to be exception-safe, use std::lock_guard
verbose = false;
nogui = false;
output_fourcc = CV_FOURCC_DEFAULT;
input_filename = "";
output_filename = output_filename_default;
num_disparities = 16;
SAD_window_size = 15;
min_disparity = 0;
pre_filter_cap = 0;
uniqueness = 0;
p1 = 0;
p2 = 0;
disp12_max_diff = 0;
speckle_window_size = 0;
speckle_range = 0;
full_dp = false;
start_frame = 0;
end_frame = 0;
g_args_mutex.unlock();
}
/**
* Check if the parameters are valid. If "correct" is set, then try to correct invalid settings.
* @param correct
* @return True if the arguments are valid after this function (in case "correct" is set).
*/
bool Arguments::is_valid(bool correct) {
bool invalid = false;
for (const Arg& a : arg_list) {
invalid = invalid ||!is_valid(a, correct);
}
return !invalid;
}
/**
* Check if a specific parameter is valid. If "correct" is set, then try to correct invalid settings.
* @param arg The argument to validate.
* @param correct Whether this function should attempt to correct invalid arguments.
* @return True if the argument is valid after this function (in case "correct" is set).
*/
bool Arguments::is_valid(const Arg &arg, bool correct) {
/*
rules:
*) verbose and full_dp are boolean and independent - so no validation
*) if nogui is set, filenames have to be set because pipes aren't handled yet.
*) if nogui is not set, filenames are optional
*) num_disparities has to be a multiple of 16 and >=0
*) min_disparity >= 0
*) SAD_window_size must be odd and >=1
*) P1/P2 have to both be >=0 and if they're not both 0, P2 > P1
*) disp12_max_diff *can* be anything, but *should* be >= -1
*) pre_filter_cap should be >=0
*) uniquness should be >=0
*) speckle_window_size >=0
*) speckle_range >=0
*/
bool valid = true;
// a lot of the variables are simple "greater than / equals" tests
auto geq = [&](int& value1, int value2) {
if (value1 < value2) {
correct ? value1 = 0 : valid = false;
}
};
g_args_mutex.lock(); // or, to be exception-safe, use std::lock_guard
switch(arg) {
case VERBOSE:
case FULL_DP:
case OUTPUT_FOURCC:
break;
case NOGUI:
if (nogui) {
if (input_filename.empty()) {
//can't correct this without using stdin/stdout
valid = false;
}
if (output_filename.empty()) {
if (correct) {
//this can be corrected by reverting to default
output_filename = output_filename_default;
} else {
valid = false;
}
}
}
break;
case OUTPUT_FILENAME:
if (nogui) {
if (output_filename.empty()) {
if (correct) {
//this can be corrected by reverting to default
output_filename = output_filename_default;
} else {
valid = false;
}
}
}
break;
case INPUT_FILENAME:
if (nogui) {
if (input_filename.empty()) {
//can't correct this without using stdin/stdout
valid = false;
}
}
break;
case NUM_DISPARITIES:
if (num_disparities % 16 || num_disparities < 0) {
if (correct) {
num_disparities = std::floor(((std::max(0,num_disparities))/16))*16;
} else {
valid = false;
}
}
break;
case SAD_WINDOW_SIZE:
if (!((SAD_window_size % 2) && (SAD_window_size >=1))) {
if (correct) {
std::max(1, SAD_window_size % 2 ? SAD_window_size : SAD_window_size + 1);
} else {
valid = false;
}
}
break;
case MIN_DISPARITY:
geq(min_disparity, 0);
break;
case PRE_FILTER_CAP:
geq(pre_filter_cap, 0);
break;
case UNIQUENESS:
geq(uniqueness, 0);
break;
case P1: //P1 and P2 have to be validated against each other
case P2:
if (!(((p1 == 0) && (p2 == 0)) || ((p2 > 1) && (p1 > 0) && (p2 > p1)))) {
if (correct) {
p1 = std::max(0, p1);
p2 = std::max(p1 > 0 ? p1+1 : 0, p2);
} else {
valid = false;
}
}
break;
case DISP12_MAX_DIFF:
geq(disp12_max_diff, -1);
break;
case SPECKLE_WINDOW_SIZE:
geq(speckle_window_size, 0);
break;
case SPECKLE_RANGE:
geq(speckle_range, 0);
break;
case START_FRAME: //START_FRAME and END_FRAME have to be validated together
case END_FRAME:
geq(start_frame, 0);
geq(end_frame, 0);
if (end_frame != 0) {
geq(end_frame, start_frame);
}
break;
default:
throw std::range_error("Error: Unknown variable index");
}
g_args_mutex.unlock();
return valid;
}