2

The program output is : std::vector(0x55f164f79450) instead of Childs[one]: What am i doing wrong here?

Any help would be appreciated !

The online example : http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/PolyTree/_Body.htm

My test code :

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

My terminal output :

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

The output of the online example :

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
max
  • 21
  • 2
  • 1
    I know nothing about `PolyTree` but looks like `Parent` is pointer and `Childs` is a collection of pointers. Try dereference the parent: `qDebug() << "parent : " << *polynode->Parent;` – 4LegsDrivenCat Nov 17 '19 at 22:40

2 Answers2

3

You are iterating over a PolyNode. According to the Clipper library documentation:

  • Parent is a pointer to a PolyNode
  • Childs is a std::vector of pointers to PolyNodes

When you try to output a pointer, operator<< usually displays the value of the pointer and not the object pointed to. This is why you have 0x... for the parent.

The QDebug class has an overload for operator<< for std::vector, which outputs the content of the vector, which is pointers, so this is why you get 0x... for the children as well.

If you want to get the output that you expect, you need to not to output the parent. And you need to iterate through the children with an index to output them one after the other, (so that you can output "Child["<<i<<"]:"<<std::endl;), outputing not the pointer to the content, but the content itself with "<< *polynode->Childs[i]"

Christophe
  • 68,716
  • 7
  • 72
  • 138
0

I have found another solution to solve the polytree crisis..

To retrieve the object id, i used the polynode->contour information.

Lookin at the first x,y coordinates i was able to identify all objects. The result is an parent->child displayed as :

  1. -color red -> cw (clockwise), outside contour

  2. -color yellow -> ccw (counterclockwise), inside contour

Note : The attached code is minimalized by me and attached as example to help others.

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

max
  • 21
  • 2