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

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

#include "graphics/screen.hpp"
#include "graphics/display_list.hpp"
#include "graphics/depth_constants.hpp"
#include "graphics/pen.hpp"

#include "indexer/scales.hpp"

#include "std/array.hpp"

namespace
{
  pair<m2::PointD, m2::PointD> ShiftArrow(pair<m2::PointD, m2::PointD> const & arrowDirection)
  {
    return pair<m2::PointD, m2::PointD>(arrowDirection.first - (arrowDirection.second - arrowDirection.first),
                                        arrowDirection.first);
  }

  void DrawArrowTriangle(graphics::Screen * dlScreen, pair<m2::PointD, m2::PointD> const & arrowDirection,
                         double arrowWidth, double arrowLength, graphics::Color arrowColor, double arrowDepth)
  {
    ASSERT(dlScreen, ());

    array<m2::PointF, 3> arrow;
    m2::GetArrowPoints(arrowDirection.first, arrowDirection.second, arrowWidth, arrowLength, arrow);
    dlScreen->drawConvexPolygon(&arrow[0], arrow.size(), arrowColor, arrowDepth);
  }
}

bool ClipArrowBodyAndGetArrowDirection(vector<m2::PointD> & ptsTurn, pair<m2::PointD, m2::PointD> & arrowDirection,
                                       size_t turnIndex, double beforeTurn, double afterTurn, double arrowLength)
{
  size_t const ptsTurnSz = ptsTurn.size();
  ASSERT_LESS(turnIndex, ptsTurnSz, ());
  ASSERT_GREATER(ptsTurnSz, 1, ());

  /// Clipping after turnIndex
  size_t i = turnIndex;
  double len = 0, vLen = 0;
  while (len < afterTurn)
  {
    if (i > ptsTurnSz - 2)
      return false;
    vLen = ptsTurn[i + 1].Length(ptsTurn[i]);
    len += vLen;
    i += 1;
  }
  if (my::AlmostEqualULPs(vLen, 0.))
    return false;

  double lenForArrow = len - afterTurn;
  double vLenForArrow = lenForArrow;
  size_t j = i;
  while (lenForArrow < arrowLength)
  {
    if (j > ptsTurnSz - 2)
      return false;
    vLenForArrow = ptsTurn[j + 1].Length(ptsTurn[j]);
    lenForArrow +=  vLenForArrow;
    j += 1;
  }
  ASSERT_GREATER(j, 0, ());
  if (m2::AlmostEqualULPs(ptsTurn[j - 1], ptsTurn[j]))
    return false;
  m2::PointD arrowEnd = m2::PointAtSegment(ptsTurn[j - 1], ptsTurn[j], vLenForArrow - (lenForArrow - arrowLength));

  if (my::AlmostEqualULPs(len, afterTurn))
    ptsTurn.resize(i + 1);
  else
  {
    if (!m2::AlmostEqualULPs(ptsTurn[i], ptsTurn[i - 1]))
    {
      m2::PointD const p = m2::PointAtSegment(ptsTurn[i - 1], ptsTurn[i], vLen - (len - afterTurn));
      ptsTurn[i] = p;
      ptsTurn.resize(i + 1);
    }
    else
      ptsTurn.resize(i);
  }

  // Calculating arrow direction
  arrowDirection.first = ptsTurn.back();
  arrowDirection.second = arrowEnd;
  arrowDirection = ShiftArrow(arrowDirection);

  /// Clipping before turnIndex
  i = turnIndex;
  len = 0;
  while (len < beforeTurn)
  {
    if (i < 1)
      return false;
    vLen = ptsTurn[i - 1].Length(ptsTurn[i]);
    len += vLen;
    i -= 1;
  }
  if (my::AlmostEqualULPs(vLen, 0.))
    return false;

  if (my::AlmostEqualULPs(len, beforeTurn))
  {
    if (i != 0)
    {
      ptsTurn.erase(ptsTurn.begin(), ptsTurn.begin() + i);
      return true;
    }
  }
  else
  {
    if (!m2::AlmostEqualULPs(ptsTurn[i], ptsTurn[i + 1]))
    {
      m2::PointD const p = m2::PointAtSegment(ptsTurn[i + 1], ptsTurn[i], vLen - (len - beforeTurn));
      ptsTurn[i] = p;
      if (i != 0)
      {
        ptsTurn.erase(ptsTurn.begin(), ptsTurn.begin() + i);
        return true;
      }
    }
    else
    {
      ptsTurn.erase(ptsTurn.begin(), ptsTurn.begin() + i);
      return true;
    }
  }
  return true;
}

