[英]boost geometry intersection give strange results
我想使用帶有線和面的boost幾何的相交函數。 我希望交點是位於多邊形內部的線的一部分。
不幸的是,boost幾何返回了位於多邊形外部的線的一部分。 這是Boost幾何中的錯誤還是我的代碼有問題?
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/intersection.hpp>
namespace bg = boost::geometry;
using value_type = double ;
using cs_type = bg::cs::cartesian;
using point_type = bg::model::point< value_type , 2 , cs_type >;
using polygon_type = bg::model::ring< point_type > ;
using line_string_type = bg::model::linestring< point_type >;
using multi_line_type = bg::model::multi_linestring< line_string_type >;
int main( int argc , char *argv[] )
{
line_string_type line;
line.push_back( point_type { 13.37688020921095 , 53.66231710654281 } );
line.push_back( point_type { 13.3857295713429 , 53.6636835518369 } );
line.push_back( point_type { 13.39213495232734 , 53.66501934623722 } );
line.push_back( point_type { 13.39719615524716 , 53.66546436809296 } );
line.push_back( point_type { 13.40724694386097 , 53.66240690770171 } );
polygon_type polygon;
polygon.push_back( point_type { 13.35 , 53.64 } );
polygon.push_back( point_type { 13.39 , 53.64 } );
polygon.push_back( point_type { 13.39 , 53.68 } );
polygon.push_back( point_type { 13.35 , 53.68 } );
polygon.push_back( point_type { 13.35 , 53.64 } );
multi_line_type intersection;
bg::intersection( line , polygon , intersection );
return 0;
}
確保您的輸入幾何形狀滿足記錄的前提條件。
您可以使用bg::correct
來解決大多數問題(例如,多邊形中點的正確CCW排序,關閉未封閉的多邊形等):
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/intersection.hpp>
namespace bg = boost::geometry;
using value_type = double;
using cs_type = bg::cs::cartesian;
using point_type = bg::model::point<value_type, 2, cs_type>;
using polygon_type = bg::model::ring<point_type>;
using line_string_type = bg::model::linestring<point_type>;
using multi_line_type = bg::model::multi_linestring<line_string_type>;
int main()
{
line_string_type line;
line.push_back(point_type{13.37688020921095, 53.66231710654281});
line.push_back(point_type{13.3857295713429, 53.6636835518369});
line.push_back(point_type{13.39213495232734, 53.66501934623722});
line.push_back(point_type{13.39719615524716, 53.66546436809296});
line.push_back(point_type{13.40724694386097, 53.66240690770171});
bg::correct(line);
polygon_type polygon;
polygon.push_back(point_type{13.35, 53.64});
polygon.push_back(point_type{13.39, 53.64});
polygon.push_back(point_type{13.39, 53.68});
polygon.push_back(point_type{13.35, 53.68});
polygon.push_back(point_type{13.35, 53.64});
bg::correct(polygon);
multi_line_type intersection;
bg::intersection(line, polygon, intersection);
std::cout << bg::wkt(intersection);
}
此打印
MULTILINESTRING((13.3769 53.6623,13.3857 53.6637,13.39 53.6646))
如果輸入未correct
編輯,它將打印以下內容:
MULTILINESTRING((13.39 53.6646,13.3921 53.665,13.3972 53.6655,13.4072 53.6624))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.