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

rule_drawer.cpp « drape_frontend - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce9fa8fe107ce248aae485e01eb7c5aab8b80509 (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
#include "drape_frontend/rule_drawer.hpp"
#include "drape_frontend/stylist.hpp"
#include "drape_frontend/engine_context.hpp"
#include "drape_frontend/apply_feature_functors.hpp"
#include "drape_frontend/visual_params.hpp"

#include "indexer/feature.hpp"
#include "indexer/feature_algo.hpp"
#include "indexer/feature_visibility.hpp"
#include "indexer/scales.hpp"
#include "indexer/ftypes_matcher.hpp"

#include "base/assert.hpp"
#include "base/logging.hpp"

#include "std/bind.hpp"

//#define DRAW_TILE_NET

#ifdef DRAW_TILE_NET
#include "drape_frontend/line_shape.hpp"
#include "drape_frontend/text_shape.hpp"

#include "base/string_utils.hpp"
#endif

namespace {

df::BaseApplyFeature::HotelData ExtractHotelData(FeatureType const & f)
{
  df::BaseApplyFeature::HotelData result;
  if (ftypes::IsBookingChecker::Instance()(f))
  {
    result.m_isHotel = true;
    result.m_rating = f.GetMetadata().Get(feature::Metadata::FMD_RATING);
    strings::to_int(f.GetMetadata().Get(feature::Metadata::FMD_STARS), result.m_stars);
    strings::to_int(f.GetMetadata().Get(feature::Metadata::FMD_PRICE_RATE), result.m_priceCategory);
  }
  return result;
}

} // namespace

namespace df
{

RuleDrawer::RuleDrawer(TDrawerCallback const & fn,
                       TCheckCancelledCallback const & checkCancelled,
                       TIsCountryLoadedByNameFn const & isLoadedFn,
                       ref_ptr<EngineContext> context, bool is3dBuildings)
  : m_callback(fn)
  , m_checkCancelled(checkCancelled)
  , m_isLoadedFn(isLoadedFn)
  , m_context(context)
  , m_is3dBuidings(is3dBuildings)
  , m_wasCancelled(false)
{
  ASSERT(m_callback != nullptr, ());
  ASSERT(m_checkCancelled != nullptr, ());

  m_globalRect = m_context->GetTileKey().GetGlobalRect();

  int32_t tileSize = df::VisualParams::Instance().GetTileSize();
  m2::RectD const r = m_context->GetTileKey().GetGlobalRect(false /* clipByDataMaxZoom */);
  ScreenBase geometryConvertor;
  geometryConvertor.OnSize(0, 0, tileSize, tileSize);
  geometryConvertor.SetFromRect(m2::AnyRectD(r));
  m_currentScaleGtoP = 1.0f / geometryConvertor.GetScale();

  int const kAverageOverlaysCount = 200;
  m_mapShapes[df::OverlayType].reserve(kAverageOverlaysCount);
}

RuleDrawer::~RuleDrawer()
{
  if (m_wasCancelled)
    return;

  for (auto const & shape : m_mapShapes[df::OverlayType])
    shape->Prepare(m_context->GetTextureManager());

  if (!m_mapShapes[df::OverlayType].empty())
  {
    TMapShapes overlayShapes;
    overlayShapes.swap(m_mapShapes[df::OverlayType]);
    m_context->FlushOverlays(move(overlayShapes));
  }
}

bool RuleDrawer::CheckCancelled()
{
  m_wasCancelled = m_checkCancelled();
  return m_wasCancelled;
}

void RuleDrawer::operator()(FeatureType const & f)
{
  if (CheckCancelled())
    return;

  Stylist s;
  m_callback(f, s);

  if (s.IsEmpty())
    return;

  int const zoomLevel = m_context->GetTileKey().m_zoomLevel;

  if (s.IsCoastLine() &&
      zoomLevel > scales::GetUpperWorldScale() &&
      f.GetID().m_mwmId.GetInfo()->GetType() == MwmInfo::COASTS)
  {
    string name;
    if (f.GetName(StringUtf8Multilang::kDefaultCode, name))
    {
      ASSERT(!name.empty(), ());
      strings::SimpleTokenizer iter(name, ";");
      while (iter)
      {
        if (m_isLoadedFn(*iter))
          return;
        ++iter;
      }
    }
  }

  // FeatureType::GetLimitRect call invokes full geometry reading and decoding.
  // That's why this code follows after all lightweight return options.
  m2::RectD const limitRect = f.GetLimitRect(zoomLevel);
  if (!m_globalRect.IsIntersect(limitRect))
    return;

#ifdef DEBUG
  // Validate on feature styles
  if (s.AreaStyleExists() == false)
  {
    int checkFlag = s.PointStyleExists() ? 1 : 0;
    checkFlag += s.LineStyleExists() ? 1 : 0;
    ASSERT(checkFlag == 1, ());
  }
#endif

  int minVisibleScale = 0;
  auto insertShape = [this, &minVisibleScale](drape_ptr<MapShape> && shape)
  {
    int const index = static_cast<int>(shape->GetType());
    ASSERT_LESS(index, m_mapShapes.size(), ());

    shape->SetFeatureMinZoom(minVisibleScale);
    m_mapShapes[index].push_back(move(shape));
  };

  if (s.AreaStyleExists())
  {
    bool isBuilding = false;
    if (f.GetLayer() >= 0)
    {
      // Looks like nonsense, but there are some osm objects with types
      // highway-path-bridge and building (sic!) at the same time (pedestrian crossing).
      isBuilding = (ftypes::IsBuildingChecker::Instance()(f) ||
                    ftypes::IsBuildingPartChecker::Instance()(f)) &&
                    !ftypes::IsBridgeChecker::Instance()(f) &&
                    !ftypes::IsTunnelChecker::Instance()(f);
    }
    bool const is3dBuilding = m_is3dBuidings && isBuilding;

    m2::PointD featureCenter;

    float areaHeight = 0.0f;
    float areaMinHeight = 0.0f;
    if (is3dBuilding)
    {
      feature::Metadata const & md = f.GetMetadata();

      constexpr double kDefaultHeightInMeters = 3.0;
      constexpr double kMetersPerLevel = 3.0;
      double heightInMeters = kDefaultHeightInMeters;

      string value = md.Get(feature::Metadata::FMD_HEIGHT);
      if (!value.empty())
      {
        strings::to_double(value, heightInMeters);
      }
      else
      {
        value = md.Get(feature::Metadata::FMD_BUILDING_LEVELS);
        if (!value.empty())
        {
          if (strings::to_double(value, heightInMeters))
            heightInMeters *= kMetersPerLevel;
        }
      }

      value = md.Get(feature::Metadata::FMD_MIN_HEIGHT);
      double minHeigthInMeters = 0.0;
      if (!value.empty())
        strings::to_double(value, minHeigthInMeters);

      featureCenter = feature::GetCenter(f, zoomLevel);
      double const lon = MercatorBounds::XToLon(featureCenter.x);
      double const lat = MercatorBounds::YToLat(featureCenter.y);

      m2::RectD rectMercator = MercatorBounds::MetresToXY(lon, lat, heightInMeters);
      areaHeight = (rectMercator.SizeX() + rectMercator.SizeY()) / 2.0;

      rectMercator = MercatorBounds::MetresToXY(lon, lat, minHeigthInMeters);
      areaMinHeight = (rectMercator.SizeX() + rectMercator.SizeY()) / 2.0;
    }

    bool applyPointStyle = s.PointStyleExists();
    if (applyPointStyle)
    {
      if (!is3dBuilding)
        featureCenter = feature::GetCenter(f, zoomLevel);
      applyPointStyle = m_globalRect.IsPointInside(featureCenter);
    }

    if (applyPointStyle || is3dBuilding)
      minVisibleScale = feature::GetMinDrawableScale(f);

    ApplyAreaFeature apply(m_globalRect.Center(), insertShape, f.GetID(), m_globalRect,
                           isBuilding, areaMinHeight, areaHeight, minVisibleScale,
                           f.GetRank(), s.GetCaptionDescription());
    f.ForEachTriangle(apply, zoomLevel);
    apply.SetHotelData(ExtractHotelData(f));
    if (applyPointStyle)
      apply(featureCenter, true /* hasArea */);

    if (CheckCancelled())
      return;

    s.ForEachRule(bind(&ApplyAreaFeature::ProcessRule, &apply, _1));
    apply.Finish();
  }
  else if (s.LineStyleExists())
  {
    ApplyLineFeature apply(m_globalRect.Center(), m_currentScaleGtoP, insertShape, f.GetID(),
                           m_globalRect, minVisibleScale, f.GetRank(), s.GetCaptionDescription(),
                           zoomLevel, f.GetPointsCount());
    f.ForEachPoint(apply, zoomLevel);

    if (CheckCancelled())
      return;

    if (apply.HasGeometry())
      s.ForEachRule(bind(&ApplyLineFeature::ProcessRule, &apply, _1));
    apply.Finish();
  }
  else
  {
    ASSERT(s.PointStyleExists(), ());

    minVisibleScale = feature::GetMinDrawableScale(f);
    ApplyPointFeature apply(m_globalRect.Center(), insertShape, f.GetID(), minVisibleScale, f.GetRank(),
                            s.GetCaptionDescription(), 0.0f /* posZ */);
    apply.SetHotelData(ExtractHotelData(f));
    f.ForEachPoint([&apply](m2::PointD const & pt) { apply(pt, false /* hasArea */); }, zoomLevel);

    if (CheckCancelled())
      return;

    s.ForEachRule(bind(&ApplyPointFeature::ProcessRule, &apply, _1));
    apply.Finish();
  }

#ifdef DRAW_TILE_NET
  TileKey key = m_context->GetTileKey();
  m2::RectD r = key.GetGlobalRect();
  vector<m2::PointD> path;
  path.push_back(r.LeftBottom());
  path.push_back(r.LeftTop());
  path.push_back(r.RightTop());
  path.push_back(r.RightBottom());
  path.push_back(r.LeftBottom());

  m2::SharedSpline spline(path);
  df::LineViewParams p;
  p.m_tileCenter = m_globalRect.Center();
  p.m_baseGtoPScale = 1.0;
  p.m_cap = dp::ButtCap;
  p.m_color = dp::Color::Red();
  p.m_depth = 20000;
  p.m_width = 5;
  p.m_join = dp::RoundJoin;

  insertShape(make_unique_dp<LineShape>(spline, p));

  df::TextViewParams tp;
  tp.m_tileCenter = m_globalRect.Center();
  tp.m_anchor = dp::Center;
  tp.m_depth = 20000;
  tp.m_primaryText = strings::to_string(key.m_x) + " " +
                     strings::to_string(key.m_y) + " " +
                     strings::to_string(key.m_zoomLevel);

  tp.m_primaryTextFont = dp::FontDecl(dp::Color::Red(), 30);

  drape_ptr<TextShape> textShape = make_unique_dp<TextShape>(r.Center(), tp, false, 0, false);
  textShape->DisableDisplacing();
  insertShape(move(textShape));
#endif

  if (CheckCancelled())
    return;

  for (auto const & shape : m_mapShapes[df::GeometryType])
    shape->Prepare(m_context->GetTextureManager());

  if (!m_mapShapes[df::GeometryType].empty())
  {
    TMapShapes geomShapes;
    geomShapes.swap(m_mapShapes[df::GeometryType]);
    m_context->Flush(move(geomShapes));
  }
}

} // namespace df