bool MergeArrows(vector<m2::PointD> & ptsCurrentTurn, vector<m2::PointD> const & ptsNextTurn, double bodyLen, double arrowLen)
{
  ASSERT_GREATER_OR_EQUAL(ptsCurrentTurn.size(), 2, ());
  ASSERT_GREATER_OR_EQUAL(ptsNextTurn.size(), 2, ());
  ASSERT_GREATER(bodyLen, 0, ());
  ASSERT_GREATER(arrowLen, 0, ());
  ASSERT_GREATER(bodyLen, arrowLen, ());

  double const distBetweenBodies = ptsCurrentTurn.back().Length(ptsNextTurn.front());
  /// The most likely the function returns here because the arrows are usually far to each other
  if (distBetweenBodies > bodyLen)
    return false;

  /// The arrows are close to each other or intersected
  double const distBetweenBodies2 = ptsCurrentTurn[ptsCurrentTurn.size() - 2].Length(ptsNextTurn[1]);
  if (distBetweenBodies < distBetweenBodies2)
  {
    /// The arrows bodies are not intersected
    if (distBetweenBodies > arrowLen)
      return false;
    else
    {
      ptsCurrentTurn.push_back(ptsNextTurn.front());
      return true;
    }
  }
  else
  {
    /// The arrows bodies are intersected. Make ptsCurrentTurn shorter if possible to prevent double rendering.
    /// ptsNextTurn[0] is not a point of route network graph. It is generated while clipping.
    /// The first point of route network graph is ptsNextTurn[1]. The same with the end of ptsCurrentTurn
    m2::PointD const pivotPnt = ptsNextTurn[1]; 
    auto const currentTurnBeforeEnd = ptsCurrentTurn.end() - 1;
    for (auto t = ptsCurrentTurn.begin() + 1; t != currentTurnBeforeEnd; ++t)
    {
      if (m2::AlmostEqualULPs(*t, pivotPnt))
      {
        ptsCurrentTurn.erase(t + 1, ptsCurrentTurn.end());
        return true;
      }
    }
    return true;
  }
}

RouteTrack::~RouteTrack()
{
  DeleteClosestSegmentDisplayList();
}

void RouteTrack::CreateDisplayListArrows(graphics::Screen * dlScreen, MatrixT const & matrix, double visualScale) const
{
  double const beforeTurn = 13. * visualScale;
  double const afterTurn = 13. * visualScale;
  double const arrowWidth = 10. * visualScale;
  double const arrowLength = 19. * visualScale;
  double const arrowBodyWidth = 8. * visualScale;
  double const arrowDepth = graphics::arrowDepth;

  pair<m2::PointD, m2::PointD> arrowDirection;
  vector<m2::PointD> ptsTurn, ptsNextTurn;

  ptsTurn.reserve(m_turnsGeom.size());
  bool drawArrowHead = true;
  auto turnsGeomEnd = m_turnsGeom.rend();
  for (auto t = m_turnsGeom.rbegin(); t != turnsGeomEnd; ++t)
  {
    if (t->m_indexInRoute < m_relevantMatchedInfo.GetIndexInRoute())
      continue;
    ptsTurn.clear();
    if (t->m_points.empty())
      continue;
    transform(t->m_points.begin(), t->m_points.end(), back_inserter(ptsTurn), DoLeftProduct<MatrixT>(matrix));

    if (!ClipArrowBodyAndGetArrowDirection(ptsTurn, arrowDirection, t->m_turnIndex, beforeTurn, afterTurn, arrowLength))
      continue;
    if (ptsTurn.size() < 2)
      continue;

    if (!ptsNextTurn.empty())
      drawArrowHead = !MergeArrows(ptsTurn, ptsNextTurn, beforeTurn + afterTurn, arrowLength);

    graphics::Pen::Info const outlineInfo(m_arrowColor, arrowBodyWidth);
    uint32_t const outlineId = dlScreen->mapInfo(outlineInfo);
    dlScreen->drawPath(&ptsTurn[0], ptsTurn.size(), 0, outlineId, arrowDepth);

    if (drawArrowHead)
      DrawArrowTriangle(dlScreen, arrowDirection, arrowWidth, arrowLength, m_arrowColor, arrowDepth);
    ptsNextTurn = ptsTurn;
  }
}
/// @todo there are some ways to optimize the code bellow.
/// 1. Call CreateDisplayListArrows only after passing the next arrow while driving
/// 2. Use several closest segments intead of one to recreate Display List for the most part of the track
///
void RouteTrack::CreateDisplayList(graphics::Screen * dlScreen, MatrixT const & matrix, bool isScaleChanged,
                                   int drawScale, double visualScale,
                                   location::RouteMatchingInfo const & matchingInfo) const
{
  if (HasDisplayLists() && !isScaleChanged &&
      m_relevantMatchedInfo.GetPosition() == matchingInfo.GetPosition())
    return;

  PolylineD const & fullPoly = GetPolyline();
  size_t const formerIndex = m_relevantMatchedInfo.GetIndexInRoute();

  if (matchingInfo.IsMatched())
    m_relevantMatchedInfo = matchingInfo;
  size_t const currentIndex = m_relevantMatchedInfo.GetIndexInRoute();

  size_t const fullPolySz = fullPoly.GetSize();
  if (currentIndex + 2 >= fullPolySz || fullPolySz < 2)
  {
    DeleteDisplayList();
    DeleteClosestSegmentDisplayList();
    return;
  }
  DeleteClosestSegmentDisplayList();
  auto const curSegIter = fullPoly.Begin() + currentIndex;

  //the most part of the route and symbols
  if (formerIndex != currentIndex ||
      !HasDisplayLists() || isScaleChanged)
  {
    DeleteDisplayList();
    dlScreen->beginFrame();

    graphics::DisplayList * dList = dlScreen->createDisplayList();
    dlScreen->setDisplayList(dList);
    SetDisplayList(dList);

    PolylineD mostPartPoly(curSegIter + 1, fullPoly.End());
    PointContainerT ptsMostPart;
    ptsMostPart.reserve(mostPartPoly.GetSize());
    TransformAndSymplifyPolyline(mostPartPoly, matrix, GetMainWidth(), ptsMostPart);
    CreateDisplayListPolyline(dlScreen, ptsMostPart);
    
    PolylineD sym(vector<m2::PointD>({fullPoly.Front(), fullPoly.Back()}));
    PointContainerT ptsSym;
    TransformPolyline(sym, matrix, ptsSym);
    CreateDisplayListSymbols(dlScreen, ptsSym);

    //arrows on the route
    if (drawScale >= scales::GetNavigationScale())
      CreateDisplayListArrows(dlScreen, matrix, visualScale);
  }
  else
    dlScreen->beginFrame();

  //closest route segment
  m_closestSegmentDL = dlScreen->createDisplayList();
  dlScreen->setDisplayList(m_closestSegmentDL);
  PolylineD closestPoly(m_relevantMatchedInfo.IsMatched() ?
          vector<m2::PointD>({m_relevantMatchedInfo.GetPosition(), fullPoly.GetPoint(currentIndex + 1)}) :
          vector<m2::PointD>({fullPoly.GetPoint(currentIndex), fullPoly.GetPoint(currentIndex + 1)}));
  PointContainerT pts;
  pts.reserve(closestPoly.GetSize());
  TransformPolyline(closestPoly, matrix, pts);
  CreateDisplayListPolyline(dlScreen, pts);

  dlScreen->setDisplayList(0);
  dlScreen->endFrame();
}

