-
Notifications
You must be signed in to change notification settings - Fork 4
/
parameters.hpp
166 lines (108 loc) · 3.78 KB
/
parameters.hpp
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
#pragma once
#include <iostream>
#include <fstream>
#include <filesystem>
#include <omp.h>
#include "common.hpp"
#include "vendor/cxxopts.hpp"
#include "utils.hpp"
#include "vendor/json.hpp"
#define DEFAULT_STD_DEV "2.5"
#define DEFAULT_MEANK "16"
#define DEFAULT_SAMPLE_RADIUS "0"
namespace fs = std::filesystem;
namespace FPCFilter
{
class Parameters
{
std::optional<Polygon> extractPolygon(const std::string& boundary)
{
Polygon polygon;
std::ifstream i(boundary);
nlohmann::json j;
i >> j;
const auto features = j["features"];
if (features.empty())
return std::nullopt;
for (const auto& f : features)
{
const auto geometry = f["geometry"];
if (geometry["type"] != "Polygon")
continue;
const auto coordinates = geometry["coordinates"][0];
// Add the points
for (auto& coord : coordinates)
polygon.addPoint(coord[0], coord[1]);
return polygon;
}
return std::nullopt;
}
public:
std::string input;
std::string output;
bool isCropRequested = false;
std::optional<Polygon> boundary;
bool isFilterRequested = false;
std::optional<double> std;
std::optional<int> meank;
bool isSampleRequested = false;
std::optional<double> radius;
int concurrency;
bool verbose;
Parameters(const int argc, char** argv)
{
cxxopts::Options options("FPCFilter", "Fast Point Cloud Filtering");
options.show_positional_help();
options.add_options()
("i,input", "Input point cloud", cxxopts::value<std::string>())
("o,output", "Output point cloud", cxxopts::value<std::string>())
("b,boundary", "Crop boundary (GeoJSON POLYGON)", cxxopts::value<std::string>())
("s,std", "Standard deviation threshold", cxxopts::value<double>())
("m,meank", "Mean number of neighbors", cxxopts::value<int>())
("r,radius", "Sample radius", cxxopts::value<double>())
("c,concurrency", "Max concurrency", cxxopts::value<int>())
("v,verbose", "Verbose output", cxxopts::value<bool>());
options.parse_positional({ "input", "output" });
const auto result = options.parse(argc, argv);
if (!result.count("input") || !result.count("output"))
throw std::invalid_argument(options.help());
input = result["input"].as<std::string>();
if (input.empty())
throw std::invalid_argument("Input file is empty");
if (!fs::exists(input))
throw std::invalid_argument(string_format("Input file '%s' does not exist", input.c_str()));
output = result["output"].as<std::string>();
if (output.empty())
throw std::invalid_argument("Output file is empty");
if (result.count("std") && result.count("meank")) {
std = result["std"].as<double>();
if (std < 0)
throw std::invalid_argument("Standard deviation threshold cannot be less than 0");
meank = result["meank"].as<int>();
if (meank < 1)
throw std::invalid_argument("Mean number of neighbors cannot be less than 1");
isFilterRequested = true;
}
if (result.count("radius")) {
radius = result["radius"].as<double>();
if (radius < 0)
throw std::invalid_argument("Sample radius cannot be less than 0");
isSampleRequested = true;
}
if (result.count("concurrency")) {
concurrency = result["concurrency"].as<int>();
if (concurrency < 1)
throw std::invalid_argument("Concurrency cannot be less than 1");
} else
concurrency = std::max(omp_get_num_procs(), 1);
verbose = result.count("verbose") != 0;
if (result.count("boundary")) {
const auto boundaryFile = result["boundary"].as<std::string>();
boundary = extractPolygon(boundaryFile);
if (!boundary.has_value())
throw std::invalid_argument(string_format("Boundary file '%s' does not contain a valid GeoJSON POLYGON", boundaryFile.c_str()));
isCropRequested = true;
}
}
};
}