[vtkusers] Ordering wrong when convert vtkImageData to vtkPolyData

Summer Sun sunxiasx at foxmail.com
Sat Mar 18 07:10:06 EDT 2017


Thank you David, I have now reach my goal and I would like to paste my code
here for others to reference.

I have an *vtkImageData* with two pixel value: 0 and 255, I want to draw the
boundary valued 255 using *vtkContourWidget*.

1. I use *vtkMarchingSquares*, and the output of marching squares is a
*vtkPolyData* while the lines segments are not ordered;
2. I use *vtkStripper* to order the output of *vtkMarchingSquares*;
3. After process by *vtkStripper*, the line segments (*vtkCellArray*) of
*vtkPolydata* is correctly ordered, and I use this to order *vtkPoints*.
4. Finally I use ordered points and lines to construct a new vtkPolyData and
draw it.

My code block is shown below:

*// get marching squares of image data*
vtkSmartPointer<vtkMarchingSquares> segContour =
vtkSmartPointer<vtkMarchingSquares>::New();
segContour->SetInputData(segmentation); // segmentation is my image data
segContour->SetValue(0, 255);
segContour->Update();

*// use vtkStripper to order line segments in correct order*
vtkSmartPointer<vtkStripper> segPolyStripper =
vtkSmartPointer<vtkStripper>::New();
segPolyStripper->SetInputData(segContour->GetOutput());
segPolyStripper->Update();

*// order points according to ordered lines segments*
vtkSmartPointer<vtkCellArray> orderedLines =
vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkPoints> originPoints = vtkSmartPointer<vtkPoints>::New();
originPoints = segContour->GetOutput()->GetPoints();
orderedLines = segPolyStripper->GetOutput()->GetLines();
int numPts = originPoints->GetNumberOfPoints();
vtkSmartPointer<vtkPoints> orderedPoints =
vtkSmartPointer<vtkPoints>::New();

*// access data of vtkCellArray as order /dictionary/*
vtkIdType numCells = orderedLines->GetNumberOfCells();
vtkIdType cellLocation = 0; // the index into the cell array
vtkIdType numIds; // to hold the size of the cell
vtkIdType *pointIds; // to hold the ids in the cell
for (vtkIdType i = 0; i < numCells; i++) {
	orderedLines->GetCell(cellLocation, numIds, pointIds);
	pointIds[0], pointIds[1], pointIds[2];
	cellLocation += 1 + numIds;
}

*// order points according to /dictionary/ above*
for (vtkIdType i = 0; i < numPts - 1; i++) {
	vtkIdType pointId = pointIds[i];
	orderedPoints->InsertPoint(static_cast<vtkIdType>(i),
originPoints->GetPoint(pointId));
}

// construct a poly data with ordered points and lines
vtkSmartPointer<vtkPolyData> segPoly = vtkSmartPointer<vtkPolyData>::New();
segPoly->SetPoints(orderedPoints);
segPoly->SetLines(orderedLines);	

// draw the contour
contourWidget->Initialize(segPoly);



--
View this message in context: http://vtk.1045678.n5.nabble.com/Ordering-wrong-when-convert-vtkImageData-to-vtkPolyData-tp5742492p5742512.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list