Merge pull request #3506 from mapycz/fix-building-symbolizer-transform

Building symbolizer improvements
This commit is contained in:
lightmare 2018-07-27 12:55:53 +02:00 committed by GitHub
commit 686355cbbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 169 deletions

View file

@ -23,120 +23,104 @@
#ifndef MAPNIK_RENDERER_COMMON_PROCESS_BUILDING_SYMBOLIZER_HPP
#define MAPNIK_RENDERER_COMMON_PROCESS_BUILDING_SYMBOLIZER_HPP
#include <mapnik/segment.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/vertex_adapters.hpp>
#include <mapnik/path.hpp>
#include <mapnik/transform_path_adapter.hpp>
#include <algorithm>
#include <deque>
#pragma GCC diagnostic push
#include <mapnik/warning_ignore_agg.hpp>
#include "agg_conv_transform.h"
#pragma GCC diagnostic pop
namespace mapnik {
namespace detail {
struct render_building_symbolizer
{
using vertex_adapter_type = geometry::polygon_vertex_adapter<double>;
using transform_path_type = transform_path_adapter<view_transform, vertex_adapter_type>;
using roof_type = agg::conv_transform<transform_path_type>;
template <typename F1, typename F2, typename F3>
void make_building(geometry::polygon<double> const& poly, double height, F1 const& face_func, F2 const& frame_func, F3 const& roof_func)
{
path_type frame(path_type::types::LineString);
path_type roof(path_type::types::Polygon);
std::deque<segment_t> face_segments;
double ring_begin_x, ring_begin_y;
double x0 = 0;
double y0 = 0;
double x,y;
geometry::polygon_vertex_adapter<double> va(poly);
va.rewind(0);
for (unsigned cm = va.vertex(&x, &y); cm != SEG_END;
cm = va.vertex(&x, &y))
{
if (cm == SEG_MOVETO)
{
frame.move_to(x,y);
ring_begin_x = x;
ring_begin_y = y;
}
else if (cm == SEG_LINETO)
{
frame.line_to(x,y);
face_segments.emplace_back(x0,y0,x,y);
}
else if (cm == SEG_CLOSE)
{
frame.close_path();
if (!face_segments.empty())
{
face_segments.emplace_back(x0, y0, ring_begin_x, ring_begin_y);
}
}
x0 = x;
y0 = y;
}
std::sort(face_segments.begin(),face_segments.end(), y_order);
for (auto const& seg : face_segments)
{
path_type faces(path_type::types::Polygon);
faces.move_to(std::get<0>(seg),std::get<1>(seg));
faces.line_to(std::get<2>(seg),std::get<3>(seg));
faces.line_to(std::get<2>(seg),std::get<3>(seg) + height);
faces.line_to(std::get<0>(seg),std::get<1>(seg) + height);
face_func(faces);
//
frame.move_to(std::get<0>(seg),std::get<1>(seg));
frame.line_to(std::get<0>(seg),std::get<1>(seg)+height);
}
va.rewind(0);
for (unsigned cm = va.vertex(&x, &y); cm != SEG_END;
cm = va.vertex(&x, &y))
{
if (cm == SEG_MOVETO)
{
frame.move_to(x,y+height);
roof.move_to(x,y+height);
}
else if (cm == SEG_LINETO)
{
frame.line_to(x,y+height);
roof.line_to(x,y+height);
}
else if (cm == SEG_CLOSE)
{
frame.close_path();
roof.close_path();
}
}
frame_func(frame);
roof_func(roof);
}
} // ns detail
template <typename F1, typename F2, typename F3>
void render_building_symbolizer(mapnik::feature_impl const& feature,
static void apply(feature_impl const& feature,
proj_transform const& prj_trans,
view_transform const& view_trans,
double height,
F1 face_func, F2 frame_func, F3 roof_func)
{
auto const& geom = feature.get_geometry();
if (geom.is<geometry::polygon<double>>())
{
auto const& poly = geom.get<geometry::polygon<double>>();
detail::make_building(poly, height, face_func, frame_func, roof_func);
vertex_adapter_type va(poly);
transform_path_type transformed(view_trans, va, prj_trans);
make_building(transformed, height, face_func, frame_func, roof_func);
}
else if (geom.is<geometry::multi_polygon<double>>())
{
auto const& multi_poly = geom.get<geometry::multi_polygon<double>>();
for (auto const& poly : multi_poly)
{
detail::make_building(poly, height, face_func, frame_func, roof_func);
vertex_adapter_type va(poly);
transform_path_type transformed(view_trans, va, prj_trans);
make_building(transformed, height, face_func, frame_func, roof_func);
}
}
}
private:
template <typename F>
static void render_face(double x0, double y0, double x, double y, double height, F const& face_func, path_type & frame)
{
path_type faces(path_type::types::Polygon);
faces.move_to(x0, y0);
faces.line_to(x, y);
faces.line_to(x, y - height);
faces.line_to(x0, y0 - height);
face_func(faces);
frame.move_to(x0, y0);
frame.line_to(x, y);
frame.line_to(x, y - height);
frame.line_to(x0, y0 - height);
}
template <typename Geom, typename F1, typename F2, typename F3>
static void make_building(Geom & poly, double height, F1 const& face_func, F2 const& frame_func, F3 const& roof_func)
{
path_type frame(path_type::types::LineString);
double ring_begin_x, ring_begin_y;
double x0 = 0;
double y0 = 0;
double x, y;
poly.rewind(0);
for (unsigned cm = poly.vertex(&x, &y); cm != SEG_END; cm = poly.vertex(&x, &y))
{
if (cm == SEG_MOVETO)
{
ring_begin_x = x;
ring_begin_y = y;
}
else if (cm == SEG_LINETO)
{
render_face(x0, y0, x, y, height, face_func, frame);
}
else if (cm == SEG_CLOSE)
{
render_face(x0, y0, ring_begin_x, ring_begin_y, height, face_func, frame);
}
x0 = x;
y0 = y;
}
frame_func(frame);
agg::trans_affine_translation tr(0, -height);
roof_type roof(poly, tr);
roof_func(roof);
}
};
} // namespace mapnik
#endif // MAPNIK_RENDERER_COMMON_PROCESS_BUILDING_SYMBOLIZER_HPP

View file

@ -1,43 +0,0 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2017 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_SEGMENT_HPP
#define MAPNIK_SEGMENT_HPP
#include <tuple>
#include <algorithm>
namespace mapnik
{
using segment_t = std::tuple<double,double,double,double>;
static inline bool y_order(segment_t const& first,segment_t const& second)
{
double miny0 = std::min(std::get<1>(first), std::get<3>(first));
double miny1 = std::min(std::get<1>(second), std::get<3>(second));
return miny0 > miny1;
}
}
#endif // MAPNIK_SEGMENT_HPP

View file

@ -27,12 +27,10 @@
#include <mapnik/agg_renderer.hpp>
#include <mapnik/agg_rasterizer.hpp>
#include <mapnik/agg_helpers.hpp>
#include <mapnik/segment.hpp>
#include <mapnik/symbolizer.hpp>
#include <mapnik/expression_evaluator.hpp>
#include <mapnik/expression.hpp>
#include <mapnik/renderer_common/process_building_symbolizer.hpp>
#include <mapnik/transform_path_adapter.hpp>
// stl
#include <deque>
@ -58,7 +56,6 @@ void agg_renderer<T0,T1>::process(building_symbolizer const& sym,
mapnik::feature_impl & feature,
proj_transform const& prj_trans)
{
using transform_path_type = transform_path_adapter<view_transform, vertex_adapter>;
using ren_base = agg::renderer_base<agg::pixfmt_rgba32_pre>;
using renderer = agg::renderer_scanline_aa_solid<ren_base>;
@ -88,13 +85,12 @@ void agg_renderer<T0,T1>::process(building_symbolizer const& sym,
double height = get<double, keys::height>(sym, feature, common_.vars_) * common_.scale_factor_;
render_building_symbolizer(
feature, height,
render_building_symbolizer::apply(
feature, prj_trans, common_.t_, height,
[&,r,g,b,a,opacity](path_type const& faces)
{
vertex_adapter va(faces);
transform_path_type faces_path (this->common_.t_,va,prj_trans);
ras_ptr->add_path(faces_path);
ras_ptr->add_path(va);
ren.color(agg::rgba8_pre(int(r*0.8), int(g*0.8), int(b*0.8), int(a * opacity)));
agg::render_scanlines(*ras_ptr, sl, ren);
this->ras_ptr->reset();
@ -102,19 +98,17 @@ void agg_renderer<T0,T1>::process(building_symbolizer const& sym,
[&,r,g,b,a,opacity](path_type const& frame)
{
vertex_adapter va(frame);
transform_path_type path(common_.t_,va, prj_trans);
agg::conv_stroke<transform_path_type> stroke(path);
agg::conv_stroke<vertex_adapter> stroke(va);
stroke.width(common_.scale_factor_);
stroke.miter_limit(common_.scale_factor_ / 2.0);
ras_ptr->add_path(stroke);
ren.color(agg::rgba8_pre(int(r*0.8), int(g*0.8), int(b*0.8), int(a * opacity)));
agg::render_scanlines(*ras_ptr, sl, ren);
ras_ptr->reset();
},
[&,r,g,b,a,opacity](path_type const& roof)
[&,r,g,b,a,opacity](render_building_symbolizer::roof_type & roof)
{
vertex_adapter va(roof);
transform_path_type roof_path (common_.t_,va,prj_trans);
ras_ptr->add_path(roof_path);
ras_ptr->add_path(roof);
ren.color(agg::rgba8_pre(r, g, b, int(a * opacity)));
agg::render_scanlines(*ras_ptr, sl, ren);
});

View file

@ -28,7 +28,6 @@
#include <mapnik/symbolizer.hpp>
#include <mapnik/proj_transform.hpp>
#include <mapnik/cairo/cairo_renderer.hpp>
#include <mapnik/transform_path_adapter.hpp>
// mapnik symbolizer generics
#include <mapnik/renderer_common/process_building_symbolizer.hpp>
@ -44,7 +43,6 @@ void cairo_renderer<T>::process(building_symbolizer const& sym,
mapnik::feature_impl & feature,
proj_transform const& prj_trans)
{
using transform_path_type = transform_path_adapter<view_transform,vertex_adapter>;
cairo_save_restore guard(context_);
composite_mode_e comp_op = get<composite_mode_e, keys::comp_op>(sym, feature, common_.vars_);
mapnik::color fill = get<color, keys::fill>(sym, feature, common_.vars_);
@ -53,33 +51,30 @@ void cairo_renderer<T>::process(building_symbolizer const& sym,
context_.set_operator(comp_op);
render_building_symbolizer(
feature, height,
render_building_symbolizer::apply(
feature, prj_trans, common_.t_, height,
[&](path_type const& faces)
{
vertex_adapter va(faces);
transform_path_type faces_path(common_.t_, va, prj_trans);
context_.set_color(fill.red() * 0.8 / 255.0, fill.green() * 0.8 / 255.0,
fill.blue() * 0.8 / 255.0, fill.alpha() * opacity / 255.0);
context_.add_path(faces_path);
context_.add_path(va);
context_.fill();
},
[&](path_type const& frame)
{
vertex_adapter va(frame);
transform_path_type path(common_.t_, va, prj_trans);
context_.set_color(fill.red() * 0.8 / 255.0, fill.green() * 0.8/255.0,
fill.blue() * 0.8 / 255.0, fill.alpha() * opacity / 255.0);
context_.set_line_width(common_.scale_factor_);
context_.add_path(path);
context_.set_miter_limit(common_.scale_factor_ / 2.0);
context_.add_path(va);
context_.stroke();
},
[&](path_type const& roof)
[&](render_building_symbolizer::roof_type & roof)
{
vertex_adapter va(roof);
transform_path_type roof_path(common_.t_, va, prj_trans);
context_.set_color(fill, opacity);
context_.add_path(roof_path);
context_.add_path(roof);
context_.fill();
});
}

View file

@ -29,8 +29,6 @@
#include <mapnik/grid/grid_renderer.hpp>
#include <mapnik/grid/grid_renderer_base.hpp>
#include <mapnik/grid/grid.hpp>
#include <mapnik/transform_path_adapter.hpp>
#include <mapnik/segment.hpp>
#include <mapnik/expression_evaluator.hpp>
#include <mapnik/expression.hpp>
#include <mapnik/symbolizer.hpp>
@ -59,7 +57,6 @@ void grid_renderer<T>::process(building_symbolizer const& sym,
using pixfmt_type = typename grid_renderer_base_type::pixfmt_type;
using color_type = typename grid_renderer_base_type::pixfmt_type::color_type;
using renderer_type = agg::renderer_scanline_bin_solid<grid_renderer_base_type>;
using transform_path_type = transform_path_adapter<view_transform, vertex_adapter>;
agg::scanline_bin sl;
grid_rendering_buffer buf(pixmap_.raw_data(), common_.width_, common_.height_, common_.width_);
@ -72,13 +69,12 @@ void grid_renderer<T>::process(building_symbolizer const& sym,
double height = get<value_double>(sym, keys::height, feature, common_.vars_, 0.0);
render_building_symbolizer(
feature, height,
render_building_symbolizer::apply(
feature, prj_trans, common_.t_, height,
[&](path_type const& faces)
{
vertex_adapter va(faces);
transform_path_type faces_path (common_.t_,va,prj_trans);
ras_ptr->add_path(faces_path);
ras_ptr->add_path(va);
ren.color(color_type(feature.id()));
agg::render_scanlines(*ras_ptr, sl, ren);
ras_ptr->reset();
@ -86,18 +82,16 @@ void grid_renderer<T>::process(building_symbolizer const& sym,
[&](path_type const& frame)
{
vertex_adapter va(frame);
transform_path_type path(common_.t_,va,prj_trans);
agg::conv_stroke<transform_path_type> stroke(path);
agg::conv_stroke<vertex_adapter> stroke(va);
stroke.miter_limit(common_.scale_factor_ / 2.0);
ras_ptr->add_path(stroke);
ren.color(color_type(feature.id()));
agg::render_scanlines(*ras_ptr, sl, ren);
ras_ptr->reset();
},
[&](path_type const& roof)
[&](render_building_symbolizer::roof_type & roof)
{
vertex_adapter va(roof);
transform_path_type roof_path (common_.t_,va,prj_trans);
ras_ptr->add_path(roof_path);
ras_ptr->add_path(roof);
ren.color(color_type(feature.id()));
agg::render_scanlines(*ras_ptr, sl, ren);
});

@ -1 +1 @@
Subproject commit 574ad9bf84450708a7f8befa63dddbc0a377cfa5
Subproject commit bbeb5ec35c75fa8b5712f8215f132a19e84ea1fb