mapnik::util::is_clockwise - translate coordinates relative to the origin (0,0) to avoid numeric precision issues while using double precision.

(ref #3402)
This commit is contained in:
artemp 2016-04-18 16:08:30 +02:00
parent c8902ac875
commit 66160e9754
2 changed files with 37 additions and 1 deletions

View file

@ -23,6 +23,8 @@
#ifndef MAPNIK_UTIL_IS_CLOCKWISE_HPP
#define MAPNIK_UTIL_IS_CLOCKWISE_HPP
#include <cassert>
namespace mapnik { namespace util {
template <typename T>
@ -30,11 +32,18 @@ bool is_clockwise(T const& ring)
{
double area = 0.0;
std::size_t num_points = ring.size();
assert(num_point > 2);
double orig_x = ring[0].x;
double orig_y = ring[0].y;
for (std::size_t i = 0; i < num_points; ++i)
{
auto const& p0 = ring[i];
auto const& p1 = ring[(i + 1) % num_points];
area += p0.x * p1.y - p0.y * p1.x;
double x0 = p0.x - orig_x;
double y0 = p0.y - orig_y;
double x1 = p1.x - orig_x;
double y1 = p1.y - orig_y;
area += x0 * y1 - x1 * y0;
}
return (area < 0.0) ? true : false;
}

View file

@ -0,0 +1,27 @@
#include "catch.hpp"
#include <mapnik/geometry.hpp>
#include <mapnik/util/is_clockwise.hpp>
TEST_CASE("Ring is_clockwise") {
// Input is rather thin triangle to test precision issues aren't getting in the way.
SECTION("Clockwise")
{
mapnik::geometry::linear_ring<double> ring;
ring.emplace_back(-13499697.0366658326, 4698431.85179749783);
ring.emplace_back(-13499697.1113113686, 4698431.85179749783);
ring.emplace_back(-13499697.0366658326, 4698431.92644303292);
ring.emplace_back(-13499697.0366658326, 4698431.85179749783);
REQUIRE(mapnik::util::is_clockwise(ring) == true);
}
SECTION("Anti-Clockwise")
{
mapnik::geometry::linear_ring<double> ring;
ring.emplace_back(-13499697.0366658326, 4698431.85179749783);
ring.emplace_back(-13499697.0366658326, 4698431.92644303292);
ring.emplace_back(-13499697.1113113686, 4698431.85179749783);
ring.emplace_back(-13499697.0366658326, 4698431.85179749783);
REQUIRE(mapnik::util::is_clockwise(ring) == false);
}
}