From 5bb51be08cd517177f1e003971121da902dbc12e Mon Sep 17 00:00:00 2001 From: vng Date: Fri, 11 Nov 2011 17:23:01 +0300 Subject: [PATCH] Dump binary drawing rules to text proto structure. --- generator/generator_tests/generator_tests.pro | 2 +- generator/generator_tool/generator_tool.cpp | 14 +- generator/generator_tool/generator_tool.pro | 3 +- indexer/classificator.hpp | 8 + indexer/drawing_rules.cpp | 154 +++++++++++++++++- indexer/drawing_rules.hpp | 2 + indexer/indexer_tests/indexer_tests.pro | 2 +- map/benchmark_tool/benchmark_tool.pro | 2 +- map/map_tests/map_tests.pro | 2 +- qt/qt.pro | 2 +- yg/yg_tests/yg_tests.pro | 2 +- 11 files changed, 184 insertions(+), 9 deletions(-) diff --git a/generator/generator_tests/generator_tests.pro b/generator/generator_tests/generator_tests.pro index 97c259edcd..cade84f33d 100644 --- a/generator/generator_tests/generator_tests.pro +++ b/generator/generator_tests/generator_tests.pro @@ -4,7 +4,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = map generator indexer platform geometry coding base expat sgitess +DEPENDENCIES = map generator indexer platform geometry coding base expat sgitess protobuf include($$ROOT_DIR/common.pri) diff --git a/generator/generator_tool/generator_tool.cpp b/generator/generator_tool/generator_tool.cpp index a2c5abcd53..f722b37f5b 100644 --- a/generator/generator_tool/generator_tool.cpp +++ b/generator/generator_tool/generator_tool.cpp @@ -11,6 +11,7 @@ #include "../generate_info.hpp" #include "../../indexer/classificator_loader.hpp" +#include "../../indexer/drawing_rules.hpp" #include "../../indexer/data_header.hpp" #include "../../indexer/features_vector.hpp" #include "../../indexer/index_builder.hpp" @@ -95,7 +96,18 @@ int main(int argc, char ** argv) // Make a classificator if (FLAGS_generate_classif) { - classificator::GenerateAndWrite(path); + //classificator::GenerateAndWrite(path); + + classificator::Read(pl.GetReader("drawing_rules.bin"), + pl.GetReader("classificator.txt"), + pl.GetReader("visibility.txt"), + pl.GetReader("types.txt")); + + string buffer; + drule::ConvertToProtocolBuffers(buffer); + + FileWriter w(path + "drules_proto.txt"); + w.Write(buffer.c_str(), buffer.size()); } // Generating intermediate files diff --git a/generator/generator_tool/generator_tool.pro b/generator/generator_tool/generator_tool.pro index 80ffe26c08..4f7c9bb48f 100644 --- a/generator/generator_tool/generator_tool.pro +++ b/generator/generator_tool/generator_tool.pro @@ -1,7 +1,8 @@ # Generator binary ROOT_DIR = ../.. -DEPENDENCIES = generator storage indexer platform geometry coding base gflags expat sgitess jansson version +DEPENDENCIES = generator storage indexer platform geometry coding base gflags expat sgitess \ + jansson version protobuf include($$ROOT_DIR/common.pri) diff --git a/indexer/classificator.hpp b/indexer/classificator.hpp index caabd66326..8012a8f55c 100644 --- a/indexer/classificator.hpp +++ b/indexer/classificator.hpp @@ -81,6 +81,7 @@ public: enum FeatureGeoType { FEATURE_TYPE_POINT = 0, FEATURE_TYPE_LINE, FEATURE_TYPE_AREA }; void GetSuitable(int scale, FeatureGeoType ft, vector & keys) const; + inline vector const & GetDrawingRules() const { return m_drawRule; } bool IsDrawable(int scale) const; bool IsDrawableAny() const; @@ -93,6 +94,13 @@ public: toDo(&m_objs[i]); } + template + void ForEachObjectConst(ToDo & toDo) const + { + for (size_t i = 0; i < m_objs.size(); ++i) + toDo(m_objs[i]); + } + typedef bitset<18> visible_mask_t; visible_mask_t GetVisibilityMask() const { return m_visibility; } void SetVisibilityMask(visible_mask_t mask) { m_visibility = mask; } diff --git a/indexer/drawing_rules.cpp b/indexer/drawing_rules.cpp index ed65ccb2b1..522ce39e7e 100644 --- a/indexer/drawing_rules.cpp +++ b/indexer/drawing_rules.cpp @@ -2,6 +2,8 @@ #include "drawing_rules.hpp" #include "scales.hpp" +#include "classificator.hpp" +#include "drules_struct.pb.h" #include "../coding/file_writer_stream.hpp" #include "../coding/file_reader_stream.hpp" @@ -18,6 +20,8 @@ #include "../std/exception.hpp" #include "../std/limits.hpp" +#include + namespace drule { @@ -830,7 +834,8 @@ namespace char const * arrClassTags[] = { "class", "mask-class" }; } -void RulesHolder::CreateRules(string const & name, uint8_t type, AttrsMapType const & attrs, vector & v) +void RulesHolder::CreateRules(string const & name, uint8_t type, AttrsMapType const & attrs, + vector & v) { bool added = false; @@ -1074,4 +1079,151 @@ RulesHolder & rules() return holder; } +namespace +{ + class RulesConvertor + { + RulesHolder & m_rules; + ContainerProto & m_cont; + + static int32_t GetStoringAlpha(BaseRule const * pSrc) + { + // 255 is default value for BaseRule - completely visible + // when storing alpha, 0 - is default value + int32_t r = 255 - pSrc->GetAlpha(); + r = r << 24; + return r; + } + + static int32_t GetColor(BaseRule const * pSrc) + { + return (pSrc->GetColor() | GetStoringAlpha(pSrc)); + } + static int32_t GetFillColor(BaseRule const * pSrc) + { + return (pSrc->GetFillColor() | GetStoringAlpha(pSrc)); + } + + static void Convert(BaseRule const * pSrc, LineRuleProto * pDest) + { + pDest->set_width(pSrc->GetWidth()); + pDest->set_color(GetColor(pSrc)); + + vector dd; + double dummy; + pSrc->GetPattern(dd, dummy); + if (!dd.empty()) + { + DashDotProto * p = pDest->mutable_dashdot(); + for_each(dd.begin(), dd.end(), bind(&DashDotProto::add_dd, p, _1)); + } + } + + static void Convert(BaseRule const * pSrc, AreaRuleProto * pDest) + { + pDest->set_color(GetFillColor(pSrc)); + if (pSrc->GetColor() != -1) + Convert(pSrc, pDest->mutable_border()); + } + + static void Convert(BaseRule const * pSrc, SymbolRuleProto * pDest) + { + string s; + pSrc->GetSymbol(s); + pDest->set_name(s); + } + + static void Convert(BaseRule const * pSrc, CaptionRuleProto * pDest) + { + pDest->set_height(pSrc->GetTextHeight()); + + if (pSrc->GetFillColor() != -1) + pDest->set_color(GetFillColor(pSrc)); + if (pSrc->GetColor() != -1) + pDest->set_stroke_color(GetColor(pSrc)); + } + + static void Convert(BaseRule const * pSrc, CircleRuleProto * pDest) + { + pDest->set_radius(pSrc->GetRadius()); + pDest->set_color(GetFillColor(pSrc)); + } + + vector m_parents; + + string GetFullName(ClassifObject const & o) const + { + string res; + for (size_t i = 0; i < m_parents.size(); ++i) + res = res + m_parents[i] + "-"; + res = res + o.GetName(); + return res; + } + + public: + RulesConvertor(ContainerProto & cont) + : m_rules(drule::rules()), m_cont(cont) + { + } + + void operator() (ClassifObject const & o) + { + using namespace drule; + + vector keys = o.GetDrawingRules(); + MakeUnique(keys); + SortByScaleTypeDepth(keys); + + if (!keys.empty()) + { + ClassifElementProto * pCE = m_cont.add_cont(); + pCE->set_name(GetFullName(o)); + + for (size_t i = 0; i < keys.size(); ++i) + { + // skip unnecessary trash + if (keys[i].m_type > circle) + continue; + + DrawElementProto * pDE = pCE->add_element(); + pDE->set_scale(keys[i].m_scale); + + BaseRule const * pRule = m_rules.Find(keys[i]); + switch (keys[i].m_type) + { + case line: + Convert(pRule, pDE->add_lines()); + break; + case area: + Convert(pRule, pDE->mutable_area()); + break; + case symbol: + Convert(pRule, pDE->mutable_symbol()); + break; + case caption: + Convert(pRule, pDE->mutable_caption()); + break; + case circle: + Convert(pRule, pDE->mutable_circle()); + break; + } + } + } + + m_parents.push_back(o.GetName()); + o.ForEachObjectConst(*this); + m_parents.pop_back(); + } + }; +} + +void ConvertToProtocolBuffers(string & res) +{ + ContainerProto cont; + RulesConvertor conv(cont); + classif().GetRoot()->ForEachObjectConst(conv); + + google::protobuf::TextFormat::PrintToString(cont, &res); +} + } diff --git a/indexer/drawing_rules.hpp b/indexer/drawing_rules.hpp index 1e42ed98b3..0753219418 100644 --- a/indexer/drawing_rules.hpp +++ b/indexer/drawing_rules.hpp @@ -170,4 +170,6 @@ namespace drule void ReadRules(ReaderPtrStream & s); RulesHolder & rules(); + + void ConvertToProtocolBuffers(string & res); } diff --git a/indexer/indexer_tests/indexer_tests.pro b/indexer/indexer_tests/indexer_tests.pro index ae389e57ef..c9fd714d57 100644 --- a/indexer/indexer_tests/indexer_tests.pro +++ b/indexer/indexer_tests/indexer_tests.pro @@ -4,7 +4,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = indexer platform coding base +DEPENDENCIES = indexer platform coding base protobuf include($$ROOT_DIR/common.pri) QT *= core diff --git a/map/benchmark_tool/benchmark_tool.pro b/map/benchmark_tool/benchmark_tool.pro index 03ed3d426f..4f67bf0765 100644 --- a/map/benchmark_tool/benchmark_tool.pro +++ b/map/benchmark_tool/benchmark_tool.pro @@ -6,7 +6,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = map indexer platform coding base gflags +DEPENDENCIES = map indexer platform coding base gflags protobuf include($$ROOT_DIR/common.pri) diff --git a/map/map_tests/map_tests.pro b/map/map_tests/map_tests.pro index 16fd5ea02e..399bbafbc6 100644 --- a/map/map_tests/map_tests.pro +++ b/map/map_tests/map_tests.pro @@ -6,7 +6,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = map yg indexer platform geometry coding base freetype fribidi expat +DEPENDENCIES = map yg indexer platform geometry coding base freetype fribidi expat protobuf include($$ROOT_DIR/common.pri) diff --git a/qt/qt.pro b/qt/qt.pro index 643868de51..3ce823fc60 100644 --- a/qt/qt.pro +++ b/qt/qt.pro @@ -1,7 +1,7 @@ # Main application in qt. ROOT_DIR = .. DEPENDENCIES = words map search storage indexer yg platform geometry coding base \ - bzip2 freetype expat fribidi tomcrypt jansson version + bzip2 freetype expat fribidi tomcrypt jansson version protobuf include($$ROOT_DIR/common.pri) diff --git a/yg/yg_tests/yg_tests.pro b/yg/yg_tests/yg_tests.pro index d141f36380..c464f26104 100644 --- a/yg/yg_tests/yg_tests.pro +++ b/yg/yg_tests/yg_tests.pro @@ -4,7 +4,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = qt_tstfrm map indexer yg platform geometry coding base expat freetype fribidi +DEPENDENCIES = qt_tstfrm map indexer yg platform geometry coding base expat freetype fribidi protobuf include($$ROOT_DIR/common.pri)