簡體   English   中英

如何使用Boost :: Geometry _union與整數

[英]How to use Boost::Geometry _union with integers

我正在嘗試將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,感謝您的回復。 這是一個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;

注意如果要對cpp_int使用未經檢查的算法,則必須確保禁用表達式模板以進行增強

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

請參見例如, 為什么在這里使用boost :: multiprecision :: cpp_int會影響尾部調用優化如何在Boost :: multiprecision中使用sqrt和ceil?

通過以上所有操作,輸出將變為:

魔盒直播

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和精確點類型

有點無關,但是不需要花費太多代碼來初始化或打印數據:

魔盒直播

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多多邊形,因此您可以:

魔盒直播

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

當然,閱讀支持也一樣:

魔盒直播

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM