2

I recently tried to work on boost::geometry library. I found the code below

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <iostream>

namespace bg = boost::geometry;

int main(void)
{
    typedef bg::model::point<double, 2, bg::cs::cartesian> point;
    typedef bg::model::polygon<point> polygon;

    //! create a polygon
    polygon p;
    p.outer().push_back(point(0., 0.));
    p.outer().push_back(point(1., 0.));
    p.outer().push_back(point(1., 2.));
    p.outer().push_back(point(2., 3.));
    p.outer().push_back(point(0., 4.));

    //! display it
    std::cout << "generated polygon:" << std::endl;
    std::cout << bg::wkt<polygon>(p) << std::endl;

    return 0;
}

Any idea for checking:

  1. Is it a simple polygon?
  2. What is the orientation (clockwise, counterclockwise)
  3. Is it closed?

By the way, I am using boost version 1.53.0.

genpfault
  • 51,148
  • 11
  • 85
  • 139
H'H
  • 1,638
  • 1
  • 15
  • 39

1 Answers1

2

The concept of Polygon in BG explains most high level decisions:

A polygon is A polygon is a planar surface defined by one exterior boundary and zero or more interior boundaries (OGC Simple Feature Specification).

So the definition of a Boost.Geometry polygon differs a bit from e.g. Wiki, where a polygon does not have holes. A polygon of Boost.Geometry is a polygon with or without holes. (A polygon without holes is a helper geometry within Boost.Geometry, and referred to as a ring.)

Under Ring we find:

  • there might be a specialization of traits::point_order defining the order or orientation of its points, clockwise or counterclockwise
  • there might be a specialization of traits::closure defining the closure, open or closed

Under Rules you will find most answers to your questions:

Besides the Concepts, which are checks on compile-time, there are some other rules that valid polygons must fulfill. This follows the opengeospatial rules (see link above).

  • Polygons are simple geometric objects (See also wiki but holes are allowed in Boost.Geometry polygons).
  • If the polygons underlying ring_type is defined as clockwise, the exterior ring must have the clockwise orientation, and any interior ring must be reversed w.r.t. the defined orientation (so: counter clockwise for clockwise exterior rings). If the ring_type is defined counter clockwise, it is vice versa.
  • If the polygons underlying ring_type is defined as closed, all rings must be closed: the first point must be spatially equal to the last point.
  • The interior is a connected point set.
  • There should be no self intersections, but self tangencies (between exterior/interior rings) are allowed (as long as the interior is a connected point set.
  • There should be no cut lines, spikes or punctures.
  • The interior rings should be located within the exterior ring. Interior rings may not be located within each other.

Note that

  1. you can use boost::geometry::correct to correct a ring/polygon that doesn't adhere to all requirements above.
  2. Boost 1.56.0 added boost::geometry::is_valid so you can verify validity of a geometry. For older versions, see e.g. polygons union using boost
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633