Ejemplo n.º 1
0
 def cutter(self):
     maxLayers = 0
     for model in self.modelDict.values():
         if len(model.layerValues) > maxLayers:
             maxLayers = len(model.layerValues)
             
     for model in self.modelDict.values():
         i = self.currentIndex
         if i < 0:
             i = 0
             self.currentIndex = -1
         elif i > len(model.layerValues) - 1:
             i = len(model.layerValues) - 1 # The last layer
             if len(model.layerValues) > maxLayers:
                 self.currentIndex = maxLayers
             
         for actor in [model._actor, model._slice_actor, model._support_actor, model._base_actor]:
             if actor is not None:
                 actor.GetProperty().SetOpacity(0)
                 
         for polydata in [model._slice_vtkpolydata, model._support_vtkpolydata, model._base_vtkpolydata]:
             if polydata is not None:
                 plane = vtk.vtkPlane() 
                 plane.SetOrigin(0, 0, model.layerValues[i] -0.005) # some errors if path height is 0.005
                 plane.SetNormal(0, 0, 1)
                 window = vtk.vtkImplicitWindowFunction()
                 window.SetImplicitFunction(plane)
                 try:
                     pathHeight = float(self.toolDict[str(model._modelMaterial)].pathHeight)
                 except:
                     pathHeight = model.pathHeight # error fix in gcode import
                 window.SetWindowRange(0, pathHeight)
                 clipper = vtk.vtkClipPolyData()
                 clipper.AddInput(polydata) 
                 clipper.SetClipFunction(window)
                 clipper.GenerateClippedOutputOn()
                 if self.tubeOn:
                     try:
                         clipActor = self.tubeView(clipper, float(self.toolDict[str(model._modelMaterial)].pathWidth))
                     except:
                         clipActor = self.tubeView(clipper, pathHeight) # error fix in gcode import
                 else:
                     clipMapper = vtk.vtkPolyDataMapper()
                     clipMapper.SetInputConnection(clipper.GetOutputPort())
                     clipActor = vtk.vtkActor()
                     clipActor.SetMapper(clipMapper)
                 self.ren.AddActor(clipActor)
                 self.newActors.append(clipActor)
Ejemplo n.º 2
0
    def legosurface(self, vmin=None, vmax=None, invert=False, cmap='afmhot_r'):
        """
        Represent a ``Volume`` as lego blocks (voxels).
        By default colors correspond to the volume's scalar.
        Returns an ``Mesh``.

        :param float vmin: the lower threshold, voxels below this value are not shown.
        :param float vmax: the upper threshold, voxels above this value are not shown.
        :param str cmap: color mapping of the scalar associated to the voxels.

        |legosurface| |legosurface.py|_
        """
        dataset = vtk.vtkImplicitDataSet()
        dataset.SetDataSet(self._imagedata)
        window = vtk.vtkImplicitWindowFunction()
        window.SetImplicitFunction(dataset)

        srng = list(self._imagedata.GetScalarRange())
        if vmin is not None:
            srng[0] = vmin
        if vmax is not None:
            srng[1] = vmax
        tol = 0.00001*(srng[1]-srng[0])
        srng[0] -= tol
        srng[1] += tol
        window.SetWindowRange(srng)

        extract = vtk.vtkExtractGeometry()
        extract.SetInputData(self._imagedata)
        extract.SetImplicitFunction(window)
        extract.SetExtractInside(invert)
        extract.ExtractBoundaryCellsOff()
        extract.Update()

        gf = vtk.vtkGeometryFilter()
        gf.SetInputData(extract.GetOutput())
        gf.Update()

        a = Mesh(gf.GetOutput()).lw(0.1).flat()
        scalars = a.getPointArray()
        if scalars is None:
            print("Error in legosurface(): no scalars found!")
            return a
        a.pointColors(scalars, vmin=srng[0], vmax=srng[1], cmap=cmap)
        a.mapPointsToCells()
        return a
