我正在尝试将Boost :: Geometry _union与整数一起用于性能和数值精度。为此,我将输入的坐标乘以10,000。因此创建最多9位数的坐标。我认为,因为我使用64位整数,所以应该可以正常工作。
不幸的是,当我运行代码时,我得到了奇怪的结果(输出多边形包括一个远离输入中任何多边形的点)。研究Boost :: Geometry的代码让我得出结论:起源是文件cart_intersect.hpp中的一个环绕问题:
set<1>(point, get<0, 1>(segment) + boost::numeric_cast
<
CoordinateType
>(numerator * dy_promoted / denominator));
当所有三个(分子,dy_promoted和分母)都很大时,乘法的结果需要超过64位,因此整个结果是不正确的。
使用具有如此大整数的Boost :: Geometry是否有效?在保持正确性和精确度的同时,将Boost :: Geometry与整数一起使用的正确方法是什么?
修改 @sehe,谢谢你的回复。这是一个用VS2013和Boost 1.63
编译的SSCCE#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
namespace bg = boost::geometry;
typedef bg::model::d2::point_xy<long long, bg::cs::cartesian> TPoint;
typedef bg::model::ring<TPoint> TRing;
typedef bg::model::polygon<TPoint> TPolygon;
typedef bg::model::multi_polygon<TPolygon> TMultiPolygon;
void PrintRing(const TRing& rng)
{
for (const auto& ver : rng)
{
cout << "(" << ver.x() << "," << ver.y() << "),";
}
}
void PrintPolygon(const TPolygon& pol)
{
cout << "Outer: ";
PrintRing(pol.outer());
cout << endl;
for (const auto& rng : pol.inners())
{
cout << "Inner: ";
PrintRing(rng);
cout << endl;
}
}
void PrintMultiPolygon(const string name, const TMultiPolygon& mp)
{
cout << "Multi-Polygon " << name << " : " << endl;
for (const auto& pol : mp)
{
PrintPolygon(pol);
}
cout << endl;
}
int main()
{
cout << "BOOST_LIB_VERSION: " << BOOST_LIB_VERSION << endl;
const vector<TPoint> verticesA{ { -405129, 2010409 }, { 3370580, 2010409 }, { 3370580, 1997709 }, { -405129, 1997709 }, { -405129, 2010409 } };
const TRing rngA(verticesA.cbegin(), verticesA.cend());
TPolygon polA;
polA.outer() = rngA;
TMultiPolygon mpA;
mpA.push_back(polA);
const vector<TPoint> verticesB{ { 3364230, -895349 }, { 3364230, 2004060 }, { 3376930, 2004059 }, { 3376930, -895350 }, { 3364230, -895349 } };
const TRing rngB(verticesB.cbegin(), verticesB.cend());
TPolygon polB;
polB.outer() = rngB;
TMultiPolygon mpB;
mpB.push_back(polB);
TMultiPolygon output;
bg::union_(mpA, mpB, output);
PrintMultiPolygon("A", mpA);
PrintMultiPolygon("B", mpB);
PrintMultiPolygon("output", output);
}
该计划的输出是:
BOOST_LIB_VERSION: 1_63
Multi-Polygon A :
Outer: (-405129,2010409),(3370580,2010409),(3370580,1997709),(-405129,1997709),(-405129,2010409),
Multi-Polygon B :
Outer: (3364230,-895349),(3364230,2004060),(3376930,2004059),(3376930,-895350),(3364230,-895349),
Multi-Polygon output :
Outer: (3370580,2004060),(3376930,2004059),(3376930,-895350),(3364230,-895349),
的(3364230,-1372382)
,(-405129,1997709),(-405129,2010409),(3370580,2010409),(3370580,2004060),
请注意以粗体显示的坐标,Y值比输入中的任何Y坐标都要远。
答案 0 :(得分:1)
确实,使用-fsanitize=undefined
打印
/home/sehe/custom/boost/boost/geometry/strategies/cartesian/intersection.hpp:190:18: runtime error: signed integer overflow: 10923345128122 * 2899409 cannot be represented in type 'long long int'
相反,您可以使用供应商定义的128位扩展,或使用Boost:
namespace mp = boost::multiprecision;
typedef mp::checked_int128_t T;
typedef bg::model::d2::point_xy<T, bg::cs::cartesian> TPoint;
实际上,您可以使用任意精度的整数:
typedef mp::checked_cpp_int T;
注意 如果要对
cpp_int
使用未经检查的算术,则必须确保禁用表达式模板以进行提升typedef mp::number<mp::backends::cpp_int_backend<0, 0, mp::signed_magnitude, mp::unchecked>, mp::et_off> T;
参见例如Why does using boost::multiprecision::cpp_int affect tail call optimization here,How to use sqrt and ceil with Boost::multiprecision?等
通过以上所有,输出变为:
<强> Live On Wandbox 强>
BOOST_LIB_VERSION: 1_64
Multi-Polygon A :
Outer: (-405129,2010409),(3370580,2010409),(3370580,1997709),(-405129,1997709),(-405129,2010409),
Multi-Polygon B :
Outer: (3364230,-895349),(3364230,2004060),(3376930,2004059),(3376930,-895350),(3364230,-895349),
Multi-Polygon output :
Outer: (3370580,2004060),(3376930,2004059),(3376930,-895350),(3364230,-895349),(3364230,1997709),(-405129,1997709),(-405129,2010409),(3370580,2010409),(3370580,2004060),
答案 1 :(得分:0)
有点不相关,但是没有必要花费这么多代码来初始化或打印数据:
<强> Live On Wandbox 强>
typedef bgm::polygon<bgm::d2::point_xy<mp::checked_int128_t, bg::cs::cartesian>> TPolygon;
typedef bgm::multi_polygon<TPolygon> TMultiPolygon;
int main() {
std::cout << "BOOST_LIB_VERSION: " << BOOST_LIB_VERSION << "\n";
TMultiPolygon
mpA{{{{ { -405129, 2010409 }, { 3370580, 2010409 }, { 3370580, 1997709 }, { -405129, 1997709 }, { -405129, 2010409 } }}}},
mpB{{{{ { 3364230, -895349 }, { 3364230, 2004060 }, { 3376930, 2004059 }, { 3376930, -895350 }, { 3364230, -895349 } }}}},
output;
bg::union_(mpA, mpB, output);
std::cout << "A : " << bg::wkt(mpA) << "\n";
std::cout << "B : " << bg::wkt(mpB) << "\n";
std::cout << "ouput : " << bg::wkt(output) << "\n";
}
打印
BOOST_LIB_VERSION: 1_64
A : POLYGON((-405129 2010409,3370580 2010409,3370580 1997709,-405129 1997709,-405129 2010409))
B : POLYGON((3364230 -895349,3364230 2004060,3376930 2004059,3376930 -895350,3364230 -895349))
ouput : MULTIPOLYGON(((3370580 2004060,3376930 2004059,3376930 -895350,3364230 -895349,3364230 1997709,-405129 1997709,-405129 2010409,3370580 2010409,3370580 2004060)))
请加入
#include <boost/geometry/io/io.hpp>
事实上,你真的不需要制作mpA / mpB多重多边形,所以你可以:
<强> Live On Wandbox 强>
TPolygon
pA{{{ { -405129, 2010409 }, { 3370580, 2010409 }, { 3370580, 1997709 }, { -405129, 1997709 }, { -405129, 2010409 } }}},
pB{{{ { 3364230, -895349 }, { 3364230, 2004060 }, { 3376930, 2004059 }, { 3376930, -895350 }, { 3364230, -895349 } }}};
TMultiPolygon output;
bg::union_(pA, pB, output);
打印:
BOOST_LIB_VERSION: 1_64
A : POLYGON((-405129 2010409,3370580 2010409,3370580 1997709,-405129 1997709,-405129 2010409))
B : POLYGON((3364230 -895349,3364230 2004060,3376930 2004059,3376930 -895350,3364230 -895349))
ouput : MULTIPOLYGON(((3370580 2004060,3376930 2004059,3376930 -895350,3364230 -895349,3364230 1997709,-405129 1997709,-405129 2010409,3370580 2010409,3370580 2004060)))
当然,阅读支持相同:
<强> Live On Wandbox 强>
TPolygon pA, pB;
bg::read_wkt("POLYGON((-405129 2010409,3370580 2010409,3370580 1997709,-405129 1997709,-405129 2010409))", pA);
bg::read_wkt("POLYGON((3364230 -895349,3364230 2004060,3376930 2004059,3376930 -895350,3364230 -895349))", pB);