gpt4 book ai didi

c++ - Boost::geometry::multi_polygon 的负缓冲区

转载 作者:行者123 更新时间:2023-11-30 05:23:05 29 4
gpt4 key购买 nike

我尝试使用 Boost::geometry 将负缓冲区应用于多面体。当我运行下面的代码时,我得到了正确的结果。但是,如果我删除标记的 block ,它只是导出并重新导入 WKT 中的几何图形,我会在 tmp_erosion 几何图形中得到一个空的 MultiPolygon。我尝试使用 boost::geometry::correct() 函数,但它没有任何改变。你知道如何解决这个问题吗?此外,我尝试将几何图形导出到 WKB,在导出-然后-导入-WKT 之前和之后是不同的。我猜这是由于某处进行了舍入操作,这可能与我的问题有关吗?

std::string wkt = "MULTIPOLYGON(((-203.3984972480392 326.87043766411114,-234.3294031269611 350.72117460025004,-234.32940582512484 350.7211767561067,-234.59541944833873 350.9413566745228,-234.5954246133754 350.9413612592119,-234.8456066177885 351.1793768235705,-234.84561145406377 351.179381753826,-235.078768547128 351.43409770182103,-235.07877303157431 351.43410295412707,-235.29379312442313 351.70430436766964,-235.29379723565108 351.7043099169743,-235.39951413618792 351.85781786966413,-240.86780715032046 350.38218215855835,-241.2049443604434 350.30361570297174,-241.20495115354115 350.3036143659985,-241.5467165739732 350.24856362767656,-241.54672344329876 350.24856276394,-241.89147886679623 350.21729162130026,-241.89148577942245 350.2172912349408,-242.23758558935015 350.20994967493,-242.58335698974216 350.2265727030494,-242.5833638895166 350.22657327470256,-242.9271563937872 350.26708130353956,-242.92716323747084 350.2670823512364,-243.26732892181855 350.3312812109238,-243.26733567660713 350.3312827296422,-243.60225063542637 350.4188666739157,-243.93029626301933 350.52941192532916,-243.93030274346452 350.52941436202866,-244.24991952642313 350.66239589829064,-244.55956286161836 350.81717054531686,-244.55956894361645 350.81717385333343,-244.8577608930744 350.99300259862656,-244.85776673160544 350.99300631954446,-245.14307795174008 351.18904635131906,-245.1430835188178 351.18905046730254,-245.41415167705114 351.40436658497003,-245.6696669314724 351.6379177381484,-245.66967187701746 351.63792258327567,-245.90841706208894 351.88859631483484,-266.58920031948566 375.16554041806563,-266.58920258870927 375.16554306360825,-266.8114462984894 375.433970996735,-266.8114505535744 375.43397651693255,-267.0144568842047 375.7172330288498,-267.0144607444406 375.7172388320306,-267.19724352509485 376.01394790994334,-267.1972469717292 376.0139539679166,-267.358918274207 376.32267424871253,-267.35892129049665 376.3226805320497,-267.49869574263016 376.6419123042001,-267.4986983139225 376.6419187823777,-267.6158969147833 376.97011127128593,-267.6158990285875 376.970117912834,-267.7099540930073 377.30568358689385,-267.7804054331035 377.6469788117333,-267.780406603398 377.6469856825949,-267.8269143132387 377.99236621139704,-267.8492510917757 378.34014040558645,-267.84925129584775 378.3401473724138,-267.84730904189666 378.68863970288874,-267.8210977526416 379.03614335774023,-267.8210969865306 379.036150285323,-267.7707424635338 379.3809909713855,-267.6964900867632 379.72148658265553,-267.59870274954784 380.0559764307823,-267.5987005618558 380.05598304835985,-267.4778489339802 380.38285544057135,-267.3345253662763 380.7005096185073,-267.334522280147 380.700515867838,-267.1694203712224 381.00741516970913,-267.1694168572867 381.0074211888939,-266.9833347199501 381.3020805089815,-266.77718414040345 381.58305694993714,-266.7771798240608 381.58306242236944,-266.5519583226944 381.8489968074004,-266.5519536356491 381.8490019658822,-266.30875282540296 382.09860488379184,-232.51589024775978 414.4450812583296,-232.51588002802416 414.4450898114167,-232.51586876173633 414.4450969296367,-232.51585664898366 414.445102486571,-232.51584390488657 414.4451063835295,-232.5158373664838 414.445107686701,-211.12681324719696 417.9713206571314,-211.12680695294904 417.97132149052527,-211.1267942693878 417.9713219522357,-211.12678793120457 417.971321578691,-211.1267816295017 417.9713208036793,-211.12676923690148 417.97131806335767,-211.1267572912145 417.97131377541353,-211.12675154646425 417.9713110717222,-177.1106691504801 400.6223337146699,-177.1106636027793 400.62233015463886,-177.11065830167098 400.6223262368518,-177.11064853019522 400.6223173975823,-177.11064000569968 400.62230735032404,-177.11063625823746 400.6223019274713,-177.11063287618194 400.6222962695118,-177.11062726542067 400.6222843475245,-177.11062327082684 400.622271791345,-177.11062190239758 400.6222653432285,-165.59426937600804 335.9234066092942,-162.6596781566315 302.06171116087233,-162.6596788436905 302.0616975137797,-162.65967988463825 302.06169075731424,-162.65968333706937 302.06167753627386,-162.65968573241835 302.0616711334854,-162.6596885596208 302.06166490931287,-162.65969545477958 302.06165311219564,-162.65970389380263 302.0616423651924,-162.65970864407186 302.06163744906183,-162.65971909523134 302.0616286463092,-162.65973064885196 302.0616213504858,-162.65974308921002 302.0616156978157,-162.6597561840252 302.0616117938428,-162.65976290080164 302.0616105217139,-162.65977651628992 302.06160936686865,-162.65979016210093 302.0616100789291,-162.65979691664793 302.06161113225346,-162.65981013134098 302.0616146089008,-162.65982274871268 302.0616198545771,-204.8758528204258 323.10306070098704,-204.8758584840069 323.10306427432,-204.87586903132672 323.1030725164298,-204.87587838335946 323.10308209337154,-204.87588255785658 323.1030873296329,-204.87588981029387 323.1030985804321,-204.8758928557106 323.103104544516,-204.87589549513 323.1031106990528,-204.87589771671574 323.10311701644275,-204.87590086845387 323.1031300258577,-204.87590225445257 323.1031433396602,-204.8759018498778 323.10315671929584,-204.8444560373964 323.42562807154223,-204.84445513319602 323.42563489982314,-204.78751382477697 323.76528940556193,-204.78751245267617 323.76529615540176,-204.70732324106254 324.10023186228716,-204.60427022620226 324.42884648764846,-204.60426794001853 324.4288529850604,-204.47883820282144 324.74959412196023,-204.47883547479194 324.74960044658303,-204.33162256579686 325.0609494910012,-203.3984972480392 326.87043766411114)))";

