Welcome to mirror list, hosted at ThFree Co, Russian Federation.

road_graph_router.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbb0cea27382d34633c03b3e00a936f772143741 (plain)
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
#include "road_graph_router.hpp"
#include "features_road_graph.hpp"
#include "route.hpp"

#include "../indexer/feature.hpp"
#include "../indexer/ftypes_matcher.hpp"
#include "../indexer/index.hpp"

#include "../geometry/distance.hpp"


namespace routing
{

class Point2RoadPos
{
  m2::PointD m_point;
  double m_minDist;
  size_t m_segIdx;
  bool m_isOneway;
  FeatureID m_fID;

public:
  Point2RoadPos(m2::PointD const & pt)
    : m_point(pt), m_minDist(numeric_limits<double>::max())
  {
  }

  void operator() (FeatureType const & ft)
  {
    if (ft.GetFeatureType() != feature::GEOM_LINE ||
        !ftypes::IsStreetChecker::Instance()(ft))
      return;

    ft.ParseGeometry(FeatureType::BEST_GEOMETRY);

    size_t const count = ft.GetPointsCount() - 1;
    for (size_t i = 0; i < count; ++i)
    {
      m2::DistanceToLineSquare<m2::PointD> segDist;
      segDist.SetBounds(ft.GetPoint(i), ft.GetPoint(i + 1));
      double const d = segDist(m_point);
      if (d < m_minDist)
      {
        m_minDist = d;
        m_segIdx = i;
        m_fID = ft.GetID();
        m_isOneway = ftypes::IsStreetChecker::Instance().IsOneway(ft);
      }
    }
  }

  size_t GetMwmID() const
  {
    return m_fID.m_mwm;
  }

  void GetResults(vector<RoadPos> & res)
  {
    if (m_fID.IsValid())
    {
      res.push_back(RoadPos(m_fID.m_offset, true, m_segIdx));
      if (!m_isOneway)
        res.push_back(RoadPos(m_fID.m_offset, false, m_segIdx));
    }
  }
};

size_t RoadGraphRouter::GetRoadPos(m2::PointD const & pt, vector<RoadPos> & pos)
{
  Point2RoadPos getter(pt);
  m_pIndex->ForEachInRect(getter,
                          MercatorBounds::RectByCenterXYAndSizeInMeters(pt, 100.0),
                          FeatureRoadGraph::GetStreetReadScale());

  getter.GetResults(pos);
  return getter.GetMwmID();
}

bool RoadGraphRouter::IsMyMWM(size_t mwmID) const
{
  return (m_pRoadGraph && dynamic_cast<FeatureRoadGraph const *>(m_pRoadGraph.get())->GetMwmID() == mwmID);
}

void RoadGraphRouter::SetFinalPoint(m2::PointD const & finalPt)
{
  vector<RoadPos> finalPos;
  size_t const mwmID = GetRoadPos(finalPt, finalPos);

  if (!finalPos.empty())
  {
    if (!IsMyMWM(mwmID))
      m_pRoadGraph.reset(new FeatureRoadGraph(m_pIndex, mwmID));

    SetFinalRoadPos(finalPos);
  }
}

void RoadGraphRouter::CalculateRoute(m2::PointD const & startPt, ReadyCallback const & callback)
{
  if (!m_pRoadGraph)
    return;

  vector<RoadPos> startPos;
  size_t const mwmID = GetRoadPos(startPt, startPos);

  if (startPos.empty() || !IsMyMWM(mwmID))
    return;

  vector<RoadPos> routePos;
  CalculateRoute(startPos, routePos);

  Route route;
  m_pRoadGraph->ReconstructPath(routePos, route);
  callback(route);
}

} // namespace routing