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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
#include "navigator.hpp"
#include "../indexer/cell_id.hpp"
#include "../platform/settings.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/file_writer.hpp"
#include "../coding/streams_sink.hpp"
#include "../geometry/angles.hpp"
#include "../geometry/transformations.hpp"
#include "../geometry/point2d.hpp"
#include "../base/std_serialization.hpp"
#include "../base/logging.hpp"
#include "../base/start_mem_debug.hpp"
Navigator::Navigator()
: m_worldRect(MercatorBounds::minX, MercatorBounds::minY, MercatorBounds::maxX, MercatorBounds::maxY),
m_InAction(false),
m_orientation(EOrientation0)
{
}
Navigator::Navigator(ScreenBase const & screen)
: m_worldRect(MercatorBounds::minX, MercatorBounds::minY, MercatorBounds::maxX, MercatorBounds::maxY),
m_StartScreen(screen),
m_Screen(screen),
m_InAction(false),
m_orientation(EOrientation0)
{
}
void Navigator::SetMinScreenParams(unsigned pxMinWidth, double metresMinWidth)
{
m_pxMinWidth = pxMinWidth;
m_metresMinWidth = metresMinWidth;
}
void Navigator::SetFromRect(m2::AnyRectD const & r)
{
m_Screen.SetFromRect(r);
m_Screen = ScaleInto(m_Screen, m_worldRect);
if (!m_InAction)
{
m_StartScreen.SetFromRect(r);
m_StartScreen = ScaleInto(m_StartScreen, m_worldRect);
}
}
void Navigator::CenterViewport(m2::PointD const & p)
{
/// Rounding center point to a pixel
/// boundary to obtain crisp centered picture.
m2::PointD pt = m_Screen.GtoP(p);
pt.x = ceil(pt.x);
pt.y = ceil(pt.y);
pt = m_Screen.PtoG(pt);
m_Screen.SetOrg(pt);
if (!m_InAction)
m_StartScreen.SetOrg(pt);
}
void Navigator::SaveState()
{
Settings::Set("ScreenClipRect", m_Screen.GlobalRect());
}
bool Navigator::LoadState()
{
m2::AnyRectD rect;
if (!Settings::Get("ScreenClipRect", rect))
return false;
SetFromRect(rect);
return true;
}
void Navigator::OnSize(int x0, int y0, int w, int h)
{
m_Screen.OnSize(x0, y0, w, h);
m_Screen = ShrinkAndScaleInto(m_Screen, m_worldRect);
if (!m_InAction)
{
m_StartScreen.OnSize(x0, y0, w, h);
m_StartScreen = ShrinkAndScaleInto(m_StartScreen, m_worldRect);
}
}
bool Navigator::CanShrinkInto(ScreenBase const & screen, m2::RectD const & boundRect)
{
m2::RectD globalRect = screen.ClipRect();
return (boundRect.SizeX() >= globalRect.SizeX()) && (boundRect.SizeY() >= globalRect.SizeY());
}
ScreenBase const Navigator::ShrinkInto(ScreenBase const & screen, m2::RectD const & boundRect)
{
ScreenBase res = screen;
m2::RectD globalRect = res.ClipRect();
if (globalRect.minX() < boundRect.minX())
globalRect.Offset(boundRect.minX() - globalRect.minX(), 0);
if (globalRect.maxX() > boundRect.maxX())
globalRect.Offset(boundRect.maxX() - globalRect.maxX(), 0);
if (globalRect.minY() < boundRect.minY())
globalRect.Offset(0, boundRect.minY() - globalRect.minY());
if (globalRect.maxY() > boundRect.maxY())
globalRect.Offset(0, boundRect.maxY() - globalRect.maxY());
res.SetFromRect(m2::AnyRectD(globalRect));
return res;
}
ScreenBase const Navigator::ScaleInto(ScreenBase const & screen, m2::RectD const & boundRect)
{
ScreenBase res = screen;
m2::RectD globalRect = res.ClipRect();
if (globalRect.minX() < boundRect.minX())
globalRect.Scale((boundRect.minX() - globalRect.Center().x) / (globalRect.minX() - globalRect.Center().x));
if (globalRect.maxX() > boundRect.maxX())
globalRect.Scale((boundRect.maxX() - globalRect.Center().x) / (globalRect.maxX() - globalRect.Center().x));
if (globalRect.minY() < boundRect.minY())
globalRect.Scale((boundRect.minY() - globalRect.Center().y) / (globalRect.minY() - globalRect.Center().y));
if (globalRect.maxY() > boundRect.maxY())
globalRect.Scale((boundRect.maxY() - globalRect.Center().y) / (globalRect.maxY() - globalRect.Center().y));
res.SetFromRect(m2::AnyRectD(globalRect));
return res;
}
ScreenBase const Navigator::ShrinkAndScaleInto(ScreenBase const & screen, m2::RectD const & boundRect)
{
ScreenBase res = screen;
m2::RectD globalRect = res.ClipRect();
if (globalRect.minX() < boundRect.minX())
{
globalRect.Offset(boundRect.minX() - globalRect.minX(), 0);
if (globalRect.maxX() > boundRect.maxX())
globalRect.Scale((globalRect.Center().x - boundRect.maxX()) / (globalRect.Center().x - globalRect.maxX()));
}
if (globalRect.maxX() > boundRect.maxX())
{
globalRect.Offset(boundRect.maxX() - globalRect.maxX(), 0);
if (globalRect.minX() < boundRect.minX())
globalRect.Scale((globalRect.Center().x - boundRect.minX()) / (globalRect.Center().x - globalRect.minX()));
}
if (globalRect.minY() < boundRect.minY())
{
globalRect.Offset(0, boundRect.minY() - globalRect.minY());
if (globalRect.maxY() > boundRect.maxY())
globalRect.Scale((globalRect.Center().y - boundRect.maxY()) / (globalRect.Center().y - globalRect.maxY()));
}
if (globalRect.maxY() > boundRect.maxY())
{
globalRect.Offset(0, boundRect.maxY() - globalRect.maxY());
if (globalRect.minY() < boundRect.minY())
globalRect.Scale((globalRect.Center().y - boundRect.minY()) / (globalRect.Center().y - globalRect.minY()));
}
res.SetFromRect(m2::AnyRectD(globalRect));
return res;
}
void Navigator::StartRotate(double a, double /*timeInSec*/)
{
m_StartAngle = a;
m_StartScreen = m_Screen;
m_InAction = true;
}
void Navigator::DoRotate(double a, double /*timeInSec*/)
{
ScreenBase tmp = m_StartScreen;
tmp.Rotate(a - m_StartAngle);
m_StartAngle = a;
m_Screen = tmp;
m_StartScreen = tmp;
}
void Navigator::StopRotate(double a, double timeInSec)
{
DoRotate(a, timeInSec);
m_InAction = false;
}
void Navigator::StartDrag(m2::PointD const & pt, double /*timeInSec*/)
{
//LOG(LDEBUG, (pt.x, pt.y));
m_StartPt1 = m_LastPt1 = pt;
m_StartScreen = m_Screen;
m_InAction = true;
}
void Navigator::DoDrag(m2::PointD const & pt, double /*timeInSec*/)
{
if (m_LastPt1 == pt)
return;
//LOG(LDEBUG, (pt.x, pt.y));
//m_Screen = m_StartScreen;
ScreenBase tmp = m_StartScreen;
tmp = ShrinkInto(tmp, m_worldRect);
double dx = pt.x - m_StartPt1.x;
double dy = pt.y - m_StartPt1.y;
tmp.Move(dx, 0);
if (!CheckBorders(tmp))
dx = 0;
tmp = m_StartScreen;
tmp = ShrinkInto(tmp, m_worldRect);
tmp.Move(0, dy);
if (!CheckBorders(tmp))
dy = 0;
tmp = m_StartScreen;
tmp = ShrinkInto(tmp, m_worldRect);
tmp.Move(dx, dy);
if (CheckBorders(tmp))
{
m_StartScreen = tmp;
m_StartPt1 = pt;
m_LastPt1 = pt;
m_Screen = tmp;
}
}
void Navigator::StopDrag(m2::PointD const & pt, double timeInSec, bool /*animate*/)
{
// TODO: animate.
// Ensure that final pos is reached.
DoDrag(pt, timeInSec);
//ASSERT(m_LastPt1 == pt, (m_LastPt1.x, m_LastPt1.y, pt.x, pt.y));
//LOG(LDEBUG, (pt.x, pt.y));
m_InAction = false;
// m_StartScreen = m_Screen;
}
bool Navigator::InAction() const
{
return m_InAction;
}
void Navigator::StartScale(m2::PointD const & pt1, m2::PointD const & pt2, double /*timeInSec*/)
{
//LOG(LDEBUG, (pt1.x, pt1.y, pt2.x, pt2.y));
m_StartScreen = m_Screen;
m_StartPt1 = pt1;
m_StartPt2 = pt2;
m_InAction = true;
}
void Navigator::ScaleToPoint(m2::PointD const & pt, double factor, double /*timeInSec*/)
{
m2::PointD startPt;
m2::PointD endPt;
// pt is in x0, x0 + width
if (pt.x != m_Screen.PixelRect().maxX())
{
// start scaling point is 1 / factor way between pt and right border
startPt.x = pt.x + ((m_Screen.PixelRect().maxX() - pt.x) / factor);
endPt.x = m_Screen.PixelRect().maxX();
}
else
{
// start scaling point is 1 - 1/factor way between left border and pt
startPt.x = pt.x + (m_Screen.PixelRect().minX() - pt.x) / factor;
endPt.x = m_Screen.PixelRect().minX();
}
if (pt.y != m_Screen.PixelRect().maxY())
{
startPt.y = pt.y + ((m_Screen.PixelRect().maxY() - pt.y) / factor);
endPt.y = m_Screen.PixelRect().maxY();
}
else
{
startPt.y = pt.y + (m_Screen.PixelRect().minY() - pt.y) / factor;
endPt.y = m_Screen.PixelRect().minY();
}
ScaleImpl(pt, endPt, pt, startPt, factor > 1);
}
bool Navigator::CheckMaxScale(ScreenBase const & screen)
{
m2::RectD r = screen.ClipRect();
// multiple by 2 to allow scale on zero level
double const maxSize = m_worldRect.SizeX();
return (r.SizeX() <= maxSize || r.SizeY() <= maxSize);
}
bool Navigator::CheckMinScale(ScreenBase const & screen)
{
m2::PointD const pt0 = screen.PtoG(m_Screen.PixelRect().Center() - m2::PointD(m_pxMinWidth / 2, 0));
m2::PointD const pt1 = screen.PtoG(m_Screen.PixelRect().Center() + m2::PointD(m_pxMinWidth / 2, 0));
double lonDiff = fabs(MercatorBounds::XToLon(pt1.x) - MercatorBounds::XToLon(pt0.x));
double metresDiff = lonDiff / MercatorBounds::degreeInMetres;
return metresDiff >= m_metresMinWidth - 1;
}
bool Navigator::CheckBorders(ScreenBase const & screen)
{
m2::RectD ScreenBounds = screen.ClipRect();
return ScreenBounds.IsRectInside(m_worldRect) || m_worldRect.IsRectInside(ScreenBounds);
}
bool Navigator::ScaleImpl(m2::PointD const & newPt1, m2::PointD const & newPt2,
m2::PointD const & oldPt1, m2::PointD const & oldPt2,
bool skipMaxScaleAndBordersCheck)
{
math::Matrix<double, 3, 3> newM = m_Screen.GtoPMatrix() * ScreenBase::CalcTransform(oldPt1, oldPt2, newPt1, newPt2);
ScreenBase tmp = m_Screen;
tmp.SetGtoPMatrix(newM);
tmp.Rotate(-tmp.GetAngle());
if (!skipMaxScaleAndBordersCheck && !CheckMaxScale(tmp))
return false;
if (!skipMaxScaleAndBordersCheck && !CheckBorders(tmp))
{
if (CanShrinkInto(tmp, m_worldRect))
tmp = ShrinkInto(tmp, m_worldRect);
else
return false;
}
if (!CheckMinScale(tmp))
return false;
/// re-checking the borders, as we might violate them a bit (don't know why).
if (!CheckBorders(tmp))
tmp = ScaleInto(tmp, m_worldRect);
m_Screen = tmp;
return true;
}
void Navigator::DoScale(m2::PointD const & pt1, m2::PointD const & pt2, double /*timeInSec*/)
{
//LOG(LDEBUG, (pt1.x, pt1.y, pt2.x, pt2.y));
if (m_LastPt1 == pt1 && m_LastPt2 == pt2)
return;
if (pt1 == pt2)
return;
ScreenBase PrevScreen = m_Screen;
m_Screen = m_StartScreen;
if (!ScaleImpl(pt1, pt2, m_StartPt1, m_StartPt2, pt1.Length(pt2) / m_StartPt1.Length(m_StartPt2) > 1))
m_Screen = PrevScreen;
m_LastPt1 = pt1;
m_LastPt2 = pt2;
}
void Navigator::StopScale(m2::PointD const & pt1, m2::PointD const & pt2, double timeInSec)
{
// Ensure that final pos is reached.
DoScale(pt1, pt2, timeInSec);
ASSERT(m_LastPt1 == pt1, (m_LastPt1.x, m_LastPt1.y, pt1.x, pt1.y));
ASSERT(m_LastPt2 == pt2, (m_LastPt2.x, m_LastPt2.y, pt2.x, pt2.y));
//LOG(LDEBUG, (pt1.x, pt1.y, pt2.x, pt2.y));
m_InAction = false;
// m_StartScreen = m_Screen;
}
void Navigator::Scale(double scale)
{
ScreenBase tmp = m_Screen;
tmp.Scale(scale);
// limit max scale to MercatorBounds
if (CheckMaxScale(tmp) && CheckMinScale(tmp) && CheckBorders(tmp))
m_Screen = tmp;
}
void Navigator::Rotate(double angle)
{
m_Screen.Rotate(angle);
}
void Navigator::Move(double azDir, double factor)
{
m2::RectD const r = m_Screen.ClipRect();
m_Screen.MoveG(m2::PointD(r.SizeX() * factor * cos(azDir), r.SizeY() * factor * sin(azDir)));
}
bool Navigator::Update(double timeInSec)
{
m_LastUpdateTimeInSec = timeInSec;
return false;
}
void Navigator::SetOrientation(EOrientation orientation)
{
m_orientation = orientation;
}
EOrientation Navigator::Orientation() const
{
return m_orientation;
}
m2::PointD const Navigator::OrientPoint(m2::PointD const & pt) const
{
m2::PointD ptShift(m_Screen.PixelRect().minX(), m_Screen.PixelRect().minY());
switch (m_orientation)
{
case EOrientation90:
return m2::PointD(m_Screen.GetWidth() - pt.y, pt.x) + ptShift;
case EOrientation180:
return m2::PointD(m_Screen.GetWidth() - pt.x, m_Screen.GetHeight() - pt.y) + ptShift;
case EOrientation270:
return m2::PointD(pt.y, m_Screen.GetHeight() - pt.x) + ptShift;
case EOrientation0:
return pt + ptShift;
};
return ptShift;
}
|