RouteTrack * RouteTrack::CreatePersistent()
{
  RouteTrack * p = new RouteTrack();
  Swap(*p);
  return p;
}

void RouteTrack::DeleteClosestSegmentDisplayList() const
{
  delete m_closestSegmentDL;
  m_closestSegmentDL = nullptr;
}

void RouteTrack::Draw(graphics::Screen * pScreen, MatrixT const & matrix) const
{
  Track::Draw(pScreen, matrix);
  pScreen->drawDisplayList(m_closestSegmentDL, matrix);
}

void RouteTrack::AddClosingSymbol(bool isBeginSymbol, string const & symbolName, graphics::EPosition pos, double depth)
{
  if (isBeginSymbol)
    m_beginSymbols.push_back(ClosingSymbol(symbolName, pos, depth));
  else
    m_endSymbols.push_back(ClosingSymbol(symbolName, pos, depth));
}

void RouteTrack::CreateDisplayListSymbols(graphics::Screen * dlScreen, PointContainerT const & pts) const
{
  ASSERT(!pts.empty(), ());
  if (!m_beginSymbols.empty() || !m_endSymbols.empty())
  {
    m2::PointD pivot = pts.front();
    auto symDrawer = [&dlScreen, &pivot] (ClosingSymbol const & symbol)
    {
      dlScreen->drawSymbol(pivot, symbol.m_iconName, symbol.m_position, symbol.m_depth);
    };

    for_each(m_beginSymbols.begin(), m_beginSymbols.end(), symDrawer);

    pivot = pts.back();
    for_each(m_endSymbols.begin(), m_endSymbols.end(), symDrawer);
  }
}

void RouteTrack::Swap(RouteTrack & rhs)
{
  Track::Swap(rhs);
  swap(m_beginSymbols, rhs.m_beginSymbols);
  swap(m_endSymbols, rhs.m_endSymbols);
  m_turnsGeom.swap(rhs.m_turnsGeom);

  rhs.m_relevantMatchedInfo.Reset();
  m_relevantMatchedInfo.Reset();

  swap(m_arrowColor, rhs.m_arrowColor);
}

void RouteTrack::CleanUp() const
{
  Track::CleanUp();
  DeleteClosestSegmentDisplayList();
}

bool RouteTrack::HasDisplayLists() const
{
  return Track::HasDisplayLists() && m_closestSegmentDL != nullptr;
}