Ejemplo n.º 3
0
# volume. This example differs from the previous clipVolume.tcl examples
# in that it uses the slower ordered triangulator and generates clip scalars.
# Quadric definition
quadric = vtk.vtkQuadric()
quadric.SetCoefficients([.5,1,.2,0,.1,0,0,.2,0,0])
sample = vtk.vtkSampleFunction()
sample.SetSampleDimensions(20,20,20)
sample.SetImplicitFunction(quadric)
sample.ComputeNormalsOff()
sample.Update()
# Program a bandpass filter to clip a range of data. What we do is transform the
# scalars so that values laying betweeen (minRange,maxRange) are >= 0.0; all
# others are < 0.0,
dataset = vtk.vtkImplicitDataSet()
dataset.SetDataSet(sample.GetOutput())
window = vtk.vtkImplicitWindowFunction()
window.SetImplicitFunction(dataset)
window.SetWindowRange(0.5,1.0)
# Generate tetrahedral mesh
clip = vtk.vtkClipVolume()
clip.SetInputConnection(sample.GetOutputPort())
clip.SetClipFunction(window)
clip.SetValue(0.0)
clip.GenerateClippedOutputOff()
clip.Mixed3DCellGenerationOff()
gf = vtk.vtkGeometryFilter()
#  gf SetInput [clip GetClippedOutput]
gf.SetInputConnection(clip.GetOutputPort())
clipMapper = vtk.vtkPolyDataMapper()
clipMapper.SetInputConnection(gf.GetOutputPort())
clipMapper.ScalarVisibilityOn()
Ejemplo n.º 4
0
# Example demonstrates how to generate a 3D tetrahedra mesh from a volume. This example
# differs from clipVolume.tcl in that the mesh is generated within a range of contour values.
#
# Quadric definition
quadric = vtk.vtkQuadric()
quadric.SetCoefficients([.5,1,.2,0,.1,0,0,.2,0,0])
sample = vtk.vtkSampleFunction()
sample.SetSampleDimensions(20,20,20)
sample.SetImplicitFunction(quadric)
sample.ComputeNormalsOff()
# Program a bandpass filter to clip a range of data. What we do is transform the
# scalars so that values laying betweeen (minRange,maxRange) are >= 0.0; all
# others are < 0.0,
dataset = vtk.vtkImplicitDataSet()
dataset.SetDataSet(sample.GetOutput())
window = vtk.vtkImplicitWindowFunction()
window.SetImplicitFunction(dataset)
window.SetWindowRange(0.5,1.0)
# Generate tetrahedral mesh
clip = vtk.vtkClipVolume()
clip.SetInputConnection(sample.GetOutputPort())
clip.SetClipFunction(window)
clip.SetValue(0.0)
clip.GenerateClippedOutputOff()
clipMapper = vtk.vtkDataSetMapper()
clipMapper.SetInputConnection(clip.GetOutputPort())
clipMapper.ScalarVisibilityOff()
clipActor = vtk.vtkActor()
clipActor.SetMapper(clipMapper)
clipActor.GetProperty().SetColor(.8,.4,.4)
# Create outline
Ejemplo n.º 5
0
def main():
    value = 2.0
    colors = vtk.vtkNamedColors()

    sphere = vtk.vtkSphere()
    sphere.SetRadius(0.2)

    # implicitFunction = vtk.vtkSuperquadric()
    # implicitFunction.SetPhiRoundness(2.5)
    # implicitFunction.SetThetaRoundness(.5)
    noiseFunction = vtk.vtkPerlinNoise()
    noiseFunction.SetAmplitude(2)
    noiseFunction2 = vtk.vtkPerlinNoise()
    noiseFunction2.SetAmplitude(3)
    noiseFunction2.SetFrequency(2,2,2)

    window = vtk.vtkImplicitWindowFunction()
    window.SetImplicitFunction(noiseFunction2)
    window.SetWindowRange(0, 10)

    noiseSum = vtk.vtkImplicitSum()
    noiseSum.AddFunction(noiseFunction)
    # noiseSum.AddFunction(window)
    noiseSum.AddFunction(sphere)

    # noiseFunction.SetFrequency([5, 5, 5])
    # window = vtk.vtkImplicitWindowFunction()
    # window.SetImplicitFunction(halo)
    # window.SetWindowRange(-0, 10)

    # Sample the function.
    sample = vtk.vtkSampleFunction()
    sample.SetSampleDimensions(30,30,30)
    sample.SetImplicitFunction(noiseSum)

    xmin, xmax, ymin, ymax, zmin, zmax = -value, value, -value, value, -value, value
    sample.SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax)

    # Create the 0 isosurface.
    contours = vtk.vtkContourFilter()
    contours.SetInputConnection(sample.GetOutputPort())
    contours.GenerateValues(1, 2.0, 2.0)

    # Map the contours to graphical primitives.
    contourMapper = vtk.vtkPolyDataMapper()
    contourMapper.SetInputConnection(contours.GetOutputPort())
    contourMapper.SetScalarRange(0.0, 1.2)
    contourMapper.ScalarVisibilityOff()

    # Create an actor for the contours.
    contourActor = vtk.vtkActor()
    contourActor.SetMapper(contourMapper)
    contourActor.GetProperty().SetColor(colors.GetColor3d("salmon"))

    # Create a box around the function to indicate the sampling volume. 

    #Create outline.
    outline = vtk.vtkOutlineFilter()
    outline.SetInputConnection(sample.GetOutputPort())

    # Map it to graphics primitives.
    outlineMapper = vtk.vtkPolyDataMapper()
    outlineMapper.SetInputConnection(outline.GetOutputPort())

    # Create an actor.
    outlineActor = vtk.vtkActor()
    outlineActor.SetMapper(outlineMapper)
    outlineActor.GetProperty().SetColor(0,0,0)

    # Visualize.
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderWindow)

    renderer.AddActor(contourActor)
    renderer.AddActor(outlineActor)
    renderer.SetBackground(colors.GetColor3d("powder_blue"))

    # Enable user interface interactor
    renderWindow.Render()

    # Sign up to receive TimerEvent
    cb = vtkTimerCallback()
    cb.noise = noiseFunction
    interactor.AddObserver('TimerEvent', cb.execute)
    timerId = interactor.CreateRepeatingTimer(1);

    interactor.Start()