gpt4 book ai didi

c++ - 如何使用 Boost::Geometry _union 与整数

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:56 25 4
gpt4 key购买 nike

我正在尝试将 Boost::Geometry _union 与整数一起使用,以 boost 性能和数值准确性。为此,我将输入的坐标乘以 10,000。从而创建最多 9 位的坐标。我认为由于我使用 64 位整数,所以应该可以正常工作。

不幸的是,当我运行代码时,我得到了奇怪的结果(输出多边形包含一个远离输入中任何多边形的点)。调查 Boost::Geometry 的代码使我得出结论,起源是文件 cart_intersect.hpp 中的环绕问题:

set<1>(point, get<0, 1>(segment) + boost::numeric_cast
<
CoordinateType
>(numerator * dy_promoted / denominator));

当三个(分子、d​​y_promoted 和分母)都很大时,乘法结果占用超过 64 位,因此整个结果不正确。

使用具有如此大整数的 Boost::Geometry 是否有效?在保持正确性和精度的同时使用带有整数的 Boost::Geometry 的正确方法是什么?


编辑@sehe,感谢您的回复。这是一个 SSCCE,用 VS2013 和 Boost 1.63 编译

#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 坐标都远。

最佳答案

确实,使用-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;

Note If you want to use unchecked arithmetic with cpp_int you will have to make sure that expression-templates are disabled for boost

typedef mp::number<mp::backends::cpp_int_backend<0, 0, mp::signed_magnitude, mp::unchecked>, mp::et_off> T;

See e.g. Why does using boost::multiprecision::cpp_int affect tail call optimization here, How to use sqrt and ceil with Boost::multiprecision?, etc.

有了上面所有的输出就变成了:

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),

查看更多:Boost Geometry and exact point types

关于c++ - 如何使用 Boost::Geometry _union 与整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com