/usr/include/mapnik/offset_converter.hpp is in libmapnik-dev 3.0.9+ds-1.
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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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_OFFSET_CONVERTER_HPP
#define MAPNIK_OFFSET_CONVERTER_HPP
#ifdef MAPNIK_LOG
#include <mapnik/debug.hpp>
#endif
#include <mapnik/global.hpp>
#include <mapnik/config.hpp>
#include <mapnik/vertex.hpp>
#include <mapnik/vertex_cache.hpp>
// stl
#include <cmath>
#include <vector>
#include <cstddef>
#include <algorithm>
namespace mapnik
{
template <typename Geometry>
struct offset_converter
{
using size_type = std::size_t;
offset_converter(Geometry & geom)
: geom_(geom)
, offset_(0.0)
, threshold_(5.0)
, half_turn_segments_(16)
, status_(initial)
, pre_first_(vertex2d::no_init)
, pre_(vertex2d::no_init)
, cur_(vertex2d::no_init)
{}
enum status
{
initial,
process
};
unsigned type() const
{
return static_cast<unsigned>(geom_.type());
}
double get_offset() const
{
return offset_;
}
double get_threshold() const
{
return threshold_;
}
void set_offset(double value)
{
if (offset_ != value)
{
offset_ = value;
reset();
}
}
void set_threshold(double value)
{
threshold_ = value;
// no need to reset(), since threshold doesn't affect
// offset vertices' computation, it only controls how
// far will we be looking for self-intersections
}
unsigned vertex(double * x, double * y)
{
if (offset_ == 0.0)
{
return geom_.vertex(x, y);
}
if (status_ == initial)
{
init_vertices();
}
if (pos_ >= vertices_.size())
{
return SEG_END;
}
pre_ = (pos_ ? cur_ : pre_first_);
cur_ = vertices_.at(pos_++);
if (pos_ == vertices_.size())
{
return output_vertex(x, y);
}
double const check_dist = offset_ * threshold_;
double const check_dist2 = check_dist * check_dist;
double t = 1.0;
double vt, ut;
for (size_t i = pos_; i+1 < vertices_.size(); ++i)
{
//break; // uncomment this to see all the curls
vertex2d const& u0 = vertices_[i];
vertex2d const& u1 = vertices_[i+1];
double const dx = u0.x - cur_.x;
double const dy = u0.y - cur_.y;
if (dx*dx + dy*dy > check_dist2)
{
break;
}
if (!intersection(pre_, cur_, &vt, u0, u1, &ut))
{
continue;
}
if (vt < 0.0 || vt > t || ut < 0.0 || ut > 1.0)
{
continue;
}
t = vt;
pos_ = i+1;
}
cur_.x = pre_.x + t * (cur_.x - pre_.x);
cur_.y = pre_.y + t * (cur_.y - pre_.y);
return output_vertex(x, y);
}
void reset()
{
geom_.rewind(0);
vertices_.clear();
status_ = initial;
pos_ = 0;
}
void rewind(unsigned)
{
pos_ = 0;
}
private:
static double explement_reflex_angle(double angle)
{
if (angle > M_PI)
{
return angle - 2 * M_PI;
}
else if (angle < -M_PI)
{
return angle + 2 * M_PI;
}
else
{
return angle;
}
}
static bool intersection(vertex2d const& u1, vertex2d const& u2, double* ut,
vertex2d const& v1, vertex2d const& v2, double* vt)
{
double const dx = v1.x - u1.x;
double const dy = v1.y - u1.y;
double const ux = u2.x - u1.x;
double const uy = u2.y - u1.y;
double const vx = v2.x - v1.x;
double const vy = v2.y - v1.y;
// the first line is not vertical
if (ux < -1e-6 || ux > 1e-6)
{
double const up = ux * dy - dx * uy;
double const dn = vx * uy - ux * vy;
if (dn > -1e-6 && dn < 1e-6)
{
return false; // they are parallel
}
*vt = up / dn;
*ut = (*vt * vx + dx) / ux;
return true;
}
// the first line is not horizontal
if (uy < -1e-6 || uy > 1e-6)
{
double const up = uy * dx - dy * ux;
double const dn = vy * ux - uy * vx;
if (dn > -1e-6 && dn < 1e-6)
{
return false; // they are parallel
}
*vt = up / dn;
*ut = (*vt * vy + dy) / uy;
return true;
}
// the first line is too short
return false;
}
/**
* @brief Translate (vx, vy) by rotated (dx, dy).
*/
static void displace(vertex2d & v, double dx, double dy, double a)
{
v.x += dx * std::cos(a) - dy * std::sin(a);
v.y += dx * std::sin(a) + dy * std::cos(a);
}
/**
* @brief Translate (vx, vy) by rotated (0, -offset).
*/
void displace(vertex2d & v, double a) const
{
v.x -= offset_ * std::sin(a);
v.y += offset_ * std::cos(a);
}
/**
* @brief (vx, vy) := (ux, uy) + rotated (0, -offset)
*/
void displace(vertex2d & v, vertex2d const& u, double a) const
{
v.x = u.x - offset_ * std::sin(a);
v.y = u.y + offset_ * std::cos(a);
v.cmd = u.cmd;
}
int point_line_position(vertex2d const& a, vertex2d const& b, vertex2d const& point) const
{
double position = (b.x - a.x) * (point.y - a.y) - (b.y - a.y) * (point.x - a.x);
if (position > 1e-6) return 1;
if (position < -1e-6) return -1;
return 0;
}
void displace2(vertex2d & v1, vertex2d const& v0, vertex2d const& v2, double a, double b) const
{
double sa = offset_ * std::sin(a);
double ca = offset_ * std::cos(a);
double h = std::tan(0.5 * (b - a));
double hsa = h * sa;
double hca = h * ca;
double abs_offset = std::abs(offset_);
double hsaca = ca-hsa;
double hcasa = -sa-hca;
double abs_hsaca = std::abs(hsaca);
double abs_hcasa = std::abs(hcasa);
double abs_hsa = std::abs(hsa);
double abs_hca = std::abs(hca);
vertex2d v_tmp(vertex2d::no_init);
v_tmp.x = v1.x - sa - hca;
v_tmp.y = v1.y + ca - hsa;
v_tmp.cmd = v1.cmd;
int same = point_line_position(v0, v2, v_tmp)*point_line_position(v0, v2, v1);
if (same >= 0 && std::abs(h) < 10)
{
v1.x = v_tmp.x;
v1.y = v_tmp.y;
}
else if ((v0.x-v1.x)*(v0.x-v1.x) + (v0.y-v1.y)*(v0.y-v1.y) +
(v0.x-v2.x)*(v0.x-v2.x) + (v0.y-v2.y)*(v0.y-v2.y) > offset_*offset_)
{
if (abs_hsa > abs_offset || abs_hca > abs_offset)
{
double scale = std::max(abs_hsa,abs_hca);
scale = scale < 1e-6 ? 1. : abs_offset / scale;
// interpolate hsa, hca to <0,abs_offset>
hsa = hsa * scale;
sa = sa * scale;
hca = hca * scale;
ca = ca * scale;
}
v1.x = v1.x - sa - hca;
v1.y = v1.y + ca - hsa;
}
else
{
if (abs_hsaca*abs_hsaca + abs_hcasa*abs_hcasa > abs_offset*abs_offset)
{
double d = (abs_hsaca*abs_hsaca + abs_hcasa*abs_hcasa);
d = d < 1e-6 ? 1. : d;
double scale = (abs_offset*abs_offset)/d;
v1.x = v1.x + hcasa*scale;
v1.y = v1.y + hsaca*scale;
}
else
{
v1.x = v1.x + hcasa;
v1.y = v1.y + hsaca;
}
}
}
status init_vertices()
{
if (status_ != initial) // already initialized
{
return status_;
}
vertex2d v0(vertex2d::no_init);
vertex2d v1(vertex2d::no_init);
vertex2d v2(vertex2d::no_init);
vertex2d w(vertex2d::no_init);
vertex2d start(vertex2d::no_init);
vertex2d start_v2(vertex2d::no_init);
std::vector<vertex2d> points;
std::vector<vertex2d> close_points;
bool is_polygon = false;
std::size_t cpt = 0;
v0.cmd = geom_.vertex(&v0.x, &v0.y);
v1 = v0;
// PUSH INITIAL
points.push_back(v0);
if (v0.cmd == SEG_END) // not enough vertices in source
{
return status_ = process;
}
start = v0;
while ((v0.cmd = geom_.vertex(&v0.x, &v0.y)) != SEG_END)
{
if (v0.cmd == SEG_CLOSE)
{
is_polygon = true;
auto & prev = points.back();
if (prev.x == start.x && prev.y == start.y)
{
prev.x = v0.x; // hack
prev.y = v0.y;
prev.cmd = SEG_CLOSE; // account for dupes (line_to(move_to) + close_path) in agg poly clipper
std::size_t size = points.size();
if (size > 1) close_points.push_back(points[size - 2]);
else close_points.push_back(prev);
continue;
}
else
{
close_points.push_back(v1);
}
}
else if (v0.cmd == SEG_MOVETO)
{
start = v0;
}
v1 = v0;
points.push_back(v0);
}
// Push SEG_END
points.push_back(vertex2d(v0.x,v0.y,SEG_END));
std::size_t i = 0;
v1 = points[i++];
v2 = points[i++];
v0.cmd = v1.cmd;
v0.x = v1.x;
v0.y = v1.y;
if (v2.cmd == SEG_END) // not enough vertices in source
{
return status_ = process;
}
double angle_a = 0;
// The vector parts from v1 to v0.
double v_x1x0 = 0;
double v_y1y0 = 0;
// The vector parts from v1 to v2;
double v_x1x2 = v2.x - v1.x;
double v_y1y2 = v2.y - v1.y;
if (is_polygon)
{
v_x1x0 = close_points[cpt].x - v1.x;
v_y1y0 = close_points[cpt].y - v1.y;
cpt++;
angle_a = std::atan2(-v_y1y0, -v_x1x0);
}
// dot product
double dot;
// determinate
double det;
double angle_b = std::atan2(v_y1y2, v_x1x2);
// Angle between the two vectors
double joint_angle;
double curve_angle;
if (!is_polygon)
{
// first vertex
displace(v1, angle_b);
push_vertex(v1);
}
else
{
dot = v_x1x0 * v_x1x2 + v_y1y0 * v_y1y2; // dot product
det = v_x1x0 * v_y1y2 - v_y1y0 * v_x1x2; // determinant
joint_angle = std::atan2(det, dot); // atan2(y, x) or atan2(sin, cos)
if (joint_angle < 0) joint_angle = joint_angle + 2 * M_PI;
joint_angle = std::fmod(joint_angle, 2 * M_PI);
if (offset_ > 0.0)
{
joint_angle = 2 * M_PI - joint_angle;
}
int bulge_steps = 0;
if (std::abs(joint_angle) > M_PI)
{
curve_angle = explement_reflex_angle(angle_b - angle_a);
// Bulge steps should be determined by the inverse of the joint angle.
double half_turns = half_turn_segments_ * std::fabs(curve_angle);
bulge_steps = 1 + static_cast<int>(std::floor(half_turns / M_PI));
}
if (bulge_steps == 0)
{
displace2(v1, v0, v2, angle_a, angle_b);
push_vertex(v1);
}
else
{
displace(v1, angle_b);
push_vertex(v1);
}
}
// Sometimes when the first segment is too short, it causes ugly
// curls at the beginning of the line. To avoid this, we make up
// a fake vertex two offset-lengths before the first, and expect
// intersection detection smoothes it out.
if (!is_polygon)
{
pre_first_ = v1;
displace(pre_first_, -2 * std::fabs(offset_), 0, angle_b);
start_ = pre_first_;
}
else
{
pre_first_ = v0;
start_ = pre_first_;
}
start_v2.x = v2.x;
start_v2.y = v2.y;
bool continue_loop = true;
vertex2d tmp_prev(vertex2d::no_init);
while (i < points.size())
{
v1 = v2;
v2 = points[i++];
if (v1.cmd == SEG_MOVETO)
{
if (is_polygon)
{
v1.x = start_.x;
v1.y = start_.y;
if (cpt < close_points.size())
{
v_x1x2 = v1.x - close_points[cpt].x;
v_y1y2 = v1.y - close_points[cpt].y;
cpt++;
}
start_v2.x = v2.x;
start_v2.y = v2.y;
}
}
if (is_polygon && v2.cmd == SEG_MOVETO)
{
start_.x = v2.x;
start_.y = v2.y;
v2.x = start_v2.x;
v2.y = start_v2.y;
}
else if (v2.cmd == SEG_END)
{
if (!is_polygon) break;
continue_loop = false;
v2.x = start_v2.x;
v2.y = start_v2.y;
}
else if (v2.cmd == SEG_CLOSE)
{
v2.x = start_.x;
v2.y = start_.y;
}
// Switch the previous vector's direction as the origin has changed
v_x1x0 = -v_x1x2;
v_y1y0 = -v_y1y2;
// Calculate new angle_a
angle_a = std::atan2(v_y1y2, v_x1x2);
// Calculate the new vector
v_x1x2 = v2.x - v1.x;
v_y1y2 = v2.y - v1.y;
// Calculate the new angle_b
angle_b = std::atan2(v_y1y2, v_x1x2);
dot = v_x1x0 * v_x1x2 + v_y1y0 * v_y1y2; // dot product
det = v_x1x0 * v_y1y2 - v_y1y0 * v_x1x2; // determinant
joint_angle = std::atan2(det, dot); // atan2(y, x) or atan2(sin, cos)
if (joint_angle < 0) joint_angle = joint_angle + 2 * M_PI;
joint_angle = std::fmod(joint_angle, 2 * M_PI);
if (offset_ > 0.0)
{
joint_angle = 2 * M_PI - joint_angle;
}
int bulge_steps = 0;
if (std::abs(joint_angle) > M_PI)
{
curve_angle = explement_reflex_angle(angle_b - angle_a);
// Bulge steps should be determined by the inverse of the joint angle.
double half_turns = half_turn_segments_ * std::fabs(curve_angle);
bulge_steps = 1 + static_cast<int>(std::floor(half_turns / M_PI));
}
#ifdef MAPNIK_LOG
if (bulge_steps == 0)
{
// inside turn (sharp/obtuse angle)
MAPNIK_LOG_DEBUG(ctrans) << "offset_converter:"
<< " Sharp joint [<< inside turn " << int(joint_angle*180/M_PI)
<< " degrees >>]";
}
else
{
// outside turn (reflex angle)
MAPNIK_LOG_DEBUG(ctrans) << "offset_converter:"
<< " Bulge joint >)) outside turn " << int(joint_angle*180/M_PI)
<< " degrees ((< with " << bulge_steps << " segments";
}
#endif
tmp_prev.cmd = v1.cmd;
tmp_prev.x = v1.x;
tmp_prev.y = v1.y;
if (v1.cmd == SEG_MOVETO)
{
if (bulge_steps == 0)
{
displace2(v1, v0, v2, angle_a, angle_b);
push_vertex(v1);
}
else
{
displace(v1, angle_b);
push_vertex(v1);
}
}
else
{
if (bulge_steps == 0)
{
displace2(v1, v0, v2, angle_a, angle_b);
push_vertex(v1);
}
else
{
displace(w, v1, angle_a);
w.cmd = SEG_LINETO;
push_vertex(w);
for (int s = 0; ++s < bulge_steps;)
{
displace(w, v1, angle_a + (curve_angle * s) / bulge_steps);
w.cmd = SEG_LINETO;
push_vertex(w);
}
displace(v1, angle_b);
push_vertex(v1);
}
}
v0.cmd = tmp_prev.cmd;
v0.x = tmp_prev.x;
v0.y = tmp_prev.y;
}
// last vertex
if (!is_polygon)
{
displace(v1, angle_b);
push_vertex(v1);
}
// initialization finished
return status_ = process;
}
unsigned output_vertex(double* px, double* py)
{
if (cur_.cmd == SEG_CLOSE) *px = *py = 0.0;
else
{
*px = cur_.x;
*py = cur_.y;
}
return cur_.cmd;
}
void push_vertex(vertex2d const& v)
{
vertices_.push_back(v);
}
Geometry & geom_;
double offset_;
double threshold_;
unsigned half_turn_segments_;
status status_;
size_t pos_;
std::vector<vertex2d> vertices_;
vertex2d start_;
vertex2d pre_first_;
vertex2d pre_;
vertex2d cur_;
};
}
#endif // MAPNIK_OFFSET_CONVERTER_HPP
|