我最近尝试开发 boost::geometry 库。我找到了下面的代码
#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;
}
任何检查的想法:
- 它是一个简单的多边形吗?
- 方向是什么(顺时针、逆时针)
- 关门了吗?
顺便说一下,我使用的是boost版本1.53.0。
最佳答案
Polygon
的概念BG 中解释了大多数高层决策:
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.)
下Ring
我们发现:
- there might be a specialization of
traits::point_order
defining the order or orientation of its points,clockwise
orcounterclockwise
- there might be a specialization of
traits::closure
defining the closure,open
orclosed
在规则
下,您可以找到问题的大部分答案:
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.
请注意
- 您可以使用
boost::geometry::correct
纠正不符合上述所有要求的环/多边形。 - 添加了 Boost 1.56.0
boost::geometry::is_valid
这样您就可以验证几何图形的有效性。对于旧版本,请参阅例如<强> polygons union using boost
关于c++ - 检查boost中的多边形特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26161076/