[vtkusers] Plot Tubular Paths from xyz

Eric Petersen peer9802 at gmail.com
Tue Aug 8 09:52:25 EDT 2017


Below is a "TubesFromSpline" example that works with Python 3.6 and VTK 7. 

 
import vtk
 
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.SetRenderWindow(renWin)
iren.Initialize()
 
 
points = vtk.vtkPoints()
points.InsertPoint(0, 1, 0, 0)
points.InsertPoint(1, 2, 0, 0)
points.InsertPoint(2, 3, 1, 0)
points.InsertPoint(3, 4, 1, 0)
points.InsertPoint(4, 5, 0, 0)
points.InsertPoint(5, 6, 0, 0)
 
# Fit a spline to the points
spline = vtk.vtkParametricSpline()
spline.SetPoints(points)
functionSource = vtk.vtkParametricFunctionSource()
functionSource.SetParametricFunction(spline)
functionSource.SetUResolution(10 * points.GetNumberOfPoints())
functionSource.Update()
 
# Interpolate the scalars
interpolatedRadius = vtk.vtkTupleInterpolator()
interpolatedRadius.SetInterpolationTypeToLinear()
interpolatedRadius.SetNumberOfComponents(1)
interpolatedRadius.AddTuple(0, [0.2,]) 
interpolatedRadius.AddTuple(1, (0.2,))
interpolatedRadius.AddTuple(2, (0.2,))
interpolatedRadius.AddTuple(3, (0.1,))
interpolatedRadius.AddTuple(4, (0.1,))
interpolatedRadius.AddTuple(5, (0.1,))
 
# Generate the radius scalars
tubeRadius = vtk.vtkDoubleArray()
n = functionSource.GetOutput().GetNumberOfPoints()
tubeRadius.SetNumberOfTuples(n)
tubeRadius.SetName("TubeRadius")
 
tMin = interpolatedRadius.GetMinimumT()
tMax = interpolatedRadius.GetMaximumT()
for i in range(n):
    t = (tMax - tMin) / (n - 1) * i + tMin
    r = list([1.0])
    interpolatedRadius.InterpolateTuple(t, r) 
    tubeRadius.SetTuple1(i, r[0])
 
# Add the scalars to the polydata
tubePolyData = functionSource.GetOutput()
tubePolyData.GetPointData().AddArray(tubeRadius)
tubePolyData.GetPointData().SetActiveScalars("TubeRadius")
 
# Create the tubes
tuber = vtk.vtkTubeFilter()
tuber.SetInputData(tubePolyData)
tuber.SetNumberOfSides(20)
tuber.SetVaryRadiusToVaryRadiusByAbsoluteScalar()
 
# Setup actors and mappers
lineMapper = vtk.vtkPolyDataMapper()
lineMapper.SetInputData(tubePolyData)
lineMapper.SetScalarRange(tubePolyData.GetScalarRange())
 
tubeMapper = vtk.vtkPolyDataMapper()
tubeMapper.SetInputConnection(tuber.GetOutputPort())
tubeMapper.SetScalarRange(tubePolyData.GetScalarRange())
 
lineActor = vtk.vtkActor()
lineActor.SetMapper(lineMapper)
tubeActor = vtk.vtkActor()
tubeActor.SetMapper(tubeMapper)
 
ren.AddActor(tubeActor)
ren.AddActor(lineActor)
 
ren.SetBackground(.4,.5,.6)
renWin.Render()
iren.Start()

Eric

Sent from my iPhone

> On Aug 7, 2017, at 11:22 PM, Justin Clark <jreileyclark at gmail.com> wrote:
> 
> Also,
> 
> "interpolatedRadius.AddTuple(0,0.2)"  fails in python. The kernel completely crashes. I've tried tuple and list arguments as well. Nothing works. 
> 
> -----Original Message-----
> From: Bill Lorensen [mailto:bill.lorensen at gmail.com] 
> Sent: Monday, August 7, 2017 12:05 PM
> To: Justin Clark <jreileyclark at gmail.com>
> Cc: VTK Users <vtkusers at vtk.org>
> Subject: Re: [vtkusers] Plot Tubular Paths from xyz
> 
> Here is a C++ example:
> https://lorensen.github.io/VTKExamples/site/Cxx/VisualizationAlgorithms/TubesFromSplines/
> 
> 
>> On Mon, Aug 7, 2017 at 1:03 PM, Justin Clark <jreileyclark at gmail.com> wrote:
>> 
>> 
>> Hi all,
>> 
>> Total vtk newbie here trying to figure out a solution in vtk python.  I would like to plot multiple tubular paths defined by a set of (x, y, z) data.  Multiple paths.  I'm trying to plot wellbores. Once the wellbores are plotted I intend to use them as sources and sinks for simulation and streamline purposes.  Any guidance would be helpful!
>> 
>> Using existing examples I believe I need to insert my xyz's into a vtkPoints object. Then interpolate using a spline, and use the spline to define my tube path?
>> 
>> Thanks!
>> _______________________________________________
>> 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
> 
> 
> 
> -- 
> Unpaid intern in BillsBasement at noware dot com
> 
> _______________________________________________
> 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


More information about the vtkusers mailing list