Skip to content

Commit

Permalink
mapnik::util::is_clockwise - translate coordinates relative to the or…
Browse files Browse the repository at this point in the history
…igin (0,0) to avoid numeric precision issues while using double precision.

(ref #3402)
  • Loading branch information
artemp committed Apr 18, 2016
1 parent c8902ac commit 66160e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/mapnik/util/is_clockwise.hpp
Expand Up @@ -23,18 +23,27 @@
#ifndef MAPNIK_UTIL_IS_CLOCKWISE_HPP
#define MAPNIK_UTIL_IS_CLOCKWISE_HPP

#include <cassert>

namespace mapnik { namespace util {

template <typename T>
bool is_clockwise(T const& ring)
{
double area = 0.0;
std::size_t num_points = ring.size();
assert(num_point > 2);

This comment has been minimized.

Copy link
@springmeyer

springmeyer Apr 18, 2016

Member

@artemp how about returning false if num_points < 3?

This comment has been minimized.

Copy link
@flippmoke

flippmoke Apr 19, 2016

Member

If it has less then 3 points or the area equals zero, it really is not clockwise or counter clockwise so.. I wonder at times if we should use an enum

This comment has been minimized.

Copy link
@artemp

artemp Apr 19, 2016

Author Member

@springmeyer - @flippmoke is correct, though returning enum is just adding to the confusion. I think this is up to the user code to ensure the input is valid e.g num_points > 2. We can simply throw on invalid input ?

This comment has been minimized.

Copy link
@artemp

artemp Apr 19, 2016

Author Member
if (ring.size() > 2)
{
     if (mapnik::util::is_clockwise(ring)
     {
          //.....
     }
}
else
{
     // invalid ring, do something e.g drop it
}
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;
}
Expand Down
27 changes: 27 additions & 0 deletions test/unit/geometry/is_clockwise.cpp
@@ -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);
}
}

0 comments on commit 66160e9

Please sign in to comment.