/usr/include/mapnik/marker_helpers.hpp is in libmapnik-dev 2.2.0+ds1-6build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 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_MARKER_HELPERS_HPP
#define MAPNIK_MARKER_HELPERS_HPP
#include <mapnik/color.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/geom_util.hpp>
#include <mapnik/markers_symbolizer.hpp>
#include <mapnik/expression_evaluator.hpp>
#include <mapnik/svg/svg_path_attributes.hpp>
#include <mapnik/svg/svg_converter.hpp>
#include <mapnik/marker.hpp> // for svg_storage_type
#include <mapnik/svg/svg_storage.hpp>
#include <mapnik/markers_placement.hpp>
// agg
#include "agg_ellipse.h"
#include "agg_basics.h"
#include "agg_renderer_base.h"
#include "agg_renderer_scanline.h"
#include "agg_rendering_buffer.h"
#include "agg_scanline_u.h"
#include "agg_image_filters.h"
#include "agg_trans_bilinear.h"
#include "agg_span_allocator.h"
#include "agg_image_accessors.h"
#include "agg_pixfmt_rgba.h"
#include "agg_span_image_filter_rgba.h"
#include "agg_span_interpolator_linear.h"
// boost
#include <boost/optional.hpp>
#include <boost/variant/apply_visitor.hpp>
namespace mapnik {
template <typename BufferType, typename SvgRenderer, typename Rasterizer, typename Detector>
struct vector_markers_rasterizer_dispatch
{
typedef typename SvgRenderer::renderer_base renderer_base;
typedef typename renderer_base::pixfmt_type pixfmt_type;
vector_markers_rasterizer_dispatch(BufferType & render_buffer,
SvgRenderer & svg_renderer,
Rasterizer & ras,
box2d<double> const& bbox,
agg::trans_affine const& marker_trans,
markers_symbolizer const& sym,
Detector & detector,
double scale_factor,
bool snap_to_pixels)
: buf_(render_buffer),
pixf_(buf_),
renb_(pixf_),
svg_renderer_(svg_renderer),
ras_(ras),
bbox_(bbox),
marker_trans_(marker_trans),
sym_(sym),
detector_(detector),
scale_factor_(scale_factor),
snap_to_pixels_(snap_to_pixels)
{
pixf_.comp_op(static_cast<agg::comp_op_e>(sym_.comp_op()));
}
template <typename T>
void add_path(T & path)
{
marker_placement_e placement_method = sym_.get_marker_placement();
if (placement_method != MARKER_LINE_PLACEMENT ||
path.type() == Point)
{
double x = 0;
double y = 0;
if (path.type() == LineString)
{
if (!label::middle_point(path, x, y))
return;
}
else if (placement_method == MARKER_INTERIOR_PLACEMENT)
{
if (!label::interior_position(path, x, y))
return;
}
else
{
if (!label::centroid(path, x, y))
return;
}
agg::trans_affine matrix = marker_trans_;
matrix.translate(x,y);
if (snap_to_pixels_)
{
// https://github.com/mapnik/mapnik/issues/1316
matrix.tx = std::floor(matrix.tx+.5);
matrix.ty = std::floor(matrix.ty+.5);
}
// TODO https://github.com/mapnik/mapnik/issues/1754
box2d<double> transformed_bbox = bbox_ * matrix;
if (sym_.get_allow_overlap() ||
detector_.has_placement(transformed_bbox))
{
svg_renderer_.render(ras_, sl_, renb_, matrix, sym_.get_opacity(), bbox_);
if (!sym_.get_ignore_placement())
{
detector_.insert(transformed_bbox);
}
}
}
else
{
markers_placement<T, Detector> placement(path, bbox_, marker_trans_, detector_,
sym_.get_spacing() * scale_factor_,
sym_.get_max_error(),
sym_.get_allow_overlap());
double x = 0;
double y = 0;
double angle = 0;
while (placement.get_point(x, y, angle))
{
agg::trans_affine matrix = marker_trans_;
matrix.rotate(angle);
matrix.translate(x, y);
svg_renderer_.render(ras_, sl_, renb_, matrix, sym_.get_opacity(), bbox_);
}
}
}
private:
agg::scanline_u8 sl_;
BufferType & buf_;
pixfmt_type pixf_;
renderer_base renb_;
SvgRenderer & svg_renderer_;
Rasterizer & ras_;
box2d<double> const& bbox_;
agg::trans_affine const& marker_trans_;
markers_symbolizer const& sym_;
Detector & detector_;
double scale_factor_;
bool snap_to_pixels_;
};
template <typename BufferType, typename Rasterizer, typename Detector>
struct raster_markers_rasterizer_dispatch
{
typedef agg::rgba8 color_type;
typedef agg::order_rgba order_type;
typedef agg::pixel32_type pixel_type;
typedef agg::comp_op_adaptor_rgba_pre<color_type, order_type> blender_type; // comp blender
typedef agg::pixfmt_custom_blend_rgba<blender_type, BufferType> pixfmt_comp_type;
typedef agg::renderer_base<pixfmt_comp_type> renderer_base;
raster_markers_rasterizer_dispatch(BufferType & render_buffer,
Rasterizer & ras,
image_data_32 const& src,
agg::trans_affine const& marker_trans,
markers_symbolizer const& sym,
Detector & detector,
double scale_factor,
bool snap_to_pixels)
: buf_(render_buffer),
pixf_(buf_),
renb_(pixf_),
ras_(ras),
src_(src),
marker_trans_(marker_trans),
sym_(sym),
detector_(detector),
scale_factor_(scale_factor),
snap_to_pixels_(snap_to_pixels)
{
pixf_.comp_op(static_cast<agg::comp_op_e>(sym_.comp_op()));
}
template <typename T>
void add_path(T & path)
{
marker_placement_e placement_method = sym_.get_marker_placement();
box2d<double> bbox_(0,0, src_.width(),src_.height());
if (placement_method != MARKER_LINE_PLACEMENT ||
path.type() == Point)
{
double x = 0;
double y = 0;
if (path.type() == LineString)
{
if (!label::middle_point(path, x, y))
return;
}
else if (placement_method == MARKER_INTERIOR_PLACEMENT)
{
if (!label::interior_position(path, x, y))
return;
}
else
{
if (!label::centroid(path, x, y))
return;
}
agg::trans_affine matrix = marker_trans_;
matrix.translate(x,y);
box2d<double> transformed_bbox = bbox_ * matrix;
if (sym_.get_allow_overlap() ||
detector_.has_placement(transformed_bbox))
{
render_raster_marker(matrix, sym_.get_opacity());
if (!sym_.get_ignore_placement())
{
detector_.insert(transformed_bbox);
}
}
}
else
{
markers_placement<T, label_collision_detector4> placement(path, bbox_, marker_trans_, detector_,
sym_.get_spacing() * scale_factor_,
sym_.get_max_error(),
sym_.get_allow_overlap());
double x, y, angle;
while (placement.get_point(x, y, angle))
{
agg::trans_affine matrix = marker_trans_;
matrix.rotate(angle);
matrix.translate(x, y);
render_raster_marker(matrix, sym_.get_opacity());
}
}
}
void render_raster_marker(agg::trans_affine const& marker_tr,
double opacity)
{
typedef agg::pixfmt_rgba32_pre pixfmt_pre;
double width = src_.width();
double height = src_.height();
if (std::fabs(1.0 - scale_factor_) < 0.001
&& (std::fabs(1.0 - marker_tr.sx) < agg::affine_epsilon)
&& (std::fabs(0.0 - marker_tr.shy) < agg::affine_epsilon)
&& (std::fabs(0.0 - marker_tr.shx) < agg::affine_epsilon)
&& (std::fabs(1.0 - marker_tr.sy) < agg::affine_epsilon))
{
agg::rendering_buffer src_buffer((unsigned char *)src_.getBytes(),src_.width(),src_.height(),src_.width() * 4);
pixfmt_pre pixf_mask(src_buffer);
renb_.blend_from(pixf_mask,
0,
std::floor(marker_tr.tx + .5),
std::floor(marker_tr.ty + .5),
unsigned(255*sym_.get_opacity()));
}
else
{
typedef agg::image_accessor_clone<pixfmt_pre> img_accessor_type;
typedef agg::span_interpolator_linear<> interpolator_type;
//typedef agg::span_image_filter_rgba_2x2<img_accessor_type,interpolator_type> span_gen_type;
typedef agg::span_image_resample_rgba_affine<img_accessor_type> span_gen_type;
typedef agg::renderer_scanline_aa_alpha<renderer_base,
agg::span_allocator<color_type>,
span_gen_type> renderer_type;
double p[8];
p[0] = 0; p[1] = 0;
p[2] = width; p[3] = 0;
p[4] = width; p[5] = height;
p[6] = 0; p[7] = height;
marker_tr.transform(&p[0], &p[1]);
marker_tr.transform(&p[2], &p[3]);
marker_tr.transform(&p[4], &p[5]);
marker_tr.transform(&p[6], &p[7]);
agg::span_allocator<color_type> sa;
agg::image_filter_lut filter;
filter.calculate(agg::image_filter_bilinear(), true);
agg::rendering_buffer marker_buf((unsigned char *)src_.getBytes(),
src_.width(),
src_.height(),
src_.width()*4);
pixfmt_pre pixf(marker_buf);
img_accessor_type ia(pixf);
agg::trans_affine final_tr(p, 0, 0, width, height);
if (snap_to_pixels_)
{
final_tr.tx = std::floor(final_tr.tx+.5);
final_tr.ty = std::floor(final_tr.ty+.5);
}
interpolator_type interpolator(final_tr);
span_gen_type sg(ia, interpolator, filter);
renderer_type rp(renb_,sa, sg, unsigned(opacity*255));
ras_.move_to_d(p[0],p[1]);
ras_.line_to_d(p[2],p[3]);
ras_.line_to_d(p[4],p[5]);
ras_.line_to_d(p[6],p[7]);
agg::render_scanlines(ras_, sl_, rp);
}
}
private:
agg::scanline_u8 sl_;
BufferType & buf_;
pixfmt_comp_type pixf_;
renderer_base renb_;
Rasterizer & ras_;
image_data_32 const& src_;
agg::trans_affine const& marker_trans_;
markers_symbolizer const& sym_;
Detector & detector_;
double scale_factor_;
bool snap_to_pixels_;
};
template <typename T>
void build_ellipse(T const& sym, mapnik::feature_impl const& feature, svg_storage_type & marker_ellipse, svg::svg_path_adapter & svg_path)
{
expression_ptr const& width_expr = sym.get_width();
expression_ptr const& height_expr = sym.get_height();
double width = 0;
double height = 0;
if (width_expr && height_expr)
{
width = boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *width_expr).to_double();
height = boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *height_expr).to_double();
}
else if (width_expr)
{
width = boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *width_expr).to_double();
height = width;
}
else if (height_expr)
{
height = boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *height_expr).to_double();
width = height;
}
svg::svg_converter_type styled_svg(svg_path, marker_ellipse.attributes());
styled_svg.push_attr();
styled_svg.begin_path();
agg::ellipse c(0, 0, width/2.0, height/2.0);
styled_svg.storage().concat_path(c);
styled_svg.end_path();
styled_svg.pop_attr();
double lox,loy,hix,hiy;
styled_svg.bounding_rect(&lox, &loy, &hix, &hiy);
styled_svg.set_dimensions(width,height);
marker_ellipse.set_dimensions(width,height);
marker_ellipse.set_bounding_box(lox,loy,hix,hiy);
}
template <typename Attr>
bool push_explicit_style(Attr const& src, Attr & dst, markers_symbolizer const& sym)
{
boost::optional<stroke> const& strk = sym.get_stroke();
boost::optional<color> const& fill = sym.get_fill();
boost::optional<float> const& fill_opacity = sym.get_fill_opacity();
if (strk || fill || fill_opacity)
{
bool success = false;
for(unsigned i = 0; i < src.size(); ++i)
{
success = true;
dst.push_back(src[i]);
mapnik::svg::path_attributes & attr = dst.last();
if (attr.stroke_flag)
{
// TODO - stroke attributes need to be boost::optional
// for this to work properly
if (strk)
{
attr.stroke_width = strk->get_width();
color const& s_color = strk->get_color();
attr.stroke_color = agg::rgba(s_color.red()/255.0,
s_color.green()/255.0,
s_color.blue()/255.0,
s_color.alpha()/255.0);
attr.stroke_opacity = strk->get_opacity();
}
}
if (attr.fill_flag)
{
if (fill)
{
color const& f_color = *fill;
attr.fill_color = agg::rgba(f_color.red()/255.0,
f_color.green()/255.0,
f_color.blue()/255.0,
f_color.alpha()/255.0);
}
if (fill_opacity)
{
attr.fill_opacity = *fill_opacity;
}
}
}
return success;
}
return false;
}
template <typename T>
void setup_transform_scaling(agg::trans_affine & tr,
double svg_width,
double svg_height,
mapnik::feature_impl const& feature,
T const& sym)
{
double width = 0;
double height = 0;
expression_ptr const& width_expr = sym.get_width();
if (width_expr)
width = boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *width_expr).to_double();
expression_ptr const& height_expr = sym.get_height();
if (height_expr)
height = boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *height_expr).to_double();
if (width > 0 && height > 0)
{
double sx = width/svg_width;
double sy = height/svg_height;
tr *= agg::trans_affine_scaling(sx,sy);
}
else if (width > 0)
{
double sx = width/svg_width;
tr *= agg::trans_affine_scaling(sx);
}
else if (height > 0)
{
double sy = height/svg_height;
tr *= agg::trans_affine_scaling(sy);
}
}
// Apply markers to a feature with multiple geometries
template <typename Converter>
void apply_markers_multi(feature_impl & feature, Converter& converter, markers_symbolizer const& sym)
{
std::size_t geom_count = feature.paths().size();
if (geom_count == 1)
{
converter.apply(feature.paths()[0]);
}
else if (geom_count > 1)
{
marker_multi_policy_e multi_policy = sym.get_marker_multi_policy();
marker_placement_e placement = sym.get_marker_placement();
if (placement == MARKER_POINT_PLACEMENT &&
multi_policy == MARKER_WHOLE_MULTI)
{
double x, y;
if (label::centroid_geoms(feature.paths().begin(), feature.paths().end(), x, y))
{
geometry_type pt(Point);
pt.move_to(x, y);
// unset any clipping since we're now dealing with a point
converter.template unset<clip_poly_tag>();
converter.apply(pt);
}
}
else if ((placement == MARKER_POINT_PLACEMENT || placement == MARKER_INTERIOR_PLACEMENT) &&
multi_policy == MARKER_LARGEST_MULTI)
{
// Only apply to path with largest envelope area
// TODO: consider using true area for polygon types
double maxarea = 0;
geometry_type* largest = 0;
BOOST_FOREACH(geometry_type & geom, feature.paths())
{
const box2d<double>& env = geom.envelope();
double area = env.width() * env.height();
if (area > maxarea)
{
maxarea = area;
largest = &geom;
}
}
if (largest)
{
converter.apply(*largest);
}
}
else
{
if (multi_policy != MARKER_EACH_MULTI && placement != MARKER_POINT_PLACEMENT)
{
MAPNIK_LOG_WARN(marker_symbolizer) << "marker_multi_policy != 'each' has no effect with marker_placement != 'point'";
}
BOOST_FOREACH(geometry_type & path, feature.paths())
{
converter.apply(path);
}
}
}
}
}
#endif //MAPNIK_MARKER_HELPERS_HPP
|