From c3da8aee9015471fc028372a1408f3caff64a09c Mon Sep 17 00:00:00 2001 From: ExMix Date: Tue, 28 Jan 2014 14:10:38 +0300 Subject: [PATCH] [drape] move TileKey to separate file. correct test drawing primitives rudiments of style applying to geometry. --- drape_frontend/drape_frontend.pro | 13 +++- drape_frontend/rule_drawer.cpp | 105 ++++++++++++++++++++++++++++++ drape_frontend/rule_drawer.hpp | 37 +++++++++++ drape_frontend/tile_info.cpp | 96 +++++++++++++-------------- drape_frontend/tile_info.hpp | 37 ++--------- drape_frontend/tile_key.cpp | 46 +++++++++++++ drape_frontend/tile_key.hpp | 21 ++++++ 7 files changed, 272 insertions(+), 83 deletions(-) create mode 100644 drape_frontend/rule_drawer.cpp create mode 100644 drape_frontend/rule_drawer.hpp create mode 100644 drape_frontend/tile_key.cpp create mode 100644 drape_frontend/tile_key.hpp diff --git a/drape_frontend/drape_frontend.pro b/drape_frontend/drape_frontend.pro index 3af86ea81b..352d3dfd57 100644 --- a/drape_frontend/drape_frontend.pro +++ b/drape_frontend/drape_frontend.pro @@ -26,7 +26,11 @@ SOURCES += \ tile_info.cpp \ coverage_update_descriptor.cpp \ stylist.cpp \ - line_shape.cpp + line_shape.cpp \ + rule_drawer.cpp \ + viewport.cpp \ + vizualization_params.cpp \ + tile_key.cpp HEADERS += \ engine_context.hpp \ @@ -47,4 +51,9 @@ HEADERS += \ read_manager.hpp \ coverage_update_descriptor.hpp \ stylist.hpp \ - line_shape.hpp + line_shape.hpp \ + shape_view_params.hpp \ + rule_drawer.hpp \ + viewport.hpp \ + vizualization_params.hpp \ + tile_key.hpp diff --git a/drape_frontend/rule_drawer.cpp b/drape_frontend/rule_drawer.cpp new file mode 100644 index 0000000000..5646a7e1a8 --- /dev/null +++ b/drape_frontend/rule_drawer.cpp @@ -0,0 +1,105 @@ +#include "rule_drawer.hpp" +#include "stylist.hpp" +#include "shape_view_params.hpp" +#include "engine_context.hpp" + +#include "line_shape.hpp" + +#include "../indexer/drules_include.hpp" +#include "../indexer/feature.hpp" + +#include "../map/geometry_processors.hpp" + +#include "../base/assert.hpp" + +namespace df +{ + namespace + { + Color ToDrapeColor(uint32_t src) + { + return Extract(src, 255 - (src >> 24)); + } + + // ==================================================== // + void Extract(::LineDefProto const * lineRule, + double visualScale, + df::LineViewParams & params) + { + params.m_color = ToDrapeColor(lineRule->color()); + params.m_width = max(lineRule->width() * visualScale, 1.0); + + switch(lineRule->cap()) + { + case ::ROUNDCAP : params.m_cap = df::RoundCap; + break; + case ::BUTTCAP : params.m_cap = df::ButtCap; + break; + case ::SQUARECAP: params.m_cap = df::SquareCap; + break; + default: + ASSERT(false, ()); + } + + switch (lineRule->join()) + { + case ::NOJOIN : params.m_join = df::NonJoin; + break; + case ::ROUNDJOIN : params.m_join = df::RoundJoin; + break; + case ::BEVELJOIN : params.m_join = df::BevelJoin; + break; + default: + ASSERT(false, ()); + } + } + } + + RuleDrawer::RuleDrawer(drawer_callback_fn const & fn, const TileKey & tileKey, EngineContext & context) + : m_callback(fn) + , m_tileKey(tileKey) + , m_context(context) + { + m_globalRect = m_tileKey.GetGlobalRect(); + + int32_t tileSize = m_context.GetScalesProcessor().GetTileSize(); + m_geometryConvertor.OnSize(0, 0, tileSize, tileSize); + m_geometryConvertor.SetFromRect(m2::AnyRectD(m_globalRect)); + } + + void RuleDrawer::operator()(FeatureType const & f) + { + Stylist s; + m_callback(f, s); + + if (s.IsEmpty()) + return; + + if (s.IsCoastLine() && (!m_coastlines.insert(s.GetCaptionDescription().GetMainText()).second)) + 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 + + using namespace gp; + if (s.AreaStyleExists()) + { + typedef filter_screenpts_adapter functor_t; + + functor_t::params p; + p.m_convertor = & m_geometryConvertor; + p.m_rect = & m_globalRect; + + functor_t fun(p); + f.ForEachTriangleExRef(fun, m_tileKey.m_zoomLevel); + list & info = fun.m_points; + } + } +} diff --git a/drape_frontend/rule_drawer.hpp b/drape_frontend/rule_drawer.hpp new file mode 100644 index 0000000000..0249c8cd80 --- /dev/null +++ b/drape_frontend/rule_drawer.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "tile_key.hpp" + +#include "../geometry/rect2d.hpp" +#include "../geometry/screenbase.hpp" + +#include "../std/function.hpp" +#include "../std/set.hpp" +#include "../std/string.hpp" + +class FeatureType; + +namespace df +{ + class EngineContext; + class Stylist; + typedef function drawer_callback_fn; + + class RuleDrawer + { + public: + RuleDrawer(drawer_callback_fn const & fn, + TileKey const & tileKey, + EngineContext & context); + + void operator() (FeatureType const & f); + + private: + drawer_callback_fn m_callback; + TileKey m_tileKey; + EngineContext & m_context; + m2::RectD m_globalRect; + ScreenBase m_geometryConvertor; + set m_coastlines; + }; +} diff --git a/drape_frontend/tile_info.cpp b/drape_frontend/tile_info.cpp index 2edf5be4e4..f7ce5e8fd6 100644 --- a/drape_frontend/tile_info.cpp +++ b/drape_frontend/tile_info.cpp @@ -1,51 +1,57 @@ #include "tile_info.hpp" - +#include "engine_context.hpp" #include "stylist.hpp" - -#include "../map/feature_vec_model.hpp" -#include "../indexer/mercator.hpp" +#include "rule_drawer.hpp" #include "line_shape.hpp" #include "area_shape.hpp" #include "engine_context.hpp" +#include "../map/feature_vec_model.hpp" + +#include "../std/bind.hpp" + namespace { df::AreaShape * CreateFakeShape1() { df::AreaShape * shape = new df::AreaShape(Extract(0xFFEEAABB), 0.3f); - shape->AddTriangle(m2::PointF(0.0f, 0.0f), - m2::PointF(1.0f, 0.0f), - m2::PointF(0.0f, 1.0f)); + shape->AddTriangle(m2::PointF(50.0f, 50.0f), + m2::PointF(100.0f, 50.0f), + m2::PointF(50.0f, 100.0f)); - shape->AddTriangle(m2::PointF(1.0f, 0.0f), - m2::PointF(0.0f, 1.0f), - m2::PointF(1.0f, 1.0f)); + shape->AddTriangle(m2::PointF(100.0f, 50.0f), + m2::PointF(50.0f, 100.0f), + m2::PointF(100.0f, 100.0f)); return shape; } df::AreaShape * CreateFakeShape2() { df::AreaShape * shape = new df::AreaShape(Extract(0xFF66AAFF), 0.0f); - shape->AddTriangle(m2::PointF(-0.5f, 0.5f), - m2::PointF(0.5f, 1.5f), - m2::PointF(0.5f, -0.5f)); + shape->AddTriangle(m2::PointF(0.0f, 50.0f), + m2::PointF(50.0f, 150.0f), + m2::PointF(50.0f, 0.0f)); - shape->AddTriangle(m2::PointF(0.5f, -0.5f), - m2::PointF(0.5f, 1.5f), - m2::PointF(1.5f, 0.5f)); + shape->AddTriangle(m2::PointF(50.0f, 0.0f), + m2::PointF(50.0f, 150.0f), + m2::PointF(150.0f, 50.0f)); return shape; } df::LineShape * CreateFakeLine1() { vector points; - const float magn = 4; + const float magn = 40; for (float x = -4*math::pi; x <= 4*math::pi; x+= math::pi/32) - points.push_back(m2::PointF(x, magn*sinf(x))); + points.push_back(m2::PointF(50 * x + 100.0, magn*sinf(x) + 200.0)); - return new df::LineShape(points, Extract(0xFFFF0000), .0f, 1.f); + df::LineViewParams params; + params.m_color = Extract(0xFFFF0000); + params.m_width = 4.0f; + + return new df::LineShape(points, 0.0f, params); } df::LineShape * CreateFakeLine2() @@ -58,7 +64,18 @@ namespace points.push_back(m2::PointF(4,4)); points.push_back(m2::PointF(0,4)); points.push_back(m2::PointF(0,0)); - return new df::LineShape(points, Extract(0xFF00FF00), .5f, 0.5f); + + for (size_t i = 0; i < points.size(); ++i) + { + m2::PointF p = points[i] * 100; + points[i] = p + m2::PointF(100.0, 300.0); + } + + df::LineViewParams params; + params.m_color = Extract(0xFF00FF00); + params.m_width = 2.0f; + + return new df::LineShape(points, 0.5f, params); } struct IDsAccumulator @@ -88,16 +105,7 @@ namespace df m2::RectD TileInfo::GetGlobalRect() const { - double const worldSizeDevisor = 1 << m_key.m_zoomLevel; - double const rectSizeX = (MercatorBounds::maxX - MercatorBounds::minX) / worldSizeDevisor; - double const rectSizeY = (MercatorBounds::maxY - MercatorBounds::minY) / worldSizeDevisor; - - m2::RectD tileRect(m_key.m_x * rectSizeX, - m_key.m_y * rectSizeY, - (m_key.m_x + 1) * rectSizeX, - (m_key.m_y + 1) * rectSizeY); - - return tileRect; + return m_key.GetGlobalRect(); } void TileInfo::ReadFeatureIndex(model::FeaturesFetcher const & model) @@ -111,7 +119,7 @@ namespace df } } - void TileInfo::ReadFeatures(model::FeaturesFetcher const & model, + void TileInfo::ReadFeatures(model::FeaturesFetcher const & model, MemoryFeatureIndex & memIndex, EngineContext & context) { @@ -130,11 +138,11 @@ namespace df } context.EndReadTile(m_key); } - // vector featuresToRead; // for_each(indexes.begin(), indexes.end(), IDsAccumulator(featuresToRead, m_featureInfo)); -// model.ReadFeatures(*this, featuresToRead); +// RuleDrawer drawer(bind(&TileInfo::InitStylist, this, _1 ,_2), context); +// model.ReadFeatures(drawer, featuresToRead); } void TileInfo::Cancel(MemoryFeatureIndex & memIndex) @@ -144,26 +152,18 @@ namespace df memIndex.RemoveFeatures(m_featureInfo); } - bool TileInfo::operator ()(FeatureType const & f) - { - Stylist s; - InitStylist(f, m_key.m_zoomLevel, s); - - if (s.PointStyleExists()) - ASSERT(s.AreaStyleExists() == false && s.LineStyleExists() == false, ()); - - if (s.LineStyleExists()) - ASSERT(s.AreaStyleExists() == false && s.PointStyleExists() == false, ()); - - return true; - } - void TileInfo::operator ()(FeatureID const & id) { m_featureInfo.push_back(id); CheckCanceled(); } + void TileInfo::InitStylist(const FeatureType & f, Stylist & s) + { + CheckCanceled(); + df::InitStylist(f, m_key.m_zoomLevel, s); + } + //====================================================// bool TileInfo::DoNeedReadIndex() const @@ -177,7 +177,7 @@ namespace df memIndex.ReadFeaturesRequest(m_featureInfo, featureIndexes); } - void TileInfo::CheckCanceled() + void TileInfo::CheckCanceled() const { if (m_isCanceled) MYTHROW(ReadCanceledException, ()); diff --git a/drape_frontend/tile_info.hpp b/drape_frontend/tile_info.hpp index 5d2dd0a15f..eeffd7fcc5 100644 --- a/drape_frontend/tile_info.hpp +++ b/drape_frontend/tile_info.hpp @@ -1,11 +1,10 @@ #pragma once +#include "tile_key.hpp" #include "memory_feature_index.hpp" #include "../indexer/feature_decl.hpp" -#include "../geometry/rect2d.hpp" - #include "../base/mutex.hpp" #include "../base/exception.hpp" @@ -20,35 +19,7 @@ class FeatureType; namespace df { class EngineContext; - - struct TileKey - { - public: - TileKey() : m_x(-1), m_y(-1), m_zoomLevel(-1) {} - TileKey(int x, int y, int zoomLevel) - : m_x(x), m_y(y), m_zoomLevel(zoomLevel) {} - - bool operator < (const TileKey & other) const - { - if (m_zoomLevel != other.m_zoomLevel) - return m_zoomLevel < other.m_zoomLevel; - if (m_y != other.m_y) - return m_y < other.m_y; - - return m_x < other.m_x; - } - - bool operator == (const TileKey & other) const - { - return m_x == other.m_x && - m_y == other.m_y && - m_zoomLevel == other.m_zoomLevel; - } - - int m_x; - int m_y; - int m_zoomLevel; - }; + class Stylist; class TileInfo : private noncopyable { @@ -67,12 +38,12 @@ namespace df TileKey const & GetTileKey() const { return m_key; } void operator ()(FeatureID const & id); - bool operator ()(FeatureType const & f); bool operator <(TileInfo const & other) const { return m_key < other.m_key; } private: + void InitStylist(FeatureType const & f, Stylist & s); void RequestFeatures(MemoryFeatureIndex & memIndex, vector & featureIndexes); - void CheckCanceled(); + void CheckCanceled() const; bool DoNeedReadIndex() const; private: diff --git a/drape_frontend/tile_key.cpp b/drape_frontend/tile_key.cpp new file mode 100644 index 0000000000..5d726eadf7 --- /dev/null +++ b/drape_frontend/tile_key.cpp @@ -0,0 +1,46 @@ +#include "tile_key.hpp" + +#include "../indexer/mercator.hpp" + +namespace df +{ + TileKey::TileKey() : + m_x(-1), m_y(-1), m_zoomLevel(-1) + { + } + + TileKey::TileKey(int x, int y, int zoomLevel) + : m_x(x), m_y(y), m_zoomLevel(zoomLevel) + { + } + + bool TileKey::operator <(const TileKey & other) const + { + if (m_zoomLevel != other.m_zoomLevel) + return m_zoomLevel < other.m_zoomLevel; + if (m_y != other.m_y) + return m_y < other.m_y; + + return m_x < other.m_x; + } + + bool TileKey::operator ==(const TileKey & other) const + { + return m_x == other.m_x && + m_y == other.m_y && + m_zoomLevel == other.m_zoomLevel; + } + + m2::RectD TileKey::GetGlobalRect() const + { + double const worldSizeDevisor = 1 << m_zoomLevel; + // Mercator SizeX and SizeY is equal + double const rectSize = (MercatorBounds::maxX - MercatorBounds::minX) / worldSizeDevisor; + + double const startX = m_x * rectSize; + double const startY = m_y * rectSize; + + return m2::RectD (startX, startY, startX + rectSize, startY + rectSize); + } + +} diff --git a/drape_frontend/tile_key.hpp b/drape_frontend/tile_key.hpp new file mode 100644 index 0000000000..ea28736ca9 --- /dev/null +++ b/drape_frontend/tile_key.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "../geometry/rect2d.hpp" + +namespace df +{ + struct TileKey + { + TileKey(); + TileKey(int x, int y, int zoomLevel); + + bool operator < (const TileKey & other) const; + bool operator == (const TileKey & other) const; + + m2::RectD GetGlobalRect() const; + + int m_x; + int m_y; + int m_zoomLevel; + }; +}