[vtkusers] Remeshing vtkPolyData with vtkMarchingCubes

Sebastian seb.an at icloud.com
Fri Mar 31 15:56:43 EDT 2017


I worked it out, here my solution:

data is the input as vtkPolyData, for example a sphere.

def marchingCubes(size):

	whiteImage = vtk.vtkImageData()
        bounds = [0]*6
        data.GetBounds(bounds)
        spacing = [0]*3 # desired volume spacing
        spacing[0] = size
        spacing[1] = size
        spacing[2] = size

        whiteImage.SetSpacing(spacing)

        dim = [0]*3
        for i in range(3):
            dim[i] = int(math.ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i])) + 1
            if (dim[i] < 1):
                dim[i] = 1
        whiteImage.SetDimensions(dim)
        whiteImage.SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1)
        origin = [0]*3
        origin[0] = bounds[0]
        origin[1] = bounds[2]
        origin[2] = bounds[4]
        whiteImage.SetOrigin(origin)

        whiteImage.AllocateScalars(vtk.VTK_UNSIGNED_CHAR,1)

        inval = 255
        outval = 0

        count = whiteImage.GetNumberOfPoints()
        i = 0
        while i < count:
            whiteImage.GetPointData().GetScalars().SetTuple1(i, inval)
            i = i + 1


        pol2stenc = vtk.vtkPolyDataToImageStencil()
        pol2stenc.SetInputData(data)

        pol2stenc.SetOutputOrigin(origin)
        pol2stenc.SetOutputSpacing(spacing)
        pol2stenc.SetOutputWholeExtent(whiteImage.GetExtent())
        pol2stenc.Update()

        imgstenc = vtk.vtkImageStencil()
        imgstenc.SetInputData(whiteImage)
        imgstenc.SetStencilConnection(pol2stenc.GetOutputPort())
        imgstenc.ReverseStencilOff()
        imgstenc.SetBackgroundValue(outval)
        imgstenc.Update()

        cf = vtk.vtkMarchingCubes()
        cf.SetInputData(imgstenc.GetOutput())
        cf.GenerateValues(1,10,10)
        cf.Update()

        reverse = vtk.vtkReverseSense()
        reverse.SetInputConnection(cf.GetOutputPort())
        reverse.ReverseCellsOn()
        reverse.ReverseNormalsOn()

        reverse.Update()

        data = reverse.GetOutput()


Hope it helps anyone.
Cheers!


Am 29.03.2017 um 02:03 schrieb Se An <seb.an at icloud.com>:

Hi everyone,

I’m new with vtk and I’m trying to remesh a vtkPolyData with the vtkMarchingCubes-Algorithm.

To do this I’m using the code described here: http://www.vtk.org/Wiki/VTK/Examples/Cxx/Modelling/MarchingCubes <http://www.vtk.org/Wiki/VTK/Examples/Cxx/Modelling/MarchingCubes>


def marchingCubes(size):

        volume = vtk.vtkImageData()

        bounds = [0.0, 0.0, 0.0 , 0.0, 0.0, 0.0]

        data.GetBounds(bounds)

        iBounds = 0
        while iBounds < 6:
            range = bounds[iBounds+1] - bounds[iBounds]
            bounds[iBounds]   = bounds[iBounds] - .0001 * range
            bounds[iBounds+1] = bounds[iBounds+1] + .0001 * range
            iBounds = iBounds + 2

        voxelModeller = vtk.vtkVoxelModeller()
        voxelModeller.SetSampleDimensions(size,size,size)   #Set cube size
        voxelModeller.SetModelBounds(bounds)
        voxelModeller.SetScalarTypeToFloat()
        voxelModeller.SetMaximumDistance(.1)

        voxelModeller.SetInputData(data)
        voxelModeller.Update()

        isoValue = 0.01 #0.001
        volume.DeepCopy(voxelModeller.GetOutput())

        surface = vtk.vtkMarchingCubes()
        if vtk.VTK_MAJOR_VERSION <= 5:
            surface.SetInput(volume)
        else:
            surface.SetInputData(volume)

        surface.ComputeScalarsOff()
        surface.SetValue(0, isoValue)

        surface.Update()

        data = surface.GetOutput()



Here my problem:
My input has just one Surface/Layer, the output has two layers as you see at the screenshot attached.
Is it possible to create an output with a remeshed polydata with just one layer?

Thanks in advance,
Sebastian


<Input.jpeg><Output.jpeg>
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170331/fe7e05de/attachment.html>


More information about the vtkusers mailing list