[vtk-developers] potential speedup for the vtkpolydatatoimagestencil
David Gobbi
david.gobbi at gmail.com
Wed Feb 29 17:06:33 EST 2012
Out of curiosity, how much of a performance difference does it make?
- David
On Wed, Feb 29, 2012 at 3:04 PM, Mark Roden <mmroden at gmail.com> wrote:
> OK, I got it to work.
>
> For people who are following this thread, converting rtstructs from
> vtkgdcmpolydata reader into a binary image but wanting to avoid
> extrusion, this code will convert the data from polys to lines:
>
>
> //just want lines returned, nothing else, for fast stenciling.
> vtkPolyData* ConvertToLines(vtkPolyData * const data) {
> vtkPolyData* returnedData = vtkPolyData::New();
> returnedData->DeepCopy(data);
>
> vtkPoints* newPoints = vtkPoints::New();
>
> int theNumPoints = returnedData->GetNumberOfPoints();
>
> if (theNumPoints == 1){
> double* pt = data->GetPoint(0);
> newPoints->InsertNextPoint(pt);
> returnedData->SetPoints(newPoints);
> } else {
>
> int i;
> //borrowed this code from the polydatatostencil code
> //we know that each polygon is coplanar, so do that here.
> //also, closes the polygon, but should check to see if that's necessary.
> vtkMergePoints* theMerge = vtkMergePoints::New();
> double bounds[6];
> data->GetBounds(bounds);
> theMerge->InitPointInsertion(newPoints, bounds);
> vtkCellArray* polys = (data->GetPolys());
> vtkCellArray* lines = vtkCellArray::New();
> returnedData->SetLines(lines);//blank them out, just in case
> //each cell in the poly array is a polyline, I contend.
> int theNumPolys = polys->GetNumberOfCells();
> lines->Allocate(theNumPolys);
> polys->InitTraversal();
> vtkPoints* oldPoints = data->GetPoints();
> for (int j = 0; j < theNumPolys; j++){
> vtkIdType numPoints;
> vtkIdType* ptIds;
> polys->GetNextCell(numPoints, ptIds);
> lines->InsertNextCell(numPoints+1);//close the end of the poly
> int zeroPtId = 0;
> double zeroPt[3];
> for (i = 0; i < numPoints; i++){
> vtkIdType ptId;
> double point[3];
> oldPoints->GetPoint(ptIds[i], point);
> theMerge->InsertUniquePoint(point, ptId);
> lines->InsertCellPoint(ptId);
> newPoints->InsertNextPoint(point);
> if (i == 0){
> zeroPtId = ptId;
> zeroPt[0] = point[0];
> zeroPt[1] = point[1];
> zeroPt[2] = point[2];
> }
> }
> newPoints->InsertNextPoint(zeroPt);
> theMerge->InsertUniquePoint(zeroPt, zeroPtId);
> lines->InsertCellPoint(zeroPtId);//start from the beginning
> }
> returnedData->SetPoints(newPoints);
> returnedData->SetLines(lines);
> vtkCellArray* blank = vtkCellArray::New();
> returnedData->SetPolys(blank);
> blank->Delete();
> lines->Delete();
> }
> newPoints->Delete();
>
> return returnedData;
> }
More information about the vtk-developers
mailing list