内存地址输出而不是值

时间:2019-11-17 22:22:07

标签: c++ clipperlib

程序输出为: std :: vector(0x55f164f79450)而不是 Childs [one] : 我在这里做错了什么?

任何帮助将不胜感激!

在线示例: 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

2 个答案:

答案 0 :(得分:3)

您正在遍历PolyNode。根据Clipper库文档:

  • Parent是指向PolyNode的指针
  • Childsstd::vector的指针PolyNode

当您尝试输出指针时,operator<<通常显示指针的值,而不显示所指向的对象。这就是为什么您将0x ...作为父级的原因。

QDebug类对operator<<的{​​{1}}有一个重载,它输出向量的内容,即指针,因此这就是为什么您得到0x ...的原因。孩子们也是。

如果要获得期望的输出,则无需输出父项。并且您需要遍历带有索引的子项,以一个接一个地输出它们(以便您可以输出std::vector),而不是输出指向内容的指针,而是使用"Child["<<i<<"]:"<<std::endl;输出内容本身

答案 1 :(得分:0)

我找到了解决多树危机的另一种方法。

要检索对象ID,我使用了 polynode->轮廓信息。

在第一个x,y坐标上查找,我能够识别所有对象。 结果是一个parent-> child显示为:

  1. -红色-> cw(顺时针),外部轮廓

  2. -黄色->逆时针(ccw)(逆时针),内部轮廓

  

注意:附件中的代码已由我最小化,并作为示例提供了帮助他人。

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

enter image description here