forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearest_edge_finder.cpp
126 lines (102 loc) · 4.24 KB
/
nearest_edge_finder.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
#include "routing/nearest_edge_finder.hpp"
#include "geometry/parametrized_segment.hpp"
#include "indexer/feature.hpp"
#include "base/assert.hpp"
namespace routing
{
using namespace std;
NearestEdgeFinder::NearestEdgeFinder(m2::PointD const & point, IsEdgeProjGood const & isEdgeProjGood)
: m_point(point), m_isEdgeProjGood(isEdgeProjGood)
{
}
void NearestEdgeFinder::AddInformationSource(IRoadGraph::FullRoadInfo const & roadInfo)
{
if (!roadInfo.m_featureId.IsValid())
return;
Candidate res;
auto const & junctions = roadInfo.m_roadInfo.m_junctions;
size_t const count = junctions.size();
ASSERT_GREATER(count, 1, ());
for (size_t i = 1; i < count; ++i)
{
m2::ParametrizedSegment<m2::PointD> segment(junctions[i - 1].GetPoint(),
junctions[i].GetPoint());
m2::PointD const closestPoint = segment.ClosestPointTo(m_point);
double const squaredDist = m_point.SquaredLength(closestPoint);
if (squaredDist < res.m_squaredDist)
{
res.m_segId = static_cast<uint32_t>(i - 1);
res.m_squaredDist = squaredDist;
}
}
if (res.m_segId == Candidate::kInvalidSegmentId)
return;
// Closest point to |this->m_point| found. It has index |res.m_segId + 1| in |junctions|.
size_t const idx = res.m_segId + 1;
geometry::PointWithAltitude const & segStart = junctions[idx - 1];
geometry::PointWithAltitude const & segEnd = junctions[idx];
geometry::Altitude const startAlt = segStart.GetAltitude();
geometry::Altitude const endAlt = segEnd.GetAltitude();
m2::ParametrizedSegment<m2::PointD> segment(junctions[idx - 1].GetPoint(),
junctions[idx].GetPoint());
m2::PointD const closestPoint = segment.ClosestPointTo(m_point);
double const segLenM = mercator::DistanceOnEarth(segStart.GetPoint(), segEnd.GetPoint());
geometry::Altitude projPointAlt = geometry::kDefaultAltitudeMeters;
if (segLenM == 0.0)
{
projPointAlt = startAlt;
}
else
{
double const distFromStartM = mercator::DistanceOnEarth(segStart.GetPoint(), closestPoint);
ASSERT_LESS_OR_EQUAL(distFromStartM, segLenM, (roadInfo.m_featureId));
projPointAlt =
startAlt + static_cast<geometry::Altitude>((endAlt - startAlt) * distFromStartM / segLenM);
}
res.m_fid = roadInfo.m_featureId;
res.m_segStart = segStart;
res.m_segEnd = segEnd;
res.m_bidirectional = roadInfo.m_roadInfo.m_bidirectional;
ASSERT_NOT_EQUAL(res.m_segStart.GetAltitude(), geometry::kInvalidAltitude, ());
ASSERT_NOT_EQUAL(res.m_segEnd.GetAltitude(), geometry::kInvalidAltitude, ());
res.m_projPoint = geometry::PointWithAltitude(closestPoint, projPointAlt);
m_candidates.emplace_back(res);
}
void NearestEdgeFinder::MakeResult(vector<pair<Edge, geometry::PointWithAltitude>> & res,
size_t maxCountFeatures)
{
sort(m_candidates.begin(), m_candidates.end(), [](Candidate const & r1, Candidate const & r2)
{
return r1.m_squaredDist < r2.m_squaredDist;
});
res.clear();
res.reserve(maxCountFeatures);
for (Candidate const & candidate : m_candidates)
{
CandidateToResult(candidate, maxCountFeatures, res);
if (res.size() >= maxCountFeatures)
return;
}
}
void NearestEdgeFinder::CandidateToResult(
Candidate const & candidate, size_t maxCountFeatures,
vector<pair<Edge, geometry::PointWithAltitude>> & res) const
{
AddResIf(candidate, true /* forward */, maxCountFeatures, res);
if (candidate.m_bidirectional)
AddResIf(candidate, false /* forward */, maxCountFeatures, res);
}
void NearestEdgeFinder::AddResIf(Candidate const & candidate, bool forward, size_t maxCountFeatures,
vector<pair<Edge, geometry::PointWithAltitude>> & res) const
{
if (res.size() >= maxCountFeatures)
return;
geometry::PointWithAltitude const & start = forward ? candidate.m_segStart : candidate.m_segEnd;
geometry::PointWithAltitude const & end = forward ? candidate.m_segEnd : candidate.m_segStart;
auto const & edge = Edge::MakeReal(candidate.m_fid, forward, candidate.m_segId, start,end);
auto const & edgeProj = make_pair(edge, candidate.m_projPoint);
if (m_isEdgeProjGood && !m_isEdgeProjGood(edgeProj))
return;
res.emplace_back(edgeProj);
}
} // namespace routing