# Problems interpolating from delaunay2d

**URL:** https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294
**Category:** Support
**Tags:** python
**Created:** [September 8, 2022, 10:15am UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294 "2022-09-08T10:15:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 8, 2022, 10:15am UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294/1 "2022-09-08T10:15:28Z")

</div>

I used vtkDelaunay2d to generate a terrain surface. I wanted to obtain the elevation at two locations following this [example](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/InterpolateMeshOnGrid/) suggested from an earlier [posting](https://discourse.vtk.org/t/interpolating-z-values-from-a-delaunay2d-mesh/9047/5). The locations are coordinates in terrain units. I have written the following code:

```auto
    # locations
     a= (533519, 4387387)
     a+= (0,)
     b= (533555, 4387387)
     b+=(0,)
     viewpts = vtkPoints()
     viewpts.InsertNextPoint(*a)
     viewpts.InsertNextPoint(*b)
     values = vtkPolyData()
     zvalues.SetPoints(viewpts)

    # interpolate
    z_probe = vtkProbeFilter()
    z_probe.SetInputData(zvalues)
    z_probe.SetSourceConnection(delaunay.GetOutputPort())
    z_probe.Update()

```

The result when I run the following `z_probe.GetOutput().GetPoint(0)` is

> (533519, 4387387, 0.0)

What am I doing wrong? Why am I not getting the z value for location a?

ps. I have also tried using,

```auto
gridWarpScalar = vtkWarpScalar()
gridWarpScalar.SetInputConnection(z_probe.GetOutputPort())
gridWarpScalar.Update()

```

But I get the same result.

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.vtk.org/user_avatar/discourse.vtk.org/christos_tsolakis/32/2076_2.png) [@Christos\_Tsolakis](https://discourse.vtk.org/u/Christos_Tsolakis)
#### Post date: [September 9, 2022, 12:36pm UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294/2 "2022-09-09T12:36:22Z")

</div>

Almost there !  
As per [documentation](https://vtk.org/doc/nightly/html/classvtkProbeFilter.html#details) the `vtkProbeFilter` computes **point attributes** (e.g., scalars, vectors, etc.) at specified point positions. So the coordinates of a,b are expected to remain the same but their attibutes will be filled by the filter. To get the z value try

```auto
z_probe.GetOutput().GetPointData().GetScalars().GetTuple1(0)

```

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 9, 2022, 3:13pm UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294/3 "2022-09-09T15:13:30Z")

</div>

Thank you Christos,

I am still unable to get some results. It appears that I am not getting any scalar back (I used `print(z_probe)` to see what I got back. I am not certain why I am getting this result given that I know that the Delaunay Tessellation computed correctly (I was able to see it) and the locations that I used are within this tesselation!  
I am going over everything to see what is missing!!!??

M

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.vtk.org/user_avatar/discourse.vtk.org/christos_tsolakis/32/2076_2.png) [@Christos\_Tsolakis](https://discourse.vtk.org/u/Christos_Tsolakis)
#### Post date: [September 9, 2022, 3:23pm UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294/4 "2022-09-09T15:23:36Z")

</div>

Without more code I cannot figure out what it could be the issue. Here is a full python example I created based on the VTK example and your test case. I hope it helps:

```python
# based on https://kitware.github.io/vtk-examples/site/Cxx/PolyData/InterpolateMeshOnGrid/
import vtk

# Create a random set of 100 points in the xy-plane in (0,10)x(0,10).
# If you had instead started with a set of (x,y,z) points, you must copy
# the zvalues into a FloatArray and set it as the data set's scalars,
# and then set the z-coordinates to zero
randomPoints = vtk.vtkPoints()

zvalues = vtk.vtkFloatArray()
zvalues.SetName("ZValues")

gridSize = 10
maxHeight = 1.0
randomSequence = vtk.vtkMinimalStandardRandomSequence()

randomSequence.SetSeed(8775070)

for i in range(100):
  x = randomSequence.GetRangeValue(0, gridSize)
  randomSequence.Next()
  y = randomSequence.GetRangeValue(0, gridSize)
  randomSequence.Next();
  z = randomSequence.GetRangeValue(0, maxHeight)
  randomSequence.Next();

  randomPoints.InsertNextPoint(x, y, 0)
  zvalues.InsertNextValue(z)

randomPolyData = vtk.vtkPolyData()
randomPolyData.SetPoints(randomPoints);
randomPolyData.GetPointData().SetScalars(zvalues);

# Triangulate the grid points. If you do not have a mesh (points
# only), the output will not be interpolated!
randomDelaunay = vtk.vtkDelaunay2D()
randomDelaunay.SetInputData(randomPolyData);
randomDelaunay.Update();

# location
a = (5,5,0)
b = (4,5,0)

viewpts = vtk.vtkPoints()
viewpts.InsertNextPoint(*a)
viewpts.InsertNextPoint(*b)

data = vtk.vtkPolyData()
data.SetPoints(viewpts)

# interpolate
z_probe = vtk.vtkProbeFilter()
z_probe.SetSourceConnection(randomDelaunay.GetOutputPort())
z_probe.SetInputData(data)
z_probe.Update()

print(z_probe.GetOutput().GetPointData().GetScalars().GetTuple1(0))
print(z_probe.GetOutput().GetPointData().GetScalars().GetTuple1(1))

## or
print(z_probe.GetOutput().GetPointData().GetArray("ZValues").GetTuple1(0))
print(z_probe.GetOutput().GetPointData().GetArray("ZValues").GetTuple1(1))

```

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [September 10, 2022, 3:52pm UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294/5 "2022-09-10T15:52:33Z")

</div>

Dear Christos,

I adapted your code and I did get results. Frankly, I am still a bit confused as to the logic (the bit where I cannot follow the logic is having to leave out z values when creating `randompoints` and having to set the scalars separately). But… it does work so I will endeavor to try to make sense of why.

Thanks again! It has been very helpful.

M.

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.vtk.org/user_avatar/discourse.vtk.org/christos_tsolakis/32/2076_2.png) [@Christos\_Tsolakis](https://discourse.vtk.org/u/Christos_Tsolakis)
#### Post date: [September 15, 2022, 2:01am UTC](https://discourse.vtk.org/t/problems-interpolating-from-delaunay2d/9294/7 "2022-09-15T02:01:26Z")

</div>

> (the bit where I cannot follow the logic is having to leave out z values when creating `randompoints` and having to set the scalars separately)

Delaunay2D can only work on 2D points. So we only pass `x` and `y`. We treat `z` as an attribute of the points which we want to interpolate( if `z` is confusing think of it as temperature/density or any other scalar value).

```auto
randomPolyData.GetPointData().SetScalars(zvalues);

```

sets `z` as a point attribute. `randomDelaunay` will then triangulate the points leaving the zvalues untouched. (We could even have appended the zvalues after running the filter)

Finally,  
`vtkProbeFilter` requires a geometric object with cells as source where we can use to locate the InputData and interpolate the zvalues onto the points of InputData.
