def PlaneSphereActors():
    ps = vtk.vtkPlaneSource()
    ps.SetXResolution(10)
    ps.SetYResolution(10)

    ss = vtk.vtkSphereSource()
    ss.SetRadius(0.3)

    group = vtk.vtkMultiBlockDataGroupFilter()
    group.AddInputConnection(ps.GetOutputPort())
    group.AddInputConnection(ss.GetOutputPort())

    ag = vtk.vtkRandomAttributeGenerator()
    ag.SetInputConnection(group.GetOutputPort())
    ag.GenerateCellScalarsOn()
    ag.AttributesConstantPerBlockOn()

    n = vtk.vtkPolyDataNormals()
    n.SetInputConnection(ag.GetOutputPort())
    n.Update()

    actors = []
    it = n.GetOutputDataObject(0).NewIterator()
    it.InitTraversal()
    while not it.IsDoneWithTraversal():
        pm = vtk.vtkPolyDataMapper()
        pm.SetInputData(it.GetCurrentDataObject())

        a = vtk.vtkActor()
        a.SetMapper(pm)
        actors.append(a)
        it.GoToNextItem()
    return actors
def PlaneSphereActors():
    ps = vtk.vtkPlaneSource()
    ps.SetXResolution(10)
    ps.SetYResolution(10)

    ss = vtk.vtkSphereSource()
    ss.SetRadius (0.3)

    group = vtk.vtkMultiBlockDataGroupFilter()
    group.AddInputConnection(ps.GetOutputPort())
    group.AddInputConnection(ss.GetOutputPort())

    ag = vtk.vtkRandomAttributeGenerator()
    ag.SetInputConnection(group.GetOutputPort())
    ag.GenerateCellScalarsOn()
    ag.AttributesConstantPerBlockOn()

    n = vtk.vtkPolyDataNormals()
    n.SetInputConnection(ag.GetOutputPort())
    n.Update ();

    actors = []
    it = n.GetOutputDataObject(0).NewIterator()
    it.InitTraversal()
    while not it.IsDoneWithTraversal():
        pm = vtk.vtkPolyDataMapper()
        pm.SetInputData(it.GetCurrentDataObject())

        a = vtk.vtkActor()
        a.SetMapper(pm)
        actors.append (a)
        it.GoToNextItem()
    return actors
Beispiel #3
0
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Create a synthetic source: sample a sphere across a volume
sphere = vtk.vtkSphere()
sphere.SetCenter(0.0, 0.0, 0.0)
sphere.SetRadius(0.25)

sample = vtk.vtkSampleFunction()
sample.SetImplicitFunction(sphere)
sample.SetModelBounds(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5)
sample.SetSampleDimensions(res, res, res)
sample.Update()

# Adds random attributes
random = vtk.vtkRandomAttributeGenerator()
random.SetGenerateCellScalars(True)
random.SetInputConnection(sample.GetOutputPort())

# Convert the image data to unstructured grid
extractionSphere = vtk.vtkSphere()
extractionSphere.SetRadius(100)
extractionSphere.SetCenter(0, 0, 0)
extract = vtk.vtkExtractGeometry()
extract.SetImplicitFunction(extractionSphere)
extract.SetInputConnection(random.GetOutputPort())
extract.Update()

# The cut plane
plane = vtk.vtkPlane()
plane.SetOrigin(0, 0, 0)
import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

res = 100
# Test the volume constraint. Create "planar" data with jittery
# z-direction coordinates. Decimate the data with and without
# planar constraints and compare.
#
ps = vtk.vtkPlaneSource()
ps.SetResolution(res,res)

tf = vtk.vtkTriangleFilter()
tf.SetInputConnection(ps.GetOutputPort())

attr = vtk.vtkRandomAttributeGenerator()
attr.SetInputConnection(tf.GetOutputPort())
attr.GeneratePointScalarsOn()
attr.SetMinimumComponentValue(-0.05)
attr.SetMaximumComponentValue(0.05)

# This jitters the geometry
warp = vtk.vtkWarpScalar()
warp.SetInputConnection(attr.GetOutputPort())
warp.SetScaleFactor(0.02)

# Decimator without volume constraint
deci = vtk.vtkQuadricDecimation()
deci.SetInputConnection(warp.GetOutputPort())
deci.SetTargetReduction(.95)
deci.AttributeErrorMetricOn()
Beispiel #5
0
extr.AddCellType(vtk.VTK_HEXAHEDRON)
extr.Update()
print("Number of hexes: {0}".format(extr.GetOutput().GetNumberOfCells()))
if extr.GetOutput().GetNumberOfCells() != 0:
    error = 1

# Generate tetrahedral mesh. The side effect of the clip filter
# is to produce tetrahedra.
clip = vtk.vtkClipVolume()
clip.SetInputConnection(sample.GetOutputPort())
clip.SetValue(-10.0)
clip.GenerateClippedOutputOff()
clip.Update()

# Create point and cell data arrays to verify whether they are properly copied
arrayGenerator = vtk.vtkRandomAttributeGenerator()
arrayGenerator.SetInputConnection(clip.GetOutputPort())
arrayGenerator.SetGenerateCellScalars(1)
arrayGenerator.SetGeneratePointScalars(1)
arrayGenerator.Update()

