[vtkusers] ExtractByLocation

Amine Aboufirass amine.aboufirass at gmail.com
Fri Dec 1 10:45:27 EST 2017


I am trying to figure out how to select the closest neighboring points to a
list of coordinates using the vtk library. I have a simple point data set
in the Unstructured Grid Format. The dataset contains one attribute named
the 'point_id' which is just enumerating the points starting from 1 instead
of 0. The raw text from the ASCII file is attached in testScalar.vtk below.

I have also written a script in python which I thought would function as


   1. List of tuples containing world coordinates [(X1,Y1,Z1),(X2,Y2,Z2)]
   2. name of attribute to extract as a string. in this case "point_id"


   1. List of floating point values of the attribute for the selected points

This does work to a certain degree, for instance when I do :

ExtractByLocation('testScalar.vtk', [(5.999,0,0)], 'point_id')

I do indeed get back the following:

array([ 2.], dtype=float32)

So point_id 2 is the closest point to the coordinates I have specified.
However, as soon as I get a bit too far from one of the points, the
function starts returning an empty list. For instance:

ExtractByLocation('testScalar.vtk', [(5.888,0,0)], 'point_id')

array([], dtype=float32)

I found out that the cut off point is 5.89-5.90. If I plugin 5.9 I get the
point, if I plugin 5.89 I get back an empty list.

According to the object reference (
 LOCATIONS is supposed to select the points near the supplied world
coordinates. Is there an option or something that selects the nearest point
PERIOD? i.e using a simple distance formula? I would like to never get back
an empty list but always get back a list of the closest point to the
supplied coordinates.

Kind Regards,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171201/e157fa3e/attachment.html>
-------------- next part --------------
import vtk
from vtk.util.numpy_support import vtk_to_numpy
import os

def ExtractByLocation(FileName, Locations, DataType):
#where Locations MUST be a list of XYZ tuples, e.g [(0,0.2,0.0),(0,0.4,0.0),(0,0.5,0.0),(0,0.6,0.0)]
	reader = vtk.vtkUnstructuredGridReader() 
	if 'Scalar' in FileName:
	elif 'Tensor' in FileName:
	elif 'Vector' in FileName:

	SelectionList = vtk.vtkDoubleArray()
	SelectionList.SetNumberOfComponents(3) #dimension of the components

	for Location in Locations:

	SelectionNode = vtk.vtkSelectionNode() 
	Selection = vtk.vtkSelection() 
	ExtractSelection = vtk.vtkExtractSelection() 
	ExtractSelection.SetInputConnection(0, reader.GetOutputPort()) 
	Selected = vtk.vtkUnstructuredGrid()
	PointData = Selected.GetPointData()
	if 'Scalar' in FileName:
		Data = vtk_to_numpy(PointData.GetScalars())
	elif 'Tensor' in FileName:
		Data = vtk_to_numpy(PointData.GetTensors())
	elif 'Vector' in FileName:
		Data = vtk_to_numpy(PointData.GetVectors())
	return Data
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testScalar.vtk
Type: application/octet-stream
Size: 231 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171201/e157fa3e/attachment.obj>

More information about the vtkusers mailing list