Beispiel #1
0
def load_file(path):
    """from path load stl and voxelize it
    
    Arguments:
        path {string} -- file path
    
    Returns:
        vtkImageData -- vtk voxel data type
    """

    stl_reader = vtk.vtkSTLReader()
    stl_reader.SetFileName(path)
    stl_reader.Update()

    bounds = stl_reader.GetOutput().GetBounds()

    vox_modeller = vtk.vtkVoxelModeller()
    vox_modeller.SetSampleDimensions(64, 64, 64)
    vox_modeller.SetModelBounds(bounds)
    vox_modeller.SetScalarTypeToInt()
    vox_modeller.SetMaximumDistance(.1)

    vox_modeller.SetInputConnection(stl_reader.GetOutputPort())
    vox_modeller.Update()

    return vox_modeller.GetOutput()
Beispiel #2
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkVoxelModeller(), 'Processing.',
         ('vtkDataSet',), ('vtkImageData',),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
Beispiel #3
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(self,
                                       module_manager,
                                       vtk.vtkVoxelModeller(),
                                       'Processing.', ('vtkDataSet', ),
                                       ('vtkImageData', ),
                                       replaceDoc=True,
                                       inputFunctions=None,
                                       outputFunctions=None)
    def __init__(self, parent = None):
        super(VTKFrame, self).__init__(parent)

        self.vtkWidget = QVTKRenderWindowInteractor(self)
        vl = QtGui.QVBoxLayout(self)
        vl.addWidget(self.vtkWidget)
        vl.setContentsMargins(0, 0, 0, 0)
 
        self.ren = vtk.vtkRenderer()
        self.ren.SetBackground(0.1, 0.2, 0.4)
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        sphereSource = vtk.vtkSphereSource()
        sphereSource.SetPhiResolution(20)
        sphereSource.SetThetaResolution(20)
        sphereSource.Update()

        bounds = [1, 1, 1, 1, 1, 1]
        sphereSource.GetOutput().GetBounds(bounds)
        for i in range(0, 6, 2):
            range_ = bounds[i+1] - bounds[i]
            bounds[i] = bounds[i] - 0.1 * range_
            bounds[i+1] = bounds[i+1] + 0.1 * range_

        voxelModeller = vtk.vtkVoxelModeller()
        voxelModeller.SetSampleDimensions(50, 50, 50)
        voxelModeller.SetModelBounds(bounds)
        voxelModeller.SetScalarTypeToFloat()
        voxelModeller.SetMaximumDistance(0.1)

        voxelModeller.SetInputConnection(sphereSource.GetOutputPort())
        voxelModeller.Update()

        volume = vtk.vtkImageData()
        volume.DeepCopy(voxelModeller.GetOutput())

        surface = vtk.vtkMarchingCubes()
        surface.SetInput(volume)
        surface.SetValue(0, 0.5)
 
        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(surface.GetOutputPort())
 
        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
 
        self.ren.AddActor(actor)
        self.ren.ResetCamera()

        self._initialized = False
Beispiel #5
0
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Now create the RenderWindow, Renderer and Interactor
#
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

sphereModel = vtk.vtkSphereSource()
sphereModel.SetThetaResolution(10)
sphereModel.SetPhiResolution(10)

voxelModel = vtk.vtkVoxelModeller()
voxelModel.SetInputConnection(sphereModel.GetOutputPort())
voxelModel.SetSampleDimensions(21, 21, 21)
voxelModel.SetModelBounds(-1.5, 1.5, -1.5, 1.5, -1.5, 1.5)
voxelModel.SetScalarTypeToBit()
voxelModel.SetForegroundValue(1)
voxelModel.SetBackgroundValue(0)

#
# If the current directory is writable, then test the writers
#
try:
    channel = open("voxelModel.vtk", "wb")
    channel.close()

    aWriter = vtk.vtkDataSetWriter()
Beispiel #6
0
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Now create the RenderWindow, Renderer and Interactor
#
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

sphereModel = vtk.vtkSphereSource()
sphereModel.SetThetaResolution(10)
sphereModel.SetPhiResolution(10)

voxelModel = vtk.vtkVoxelModeller()
voxelModel.SetInputConnection(sphereModel.GetOutputPort())
voxelModel.SetSampleDimensions(21, 21, 21)
voxelModel.SetModelBounds(-1.5, 1.5, -1.5, 1.5, -1.5, 1.5)
voxelModel.SetScalarTypeToBit()
voxelModel.SetForegroundValue(1)
voxelModel.SetBackgroundValue(0)

#
# If the current directory is writable, then test the writers
#
try:
    channel = open("voxelModel.vtk", "wb")
    channel.close()

    aWriter = vtk.vtkDataSetWriter()
def main():
    # vtkFlyingEdges3D was introduced in VTK >= 8.2
    use_flying_edges = vtk_version_ok(8, 2, 0)

    colors = vtk.vtkNamedColors()

    dicom_dir, iso_value = get_program_parameters()
    if iso_value is None and dicom_dir is not None:
        print('An ISO value is needed.')
        return ()

    volume = vtk.vtkImageData()
    if dicom_dir is None:
        sphere_source = vtk.vtkSphereSource()
        sphere_source.SetPhiResolution(20)
        sphere_source.SetThetaResolution(20)
        sphere_source.Update()

        bounds = list(sphere_source.GetOutput().GetBounds())
        for i in range(0, 6, 2):
            dist = bounds[i + 1] - bounds[i]
            bounds[i] = bounds[i] - 0.1 * dist
            bounds[i + 1] = bounds[i + 1] + 0.1 * dist
        voxel_modeller = vtk.vtkVoxelModeller()
        voxel_modeller.SetSampleDimensions(50, 50, 50)
        voxel_modeller.SetModelBounds(bounds)
        voxel_modeller.SetScalarTypeToFloat()
        voxel_modeller.SetMaximumDistance(0.1)

        voxel_modeller.SetInputConnection(sphere_source.GetOutputPort())
        voxel_modeller.Update()
        iso_value = 0.5
        volume.DeepCopy(voxel_modeller.GetOutput())
    else:
        reader = vtk.vtkDICOMImageReader()
        reader.SetDirectoryName(dicom_dir)
        reader.Update()
        volume.DeepCopy(reader.GetOutput())

    if use_flying_edges:
        try:
            surface = vtk.vtkFlyingEdges3D()
        except AttributeError:
            surface = vtk.vtkMarchingCubes()
    else:
        surface = vtk.vtkMarchingCubes()
    surface.SetInputData(volume)
    surface.ComputeNormalsOn()
    surface.SetValue(0, iso_value)

    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('DarkSlateGray'))

    render_window = vtk.vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetWindowName('MarchingCubes')

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(render_window)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(surface.GetOutputPort())
    mapper.ScalarVisibilityOff()

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(colors.GetColor3d('MistyRose'))

    renderer.AddActor(actor)

    render_window.Render()
    interactor.Start()