-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameters.cpp
334 lines (286 loc) · 16.1 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*****************************************************************************
* This file is part of uvgVPCCenc V-PCC encoder.
*
* Copyright (c) 2024, Tampere University, ITU/ISO/IEC, project contributors
* 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 Tampere University or ITU/ISO/IEC 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
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
****************************************************************************/
/// \file Library parameters related operations.
#include "utils/parameters.hpp"
#include <numeric>
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include <regex>
#include <exception>
#include <limits>
#include "uvgvpcc/log.hpp"
namespace uvgvpcc_enc {
namespace {
std::unordered_map<std::string, ParameterInfo> parameterMap;
inline int toInt(const std::string& paramValue, const std::string& paramName) {
try {
size_t pos = 0;
const int value = std::stoi(paramValue,&pos);
// If pos is not at the end of the string, it means there were non-numeric characters
if(pos != paramValue.length()) {
throw std::invalid_argument("");
}
return value;
} catch (const std::exception& e) {
throw std::runtime_error("During the parsing of the uvgVPCC library command, an error occured: " + std::string(e.what()) +
"\nThe value assign to '" + paramName + "' is: '" + paramValue +
"'\nThis value was not converted into an int.");
}
}
inline size_t toUInt(const std::string& paramValue, const std::string& paramName) {
try {
if (paramValue[0] == '-') {
throw std::runtime_error("");
}
size_t pos = 0;
const size_t value = static_cast<size_t>(std::stoi(paramValue,&pos));
// If pos is not at the end of the string, it means there were non-numeric characters
if(pos != paramValue.length()) {
throw std::invalid_argument("");
}
// TODO(lf): check the overflow during int and size_t conversion
return value;
} catch (const std::exception& e) {
throw std::runtime_error("During the parsing of the uvgVPCC library command, an error occured: " + std::string(e.what()) +
"\nThe value assign to '" + paramName + "' is: '" + paramValue +
"'\nThis value was not converted into an unsigned int (size_t).");
}
}
inline float toFloat(const std::string& paramValue, const std::string& paramName) {
try {
size_t pos = 0;
const float value = std::stof(paramValue,&pos);
// If pos is not at the end of the string, it means there were non-numeric characters
if(pos != paramValue.length()) {
throw std::invalid_argument("");
}
return value;
} catch (const std::exception& e) {
throw std::runtime_error("During the parsing of the uvgVPCC library command, an error occured: " + std::string(e.what()) +
"\nThe value assign to '" + paramName + "' is: '" + paramValue +
"'\nThis value was not converted into a float.");
}
}
inline double toDouble(const std::string& paramValue, const std::string& paramName) {
try {
size_t pos = 0;
const double value = std::stod(paramValue,&pos);
// If pos is not at the end of the string, it means there were non-numeric characters
if(pos != paramValue.length()) {
throw std::invalid_argument("");
}
// TODO(lf): check the overflow during int and size_t conversion
return value;
} catch (const std::exception& e) {
throw std::runtime_error("During the parsing of the uvgVPCC library command, an error occured: " + std::string(e.what()) +
"\nThe value assign to '" + paramName + "' is: '" + paramValue +
"'\nThis value was not converted into a double.");
}
}
inline bool toBool(const std::string& paramValue, const std::string& paramName) {
if (paramValue == "true" || paramValue == "True" || paramValue == "1") {
return true;
}
if (paramValue == "false" || paramValue == "False" || paramValue == "0") {
return false;
}
throw std::runtime_error("During the parsing of the uvgVPCC library command, an error occured.\nThe value assign to '" + paramName +
"' is: '" + paramValue +
"'\nThis value was not converted into a boolean. Only those values are accepted: [true,false,1,0]");
}
} // anonymous namespace
void initializeParameterMap(Parameters& param) {
parameterMap = {
// ___ General parameters __ //
{"geoBitDepthInput", {UINT, "", ¶m.geoBitDepthInput}},
{"presetName", {STRING, "fast,slow", ¶m.presetName}},
{"intermediateFilesDir", {STRING, "", ¶m.intermediateFilesDir}},
{"sizeGOF", {UINT, "8,16", ¶m.sizeGOF}}, // TODO(lf)merge both gof size param ?
{"nbThreadPCPart", {UINT, "", ¶m.nbThreadPCPart}},
{"doubleLayer", {BOOL, "", ¶m.doubleLayer}},
{"logLevel", {STRING, std::accumulate(std::next(std::begin(LogLevelStr)), std::end(LogLevelStr), LogLevelStr[0],
[](const std::string& a, const std::string& b) { return a + "," + b; }), ¶m.logLevel}},
{"errorsAreFatal", {BOOL, "", ¶m.errorsAreFatal}},
// ___ Debug parameters ___ //
{"exportIntermediateMaps", {BOOL, "", ¶m.exportIntermediateMaps}},
{"exportIntermediatePointClouds", {BOOL, "", ¶m.exportIntermediatePointClouds}},
// ___ Activate or not some features ___ //
{"lowDelayBitstream", {BOOL, "", ¶m.lowDelayBitstream}},
// ___ Voxelization ___ // (grid-based segmentation)
{"geoBitDepthVoxelized", {UINT, "", ¶m.geoBitDepthVoxelized}},
// ___ KdTree ___ //
{"kdTreeMaxLeafSize", {UINT, "", ¶m.kdTreeMaxLeafSize}},
// Normal computation //
{"normalComputationKnnCount", {UINT, "", ¶m.normalComputationKnnCount}},
{"normalComputationMaxDiagonalStep", {UINT, "", ¶m.normalComputationMaxDiagonalStep}},
// Normal orientation //
{"normalOrientationKnnCount", {UINT, "", ¶m.normalOrientationKnnCount}},
// PPI segmentation //
// ___ PPI smoothing ___ // (fast grid-based refine segmentation)
{"geoBitDepthRefineSegmentation", {UINT, "", ¶m.geoBitDepthRefineSegmentation}},
{"refineSegmentationMaxNNVoxelDistanceLUT", {UINT, "", ¶m.refineSegmentationMaxNNVoxelDistanceLUT}},
{"refineSegmentationMaxNNTotalPointCount", {UINT, "", ¶m.refineSegmentationMaxNNTotalPointCount}},
{"refineSegmentationLambda", {DOUBLE, "", ¶m.refineSegmentationLambda}},
{"refineSegmentationIterationCount", {UINT, "", ¶m.refineSegmentationIterationCount}},
// ___ Patch generation ___ // (patch segmentation)
{"maxAllowedDist2RawPointsDetection", {UINT, "", ¶m.maxAllowedDist2RawPointsDetection}},
{"minPointCountPerCC", {UINT, "", ¶m.minPointCountPerCC}},
{"maxPatchSize", {UINT, "", ¶m.maxPatchSize}},
{"maxNNCountPatchSegmentation", {UINT, "", ¶m.maxNNCountPatchSegmentation}},
{"patchSegmentationMaxPropagationDistance", {UINT, "", ¶m.patchSegmentationMaxPropagationDistance}},
{"enablePatchSplitting", {BOOL, "", ¶m.enablePatchSplitting}},
{"minLevel", {UINT, "", ¶m.minLevel}},
{"log2QuantizerSizeX", {UINT, "", ¶m.log2QuantizerSizeX}},
{"log2QuantizerSizeY", {UINT, "", ¶m.log2QuantizerSizeY}},
{"quantizerSizeX", {UINT, "", ¶m.quantizerSizeX}},
{"quantizerSizeY", {UINT, "", ¶m.quantizerSizeY}},
{"surfaceThickness", {UINT, "", ¶m.surfaceThickness}},
// ___ Patch packing ___ //
{"mapWidth", {UINT, "", ¶m.mapWidth}},
{"minimumMapHeight", {UINT, "", ¶m.minimumMapHeight}},
{"spacePatchPacking", {UINT, "", ¶m.spacePatchPacking}},
{"interPatchPacking", {BOOL, "", ¶m.interPatchPacking}},
{"gpaTresholdIoU", {FLOAT, "", ¶m.gpaTresholdIoU}},
// ___ Map generation ___ //
{"mapGenerationFillEmptyBlock", {BOOL, "", ¶m.mapGenerationFillEmptyBlock}},
{"mapGenerationBackgroundValueAttribute", {UINT, "", ¶m.mapGenerationBackgroundValueAttribute}},
{"mapGenerationBackgroundValueGeometry", {UINT, "", ¶m.mapGenerationBackgroundValueGeometry}},
// ___ 2D encoding parameters ___ //
{"basenameOccupancyFiles", {STRING, "", ¶m.basenameOccupancyFiles}},
{"basenameGeometryFiles", {STRING, "", ¶m.basenameGeometryFiles}},
{"basenameAttributeFiles", {STRING, "", ¶m.basenameAttributeFiles}},
{"sizeGOP2DEncoding", {UINT, "8,16", ¶m.sizeGOP2DEncoding}},
{"intraFramePeriod", {UINT, "", ¶m.intraFramePeriod}},
// Occupancy map
{"occupancyEncoderName", {STRING, "Kvazaar", ¶m.occupancyEncoderName}},
{"occupancyEncodingIsLossless", {BOOL, "", ¶m.occupancyEncodingIsLossless}},
{"occupancyEncodingMode", {STRING, "AI,RA", ¶m.occupancyEncodingMode}},
{"occupancyEncodingFormat", {STRING, "YUV420", ¶m.occupancyEncodingFormat}},
{"occupancyEncodingNbThread", {UINT, "", ¶m.occupancyEncodingNbThread}},
{"occupancyMapResolution", {UINT, "1,2,4,8", ¶m.occupancyMapResolution}},
{"occupancyEncodingPreset", {STRING, "ultrafast,superfast,veryfast,faster,fast,medium,slow,slower,veryslow", ¶m.occupancyEncodingPreset}},
// Geometry map
{"geometryEncoderName", {STRING, "Kvazaar", ¶m.geometryEncoderName}},
{"geometryEncodingIsLossless", {BOOL, "", ¶m.geometryEncodingIsLossless}},
{"geometryEncodingMode", {STRING, "AI,RA", ¶m.geometryEncodingMode}},
{"geometryEncodingFormat", {STRING, "YUV420", ¶m.geometryEncodingFormat}},
{"geometryEncodingNbThread", {UINT, "", ¶m.geometryEncodingNbThread}},
{"geometryEncodingQp", {UINT, "", ¶m.geometryEncodingQp}},
{"geometryEncodingPreset", {STRING, "ultrafast,superfast,veryfast,faster,fast,medium,slow,slower,veryslow", ¶m.geometryEncodingPreset}},
// Attribute map
{"attributeEncoderName", {STRING, "Kvazaar", ¶m.attributeEncoderName}},
{"attributeEncodingIsLossless", {BOOL, "", ¶m.attributeEncodingIsLossless}},
{"attributeEncodingMode", {STRING, "AI,RA", ¶m.attributeEncodingMode}},
{"attributeEncodingFormat", {STRING, "YUV420", ¶m.attributeEncodingFormat}},
{"attributeEncodingNbThread", {UINT, "", ¶m.attributeEncodingNbThread}},
{"attributeEncodingQp", {UINT, "", ¶m.attributeEncodingQp}},
{"attributeEncodingPreset", {STRING, "ultrafast,superfast,veryfast,faster,fast,medium,slow,slower,veryslow", ¶m.attributeEncodingPreset}},
};
}
namespace {
size_t levenshteinDistance(const std::string& a, const std::string& b) {
const size_t m = a.size();
const size_t n = b.size();
std::vector<std::vector<size_t>> dp(m + 1, std::vector<size_t>(n + 1));
for (size_t i = 0; i <= m; ++i) {
dp[i][0] = i;
}
for (size_t j = 0; j <= n; ++j) {
dp[0][j] = j;
}
for (size_t i = 1; i <= m; ++i) {
for (size_t j = 1; j <= n; ++j) {
const int cost = (a[i - 1] == b[j - 1]) ? 0 : 1;
dp[i][j] = std::min({
dp[i - 1][j] + 1, // Deletion
dp[i][j - 1] + 1, // Insertion
dp[i - 1][j - 1] + cost // Substitution
});
}
}
return dp[m][n];
}
std::string suggestClosestString(const std::string& inputStr) {
size_t minDistance = std::numeric_limits<size_t>::max();
std::string closestString;
for (const auto& option : parameterMap) {
const size_t distance = levenshteinDistance(inputStr, option.first);
if (distance < minDistance) {
minDistance = distance;
closestString = option.first;
}
}
return closestString;
}
} // anonymous namespace
void setParameterValue(const std::string& parameterName,const std::string& parameterValue, const bool& fromPreset) {
uvgvpcc_enc::Logger::log(
uvgvpcc_enc::LogLevel::DEBUG, "API","Set parameter value: " + parameterName + " -> " + parameterValue + "\n");
if(!parameterMap.contains(parameterName)) {
throw std::invalid_argument(std::string(fromPreset ? "[PRESET] " : "") + "The parameter '" + parameterName + "' is not a valid parameter name. Did you mean '" + suggestClosestString(parameterName) + "'? (c.f. parameterMap)");
}
if (parameterValue.empty()) {
throw std::invalid_argument("It seems an empty value is assigned to the parameter " + parameterName + ".");
}
ParameterInfo& paramInfo = parameterMap.find(parameterName)->second;
if(!paramInfo.possibleValues.empty()) {
// Make a matching regex from the list of possible values
const std::regex possibleValueRegex("^(" + std::regex_replace(paramInfo.possibleValues, std::regex(","), "|") + ")$");
// Check if the matched value is valid
if (!std::regex_match(parameterValue, possibleValueRegex)) {
throw std::invalid_argument("Invalid value for parameter '"+ parameterName + "': '" + parameterValue + "'. Accepted values are: [" + paramInfo.possibleValues + "]");
}
}
// Assign the parameter value to the correct parameter variable. The 'paramInfo.parameterPtr' is a pointer to one member of p_, the only uvgvpcc_enc::Parameters instance of uvgVPCCenc.
switch (paramInfo.type) {
case INT: *static_cast<int*>(paramInfo.parameterPtr) = toInt(parameterValue, parameterName); break;
case BOOL: *static_cast<bool*>(paramInfo.parameterPtr) = toBool(parameterValue, parameterName); break;
case UINT: *static_cast<size_t*>(paramInfo.parameterPtr) = toUInt(parameterValue, parameterName); break;
case FLOAT: *static_cast<float*>(paramInfo.parameterPtr) = toFloat(parameterValue, parameterName); break;
case DOUBLE: *static_cast<double*>(paramInfo.parameterPtr) = toDouble(parameterValue, parameterName); break;
case STRING: *static_cast<std::string*>(paramInfo.parameterPtr) = parameterValue; break;
default:assert(false);
}
if(fromPreset) {
paramInfo.inPreset = true;
} else if (paramInfo.inPreset) {
uvgvpcc_enc::Logger::log(uvgvpcc_enc::LogLevel::INFO, "API","The value assigned to parameter '" + parameterName + "' overwrite the preset value.\n");
}
}
} // namespace uvgvpcc_enc