Segmentation fault on a wrapper class for vtkPolyData

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Segmentation fault on a wrapper class for vtkPolyData

Ruben Di Battista
Hello everybody, I’m trying to gain a bit more awareness on C++ programming and I wanted to try to iterate using C++11/14 iterators over a vtkPolyData structure but I’m experiencing SIGSEV. 

I will present before the code structure.

1) I have a `PolyDataWrapper` class that accepts as argument a pointer to a `vtkPolyData` structure containing a triangulated surface. This class offers a method `iterateOver<Item>` that returns an iterator so that you can iterate over the points of the vtkPolydata doing
    
    for (PolyDataPoint p : polydata.iterateOver<PolyDataPoint>()){
        // Do something
     }

And equivalently for cells:

    for (PolyDataCell c : postdata.iterateOver<PolyDataCell>() { /*…*/}

And moreover it exposes some methods like `GetCellPoints` that use the underlying pointer to the “raw” vtkPolyData object.

Here's the class for the PolyDataCell

2) I have a `PolyDataIterator` that implements the C++14 compatible range-based for loop. 

The problem is that running the following code raises a Segmentation fault if I’m using the PolyDataCell::GetCellPoints() method that the only thing it does it calling the PolyDataWrapper::GetCellPoints(PolyDataCell *).

I probably did a mistake passing around pointers, but I was thinking that maybe I’m doing something that is messed up by the garbage collection of the vtk smart pointer. Could it be? Help is very much appreciated!

#define SPHERE_RADIUS 1.0

#include "vtkNew.h"
#include "vtkSmartPointer.h"
#include "vtkTriangleFilter.h"
#include "vtkSphereSource.h"

#include "types.h"
#include "PolyDataWrapper.h"

vtkNew<vtkSphereSource> sphere;
sphere->SetRadius(SPHERE_RADIUS);
sphere->Update();

// Transform the sphere into a PolyData
vtkNew<vtkTriangleFilter> triFilter;
triFilter->SetInputData(sphere->GetOutput());
triFilter->Update();

// Triangulated version of the sphere
vtkSmartPointer<vtkPolyData> triSphere = triFilter->GetOutput();

// Build cells
triSphere->BuildCells();

// Get total number of points
IdType nPoints = triSphere->GetNumberOfPoints();

// Get total number of cells
IdType nCells = triSphere->GetNumberOfCells();

//Initialize PolyDataWrapper
PolyDataWrapper polydata(triFilter->GetOutput());

for(PolyDataCell cell : polydata.iterateOver<PolyDataCell>()) {
std::array<PolyDataPoint, 3> points;
    points = polydata.GetCellPoints(cell); // This is ok
    // points = cell.GetCellPoints(); // This causes SIGSEV
}

          _   
-.     .´  |∞∞∞∞
  ',  ;    |∞∞∞∞∞∞
    ˜˜     |∞∞∞∞∞∞∞∞∞ RdB
    ,.,    |∞∞∞∞∞∞
  .'   '.  |∞∞∞∞
-'       `’

http://rdb.is

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers