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

decoded_path_test.cpp « openlr_tests « openlr - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9035e3eddb15bdccc02279b484efa66cef07b4b (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "testing/testing.hpp"

#include "openlr/decoded_path.hpp"

#include "generator/generator_tests_support/test_feature.hpp"
#include "generator/generator_tests_support/test_mwm_builder.hpp"

#include "indexer/classificator_loader.hpp"
#include "indexer/data_source.hpp"

#include "platform/country_file.hpp"
#include "platform/local_country_file.hpp"
#include "platform/platform.hpp"
#include "platform/platform_tests_support/scoped_dir.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"

#include "base/file_name_utils.hpp"
#include "base/checked_cast.hpp"

#include <algorithm>
#include <iomanip>
#include <sstream>
#include <vector>

#include "3party/pugixml/src/pugixml.hpp"
#include "3party/pugixml/src/utils.hpp"

using namespace generator::tests_support;
using namespace platform::tests_support;
using namespace platform;
using namespace std;

namespace
{
string const kTestDir = "openlr_decoded_path_test";
string const kTestMwm = "test";

double RoughUpToFive(double d)
{
  stringstream s;
  s << setprecision(5) << fixed;
  s << d;
  s >> d;
  return d;
}

m2::PointD RoughPoint(m2::PointD const & p) { return {RoughUpToFive(p.x), RoughUpToFive(p.y)}; }

geometry::PointWithAltitude RoughJunction(geometry::PointWithAltitude const & j)
{
  return geometry::PointWithAltitude(RoughPoint(j.GetPoint()), j.GetAltitude());
}

routing::Edge RoughEdgeJunctions(routing::Edge const & e)
{
  return routing::Edge::MakeReal(e.GetFeatureId(), e.IsForward(), e.GetSegId(),
                                 RoughJunction(e.GetStartJunction()),
                                 RoughJunction(e.GetEndJunction()));
}

void RoughJunctionsInPath(openlr::Path & p)
{
  for (auto & e : p)
    e = RoughEdgeJunctions(e);
}

void TestSerializeDeserialize(openlr::Path const & path, DataSource const & dataSource)
{
  pugi::xml_document doc;
  openlr::PathToXML(path, doc);

  openlr::Path restoredPath;
  openlr::PathFromXML(doc, dataSource, restoredPath);

  // Fix mercator::From/ToLatLon floating point error
  // for we could use TEST_EQUAL on result.
  RoughJunctionsInPath(restoredPath);

  TEST_EQUAL(path, restoredPath, ());
}

openlr::Path MakePath(FeatureType const & road, bool const forward)
{
  CHECK_EQUAL(road.GetGeomType(), feature::GeomType::Line, ());
  CHECK_GREATER(road.GetPointsCount(), 0, ());
  openlr::Path path;

  size_t const maxPointIndex = road.GetPointsCount() - 1;
  for (size_t i = 0; i < maxPointIndex; ++i)
  {
    size_t current{};
    size_t next{};
    if (forward)
    {
      current = i;
      next = i + 1;
    }
    else
    {
      current = maxPointIndex - i;
      next = current - 1;
    }

    auto const from = road.GetPoint(current);
    auto const to = road.GetPoint(next);
    path.push_back(routing::Edge::MakeReal(
        road.GetID(), forward,
        base::checked_cast<uint32_t>(current - static_cast<size_t>(!forward)) /* segId */,
        geometry::PointWithAltitude(from, 0 /* altitude */),
        geometry::PointWithAltitude(to, 0 /* altitude */)));
  }

  RoughJunctionsInPath(path);

  return path;
}

template <typename Func>
void WithRoad(vector<m2::PointD> const & points, Func && fn)
{
  classificator::Load();
  auto & platform = GetPlatform();

  auto const mwmPath = base::JoinPath(platform.WritableDir(), kTestDir);

  LocalCountryFile country(mwmPath, CountryFile(kTestMwm), 0 /* version */);
  ScopedDir testScopedDir(kTestDir);
  ScopedFile testScopedMwm(base::JoinPath(kTestDir, kTestMwm + DATA_FILE_EXTENSION),
                           ScopedFile::Mode::Create);

  {
    TestMwmBuilder builder(country, feature::DataHeader::MapType::Country);
    builder.Add(TestRoad(points, "Interstate 60", "en"));
  }

  FrozenDataSource dataSource;
  auto const regResult = dataSource.RegisterMap(country);
  TEST_EQUAL(regResult.second, MwmSet::RegResult::Success, ());

  MwmSet::MwmHandle mwmHandle = dataSource.GetMwmHandleById(regResult.first);
  TEST(mwmHandle.IsAlive(), ());

  FeaturesLoaderGuard const guard(dataSource, regResult.first);
  auto road = guard.GetFeatureByIndex(0);
  TEST(road, ());

  road->ParseGeometry(FeatureType::BEST_GEOMETRY);
  fn(dataSource, *road);
}

UNIT_TEST(MakePath_Test)
{
  std::vector<m2::PointD> const points{{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  WithRoad(points, [&points](DataSource const & dataSource, FeatureType & road) {
    auto const & id = road.GetID();
    {
      openlr::Path const expected{
          routing::Edge::MakeReal(id, true /* forward */, 0 /* segId*/,
                                  {points[0], 0 /* altitude */}, {points[1], 0 /* altitude */}),
          routing::Edge::MakeReal(id, true /* forward */, 1 /* segId*/,
                                  {points[1], 0 /* altitude */}, {points[2], 0 /* altitude */}),
          routing::Edge::MakeReal(id, true /* forward */, 2 /* segId*/,
                                  {points[2], 0 /* altitude */}, {points[3], 0 /* altitude */})};
      auto const path = MakePath(road, true /* forward */);
      TEST_EQUAL(path, expected, ());
    }
    {
      openlr::Path const expected{
          routing::Edge::MakeReal(id, false /* forward */, 2 /* segId*/,
                                  {points[3], 0 /* altitude */}, {points[2], 0 /* altitude */}),
          routing::Edge::MakeReal(id, false /* forward */, 1 /* segId*/,
                                  {points[2], 0 /* altitude */}, {points[1], 0 /* altitude */}),
          routing::Edge::MakeReal(id, false /* forward */, 0 /* segId*/,
                                  {points[1], 0 /* altitude */}, {points[0], 0 /* altitude */})};
      {
        auto const path = MakePath(road, false /* forward */);
        TEST_EQUAL(path, expected, ());
      }
    }
  });
}

UNIT_TEST(PathSerializeDeserialize_Test)
{
  WithRoad({{0, 0}, {0, 1}, {1, 0}, {1, 1}}, [](DataSource const & dataSource, FeatureType & road) {
    {
      auto const path = MakePath(road, true /* forward */);
      TestSerializeDeserialize(path, dataSource);
    }
    {
      auto const path = MakePath(road, false /* forward */);
      TestSerializeDeserialize(path, dataSource);
    }
  });
}

UNIT_TEST(GetPoints_Test)
{
  vector<m2::PointD> const points{{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  WithRoad(points, [&points](DataSource const &, FeatureType & road) {
    {
      auto path = MakePath(road, true /* forward */);
      // RoughJunctionsInPath(path);
      TEST_EQUAL(GetPoints(path), points, ());
    }
    {
      auto path = MakePath(road, false /* forward */);
      // RoughJunctionsInPath(path);
      auto reversed = points;
      reverse(begin(reversed), end(reversed));
      TEST_EQUAL(GetPoints(path), reversed, ());
    }
  });
}
}  // namespace