render building symbolizer rewrite

- make projection and view transforms first
- removed dependency on segment_t
- no need for sorting faces
This commit is contained in:
Jiri Drbalek 2016-08-29 12:35:28 +00:00
parent 6e71bad70c
commit 4a93209423
5 changed files with 92 additions and 168 deletions

View file

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

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

View file

@ -28,7 +28,6 @@
#include <mapnik/symbolizer.hpp> #include <mapnik/symbolizer.hpp>
#include <mapnik/proj_transform.hpp> #include <mapnik/proj_transform.hpp>
#include <mapnik/cairo/cairo_renderer.hpp> #include <mapnik/cairo/cairo_renderer.hpp>
#include <mapnik/transform_path_adapter.hpp>
// mapnik symbolizer generics // mapnik symbolizer generics
#include <mapnik/renderer_common/process_building_symbolizer.hpp> #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, mapnik::feature_impl & feature,
proj_transform const& prj_trans) proj_transform const& prj_trans)
{ {
using transform_path_type = transform_path_adapter<view_transform,vertex_adapter>;
cairo_save_restore guard(context_); cairo_save_restore guard(context_);
composite_mode_e comp_op = get<composite_mode_e, keys::comp_op>(sym, feature, common_.vars_); 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_); 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); context_.set_operator(comp_op);
render_building_symbolizer( render_building_symbolizer::apply(
feature, height, feature, prj_trans, common_.t_, height,
[&](path_type const& faces) [&](path_type const& faces)
{ {
vertex_adapter va(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, 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); fill.blue() * 0.8 / 255.0, fill.alpha() * opacity / 255.0);
context_.add_path(faces_path); context_.add_path(va);
context_.fill(); context_.fill();
}, },
[&](path_type const& frame) [&](path_type const& frame)
{ {
vertex_adapter va(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, 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); fill.blue() * 0.8 / 255.0, fill.alpha() * opacity / 255.0);
context_.set_line_width(common_.scale_factor_); 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(); 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_.set_color(fill, opacity);
context_.add_path(roof_path); context_.add_path(roof);
context_.fill(); context_.fill();
}); });
} }

View file

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