Factored out common implementation of point symbolizer.

This commit is contained in:
Matt Amos 2013-12-06 17:30:06 +00:00
parent 05982c4caf
commit da5574d247
4 changed files with 126 additions and 188 deletions

View file

@ -0,0 +1,102 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 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_RENDERER_COMMON_PROCESS_POINT_SYMBOLIZER_HPP
#define MAPNIK_RENDERER_COMMON_PROCESS_POINT_SYMBOLIZER_HPP
namespace mapnik {
template <typename F>
void render_point_symbolizer(point_symbolizer const &sym,
mapnik::feature_impl &feature,
proj_transform const &prj_trans,
renderer_common &common,
F render_marker)
{
std::string filename = get<std::string>(sym, keys::file, feature);
boost::optional<mapnik::marker_ptr> marker;
if ( !filename.empty() )
{
marker = marker_cache::instance().find(filename, true);
}
else
{
marker.reset(std::make_shared<mapnik::marker>());
}
if (marker)
{
double opacity = get<double>(sym,keys::opacity,feature, 1.0);
bool allow_overlap = get<bool>(sym, keys::allow_overlap, feature, false);
bool ignore_placement = get<bool>(sym, keys::ignore_placement, feature, false);
point_placement_enum placement= get<point_placement_enum>(sym, keys::point_placement_type, feature, CENTROID_POINT_PLACEMENT);
box2d<double> const& bbox = (*marker)->bounding_box();
coord2d center = bbox.center();
agg::trans_affine tr;
auto image_transform = get_optional<transform_type>(sym, keys::image_transform);
if (image_transform) evaluate_transform(tr, feature, *image_transform);
agg::trans_affine_translation recenter(-center.x, -center.y);
agg::trans_affine recenter_tr = recenter * tr;
box2d<double> label_ext = bbox * recenter_tr * agg::trans_affine_scaling(common.scale_factor_);
for (std::size_t i=0; i<feature.num_geometries(); ++i)
{
geometry_type const& geom = feature.get_geometry(i);
double x;
double y;
double z=0;
if (placement == CENTROID_POINT_PLACEMENT)
{
if (!label::centroid(geom, x, y))
return;
}
else
{
if (!label::interior_position(geom ,x, y))
return;
}
prj_trans.backward(x,y,z);
common.t_.forward(&x,&y);
label_ext.re_center(x,y);
if (allow_overlap ||
common.detector_->has_placement(label_ext))
{
render_marker(pixel_position(x, y),
**marker,
tr,
opacity);
if (!ignore_placement)
common.detector_->insert(label_ext);
}
}
}
}
} // namespace mapnik
#endif /* MAPNIK_RENDERER_COMMON_PROCESS_POINT_SYMBOLIZER_HPP */

View file

