Ricky
Ricky

Reputation: 1

VTK marching cubes or contour for surface reconstruction using python

I am new to VTK python. I have a csv file with 8000 point (X, Y, Z) and corresponding 8000 signed distances (sdf)

I am trying to read the data and apply VTK filter to reconstruct the surface at sdf = 0.0, but I dont see any option to create surface at such a value. I have tried this using skimage.measure and pyvista. Both work very well but somehow VTK doesnt product desired input.



import numpy as np
import vtk

pointSource = vtk.vtkProgrammableSource()

def readPoints():
    output = pointSource.GetPolyDataOutput()
    points = vtk.vtkPoints()
    output.SetPoints(points)


    pts = np.loadtxt('sdf.csv',delimiter=',')
    for pi in pts:
        points.InsertNextPoint(pi[0], pi[1], pi[2])

pointSource.SetExecuteMethod(readPoints)


# Construct the surface and create isosurface.
surf = vtk.vtkSurfaceReconstructionFilter()
surf.SetInputConnection(pointSource.GetOutputPort())

cf = vtk.vtkContourFilter()
cf.SetInputConnection(surf.GetOutputPort())
cf.SetValue(0, 0.0)
cf.Update()
print(cf.GetNumberOfContours())

map = vtk.vtkPolyDataMapper()
map.SetInputConnection(cf.GetOutputPort())
map.ScalarVisibilityOff()

surfaceActor = vtk.vtkActor()
surfaceActor.SetMapper(map)
surfaceActor.GetProperty().SetDiffuseColor(1.0000, 0.3882, 0.2784)
surfaceActor.GetProperty().SetSpecularColor(1, 1, 1)
surfaceActor.GetProperty().SetSpecular(.4)
surfaceActor.GetProperty().SetSpecularPower(50)

# Create the RenderWindow, Renderer and both Actors
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Add the actors to the renderer, set the background and size
ren.AddActor(surfaceActor)
ren.SetBackground(1, 1, 1)
renWin.SetSize(400, 400)
ren.GetActiveCamera().SetFocalPoint(0, 0, 0)
ren.GetActiveCamera().SetPosition(1, 0, 0)
ren.GetActiveCamera().SetViewUp(0, 0, 1)
ren.ResetCamera()
ren.GetActiveCamera().Azimuth(20)
ren.GetActiveCamera().Elevation(30)
ren.GetActiveCamera().Dolly(1.2)
ren.ResetCameraClippingRange()

iren.Initialize()
renWin.Render()
iren.Start()

Incorrect output

The data of 8000 points with sdfs is here https://drive.google.com/file/d/1FiAjQc6BR_3LAkaNR3qbEfK4odAm30ji/view?usp=sharing

Also Contour or marching cube feature is very slow compared to skimage and pyvista. Is there anything wrong with my code?

Expected output is a Surface, representing a rectangular box around the red colored points point data and sdf

Also I am expecting vtk.marchingCube to be very fast. But it takes lot of time to get output for large point cloud

Upvotes: 0

Views: 500

Answers (0)

Related Questions