typedef double coordinate_2d;
typedef boost::geometry::model::d2::point_xy<coordinate_2d> point_2d;
typedef boost::geometry::model::polygon<point_2d> polygon_2d;
typedef boost::geometry::model::multi_polygon<polygon_2d> multi_polygon_2d;
multi_polygon_2d test_erosion, tmp_erosion;

// Read WKT
boost::geometry::read_wkt(wkt, test_erosion);

// Check validity
std::cout<<boost::geometry::is_valid(test_erosion)<<std::endl;

//////////////////////////////////////////////
// WHY DO I NEED THIS BLOCK???
//////////////////////////////////////////////
// Export to wkt
std::ostringstream tmp_wkt;
tmp_wkt << boost::geometry::wkt(test_erosion);

// And read it again
boost::geometry::read_wkt(tmp_wkt.str(), test_erosion);
std::cout<<boost::geometry::is_valid(test_erosion)<<std::endl;
//////////////////////////////////////////////

// Apply buffer
boost::geometry::buffer(test_erosion, tmp_erosion,
boost::geometry::strategy::buffer::distance_symmetric<double>(-0.00001),
boost::geometry::strategy::buffer::side_straight(),
boost::geometry::strategy::buffer::join_round(),
boost::geometry::strategy::buffer::end_round(),
boost::geometry::strategy::buffer::point_circle(360)
);

// Print result
std::cout<<boost::geometry::wkt(test_erosion)<<std::endl;
std::cout<<boost::geometry::wkt(tmp_erosion)<<std::endl;

最佳答案

But if I remove the marked block, which just export and re-import the geometry in WKT, I get an empty MultiPolygon in the tmp_erosion geometry.

tmp_erosion 在这种情况下是空的,因为 boost::geometry::buffer 没有向它写入任何内容。

