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

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

#include "routing/routing_exception.hpp"

namespace routing
{
Joint::Id JointIndex::InsertJoint(RoadPoint const & rp)
{
  Joint::Id const jointId = GetNumJoints();
  m_points.emplace_back(rp);
  m_offsets.emplace_back(m_points.size());
  return jointId;
}

void JointIndex::AppendToJoint(Joint::Id jointId, RoadPoint rp)
{
  m_dynamicJoints[jointId].AddPoint(rp);
}

void JointIndex::FindPointsWithCommonFeature(Joint::Id jointId0, Joint::Id jointId1,
                                             vector<pair<RoadPoint, RoadPoint>> & result) const
{
  result.clear();
  ForEachPoint(jointId0, [&](RoadPoint const & rp0)
  {
    ForEachPoint(jointId1, [&](RoadPoint const & rp1)
    {
      if (rp0.GetFeatureId() == rp1.GetFeatureId())
        result.emplace_back(rp0, rp1);
    });
  });

  if (result.empty())
    MYTHROW(RootException, ("Can't find common feature for joints", jointId0, jointId1));
}

void JointIndex::Build(RoadIndex const & roadIndex, uint32_t numJoints)
{
  // + 1 is protection for 'End' method from out of bounds.
  // Call End(numJoints-1) requires more size, so add one more item.
  // Therefore m_offsets.size() == numJoints + 1,
  // And m_offsets.back() == m_points.size()
  m_offsets.assign(numJoints + 1, 0);

  // Calculate sizes.
  // Example for numJoints = 6:
  // 2, 5, 3, 4, 2, 3, 0
  roadIndex.ForEachRoad([this, numJoints](uint32_t /* featureId */, RoadJointIds const & road) {
    road.ForEachJoint([this, numJoints](uint32_t /* pointId */, Joint::Id jointId) {
      ASSERT_LESS(jointId, numJoints, ());
      ++m_offsets[jointId];
    });
  });

  // Fill offsets with end bounds.
  // Example: 2, 7, 10, 14, 16, 19, 19
  for (size_t i = 1; i < m_offsets.size(); ++i)
    m_offsets[i] += m_offsets[i - 1];

  m_points.resize(m_offsets.back());

  // Now fill points.
  // Offsets after this operation are begin bounds:
  // 0, 2, 7, 10, 14, 16, 19
  roadIndex.ForEachRoad([this](uint32_t featureId, RoadJointIds const & road) {
    road.ForEachJoint([this, featureId](uint32_t pointId, Joint::Id jointId) {
      uint32_t & offset = m_offsets[jointId];
      --offset;
      m_points[offset] = {featureId, pointId};
    });
  });

  if (m_offsets[0] != 0)
    MYTHROW(RoutingException, ("Wrong offsets calculation: m_offsets[0] =", m_offsets[0]));

  if (m_offsets.back() != m_points.size())
    MYTHROW(RoutingException, ("Wrong offsets calculation: m_offsets.back() =", m_offsets.back(),
                               ", m_points.size()=", m_points.size()));
}
}  // namespace routing