Dump binary drawing rules to text proto structure.

This commit is contained in:
vng 2011-11-11 17:23:01 +03:00 committed by Alex Zolotarev
parent 495ad02617
commit 5bb51be08c
11 changed files with 184 additions and 9 deletions

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -81,6 +81,7 @@ public:
enum FeatureGeoType { FEATURE_TYPE_POINT = 0, FEATURE_TYPE_LINE, FEATURE_TYPE_AREA };
void GetSuitable(int scale, FeatureGeoType ft, vector<drule::Key> & keys) const;
inline vector<drule::Key> const & GetDrawingRules() const { return m_drawRule; }
bool IsDrawable(int scale) const;
bool IsDrawableAny() const;
@ -93,6 +94,13 @@ public:
toDo(&m_objs[i]);
}
template <class ToDo>
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; }

View file

@ -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 <google/protobuf/text_format.h>
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<Key> & v)
void RulesHolder::CreateRules(string const & name, uint8_t type, AttrsMapType const & attrs,
vector<Key> & 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<double> 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<string> 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<drule::Key> 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);
}
}

View file

@ -170,4 +170,6 @@ namespace drule
void ReadRules(ReaderPtrStream & s);
RulesHolder & rules();
void ConvertToProtocolBuffers(string & res);
}

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)