没有写的原因是因为 boost::geometry::strategy::buffer::distance_symmetric<double>(-0.00001) 太粗糙了。带有更多小数位的 boost::geometry::strategy::buffer::distance_symmetric<double>(-0.0000001) 可以工作。

I guess it is due to some rounding operation somewhere

是的。

std::ostringstream tmp_wkt; 有一个 default precision 集。目前该程序不会写入所有数字,因此会应用四舍五入。

tmp_wkt.precision(16); 之前添加 tmp_wkt << boost::geometry::wkt(test_erosion); 将导致与没有 block 相同的行为,因为现在所有数字都已写入。

这是一个更新的程序,用于显示这些要点:

#include <iostream>
#include <sstream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

int main()
{
std::string wkt = "MULTIPOLYGON(((-203.3984972480392 326.87043766411114,-234.3294031269611 350.72117460025004,-234.32940582512484 350.7211767561067,-234.59541944833873 350.9413566745228,-234.5954246133754 350.9413612592119,-234.8456066177885 351.1793768235705,-234.84561145406377 351.179381753826,-235.078768547128 351.43409770182103,-235.07877303157431 351.43410295412707,-235.29379312442313 351.70430436766964,-235.29379723565108 351.7043099169743,-235.39951413618792 351.85781786966413,-240.86780715032046 350.38218215855835,-241.2049443604434 350.30361570297174,-241.20495115354115 350.3036143659985,-241.5467165739732 350.24856362767656,-241.54672344329876 350.24856276394,-241.89147886679623 350.21729162130026,-241.89148577942245 350.2172912349408,-242.23758558935015 350.20994967493,-242.58335698974216 350.2265727030494,-242.5833638895166 350.22657327470256,-242.9271563937872 350.26708130353956,-242.92716323747084 350.2670823512364,-243.26732892181855 350.3312812109238,-243.26733567660713 350.3312827296422,-243.60225063542637 350.4188666739157,-243.93029626301933 350.52941192532916,-243.93030274346452 350.52941436202866,-244.24991952642313 350.66239589829064,-244.55956286161836 350.81717054531686,-244.55956894361645 350.81717385333343,-244.8577608930744 350.99300259862656,-244.85776673160544 350.99300631954446,-245.14307795174008 351.18904635131906,-245.1430835188178 351.18905046730254,-245.41415167705114 351.40436658497003,-245.6696669314724 351.6379177381484,-245.66967187701746 351.63792258327567,-245.90841706208894 351.88859631483484,-266.58920031948566 375.16554041806563,-266.58920258870927 375.16554306360825,-266.8114462984894 375.433970996735,-266.8114505535744 375.43397651693255,-267.0144568842047 375.7172330288498,-267.0144607444406 375.7172388320306,-267.19724352509485 376.01394790994334,-267.1972469717292 376.0139539679166,-267.358918274207 376.32267424871253,-267.35892129049665 376.3226805320497,-267.49869574263016 376.6419123042001,-267.4986983139225 376.6419187823777,-267.6158969147833 376.97011127128593,-267.6158990285875 376.970117912834,-267.7099540930073 377.30568358689385,-267.7804054331035 377.6469788117333,-267.780406603398 377.6469856825949,-267.8269143132387 377.99236621139704,-267.8492510917757 378.34014040558645,-267.84925129584775 378.3401473724138,-267.84730904189666 378.68863970288874,-267.8210977526416 379.03614335774023,-267.8210969865306 379.036150285323,-267.7707424635338 379.3809909713855,-267.6964900867632 379.72148658265553,-267.59870274954784 380.0559764307823,-267.5987005618558 380.05598304835985,-267.4778489339802 380.38285544057135,-267.3345253662763 380.7005096185073,-267.334522280147 380.700515867838,-267.1694203712224 381.00741516970913,-267.1694168572867 381.0074211888939,-266.9833347199501 381.3020805089815,-266.77718414040345 381.58305694993714,-266.7771798240608 381.58306242236944,-266.5519583226944 381.8489968074004,-266.5519536356491 381.8490019658822,-266.30875282540296 382.09860488379184,-232.51589024775978 414.4450812583296,-232.51588002802416 414.4450898114167,-232.51586876173633 414.4450969296367,-232.51585664898366 414.445102486571,-232.51584390488657 414.4451063835295,-232.5158373664838 414.445107686701,-211.12681324719696 417.9713206571314,-211.12680695294904 417.97132149052527,-211.1267942693878 417.9713219522357,-211.12678793120457 417.971321578691,-211.1267816295017 417.9713208036793,-211.12676923690148 417.97131806335767,-211.1267572912145 417.97131377541353,-211.12675154646425 417.9713110717222,-177.1106691504801 400.6223337146699,-177.1106636027793 400.62233015463886,-177.11065830167098 400.6223262368518,-177.11064853019522 400.6223173975823,-177.11064000569968 400.62230735032404,-177.11063625823746 400.6223019274713,-177.11063287618194 400.6222962695118,-177.11062726542067 400.6222843475245,-177.11062327082684 400.622271791345,-177.11062190239758 400.6222653432285,-165.59426937600804 335.9234066092942,-162.6596781566315 302.06171116087233,-162.6596788436905 302.0616975137797,-162.65967988463825 302.06169075731424,-162.65968333706937 302.06167753627386,-162.65968573241835 302.0616711334854,-162.6596885596208 302.06166490931287,-162.65969545477958 302.06165311219564,-162.65970389380263 302.0616423651924,-162.65970864407186 302.06163744906183,-162.65971909523134 302.0616286463092,-162.65973064885196 302.0616213504858,-162.65974308921002 302.0616156978157,-162.6597561840252 302.0616117938428,-162.65976290080164 302.0616105217139,-162.65977651628992 302.06160936686865,-162.65979016210093 302.0616100789291,-162.65979691664793 302.06161113225346,-162.65981013134098 302.0616146089008,-162.65982274871268 302.0616198545771,-204.8758528204258 323.10306070098704,-204.8758584840069 323.10306427432,-204.87586903132672 323.1030725164298,-204.87587838335946 323.10308209337154,-204.87588255785658 323.1030873296329,-204.87588981029387 323.1030985804321,-204.8758928557106 323.103104544516,-204.87589549513 323.1031106990528,-204.87589771671574 323.10311701644275,-204.87590086845387 323.1031300258577,-204.87590225445257 323.1031433396602,-204.8759018498778 323.10315671929584,-204.8444560373964 323.42562807154223,-204.84445513319602 323.42563489982314,-204.78751382477697 323.76528940556193,-204.78751245267617 323.76529615540176,-204.70732324106254 324.10023186228716,-204.60427022620226 324.42884648764846,-204.60426794001853 324.4288529850604,-204.47883820282144 324.74959412196023,-204.47883547479194 324.74960044658303,-204.33162256579686 325.0609494910012,-203.3984972480392 326.87043766411114)))";

typedef double coordinate_2d;
typedef boost::geometry::model::d2::point_xy<coordinate_2d> point_2d;
typedef boost::geometry::model::polygon<point_2d> polygon_2d;
typedef boost::geometry::model::multi_polygon<polygon_2d> multi_polygon_2d;
multi_polygon_2d test_erosion, tmp_erosion;

// Read WKT
boost::geometry::read_wkt(wkt, test_erosion);

// Check validity
std::cout << boost::geometry::is_valid(test_erosion) << std::endl;

//////////////////////////////////////////////
// WHY DO I NEED THIS BLOCK???
//////////////////////////////////////////////
// Export to wkt
std::ostringstream tmp_wkt;
tmp_wkt.precision(16); // changing the precision to include all digits will render this block moot
tmp_wkt << boost::geometry::wkt(test_erosion); // this will round according to the precision for the stream

// And read it again
boost::geometry::read_wkt(tmp_wkt.str(), test_erosion);
std::cout << boost::geometry::is_valid(test_erosion) << std::endl;
//////////////////////////////////////////////

// Apply buffer
boost::geometry::buffer(test_erosion, tmp_erosion,
boost::geometry::strategy::buffer::distance_symmetric<double>(-0.0000001), // this will produce output in tmp_erosion, even with all the digits
//boost::geometry::strategy::buffer::distance_symmetric<double>(-0.00001), // this will not produce output if the coordinates are not rounded
boost::geometry::strategy::buffer::side_straight(),
boost::geometry::strategy::buffer::join_round(),
boost::geometry::strategy::buffer::end_round(),
boost::geometry::strategy::buffer::point_circle(360)
);

// Print result
std::cout << boost::geometry::wkt(test_erosion) << std::endl;
std::cout << boost::geometry::wkt(tmp_erosion) << std::endl;;

关于c++ - Boost::geometry::multi_polygon 的负缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354086/

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