-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvgen_distance.h
97 lines (86 loc) · 2.54 KB
/
vgen_distance.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
// .___
// ___ __ ____ ___ ___ ____ __| _/
// \ \/ // __ \\ \/ // __ \ / __ |
// \ /\ ___/ > <\ ___// /_/ |
// \_/ \___ >__/\_ \\___ >____ |
// \/ \/ \/ \/ __ .__
// ____ ____ ____ ________________ _/ |_|__| ____ ____
// / ___\_/ __ \ / \_/ __ \_ __ \__ \\ __\ |/ _ \ / \
// / /_/ > ___/| | \ ___/| | \// __ \| | | ( <_> ) | \
// \___ / \___ >___| /\___ >__| (____ /__| |__|\____/|___| /
// /_____/ \/ \/ \/ \/ \/
//
// (c) 2016 - 2020 Karsten Schmidt // ASL 2.0 licensed
#ifndef __vgen_distance_h__
#define __vgen_distance_h__
vector vg_closest_point_line(const vector a; const vector b; const vector p) {
float len = length(b - a);
if (len < EPS) {
return a;
}
float t = dot(p - a, b - a) / (len * len);
if (t < 0) {
return a;
} else if (t > 1) {
return b;
}
return a + (b - a) * t;
}
/**
* Computes closest point to `p` in given array of point IDs.
* Returns -1 if `pts` was empty.
*/
int vg_closest_point_id(int geo; const int pts[]; const vector p) {
int closest = -1;
float minD = 1e6;
for (int i = len(pts); --i >= 0;) {
float d = distance(p, point(geo, "P", pts[i]));
if (d < minD) {
minD = d;
closest = pts[i];
}
}
return closest;
}
/**
* Computes closest point on given set of edges (pairs of point IDs).
*/
vector vg_closest_point_edges(int geo; const int edges[]; const vector p) {
vector closest;
float minD = 1e6;
for (int i = len(edges) - 2; i >= 0; i -= 2) {
vector q = vg_closest_point_line(point(geo, "P", edges[i]),
point(geo, "P", edges[i + 1]), p);
float d = distance(p, q);
if (d < minD) {
minD = d;
closest = q;
}
}
return closest;
}
float vg_dist_manhattan(vector2 a, b) {
vector d = abs(a - b);
return d.x + d.y;
}
float vg_dist_manhattan(vector a, b) {
vector d = abs(a - b);
return d.x + d.y + d.z;
}
float vg_dist_manhattan(vector4 a, b) {
vector4 d = abs(a - b);
return d.x + d.y + d.z + d.w;
}
float vg_dist_chebyshev(vector2 a, b) {
vector d = abs(a - b);
return max(d.x, d.y);
}
float vg_dist_chebyshev(vector a, b) {
vector d = abs(a - b);
return max(d.x, d.y, d.z);
}
float vg_dist_chebyshev(vector4 a, b) {
vector4 d = abs(a - b);
return max(d.x, d.y, d.z, d.w);
}
#endif