程序输出是:std::vector(0x55f164f79450)而不是儿童:我在这里做错了什么?
任何帮助都将不胜感激!
在线示例:http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/PolyTree/_Body.htm
我的测试代码:
#include <clipper/include/clipper.hpp>
#include <iterator>
#include <algorithm>
using namespace ClipperLib;
using namespace std;
area_partition::area_partition()
{
//Paths
Path one = {{10,10},{100,10},{100,100},{10,100}};
Path two = {{20,20},{20,90},{90,90},{90,20}};
Path three = {{30,30},{50,30},{50,50},{30,50}};
Path four = {{60,60},{80,60},{80,80},{60,80}};
PolyTree array;
//Setup output..
ClipperLib::Clipper c;
c.AddPath(one, ptSubject, true);
c.AddPath(two, ptSubject, true);
c.AddPath(three, ptSubject, true);
c.AddPath(four, ptSubject, true);
c.Execute(ctDifference,array);
c.Clear();
//call to Clipper.Execute method here which fills 'polytree'
PolyNode *polynode = array.GetFirst();
while (polynode)
{
//do stuff with polynode here
qDebug()<< "parent : " << polynode->Parent;
qDebug()<< "childs : " << polynode->Childs;
qDebug()<< "isHole : " << polynode->IsHole();
qDebug()<< "childcount : " << polynode->ChildCount();
qDebug()<< " ";
qDebug()<< " ";
polynode = polynode->GetNext();
}
}我的终端输出:
parent : 0x7ffc246b6730
childs : std::vector(0x55f164f79450)
isHole : false
childcount : 1
parent : 0x55f164f793f0
childs : std::vector(0x55f165763b40, 0x55f165763bf0)
isHole : true
childcount : 2
parent : 0x55f164f79450
childs : std::vector()
isHole : false
childcount : 0
parent : 0x55f164f79450
childs : std::vector()
isHole : false
childcount : 0在线示例的输出:
ChildCount = 1
Childs[0]:
Contour = ((10,10),(100,10),(100,100),(10,100))
IsHole = False
ChildCount = 1
Childs[0]:
Contour = ((20,20),(20,90),(90,90),(90,20))
IsHole = True
ChildCount = 2
Childs[0]:
Contour = ((30,30),(50,30),(50,50),(30,50))
IsHole = False
ChildCount = 0
Childs[1]:
Contour = ((60,60),(80,60),(80,80),(60,80))
IsHole = False
ChildCount = 0发布于 2019-11-17 22:44:46
您正在迭代一个PolyNode。根据快船图书馆的文件:
Parent是指向PolyNode的指针,Childs是指向PolyNodes的指针的std::vector
当您尝试输出指针时,operator<<通常会显示指针的值,而不是指向的对象。这就是为什么你有.为了父母。
QDebug类为operator<<为std::vector提供了一个重载,它输出向量的内容,即指针,所以这就是为什么得到0x.也是为了孩子们。
如果希望获得预期的输出,则需要不输出父级。并且您需要使用一个索引迭代这些子节点,以便一个接一个地输出它们(以便您可以输出"Child["<<i<<"]:"<<std::endl;),输出的不是指向内容的指针,而是使用"<< *polynode->Childs[i]"输出内容本身。
发布于 2019-11-20 00:37:16
我找到了另一个解决多树危机的办法。
为了检索对象id,我使用了polynode->contour信息。
看看第一个x,y坐标,我就能识别出所有的物体。结果是一个父->子显示为:
注释:我将附加的代码最小化,并附加示例以帮助其他人。
Path path;
Paths total;
path.push_back(IntPoint(x_start, y_start));
path.push_back(IntPoint(x_start, y_start));
path.push_back(IntPoint(x_start, y_start));
etc..
etc..
CleanPolygon(path, 1.415);
total.push_back(path);
path.clear();
PolyNode* polynode = array.GetFirst();
PolyTree array;
ClipperLib::Clipper c;
c.Clear();
c.AddPaths(total, ptSubject, true);
c.Execute(ctDifference,array);
PolyNode* polynode = array.GetFirst();
while (polynode)
{
if(int(your_x_pos) == int(polynode->Contour.at(0).X) && int(your_y_pos) == int(polynode->Contour.at(0).Y)){
//match found, now you know the polygon id..
if(polynode->IsHole() == 0){
//do something..
}
if(polynode->IsHole() == 1){
//do something..
}
}
polynode = polynode->GetNext();
}
//clean up memory..
path.clear();
path.shrink_to_fit();
total.clear();
total.shrink_to_fit();

https://stackoverflow.com/questions/58905767
复制相似问题