# Extract tetra cells as unstructured grid - should be 5*res*res*res tets
extr.SetInputConnection(arrayGenerator.GetOutputPort())
extr.AddCellType(vtk.VTK_TETRA)
extr.Update()
print("Number of tets: {0}".format(extr.GetOutput().GetNumberOfCells()))
if extr.GetOutput().GetNumberOfCells() != 5*res*res*res:
    error = 1

# Check number of points
if extr.GetOutput().GetNumberOfPoints() != 9261:
map1 = vtk.vtkImageSliceMapper()
map1.BorderOn()
map1.SliceAtFocalPointOn()
map1.SliceFacesCameraOn()
map1.SetInputConnection(dens1.GetOutputPort())

slice1 = vtk.vtkImageSlice()
slice1.SetMapper(map1)
slice1.GetProperty().SetColorWindow(vrange[1]-vrange[0])
slice1.GetProperty().SetColorLevel(0.5*(vrange[0]+vrange[1]))

# Generate density field from points
# Use fixed radius and weighted point density and volume normalized density
# First need to generate some scalar attributes (weights)
weights = vtk.vtkRandomAttributeGenerator()
weights.SetInputConnection(clipper.GetOutputPort())
weights.SetMinimumComponentValue(0.25)
weights.SetMaximumComponentValue(1.75)
weights.GenerateAllDataOff()
weights.GeneratePointScalarsOn()

dens2 = vtk.vtkPointDensityFilter()
dens2.SetInputConnection(weights.GetOutputPort())
dens2.SetSampleDimensions(res,res,res)
dens2.SetDensityEstimateToFixedRadius()
dens2.SetRadius(0.05)
#dens2.SetDensityEstimateToRelativeRadius()
dens2.SetRelativeRadius(2.5)
dens2.SetDensityFormToVolumeNormalized()
dens2.ScalarWeightingOn()
Beispiel #7
0
map1 = vtk.vtkImageSliceMapper()
map1.BorderOn()
map1.SliceAtFocalPointOn()
map1.SliceFacesCameraOn()
map1.SetInputConnection(dens1.GetOutputPort())

slice1 = vtk.vtkImageSlice()
slice1.SetMapper(map1)
slice1.GetProperty().SetColorWindow(vrange[1] - vrange[0])
slice1.GetProperty().SetColorLevel(0.5 * (vrange[0] + vrange[1]))

# Generate density field from points
# Use fixed radius and weighted point density and volume normalized density
# First need to generate some scalar attributes (weights)
weights = vtk.vtkRandomAttributeGenerator()
weights.SetInputConnection(clipper.GetOutputPort())
weights.SetMinimumComponentValue(0.25)
weights.SetMaximumComponentValue(1.75)
weights.GenerateAllDataOff()
weights.GeneratePointScalarsOn()

dens2 = vtk.vtkPointDensityFilter()
dens2.SetInputConnection(weights.GetOutputPort())
dens2.SetSampleDimensions(res, res, res)
dens2.SetDensityEstimateToFixedRadius()
dens2.SetRadius(0.05)
#dens2.SetDensityEstimateToRelativeRadius()
dens2.SetRelativeRadius(2.5)
dens2.SetDensityFormToVolumeNormalized()
dens2.ScalarWeightingOn()
Beispiel #8
0
def warp_surface(args):
    print("warp the surface ")
    
    reader = vmtkscripts.vmtkSurfaceReader()
    reader.InputFileName = args.surface
    reader.Execute()
    Surface = reader.Surface    

    narrays = Surface.GetPointData().GetNumberOfArrays()
    has_normals = False
    for i in range(narrays):
        if (  Surface.GetPointData().GetArrayName(i) == "Normals"):
            has_normals = True
            break

    if(has_normals):
        normals = Surface
        print("already have")
    else:
        get_normals = vtk.vtkPolyDataNormals()
        get_normals.SetInputData(Surface)
        get_normals.SetFeatureAngle(30.0) # default
        get_normals.SetSplitting(True)
        get_normals.Update()
        get_normals.GetOutput().GetPointData().SetActiveVectors("Normals")
        normals = get_normals.GetOutput()
        print("normals generated")

    random = vtk.vtkRandomAttributeGenerator()
    random.SetInputData(normals)
    random.SetDataTypeToDouble()
    random.GeneratePointScalarsOn	()
    random.SetComponentRange(-0.5, 0.5)
    random.Update()
    
    #n = random.GetOutput().GetPointData().GetNumberOfArrays()
    #for i in range(n):
        #print(random.GetOutput().GetPointData().GetArrayName(i))
    
    calc = vtk.vtkArrayCalculator()
    calc.SetInputConnection(random.GetOutputPort())
    calc.AddScalarArrayName("RandomPointScalars", 0)
    calc.AddVectorArrayName("Normals", 0, 1, 2)
    calc.SetFunction("Normals * RandomPointScalars")
    calc.SetResultArrayName("RandomLengthNormalVectors")
    calc.Update()
    
    
    warp = vtk.vtkWarpVector()
    warp.SetInputConnection(calc.GetOutputPort())
    warp.SetInputArrayToProcess(0, 0, 0,
                                vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS,
                                "RandomLengthNormalVectors");
    warp.SetScaleFactor(args.fuzz_scale)
    warp.Update()


    writer = vmtkscripts.vmtkSurfaceWriter()
    writer.OutputFileName = args.file_out
    writer.Input = warp.GetOutput()
    writer.Execute()