1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#!/usr/bin/env python
from vtkmodules.vtkCommonCore import (
vtkFloatArray,
vtkPoints,
)
from vtkmodules.vtkCommonDataModel import (
vtkCellArray,
vtkPolyData,
)
from vtkmodules.vtkFiltersCore import vtkFeatureEdges
from vtkmodules.vtkFiltersModeling import vtkPolyDataPointSampler
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
# Create the RenderWindow, Renderer and interactive renderer
#
ren1 = vtkRenderer()
renWin = vtkRenderWindow()
renWin.SetMultiSamples(0)
renWin.AddRenderer(ren1)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Create a synthetic polydata with two triangles, a quad, a general polygon,
# and a triangle strip. The polydata also has point attributes.
#
pts = vtkPoints()
pts.SetNumberOfPoints(11)
pts.SetPoint(0, 0.0,0.0,0.0)
pts.SetPoint(1, 1.0,0.0,0.0)
pts.SetPoint(2, 0.0,1.0,0.0)
pts.SetPoint(3, 1.0,1.0,0.0)
pts.SetPoint(4, 0.0,2.0,0.0)
pts.SetPoint(5, 1.0,2.0,0.0)
pts.SetPoint(6, 0.0,3.0,0.0)
pts.SetPoint(7, 1.0,3.0,0.0)
pts.SetPoint(8, 0.5,3.5,0.0)
pts.SetPoint(9, 0.0,-1,0.0)
pts.SetPoint(10, 1.0,-1,0.0)
cells = vtkCellArray()
cells.InsertNextCell(3)
cells.InsertCellPoint(0)
cells.InsertCellPoint(1)
cells.InsertCellPoint(2)
cells.InsertNextCell(3)
cells.InsertCellPoint(2)
cells.InsertCellPoint(1)
cells.InsertCellPoint(3)
cells.InsertNextCell(4)
cells.InsertCellPoint(2)
cells.InsertCellPoint(3)
cells.InsertCellPoint(5)
cells.InsertCellPoint(4)
cells.InsertNextCell(5)
cells.InsertCellPoint(4)
cells.InsertCellPoint(5)
cells.InsertCellPoint(7)
cells.InsertCellPoint(8)
cells.InsertCellPoint(6)
strip = vtkCellArray()
strip.InsertNextCell(4)
strip.InsertCellPoint(10)
strip.InsertCellPoint(1)
strip.InsertCellPoint(9)
strip.InsertCellPoint(0)
scalars = vtkFloatArray()
scalars.SetNumberOfTuples(11)
scalars.SetTuple1(0,0.0)
scalars.SetTuple1(1,1.0)
scalars.SetTuple1(2,0.0)
scalars.SetTuple1(3,1.0)
scalars.SetTuple1(4,0.0)
scalars.SetTuple1(5,1.0)
scalars.SetTuple1(6,0.0)
scalars.SetTuple1(7,1.0)
scalars.SetTuple1(8,0.5)
scalars.SetTuple1(9,0.0)
scalars.SetTuple1(10,1.0)
# Assemble the polydata
pd = vtkPolyData()
pd.SetPoints(pts)
pd.SetPolys(cells)
pd.SetStrips(strip)
pd.GetPointData().SetScalars(scalars)
# Finally test the point sampler
# Note that generating points on edges and vertices is less than random
sampler = vtkPolyDataPointSampler()
sampler.SetInputData(pd)
sampler.SetDistance(0.025)
sampler.SetPointGenerationModeToRandom()
sampler.InterpolatePointDataOn()
sampler.GenerateVertexPointsOff()
sampler.GenerateEdgePointsOff()
sampler.GenerateInteriorPointsOn()
sampler.Update()
# Mapper and actor
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(sampler.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
# Produce an outline around the polygons
feat = vtkFeatureEdges()
feat.SetInputData(pd)
feat.BoundaryEdgesOn()
featMapper = vtkPolyDataMapper()
featMapper.SetInputConnection(feat.GetOutputPort())
featActor = vtkActor()
featActor.SetMapper(featMapper)
# Setup graphics stuff
ren1.AddActor(actor)
ren1.AddActor(featActor)
ren1.SetBackground(0,0,0)
renWin.SetSize(300,300)
renWin.Render()
iren.Start()
# --- end of script --
|