[vtkusers] Deleting points form vtkPolyData object

David Doria daviddoria+vtk at gmail.com
Mon Nov 2 16:20:57 EST 2009


On Mon, Nov 2, 2009 at 2:12 PM, Bill Lorensen <bill.lorensen at gmail.com> wrote:
> BTW: I think you will be disappointed. DeletePoint does not really
> delete a point in the polydata.
>
> I'll leave as a exercise to the reader to see what it does.
>
> Hint: in all of VTK it is only used in vtkDecimatePro and vtkQuadricDecimation.
>
> Bill
>
> On Mon, Nov 2, 2009 at 12:39 PM, Bill Lorensen <bill.lorensen at gmail.com> wrote:
>> Looking at the code in vtkPolydata.h, DeletePoint must be preceeded by
>> BuildLinks(). I'm not sure why, but here is the current
>> implementation:
>> inline void vtkPolyData::DeletePoint(vtkIdType ptId)
>> {
>>  this->Links->DeletePoint(ptId);
>> }
>>
>> Since Links is NULL until BuildLinks() is called, this will seg fault
>> as you both discovered.
>>
>> Bill
>>
>>
>> On Mon, Nov 2, 2009 at 12:26 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
>>> On Mon, Nov 2, 2009 at 12:24 PM, da <remywendy at gmail.com> wrote:
>>>> Its not clear if you're adding multiple points or not...
>>>>
>>>> Given the code you've included, you're only inserting one point from the
>>>> line
>>>> m_Points->GetPoints()->InsertNextPoint(m_daPointPosition);
>>>> and then deleting the second point (index 1, but you only have index 0),
>>>> which would cause a null pointer exception?
>>>>
>>>>
>>>> If you're adding multiple points in the lines of code that are not included,
>>>> make sure to include them.
>>>>
>>>>
>>>>
>>>> On Mon, Nov 2, 2009 at 1:30 AM, Lodron, Gerald <Gerald.Lodron at joanneum.at>
>>>> wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> I stored a point list into the vtkPolyData object using these lines:
>>>>>
>>>>> //m_Points ... vtkPolyData Object pointer
>>>>> //m_daPointPosition ... Three dimensional double array specifying x,y and
>>>>> z coordinate of the point
>>>>>
>>>>> m_Points->GetPoints()->InsertNextPoint(m_daPointPosition);
>>>>> m_Points->GetVerts()->InsertNextCell(1);
>>>>>
>>>>> m_Points->GetVerts()->InsertCellPoint(m_Points->GetPoints()->GetNumberOfPoints()-1);
>>>>> m_Points->Update();
>>>>>
>>>>> This works fine, I also can visualize it. Now I want to delete a specific
>>>>> point, e.g. the second point which was inserted, but I always get a null
>>>>> pointer exception:
>>>>>
>>>>> m_Points->DeletePoint(1); //Null pointer exception
>>>>> m_Points->DeleteCell(1);
>>>>>
>>>>> vtkCleanPolyData *Cleaner = vtkCleanPolyData::New();
>>>>> Cleaner->SetInput(m_Points);
>>>>> Cleaner->Update();
>>>>>
>>>>> m_Points = Cleaner->GetOutput();
>>>>> m_Points->Update();
>>>>> m_Points->BuildCells();
>>>>> m_Points->BuildLinks();
>>>>>
>>>>> Cleaner = NULL; //Use smart pointers to delete
>>>>>
>>>>>
>>>>> Have anybody a suggestion what i made false?
>>>>> _______________________________________________
>>>>> 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
>>>>>
>>>>> Follow this link to subscribe/unsubscribe:
>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>>
>>>>
>>>> _______________________________________________
>>>> 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
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>>
>>>>
>>>
>>> I also get a segfault. Here is my example:
>>>
>>> #include <vtkSmartPointer.h>
>>> #include <vtkPoints.h>
>>> #include <vtkPolyData.h>
>>>
>>> int main(int argc, char *argv[])
>>> {
>>>  //create a set of points
>>>  vtkSmartPointer<vtkPoints> Points = vtkSmartPointer<vtkPoints>::New();
>>>  Points->InsertNextPoint ( 1.0, 0.0, 0.0 );
>>>  Points->InsertNextPoint ( 0.0, 0.0, 0.0 );
>>>  Points->InsertNextPoint ( 0.0, 1.0, 0.0 );
>>>
>>>  vtkSmartPointer<vtkPolyData> Polydata = vtkSmartPointer<vtkPolyData>::New();
>>>  Polydata->SetPoints(Points);
>>>
>>>  vtkstd::cout << "Number of points: " <<
>>> Polydata->GetNumberOfPoints() << vtkstd::endl;
>>>
>>>  Polydata->DeletePoint(1);//segfault on this line
>>>
>>>  vtkstd::cout << "Number of points: " <<
>>> Polydata->GetNumberOfPoints() << vtkstd::endl;
>>>
>>>  return 0;
>>> }
>>>
>>>
>>> Thanks,
>>>
>>> David

At least in this example, it doesn't seem to do anything. Maybe it has
something to do with the cells?

I'm not sure how to handle updating the cells once the point is
removed, but you can naively remove a point as follows. Maybe there
should be some capabilities built in to do this type of thing?

#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

void OutputPoints(vtkPoints* Points);
void ReallyDeletePoint(vtkPoints* Points, unsigned int id);

int main(int argc, char *argv[])
{
  //create a set of points
  vtkSmartPointer<vtkPoints> Points = vtkSmartPointer<vtkPoints>::New();
  Points->InsertNextPoint ( 1.0, 0.0, 0.0 );
  Points->InsertNextPoint ( 0.0, 1.0, 0.0 );
  Points->InsertNextPoint ( 0.0, 0.0, 1.0 );

  vtkstd::cout << "Number of points: " << Points->GetNumberOfPoints()
<< vtkstd::endl;
  OutputPoints(Points);

  ReallyDeletePoint(Points, 1);

  vtkstd::cout << "Number of points: " << Points->GetNumberOfPoints()
<< vtkstd::endl;
  OutputPoints(Points);

  return 0;
}

void OutputPoints(vtkPoints* Points)
{
  for(unsigned int i = 0; i < Points->GetNumberOfPoints(); i++)
  {
    double p[3];
    Points->GetPoint(i,p);
    vtkstd::cout << p[0] << " " << p[1] << " " << p[2] << vtkstd::endl;
  }
}

void ReallyDeletePoint(vtkPoints* Points, unsigned int id)
{
  vtkSmartPointer<vtkPoints> NewPoints = vtkSmartPointer<vtkPoints>::New();

  for(unsigned int i = 0; i < Points->GetNumberOfPoints(); i++)
  {
    if(i != id)
    {
      double p[3];
      Points->GetPoint(i,p);
      NewPoints->InsertNextPoint(p);
    }
  }

  Points->ShallowCopy(NewPoints);
}

Thanks,

David



More information about the vtkusers mailing list