[vtk-developers] Trying to extract points by location

Amine Aboufirass amine.aboufirass at gmail.com
Fri Nov 17 08:09:18 EST 2017


Hello,

I am attempting to extract scalar data from an unstructured grid dataset
based on point locations using python. First I am importing some libraries:

import vtk
> from vtk.util.numpy_support import vtk_to_numpy
> import os
>

Then I define a list of tuples where the location coordinates are stored.

> Locations = [(0,0.2,0.0),(0,0.4,0.0),(0,0.5,0.0),(0,0.6,0.0)]
>

Next I create a vtkUnstructuredGridReader object, provide the filename and
scalar field and update it:

> reader = vtk.vtkUnstructuredGridReader()
> reader.SetFileName('MyData.vtk')
> reader.SetScalarsName('mass_solid')
> reader.Update()
>

Then I create a SelectionList as a vtkFloatArray() instance. This was
inspired from this example (
https://www.vtk.org/Wiki/VTK/Examples/Python/PolyData/ExtractSelectionCells
). I also feed in the location tuples into my SelectionList object.

SelectionList = vtk.vtkFloatArray()
> SelectionList.SetNumberOfComponents(3) #dimension of the components
> for Location in Locations:
> SelectionList.InsertNextTuple(Location)
>

I create a SelectionNode object and define the parameters for selection.

> SelectionNode = vtk.vtkSelectionNode()
> SelectionNode.SetFieldType(vtk.vtkSelectionNode.POINT)
> SelectionNode.SetContentType(vtk.vtkSelectionNode.LOCATIONS)
> SelectionNode.SetSelectionList(SelectionList)
>

I create a Selection and Add the SelectionNode object to it.

> Selection = vtk.vtkSelection()
> Selection.AddNode(SelectionNode)
>

I create an ExtractSelection, define input connection, selection etc,

> ExtractSelection = vtk.vtkExtractSelection()
> ExtractSelection.SetInputConnection(0, reader.GetOutputPort())
> ExtractSelection.SetInput(1,Selection)
> ExtractSelection.Update()
>

I generate a new Unstructured Grid from my selection:


> Selected = vtk.vtkUnstructuredGrid()
> Selected.ShallowCopy(ExtractSelection.GetOutput())
>

I attempt to return the extracted data:

> PointData = Selected.GetPointData()
> Data = vtk_to_numpy(PointData.GetScalars())


This last line however throws an error, and when I check to see why, the
PointData is indeed a vtkObject but what is output from it's GetPointData()
method is NoneType. This is frustrating since I had already defined the
scalars to extract and I know they are there. Does anyone know what I am
doing Wrong?

Thanks

Amine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20171117/1a79f9c9/attachment.html>


More information about the vtk-developers mailing list