// Boost.Geometry (aka GGL, Generic Geometry Library)
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
// Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <algorithm> // for reverse, unique
#include <iostream>
#include <string>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/extensions/algorithms/dissolve.hpp>
// dissolve.hpp の中で省略系の namespace が宣言なしに使用されていたので追加
namespace bg = boost::geometry;
BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
int main(void)
{
using namespace boost::geometry;
typedef model::d2::point_xy<double> point_2d;
typedef model::polygon<point_2d> polygon_2d;
typedef model::box<point_2d> box_2d;
typedef model::multi_polygon<polygon_2d> multi_polygon_2d;
// Define a polygon and fill the outer ring.
// In most cases you will read it from a file or database
polygon_2d poly;
{
const double coor[][2] = {
{ -162.277344, -684.941406 },
{ -154.300781, -684.707031 },
{ -147.291016, -768.253906 },
{ -148.601562, -752.630859 },
{ -156.675781, -753.292969 },
{ -155.425781, -768.546875 }
};
assign_points(poly, coor);
}
// Polygons should be closed, and directed clockwise. If you're not sure if that is the case,
// call the correct algorithm
correct(poly);
// 自己交差を解消するためには、出力先はマルチポリゴンである必要があります。
multi_polygon_2d solved;
// ポリゴンを入れて、自己交差を解消し、分解して solved に入れます。
dissolve(poly, solved);
// Polygons can be streamed as text
// (or more precisely: as DSV (delimiter separated values))
std::cout << dsv(poly) << std::endl;
multi_polygon_2d v;
try {
// intersection(poly, poly, v); では例外が発生しますが…
intersection(solved, solved, v);
} catch(const std::exception& e) {
std::cerr << e.what() << std::endl;
}
// 大丈夫!
std::cout << "Clipped output polygons" << std::endl;
for (multi_polygon_2d::const_iterator it = v.begin(); it != v.end(); ++it)
{
std::cout << dsv(*it) << std::endl;
}
return 0;
}
2016年1月29日金曜日
boost::geometry 自己交差解消
boost::geometry では、自己交差しているポリゴン図形の入力はエラーとして受け付けません。
関連:boost::geometry でもハマった
とは言っても、人間の手を介して入力される限り、自己交差しているような図形は後を絶ちません。
やっぱり、これを解消する手段は、是非とも欲しいところであります。
参考:http://boost-geometry.203548.n3.nabble.com/How-to-repair-self-intersections-td4025893.html
参考URLによりますと、開発バージョンの中には、extensions というヘッダが構築されておりまして、その中にありました。
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