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
|
#include <mbgl/text/check_max_angle.hpp>
#include <mbgl/geometry/anchor.hpp>
#include <mbgl/util/math.hpp>
#include <queue>
namespace mbgl{
struct Corner {
Corner(float _distance, float _angleDelta) :
distance(_distance), angleDelta(_angleDelta) {}
float distance;
float angleDelta;
};
bool checkMaxAngle(const GeometryCoordinates& line,
const Anchor& anchor,
const float labelLength,
const float windowSize,
const float maxAngle) {
// horizontal labels always pass
if (anchor.segment < 0) return true;
GeometryCoordinate anchorPoint = convertPoint<int16_t>(anchor.point);
GeometryCoordinate &p = anchorPoint;
int index = anchor.segment + 1;
float anchorDistance = 0;
// move backwards along the line to the first segment the label appears on
while (anchorDistance > -labelLength / 2) {
index--;
// there isn't enough room for the label after the beginning of the line
if (index < 0) return false;
anchorDistance -= util::dist<float>(line[index], p);
p = line[index];
}
anchorDistance += util::dist<float>(line[index], line[index + 1]);
index++;
// store recent corners and their total angle difference
std::queue<Corner> recentCorners;
float recentAngleDelta = 0;
// move forwards by the length of the label and check angles along the way
while (anchorDistance < labelLength / 2) {
// there isn't enough room for the label before the end of the line
if (index + 1 >= (int)line.size()) return false;
auto& prev = line[index - 1];
auto& current = line[index];
auto& next = line[index + 1];
float angleDelta = util::angle_to(prev, current) - util::angle_to(current, next);
// restrict angle to -pi..pi range
angleDelta = std::fabs(std::fmod(angleDelta + 3 * M_PI, M_PI * 2) - M_PI);
recentCorners.emplace(anchorDistance, angleDelta);
recentAngleDelta += angleDelta;
// remove corners that are far enough away from the list of recent anchors
while (anchorDistance - recentCorners.front().distance > windowSize) {
recentAngleDelta -= recentCorners.front().angleDelta;
recentCorners.pop();
}
// the sum of angles within the window area exceeds the maximum allowed value. check fails.
if (recentAngleDelta > maxAngle) return false;
index++;
anchorDistance += util::dist<float>(current, next);
}
// no part of the line had an angle greater than the maximum allowed. check passes.
return true;
}
} // namespace mbgl
|