[vtkusers] Write a PolygonPolydata

Nens nicolas.narbonne at gmail.com
Thu Jan 16 04:08:37 EST 2014


Hi,

I have a problem to write a png file with a PolygonPolyData.
I think, maybe, it is because my polydata isn't close.
Right now, when is use my program that only create a black png file. I don't
know how to add my polygon on this image (white polygon).
Here the code :

    vtkSmartPointer<vtkImageData> whiteImage =
            vtkSmartPointer<vtkImageData>::New();
    double bounds[6];
    polygonPolyData->GetBounds(bounds);
    double spacing[3]; // desired volume spacing
    spacing[0] = 0.5;
    spacing[1] = 0.5;
    spacing[2] = 0.5;
//    whiteImage->SetSpacing(spacing);

    // compute dimensions
    int dim[3];
    for (int i = 0; i < 3; i++)
    {
        dim[i] = static_cast<int>(ceil((bounds[i * 2 + 1] - bounds[i * 2])
/*/ spacing[i]*/));
    }
    whiteImage->SetDimensions(dim);
    std::cout<<"dim :"<<dim[0]<<"
"<<dim[1]<<" "<<dim[2]<<"
"<<std::endl;
    std::cout<<"bounds :"<<bounds[0]<<"
"<<bounds[1]<<" "<<bounds[2]<<"
"<<bounds[3]<<" "<<bounds[4]<<"
"<<bounds[5]<<std::endl;
    whiteImage->SetExtent(0, imageActor->GetBounds()[1], 0,
imageActor->GetBounds()[3], 0, 0);

    double origin[3];
    origin[0] = imageActor->GetBounds()[0] /*+ spacing[0]*/ / 2;
    origin[1] = imageActor->GetBounds()[2] /*+ spacing[1]*/ / 2;
    origin[2] = 0;
    whiteImage->SetOrigin(origin);

#if VTK_MAJOR_VERSION <= 5
    whiteImage->SetScalarTypeToUnsignedChar();
    whiteImage->AllocateScalars();
#else
    whiteImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
#endif
    // fill the image with foreground voxels:
    unsigned char inval = 255;
    unsigned char outval = 0;
    vtkIdType count = whiteImage->GetNumberOfPoints();
    std::cout<<"count :"<<count<<std::endl;
    for (vtkIdType i = 0; i < count; ++i)
    {
        whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
    }

    vtkFeatureEdges *edges = vtkFeatureEdges::New();
    edges->SetInput(polygonPolyData);
//    edges->FeatureEdgesOff();
    edges->NonManifoldEdgesOn();
    edges->BoundaryEdgesOn();
    edges->FeatureEdgesOff();
    edges->UpdateWholeExtent();
    edges->Update();
    cout << edges->GetOutput()->GetNumberOfCells() << endl;  // return a
number > 0

    // polygonal data --> image stencil:
    vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc =
      vtkSmartPointer<vtkPolyDataToImageStencil>::New();
    pol2stenc->SetTolerance(0); 
    pol2stenc->SetInput(polygonPolyData);
    pol2stenc->SetOutputOrigin(0,0,0);
//    pol2stenc->SetOutputSpacing(spacing);
    pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());
    pol2stenc->Update();



    // cut the corresponding white image and set the background:
    vtkSmartPointer<vtkImageStencil> imgstenc =
            vtkSmartPointer<vtkImageStencil>::New();
#if VTK_MAJOR_VERSION <= 5
    imgstenc->SetInput(whiteImage);
    imgstenc->SetStencil(pol2stenc->GetOutput());
#else
    imgstenc->SetInputData(whiteImage);
    imgstenc->SetStencilConnection(pol2stenc->GetOutputPort());
#endif
    imgstenc->ReverseStencilOff();
    imgstenc->SetBackgroundValue(outval);
    imgstenc->Update();

    vtkSmartPointer<vtkPNGWriter  > writer = vtkSmartPointer<vtkPNGWriter
>::New();

    std::string fileName = "./../maskDef.png" ;
    writer->SetFileName(fileName.c_str() );
    writer->SetInput(imgstenc->GetSurface());
    writer->Write();
    std::cout<<"write"<<std::endl;

Thank you.



--
View this message in context: http://vtk.1045678.n5.nabble.com/Write-a-PolygonPolydata-tp5725437.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list