forked from 1flei/obstacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
485 lines (421 loc) · 11.6 KB
/
util.h
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#pragma once
#include <optional>
#include <vector>
#include "tl/function_ref.hpp"
#include <tuple>
#include <iomanip>
// const double time_interval = 600; //ten minites
// const double time_interval_freq = 30; //30 seconds
// const double time_interval = 30; //30 seconds
// const int sliding_window_len = 6;
// const int sliding_window_step = 1;
const double eps = 1e-7;
inline bool eps_eq(double a, double b, double eps_=eps)
{
return abs(a-b) < eps_;
}
inline bool eps_gte(double a, double b, double eps_=eps)
{
return a-b > -eps_;
}
inline bool eps_gt(double a, double b, double eps_=eps)
{
return a-b > eps_;
}
struct Pnt3
{
double t, x, y;
Pnt3() : t(0), x(0), y(0) {}
Pnt3(double t, double x, double y): t(t), x(x), y(y) {}
bool operator<(const Pnt3& p) const {
return t<p.t;
}
template<class OS>
friend OS& operator<<(OS& o, const Pnt3& p) {
return o << "(" << p.t << "," << p.x << "," << p.y << ")";
}
};
inline double eudist2(const Pnt3& p0, const Pnt3& p1)
{
return (p0.x-p1.x)*(p0.x-p1.x) + (p0.y-p1.y)*(p0.y-p1.y);
}
inline double seg_eudist2(const Pnt3& p0, const Pnt3& p1, const Pnt3& p2, const Pnt3& p3)
{
return (p1.x+p2.x-p0.x-p3.x)*(p1.x+p2.x-p0.x-p3.x) + (p1.y+p2.y-p0.y-p3.y)*(p1.y+p2.y-p0.y-p3.y);
}
struct Traj
{
std::vector<double> data;
Pnt3& pnt_at(int i){
return *((Pnt3*)&(data[i*3]));
}
Pnt3& operator[](int i){
return pnt_at(i);
}
const Pnt3& pnt_at(int i) const{
return *((Pnt3*)&(data[i*3]));
}
const Pnt3& operator[](int i) const{
return pnt_at(i);
}
void push_back(const Pnt3& p){
data.push_back(p.t);
data.push_back(p.x);
data.push_back(p.y);
}
void emplace_back(double t, double x, double y){
data.push_back(t);
data.push_back(x);
data.push_back(y);
}
//might be unsafe
void push_back_partial(double x){
data.push_back(x);
}
std::vector<double>& get_flat_data(){
return data;
}
int64_t get_num_pnts() const {
return data.size()/3;
}
Pnt3* begin() const {
return (Pnt3*)(&data[0]);
}
Pnt3* end() const {
return begin()+get_num_pnts();
}
};
struct TrajView
{
Pnt3* p;
int64_t nPnts;
TrajView() : p(nullptr), nPnts(0) {}
TrajView(const Pnt3* p_, int64_t nPnts_) : p((Pnt3*)p_), nPnts(nPnts_){}
TrajView(const Traj& t, int offset, int64_t nPnts_) : p((Pnt3*)&t[offset]), nPnts(nPnts_){}
Pnt3& pnt_at(int i){
return *((Pnt3*)&(p[i]));
}
Pnt3& operator[](int i){
return pnt_at(i);
}
const Pnt3& pnt_at(int i) const{
return *((Pnt3*)&(p[i]));
}
const Pnt3& operator[](int i) const{
return pnt_at(i);
}
int64_t get_num_pnts() const {
return nPnts;
}
template<class OS>
friend OS& operator<<(OS& o, const TrajView& idx) {
if(idx.nPnts<=0) {
return o << "[]";
}
o << "[" << idx.p[0];
for(int i=1;i<idx.nPnts;i++){
o << "," << idx.p[i];
}
return o << "]";
}
std::vector<double> to_flat_vec() const {
std::vector<double> ret;
for(int i=0;i<nPnts;i++){
Pnt3& pt = p[i];
ret.push_back(pt.t);
ret.push_back(pt.x);
ret.push_back(pt.y);
}
return ret;
}
std::vector<double> to_flat_vec_spatial() const {
std::vector<double> ret;
for(int i=0;i<nPnts;i++){
Pnt3& pt = p[i];
ret.push_back(pt.x);
ret.push_back(pt.y);
}
return ret;
}
Pnt3* begin() const {
return p;
}
Pnt3* end() const {
return begin()+nPnts;
}
};
inline std::optional<TrajView> try_get_trajview(const Traj& t, int offset, int64_t nPnts, double interval)
{
if(offset <0 || offset+nPnts > t.get_num_pnts()) {
return {};
}
double t0 = t[offset].t;
double t1 = t[offset+nPnts-1].t;
if( eps_eq(t1-t0, interval*(nPnts-1) ) ) {
return TrajView(t, offset, nPnts);
}
return {};
}
inline std::optional<TrajView> try_get_trajview_clamp01(const Traj& t, int offset, int64_t nPnts, double interval)
{
for(int cur_offset=offset; cur_offset>=0;--cur_offset){
auto ret = try_get_trajview(t, cur_offset, nPnts, interval);
if(ret){
return ret;
}
}
return {};
}
// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// https://stackoverflow.com/questions/4948780
template <class T0, class T>
inline void hash_combine(T0& seed, T const& v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
template <class T0, class T>
inline T0 hash_combine_bop(T0 const& seed, T const& v)
{
return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2) );
}
//---------------------------------------------------------
//tuple hasher
// function has to live in the std namespace
// so that it is picked up by argument-dependent name lookup (ADL).
namespace std{
namespace
{
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl
{
static void apply(size_t& seed, Tuple const& tuple)
{
HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
hash_combine(seed, get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple,0>
{
static void apply(size_t& seed, Tuple const& tuple)
{
hash_combine(seed, get<0>(tuple));
}
};
}
template <typename ... TT>
struct hash<std::tuple<TT...>>
{
size_t
operator()(std::tuple<TT...> const& tt) const
{
size_t seed = 0;
HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
return seed;
}
};
}
//format
#define FMT_HEADER_ONLY
#include "fmt/format.h"
#include "fmt/ranges.h"
#include "fmt/ostream.h"
#include "fmt/printf.h"
//use function_ref as callback
inline void sliding_window(const Traj& t,
int len,
int step,
double interval,
const tl::function_ref<void(const TrajView&)>& f
)
{
for(int i=0;i+len<=t.get_num_pnts();i+=step){
double t0 = t[i].t;
double t1 = t[i+len-1].t;
if( eps_eq(t1-t0, interval*(len-1) ) ) {
f(TrajView(&t[i], len));
}
}
}
inline void enum_sliding_window(const Traj& t,
int len,
int step,
double interval,
const tl::function_ref<void(int, const TrajView&)>& f
)
{
for(int i=0;i+len<=t.get_num_pnts();i+=step){
double t0 = t[i].t;
double t1 = t[i+len-1].t;
// fmt::print("delta-t={}, actual_interval={}\n", t1-t0, interval*(len-1));
if( eps_eq(t1-t0, interval*(len-1) ) ) {
f(i, TrajView(&t[i], len));
}
}
}
inline void sliding_window_n(const Traj& t,
int cnt,
int len,
int step,
double interval,
const tl::function_ref<void(const TrajView&)>& f
)
{
for(int i=0;i+len<=t.get_num_pnts() && cnt>=0;i+=step){
double t0 = t[i].t;
double t1 = t[i+len-1].t;
if( eps_eq(t1-t0, interval*(len-1) ) ) {
f(TrajView(&t[i], len));
--cnt;
}
}
}
// -----------------------------------------------------------------------------
template<class ScalarType>
ScalarType sqr(ScalarType x)
{
return x*x;
}
//FProd :: ParamType -> ParamType -> ScalarType
//FSum :: ScalarType -> ScalarType -> ScalarType
template<class ScalarType, class ParamType, class FProd, class FSum>
inline ScalarType fast_reduce(int dim, const ParamType* x, const ParamType* y,
FProd&& fp, FSum&& fs)
{
unsigned d = dim & ~unsigned(7);
const ParamType *aa = x, *end_a = aa + d;
const ParamType *bb = y, *end_b = bb + d;
#ifdef __GNUC__
__builtin_prefetch(aa, 0, 3);
__builtin_prefetch(bb, 0, 0);
#endif
ScalarType r = 0.0;
ScalarType r0, r1, r2, r3, r4, r5, r6, r7;
const ParamType *a = end_a, *b = end_b;
r0 = r1 = r2 = r3 = r4 = r5 = r6 = r7 = 0.0;
switch (dim & 7) {
case 7: r6 = fp(a[6], b[6]);
// fall through
case 6: r5 = fp(a[5], b[5]);
// fall through
case 5: r4 = fp(a[4], b[4]);
// fall through
case 4: r3 = fp(a[3], b[3]);
// fall through
case 3: r2 = fp(a[2], b[2]);
// fall through
case 2: r1 = fp(a[1], b[1]);
// fall through
case 1: r0 = fp(a[0], b[0]);
}
a = aa; b = bb;
const auto fsum8 = [&](){
auto r01 = fs(r0, r1);
auto r23 = fs(r2, r3);
auto r45 = fs(r4, r5);
auto r67 = fs(r6, r7);
auto r0123 = fs(r01, r23);
auto r4567 = fs(r45, r67);
return fs(r0123, r4567);
};
for (; a < end_a; a += 8, b += 8) {
#ifdef __GNUC__
__builtin_prefetch(a + 32, 0, 3);
__builtin_prefetch(b + 32, 0, 0);
#endif
r = fs(r, fsum8() );
r0 = fp(a[0], b[0]);
r1 = fp(a[1], b[1]);
r2 = fp(a[2], b[2]);
r3 = fp(a[3], b[3]);
r4 = fp(a[4], b[4]);
r5 = fp(a[5], b[5]);
r6 = fp(a[6], b[6]);
r7 = fp(a[7], b[7]);
}
r = fs(r, fsum8() );
return r;
}
template<class ScalarType>
inline ScalarType calc_l2_sqr(int dim, const ScalarType* x, const ScalarType* y)
{
const auto fProd = [](ScalarType a, ScalarType b) -> ScalarType{
return sqr(a-b);
};
const auto fSum = [](ScalarType a, ScalarType b) -> ScalarType{
return a+b;
};
return fast_reduce<ScalarType, ScalarType>(dim, x, y, fProd, fSum);
}
template<class ScalarType>
inline ScalarType calc_l1_dist(int dim, const ScalarType* x, const ScalarType* y)
{
const auto fProd = [](ScalarType a, ScalarType b){
return abs(a-b);
};
const auto fSum = [](ScalarType a, ScalarType b){
return a+b;
};
return fast_reduce<ScalarType, ScalarType>(dim, x, y, fProd, fSum);
}
template<class ScalarType>
inline ScalarType calc_inner_product(int dim, const ScalarType* x, const ScalarType* y)
{
const auto fProd = [](ScalarType a, ScalarType b){
return a*b;
};
const auto fSum = [](ScalarType a, ScalarType b){
return a+b;
};
return fast_reduce<ScalarType, ScalarType>(dim, x, y, fProd, fSum);
}
// -----------------------------------------------------------------------------
template<class ScalarType>
ScalarType calc_l2_dist( // calc L2 distance
int dim, // dimension
const ScalarType *p1, // 1st point
const ScalarType *p2) // 2nd point
{
return sqrt(calc_l2_sqr(dim, p1, p2));
}
struct BBox
{
double minlat, maxlat;
double minlng, maxlng;
double mint, maxt;
BBox() : minlat(-180), maxlat(180), minlng(-180), maxlng(180), mint(0), maxt(2e9){}
BBox(double minlng, double maxlng, double minlat, double maxlat, double mint, double maxt) :
minlat(minlat), maxlat(maxlat), minlng(minlng), maxlng(maxlng), mint(mint), maxt(maxt){}
};
template<class IdxType=int>
class HashDisjointSet
{
public:
HashDisjointSet(){}
~HashDisjointSet(){}
IdxType get(IdxType u){
if(parent.find(u)==parent.end()){
parent.emplace(u, u);
return u;
} else if(parent[u]==u){
return u;
} else{
IdxType ret = get(parent[u]);
parent[u] = ret;
return ret;
}
}
//make sure pu is parent of pv
void merge(IdxType u, IdxType v){
IdxType pu = get(u);
IdxType pv = get(v);
if(pu!=pv){
parent[pv] = pu;
}
}
std::unordered_map<IdxType, IdxType> parent;
};