-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
projection_params.cpp
320 lines (293 loc) · 11.1 KB
/
projection_params.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
// Copyright (C) 2020 I. Bogoslavskyi, C. Stachniss
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include "projections/projection_params.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#include "utils/mem_utils.h"
namespace depth_clustering {
using std::vector;
using std::string;
using std::upper_bound;
using boost::algorithm::starts_with;
void ProjectionParams::SetSpan(const SpanParams& span_params,
const SpanParams::Direction& direction) {
vector<SpanParams> params_vec = {{span_params}};
this->SetSpan(params_vec, direction);
}
void ProjectionParams::SetSpan(const vector<SpanParams>& span_params,
const SpanParams::Direction& direction) {
int num_beams = 0;
for (const auto& span : span_params) {
num_beams += span.num_beams();
}
switch (direction) {
case SpanParams::Direction::HORIZONTAL:
_h_span_params = SpanParams(span_params.front().start_angle(),
span_params.back().end_angle(), num_beams);
_col_angles = FillVector(span_params);
break;
case SpanParams::Direction::VERTICAL:
_v_span_params = SpanParams(span_params.front().start_angle(),
span_params.back().end_angle(), num_beams);
_row_angles = FillVector(span_params);
break;
}
FillCosSin();
}
vector<Radians> ProjectionParams::FillVector(const SpanParams& span_params) {
vector<SpanParams> params_vec = {{span_params}};
return this->FillVector(params_vec);
}
vector<Radians> ProjectionParams::FillVector(
const vector<SpanParams>& span_params) {
vector<Radians> res;
for (const auto span_param : span_params) {
Radians rad = span_param.start_angle();
for (int i = 0; i < span_param.num_beams(); ++i) {
res.push_back(rad);
rad += span_param.step();
}
}
return res;
}
bool ProjectionParams::valid() {
bool all_params_valid = _v_span_params.valid() && _h_span_params.valid();
bool arrays_empty = _row_angles.empty() && _col_angles.empty();
bool cos_sin_empty = _row_angles_sines.empty() &&
_row_angles_cosines.empty() &&
_col_angles_sines.empty() && _col_angles_cosines.empty();
if (!all_params_valid) {
throw std::runtime_error("Projection parameters invalid.");
}
if (arrays_empty) {
throw std::runtime_error("Projection parameters arrays not filled.");
}
if (cos_sin_empty) {
throw std::runtime_error(
"Projection parameters sin and cos arrays not filled.");
}
return true;
}
const Radians ProjectionParams::AngleFromRow(int row) const {
if (row >= 0 && static_cast<size_t>(row) < _row_angles.size()) {
return _row_angles[row];
}
fprintf(stderr, "ERROR: row %d is wrong\n", row);
return 0.0_deg;
}
const Radians ProjectionParams::AngleFromCol(int col) const {
int actual_col = col;
if (col < 0) {
actual_col = col + _col_angles.size();
} else if (static_cast<size_t>(col) >= _col_angles.size()) {
actual_col = col - _col_angles.size();
}
// everything is normal
return _col_angles[actual_col];
}
size_t ProjectionParams::RowFromAngle(const Radians& angle) const {
return FindClosest(_row_angles, angle);
}
size_t ProjectionParams::ColFromAngle(const Radians& angle) const {
return FindClosest(_col_angles, angle);
}
size_t ProjectionParams::FindClosest(const vector<Radians>& vec,
const Radians& val) {
size_t found = 0;
if (vec.front() < vec.back()) {
found = upper_bound(vec.begin(), vec.end(), val) - vec.begin();
} else {
found = vec.rend() - upper_bound(vec.rbegin(), vec.rend(), val);
}
if (found == 0) {
return found;
}
if (found == vec.size()) {
return found - 1;
}
auto diff_next = Radians::Abs(vec[found] - val);
auto diff_prev = Radians::Abs(val - vec[found - 1]);
return diff_next < diff_prev ? found : found - 1;
}
std::unique_ptr<ProjectionParams> ProjectionParams::VLP_16() {
auto params = ProjectionParams();
params.SetSpan(SpanParams(-180_deg, 180_deg, 870),
SpanParams::Direction::HORIZONTAL);
params.SetSpan(SpanParams(15_deg, -15_deg, 16),
SpanParams::Direction::VERTICAL);
params.FillCosSin();
if (!params.valid()) {
fprintf(stderr, "ERROR: params are not valid!\n");
return nullptr;
}
return mem_utils::make_unique<ProjectionParams>(params);
}
std::unique_ptr<ProjectionParams> ProjectionParams::HDL_32() {
auto params = ProjectionParams();
params.SetSpan(SpanParams(-180_deg, 180_deg, 870),
SpanParams::Direction::HORIZONTAL);
params.SetSpan(SpanParams(10.0_deg, -30.0_deg, 32),
SpanParams::Direction::VERTICAL);
params.FillCosSin();
if (!params.valid()) {
fprintf(stderr, "ERROR: params are not valid!\n");
return nullptr;
}
return mem_utils::make_unique<ProjectionParams>(params);
}
std::unique_ptr<ProjectionParams> ProjectionParams::HDL_64_EQUAL() {
auto params = ProjectionParams();
params.SetSpan(SpanParams(-180_deg, 180_deg, 870),
SpanParams::Direction::HORIZONTAL);
params.SetSpan(SpanParams(2.0_deg, -24.0_deg, 64),
SpanParams::Direction::VERTICAL);
params.FillCosSin();
if (!params.valid()) {
fprintf(stderr, "ERROR: params are not valid!\n");
return nullptr;
}
return mem_utils::make_unique<ProjectionParams>(params);
}
std::unique_ptr<ProjectionParams> ProjectionParams::HDL_64() {
auto params = ProjectionParams();
params.SetSpan(SpanParams(-180_deg, 180_deg, 870),
SpanParams::Direction::HORIZONTAL);
SpanParams span_top(2.0_deg, -8.5_deg, 32);
SpanParams span_bottom(-8.87_deg, -24.87_deg, 32);
vector<SpanParams> spans = {{span_top, span_bottom}};
params.SetSpan(spans, SpanParams::Direction::VERTICAL);
params.FillCosSin();
if (!params.valid()) {
fprintf(stderr, "ERROR: params are not valid!\n");
return nullptr;
}
return mem_utils::make_unique<ProjectionParams>(params);
}
std::unique_ptr<ProjectionParams> ProjectionParams::FullSphere(
const Radians& discretization) {
auto params = ProjectionParams();
params.SetSpan(SpanParams(-180_deg, 180_deg, discretization),
SpanParams::Direction::HORIZONTAL);
params.SetSpan(SpanParams(-90_deg, 90_deg, discretization),
SpanParams::Direction::VERTICAL);
params.FillCosSin();
if (!params.valid()) {
fprintf(stderr, "ERROR: params are not valid!\n");
return nullptr;
}
return mem_utils::make_unique<ProjectionParams>(params);
}
std::unique_ptr<ProjectionParams> ProjectionParams::FromConfigFile(
const std::string& path) {
fprintf(stderr, "INFO: Set en_US.UTF-8 locale.\n");
std::locale::global(std::locale("en_US.UTF-8"));
fprintf(stderr, "INFO: Reading config.\n");
ProjectionParams params;
// we need to fill this thing. Parsing text files again. Is that what PhD in
// Robotics is about?
std::ifstream file(path.c_str());
if (!file.is_open()) {
fprintf(stderr, "ERROR: cannot open file: '%s'\n", path.c_str());
return nullptr;
}
for (std::string line; std::getline(file, line, '\n');) {
if (starts_with(line, "#")) {
// we have found a commentary
fprintf(stderr, "INFO: Skipping commentary: \n\t %s\n", line.c_str());
} else {
// here we parse the line
vector<string> str_angles;
boost::split(str_angles, line, boost::is_any_of(";"));
if (str_angles.size() < 4) {
fprintf(stderr, "ERROR: format of line is wrong.\n");
return nullptr;
}
int cols = std::stoi(str_angles[0]);
int rows = std::stoi(str_angles[1]);
params._h_span_params =
SpanParams(Radians::FromDegrees(std::stod(str_angles[2])),
Radians::FromDegrees(std::stod(str_angles[3])), cols);
fprintf(stderr, "start:%f, stop:%f, span:%f, step:%f\n",
params._h_span_params.start_angle().ToDegrees(),
params._h_span_params.end_angle().ToDegrees(),
params._h_span_params.span().ToDegrees(),
params._h_span_params.step().ToDegrees());
// fill the cols spacing
for (int c = 0; c < cols; ++c) {
params._col_angles.push_back(params._h_span_params.start_angle() +
params._h_span_params.step() * c);
}
// fill the rows
params._v_span_params =
SpanParams(Radians::FromDegrees(std::stod(str_angles[4])),
Radians::FromDegrees(std::stod(str_angles.back())), rows);
// fill the rows with respect to img.cfg spacings
for (size_t i = 4; i < str_angles.size(); ++i) {
params._row_angles.push_back(
Radians::FromDegrees(std::stof(str_angles[i])));
}
if (params._row_angles.size() != static_cast<size_t>(rows)) {
fprintf(stderr, "ERROR: wrong config\n");
return nullptr;
}
}
}
// fill cos and sin arrays
params.FillCosSin();
// check validity
if (!params.valid()) {
fprintf(stderr, "ERROR: the config read was not valid.\n");
return nullptr;
}
fprintf(stderr, "INFO: Params sucessfully read. Rows: %lu, Cols: %lu\n",
params._row_angles.size(), params._col_angles.size());
return mem_utils::make_unique<ProjectionParams>(params);
}
const std::vector<float>& ProjectionParams::RowAngleCosines() const {
return _row_angles_cosines;
}
const std::vector<float>& ProjectionParams::ColAngleCosines() const {
return _col_angles_cosines;
}
const std::vector<float>& ProjectionParams::RowAngleSines() const {
return _row_angles_sines;
}
const std::vector<float>& ProjectionParams::ColAngleSines() const {
return _col_angles_sines;
}
void ProjectionParams::FillCosSin() {
_row_angles_sines.clear();
_row_angles_cosines.clear();
for (const auto& angle : _row_angles) {
_row_angles_sines.push_back(sin(angle.val()));
_row_angles_cosines.push_back(cos(angle.val()));
}
_col_angles_sines.clear();
_col_angles_cosines.clear();
for (const auto& angle : _col_angles) {
_col_angles_sines.push_back(sin(angle.val()));
_col_angles_cosines.push_back(cos(angle.val()));
}
}
} // namespace depth_clustering