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

spline_test.cpp « geometry_tests « geometry - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d90134058387c137ae6bcdc81adc7f99ff306f22 (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
#include "../../base/SRC_FIRST.hpp"
#include "../../testing/testing.hpp"
#include "equality.hpp"
#include "../spline.hpp"

using m2::Spline;
using m2::PointD;

void TestPointDDir(PointD const & dst, PointD const & src)
{
  double len1 = dst.Length();
  double len2 = src.Length();
  TEST_ALMOST_EQUAL(dst.x/len1, src.x/len2, ());
  TEST_ALMOST_EQUAL(dst.y/len1, src.y/len2, ());
}

UNIT_TEST(SmoothedDirections)
{
  vector<PointD> path;
  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 0));

  Spline spl(path);
  double const sqrt2 = sqrt(2.0);
  Spline::iterator itr;
  PointD dir1(sqrt2 / 2.0, sqrt2 / 2.0);
  PointD dir2(sqrt2 / 2.0, -sqrt2 / 2.0);
  itr.Attach(spl);
  TestPointDDir(itr.m_avrDir, dir1);
  itr.Advance(sqrt2 * 30.0);
  TestPointDDir(itr.m_avrDir, dir1);
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_avrDir, dir1 * 0.25 + dir2 * 0.75);
  itr.Advance(sqrt2 * 10.0);
  TestPointDDir(itr.m_avrDir, dir2);

  path.clear();

  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 40));
  path.push_back(PointD(120, 0));

  PointD dir12(1.0, 0.0);
  Spline spl2(path);
  itr.Attach(spl2);
  TestPointDDir(itr.m_avrDir, dir1);
  itr.Advance(sqrt2 * 80.0 + 40.0);
  TestPointDDir(itr.m_avrDir, dir12);
  itr.Attach(spl2);
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_avrDir, dir1);
  itr.Advance(80.0);
  TestPointDDir(itr.m_avrDir, dir12 * 0.5 + dir2 * 0.5);
}

UNIT_TEST(UsualDirections)
{
  vector<PointD> path;
  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 0));

  Spline spl(path);
  double const sqrt2 = sqrtf(2.0);
  Spline::iterator itr;
  PointD dir1(sqrt2 / 2.0, sqrt2 / 2.0);
  PointD dir2(sqrt2 / 2.0, -sqrt2 / 2.0);
  itr.Attach(spl);
  TestPointDDir(itr.m_dir, dir1);
  itr.Advance(sqrt2 * 30.0);
  TestPointDDir(itr.m_dir, dir1);
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_dir, dir2);

  path.clear();

  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 40));
  path.push_back(PointD(120, 0));

  PointD dir12(1.0, 0.0);
  Spline spl2(path);
  itr.Attach(spl2);
  TestPointDDir(itr.m_dir, dir1);
  itr.Advance(sqrt2 * 80.0 + 35.0);
  TestPointDDir(itr.m_dir, dir2);
  itr.Attach(spl2);
  itr.Advance(sqrt2 * 45.0);
  TestPointDDir(itr.m_dir, dir12);
  itr.Advance(80.0);
  TestPointDDir(itr.m_dir, dir2);
}

UNIT_TEST(Positions)
{
  vector<PointD> path;
  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 0));

  Spline spl0(path);
  Spline spl4;
  spl4 = spl0;
  double const sqrt2 = sqrt(2.0);
  Spline::iterator itr;
  itr.Attach(spl0);
  TestPointDDir(itr.m_pos, PointD(0, 0));
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_pos, PointD(40, 40));
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_pos, PointD(80, 0));
  itr.Attach(spl4);
  TestPointDDir(itr.m_pos, PointD(0, 0));
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_pos, PointD(40, 40));
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_pos, PointD(80, 0));

  path.clear();

  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 40));
  path.push_back(PointD(120, 0));

  Spline spl2(path);
  Spline spl3 = spl2;
  itr.Attach(spl3);
  TestPointDDir(itr.m_pos, PointD(0, 0));
  itr.Advance(sqrt2 * 80.0 + 40.0);
  TestPointDDir(itr.m_pos, PointD(120, 0));
  itr.Attach(spl2);
  itr.Advance(sqrt2 * 40.0);
  TestPointDDir(itr.m_pos, PointD(40, 40));
  itr.Advance(2.0);
  TestPointDDir(itr.m_pos, PointD(42, 40));
  itr.Advance(20.0);
  TestPointDDir(itr.m_pos, PointD(62, 40));
  itr.Advance(18.0);
  TestPointDDir(itr.m_pos, PointD(80, 40));
}

UNIT_TEST(BeginAgain)
{
  vector<PointD> path;
  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 0));

  Spline spl(path);
  double const sqrt2 = sqrtf(2.0);
  Spline::iterator itr;
  PointD dir1(sqrt2 / 2.0, sqrt2 / 2.0);
  PointD dir2(sqrt2 / 2.0, -sqrt2 / 2.0);
  itr.Attach(spl);
  TEST_EQUAL(itr.BeginAgain(), false, ());
  itr.Advance(90.0);
  TEST_EQUAL(itr.BeginAgain(), false, ());
  itr.Advance(90.0);
  TEST_EQUAL(itr.BeginAgain(), true, ());
  itr.Advance(190.0);
  TEST_EQUAL(itr.BeginAgain(), true, ());

  path.clear();

  path.push_back(PointD(0, 0));
  path.push_back(PointD(40, 40));
  path.push_back(PointD(80, 40));
  path.push_back(PointD(120, 0));

  Spline spl2(path);
  itr.Attach(spl2);
  TEST_EQUAL(itr.BeginAgain(), false, ());
  itr.Advance(90.0);
  TEST_EQUAL(itr.BeginAgain(), false, ());
  itr.Advance(90.0);
  TEST_EQUAL(itr.BeginAgain(), true, ());
  itr.Advance(190.0);
  TEST_EQUAL(itr.BeginAgain(), true, ());
}

UNIT_TEST(Length)
{
  vector<PointD> path;
  PointD const p1(27.5536633, 64.2492523);
  PointD const p2(27.5547638, 64.2474289);
  PointD const p3(27.5549412, 64.2471237);
  PointD const p4(27.5559044, 64.2456436);
  PointD const p5(27.556284, 64.2451782);
  path.push_back(p1);
  path.push_back(p2);
  path.push_back(p3);
  path.push_back(p4);
  path.push_back(p5);
  Spline spl(path);
  double len1 = spl.GetLength();
  double l1 = p1.Length(p2);
  double l2 = p2.Length(p3);
  double l3 = p3.Length(p4);
  double l4 = p4.Length(p5);
  double len2 = l1 + l2 + l3 + l4;
  TEST_ALMOST_EQUAL(len1, len2, ());
}