Getting 3D ImageData from polyData

Hi all,

I am trying to use vtk to load in an object stored in an .stl file, and fill a structured 3D grid with the volume of the object present in each grid cell.

(If there is an obvious way to do this which I am missing, which doesn’t use ImageData and polyData, that would also be great!)

I can load in the .stl files into a vtkPolyData, and that works fine. Then, given a vtkImageData filled with either volume or signed-distance, I can access the imageData fine. But it is going from vtkPolyData to vtkImageData that doesn’t work.

I tried using vtkSurfaceReconstructionFilter, then vtkImageResize and getting the data from there. This does work for simple shapes, but when I try a complex mesh like Orion Capsule | 3D Resources , the surface it produces looks awful - some parts of the domain is just filled with random mess. I feel like given that I already have a definition of the surface connectivity, doing a surface reconstruction from a point cloud is just not using that information? The reconstruction seems to fall down in areas that don’t have very many points, which makes sense.

Is there a better way to do this?

If anyone has any insight on the best way to do this I would be immensely grateful! (My current code is below)

Many thanks!

vtkSmartPointer<vtkPolyData> ReadPolyData(std::string const& fileName)
  vtkSmartPointer<vtkPolyData> polyData;
  std::string extension = "";
  if (fileName.find_last_of(".") != std::string::npos)
    extension = fileName.substr(fileName.find_last_of("."));
  // Make the extension lowercase
  std::transform(extension.begin(), extension.end(), extension.begin(),
  if (extension == ".ply")
    auto reader = vtkSmartPointer<vtkPLYReader>::New();
    polyData = reader->GetOutput();
  else if (extension == ".stl")
    auto reader = vtkSmartPointer<vtkSTLReader>::New();
    polyData = reader->GetOutput();

  return polyData;

STLReader(std::string inputFilename, const Real* dx, const Real* prob_lo, const Real* prob_hi)

    //dx stores cell size
    //prob_lo and and prob_hi store the coordinates of my 3D domain

    double bounds[6];

    for(int d=0 ; d<3 ; d++)
        bounds[2*d]   = prob_lo[d]  + dx[d]/2.0;
        bounds[2*d+1] = prob_hi[d] - dx[d]/2.0;

    AllPrint() << "Loading file"<< std::endl;

    auto polyData = ReadPolyData(inputFilename);

    // Construct the surface .
    vtkNew<vtkSurfaceReconstructionFilter> surfaceReconstruction;


    //Getting output

    vtkNew<vtkImageResize> scale;

    vtkNew<vtkImageBSplineInterpolator> interp;

    scale->SetOutputSpacing(dx[0], dx[1], dx[2]);


    // Get output Array
    vtkImageData* data = scale->GetOutput();

    float* dataIt = (float*)data->GetScalarPointer();

   //Copy into another array as required...