[vtkusers] vtkDataSet::GetCell(vtkIdType) with multiple cell objects

Biddiscombe, John A. biddisco at cscs.ch
Fri Jan 11 07:20:41 EST 2013


Because for performance reasons the dataset class internally caches a cell pointer which gets reused again and again. Method 1 is really just getting the same object twice and is no good for multithreaded use or comparisons of the kind you're making. 

Accessing cells like this can result in very inefficient code as a lot of copying can take place. You should think very hard about what you want to do with the cells you fetch and only access the information you really need. Remember that internally, the cells are all stored in a cell array list which has only
Npts, pts
And the cll type and location of the cell in the array is stored elsewhere. 

Here's an example of use where we don't make copies 

  vtkIdType npts, *pts;
  // polydata requires a cell map (verts/lines/polys/strips) to be present before we traverse cells
  pdata->BuildCells();
  //
  // for each cell ...
  //
  for (cellId=0; cellId<numCells; cellId++) {
    // get a pointer to the cell points
    pdata->GetCellPoints(cellId, npts, pts); }
    // loop over cell points
    for (j=0; j<npts; j++) {
      }
    }

I've removed the actual algorithm, but this is a fast way to traverse cell contents. It may not be what you want to do, but I just thought it'd help you understand what's going on internally (and I was writing this code earlier today so it seemed easy to cut'n paste...)

JB

-----Original Message-----
From: vtkusers-bounces at vtk.org [mailto:vtkusers-bounces at vtk.org] On Behalf Of Sunrise
Sent: 11 January 2013 06:15
To: vtkusers at vtk.org
Subject: [vtkusers] vtkDataSet::GetCell(vtkIdType) with multiple cell objects

I have problem of using GetCell  in vtkDataSet. Consider the following
methods:

Method 1- vtkCell * = vtkDataSet->GetCell(vtkIdType)         // fails if 
I declare more than one cell
Method 2- vtkDataSet->GetCell(vtkidType,vtkGenericCell)   // this one 
works well.

By comparing two overloads of GetCell, the first one fails if I declare two cell objects, while second method is working fine. Here are examples of both, assume I want to get two cells with different CellIds:

// Method 1

vtkSmartPointer<vtkTriangle> Cell1 =
vtkTriangle::SafeDownCast(MyDataSet->GetCell(CellId1));
vtkSmartPointer<vtkTriangle> Cell2 =
vtkTriangle::SafeDownCast(MyDataSet->GetCell(CellId2));

// now see the problem here

if(Cell1 == Cell2)
{
     std::cout << "They should be different!!" << std::endl; }

Or by printing Cell1->GetPointId(SomeIdHere), we can see both Cell1 and
Cell2 are the same. Why?

// Method 2

vtkSmartPointer<vtkGenericCell> Cell1 = vtkSmartPointer<vtkGenericCell>::New();
vtkSmartPointer<vtkGenericCell> Cell2 = vtkSmartPointer<vtkGenericCell>::New();

MyDataSet->GetCell(CellId1,GenericCell1);
MyDataSet->GetCell(CellId2,GenericCell2);

Cell1->SetCellTypeToTriangle();
Cell2->SetCellTypeToTriangle();

Since the second method copies cell by value to allocated memory on stack, it works fine.

But I would like to know why the first method gets into the problem? and how to avoid it? Using first method is preferred since I do not have to declare Generic cell instead of Triangle, and also I do not have to copy cell from data to an other object.

Thanks.
_______________________________________________
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



More information about the vtkusers mailing list