@ -33,7 +33,7 @@
#include <mapnik/label_collision_detector.hpp> #include <mapnik/label_collision_detector.hpp>
#include <mapnik/parse_path.hpp> #include <mapnik/parse_path.hpp>
#include <mapnik/pixel_position.hpp> #include <mapnik/pixel_position.hpp>
#include <mapnik/renderer_common/process_point_symbolizer.hpp>
// agg // agg
#include "agg_trans_affine.h" #include "agg_trans_affine.h"
@ -50,71 +50,14 @@ void agg_renderer<T0,T1>::process(point_symbolizer const& sym,
mapnik::feature_impl & feature, mapnik::feature_impl & feature,
proj_transform const& prj_trans) proj_transform const& prj_trans)
{ {
std::string filename = get<std::string>(sym, keys::file, feature); composite_mode_e comp_op = get<composite_mode_e>(sym, keys::comp_op, feature, src_over);
boost::optional<mapnik::marker_ptr> marker;
if ( !filename.empty() )
{
marker = marker_cache::instance().find(filename, true);
}
else
{
marker.reset(std::make_shared<mapnik::marker>());
}
if (marker) render_point_symbolizer(
{ sym, feature, prj_trans, common_,
double opacity = get<double>(sym,keys::opacity,feature, 1.0); [&](pixel_position const& pos, marker const& marker,
bool allow_overlap = get<bool>(sym, keys::allow_overlap, feature, false); agg::trans_affine const& tr, double opacity) {
bool ignore_placement = get<bool>(sym, keys::ignore_placement, feature, false); render_marker(pos, marker, tr, opacity, comp_op);
point_placement_enum placement= get<point_placement_enum>(sym, keys::point_placement_type, feature, CENTROID_POINT_PLACEMENT); });
composite_mode_e comp_op = get<composite_mode_e>(sym, keys::comp_op, feature, src_over);
box2d<double> const& bbox = (*marker)->bounding_box();
coord2d center = bbox.center();
agg::trans_affine tr;
auto image_transform = get_optional<transform_type>(sym, keys::image_transform);
if (image_transform) evaluate_transform(tr, feature, *image_transform);
agg::trans_affine_translation recenter(-center.x, -center.y);
agg::trans_affine recenter_tr = recenter * tr;
box2d<double> label_ext = bbox * recenter_tr * agg::trans_affine_scaling(common_.scale_factor_);
for (std::size_t i=0; i<feature.num_geometries(); ++i)
{
geometry_type const& geom = feature.get_geometry(i);
double x;
double y;
double z=0;
if (placement == CENTROID_POINT_PLACEMENT)
{
if (!label::centroid(geom, x, y))
return;
}
else
{
if (!label::interior_position(geom ,x, y))
return;
}
prj_trans.backward(x,y,z);
common_.t_.forward(&x,&y);
label_ext.re_center(x,y);
if (allow_overlap ||
common_.detector_->has_placement(label_ext))
{
render_marker(pixel_position(x, y),
**marker,
tr,
opacity,
comp_op);
if (!ignore_placement)
common_.detector_->insert(label_ext);
}
}
}
} }
template void agg_renderer<image_32>::process(point_symbolizer const&, template void agg_renderer<image_32>::process(point_symbolizer const&,

View file

@ -56,6 +56,7 @@
// mapnik symbolizer generics // mapnik symbolizer generics
#include <mapnik/renderer_common/process_building_symbolizer.hpp> #include <mapnik/renderer_common/process_building_symbolizer.hpp>
#include <mapnik/renderer_common/process_point_symbolizer.hpp>
// cairo // cairo
#include <cairo.h> #include <cairo.h>
@ -561,71 +562,17 @@ void cairo_renderer_base::process(point_symbolizer const& sym,
mapnik::feature_impl & feature, mapnik::feature_impl & feature,
proj_transform const& prj_trans) proj_transform const& prj_trans)
{ {
std::string filename = get<std::string>(sym, keys::file, feature);
double opacity = get<double>(sym, keys::opacity, feature, 1.0);
point_placement_enum placement = get<point_placement_enum>(sym, keys::point_placement_type, feature, CENTROID_POINT_PLACEMENT);
bool allow_overlap = get<bool>(sym, keys::allow_overlap, feature, false);
bool ignore_placement = get<bool>(sym, keys::ignore_placement, feature, false);
auto geom_transform = get_optional<transform_type>(sym, keys::geometry_transform);
composite_mode_e comp_op = get<composite_mode_e>(sym, keys::comp_op, feature, src_over); composite_mode_e comp_op = get<composite_mode_e>(sym, keys::comp_op, feature, src_over);
cairo_save_restore guard(context_); cairo_save_restore guard(context_);
context_.set_operator(comp_op); context_.set_operator(comp_op);
boost::optional<marker_ptr> marker; render_point_symbolizer(
if ( !filename.empty() ) sym, feature, prj_trans, common_,
{ [&](pixel_position const& pos, marker const& marker,
marker = marker_cache::instance().find(filename, true); agg::trans_affine const& tr, double opacity) {
} render_marker(pos, marker, tr, opacity);
else });
{
marker.reset(std::make_shared<mapnik::marker>());
}
if (marker)
{
box2d<double> const& bbox = (*marker)->bounding_box();
coord2d center = bbox.center();
agg::trans_affine tr;
if (geom_transform) { evaluate_transform(tr, feature, *geom_transform); }
agg::trans_affine_translation recenter(-center.x, -center.y);
agg::trans_affine recenter_tr = recenter * tr;
box2d<double> label_ext = bbox * recenter_tr * agg::trans_affine_scaling(common_.scale_factor_);
for (std::size_t i = 0; i < feature.num_geometries(); ++i)
{
geometry_type const& geom = feature.get_geometry(i);
double x;
double y;
double z = 0;
if (placement == CENTROID_POINT_PLACEMENT)
{
if (!label::centroid(geom, x, y))
return;
}
else
{
if (!label::interior_position(geom ,x, y))
return;
}
prj_trans.backward(x, y, z);
common_.t_.forward(&x, &y);
label_ext.re_center(x,y);
if (allow_overlap ||
common_.detector_->has_placement(label_ext))
{
render_marker(pixel_position(x,y),**marker, tr, opacity);
if (!ignore_placement)
common_.detector_->insert(label_ext);
}
}
}
} }
void cairo_renderer_base::process(shield_symbolizer const& sym, void cairo_renderer_base::process(shield_symbolizer const& sym,

View file

@ -33,6 +33,7 @@
#include <mapnik/marker_cache.hpp> #include <mapnik/marker_cache.hpp>
#include <mapnik/parse_path.hpp> #include <mapnik/parse_path.hpp>
#include <mapnik/pixel_position.hpp> #include <mapnik/pixel_position.hpp>
#include <mapnik/renderer_common/process_point_symbolizer.hpp>
// agg // agg
#include "agg_trans_affine.h" #include "agg_trans_affine.h"
@ -49,75 +50,20 @@ void grid_renderer<T>::process(point_symbolizer const& sym,
mapnik::feature_impl & feature, mapnik::feature_impl & feature,
proj_transform const& prj_trans) proj_transform const& prj_trans)
{ {
std::string filename = get<std::string>(sym, keys::file, feature); composite_mode_e comp_op = get<composite_mode_e>(sym, keys::comp_op, feature, src_over);
boost::optional<mapnik::marker_ptr> marker;
if ( !filename.empty() )
{
marker = marker_cache::instance().find(filename, true);
}
else
{
marker.reset(std::make_shared<mapnik::marker>());
}
if (marker)
{
double opacity = get<double>(sym,keys::opacity,feature, 1.0);
bool allow_overlap = get<bool>(sym, keys::allow_overlap, feature, false);
bool ignore_placement = get<bool>(sym, keys::ignore_placement, feature, false);
point_placement_enum placement= get<point_placement_enum>(sym, keys::point_placement_type, feature, CENTROID_POINT_PLACEMENT);
auto img_transform = get_optional<transform_type>(sym, keys::image_transform);
composite_mode_e comp_op = get<composite_mode_e>(sym, keys::comp_op, feature, src_over);
box2d<double> const& bbox = (*marker)->bounding_box();
coord2d center = bbox.center();
agg::trans_affine tr;
if (img_transform) { evaluate_transform(tr, feature, *img_transform); }
tr = agg::trans_affine_scaling(common_.scale_factor_) * tr;
agg::trans_affine_translation recenter(-center.x, -center.y);
agg::trans_affine recenter_tr = recenter * tr;
box2d<double> label_ext = bbox * recenter_tr * agg::trans_affine_scaling(common_.scale_factor_) ;
for (std::size_t i=0; i<feature.num_geometries(); ++i)
{
geometry_type const& geom = feature.get_geometry(i);
double x;
double y;
double z=0;
if (placement == CENTROID_POINT_PLACEMENT)
{
if (!label::centroid(geom, x, y))
return;
}
else
{
if (!label::interior_position(geom ,x, y))
return;
}
prj_trans.backward(x,y,z);
common_.t_.forward(&x,&y);
label_ext.re_center(x,y);
if (allow_overlap ||
common_.detector_->has_placement(label_ext))
{
render_point_symbolizer(
sym, feature, prj_trans, common_,
[&](pixel_position const &pos, marker const &marker,
agg::trans_affine const &tr, double opacity) {
render_marker(feature, render_marker(feature,
pixmap_.get_resolution(), pixmap_.get_resolution(),
pixel_position(x, y), pos,
**marker, marker,
tr, tr,
opacity, opacity,
comp_op); comp_op);
});
if (!ignore_placement)
common_.detector_->insert(label_ext);
}
}
}
} }
template void grid_renderer<grid>::process(point_symbolizer const&, template void grid_renderer<grid>::process(point_symbolizer const&,