# GetBounds() fails with NaN points in VTK9

**URL:** https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351
**Category:** Support
**Created:** [October 4, 2020, 5:13pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351 "2020-10-04T17:13:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [October 4, 2020, 5:13pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/1 "2020-10-04T17:13:58Z")

</div>

Did `vtkWarpScalar` change in the 9.x release of VTK?

Take this example dataset: [surface.vtp](https://discourse.vtk.org/uploads/short-url/8NKHvd6ktKsEVJxgr9Ll2SOK65d.vtp) (3.8 MB)

```auto
import vtk

reader = vtk.vtkXMLPolyDataReader()
reader.SetFileName("surface.vtp")
reader.Update()
surface = reader.GetOutputDataObject(0)

alg = vtk.vtkWarpScalar()
alg.SetInputDataObject(surface)
# args: (idx, port, connection, field, name)
alg.SetInputArrayToProcess(0, 0, 0, 0, "my data")
alg.SetScaleFactor(1.0)
alg.Update()
output = alg.GetOutputDataObject(0)

print(output.GetBounds())

```

Output:

| VTK 8.x | VTK 9.x |
| --- | --- |
| `(639926.0, 728426.0, 5669105.0, 5818551.0, 218.24110412597656, 1170.1644287109375`) | `(nan, nan, nan, nan, nan, nan)` |

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [October 4, 2020, 5:19pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/2 "2020-10-04T17:19:44Z")

</div>

Basically, the problem arises when the scalar array contains NaN values.

Perhaps this is red herring and the real issue lives in the`GetBounds()`/points implementation? In VTK 8 and 9, the `vtkWarpScalar` outputs NaN points in this case, but the bounds are not correctly computed in VTK 9

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [October 4, 2020, 5:27pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/3 "2020-10-04T17:27:07Z")

</div>

To add to the oddness, (in VTK9) `GetBounds()` on the `vtkPolyData` object returns Nan while `GetBounds()` on the `vtkPoints` returns the correct bounds

```auto
>>> output.GetBounds()
(nan, nan, nan, nan, nan, nan)
>>> output.GetPoints().GetBounds()
(639926.0, 728426.0, 5669105.0, 5818551.0, 218.24110412597656, 1170.1644287109375)

```

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [October 4, 2020, 6:08pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/4 "2020-10-04T18:08:24Z")

</div>

Also, I just downloaded the latest nightly ParaView (which presumably has VTK9?) and I loaded this surface, applied the “Warp by Scalar” filter, and the result is not able to be displayed

(previous versions of ParaView with VTK8 can show the output)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 5, 2020, 5:07am UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/5 "2020-10-05T05:07:26Z")

</div>

This problem was fixed in June 2015 in [this commit](https://github.com/Kitware/VTK/commit/06d553fb232ac3992b99df47613506f106d2f46d). Since VTK-8.2 was released, the file was modified twice: [[1]](https://github.com/Kitware/VTK/commit/cd9ec58e4bae1f9c4e2d8544f86126eb1faca1ff#diff-ab58acd1b5b2b6f1f7fdbb0225a8b657), [[2]](https://github.com/Kitware/VTK/commit/d18b31c8a0dcbaa2f459213fdfe2f9a7a5ee1e0d#diff-ab58acd1b5b2b6f1f7fdbb0225a8b657). You could confirm that one of these commits caused the regression by reverting them and rebuilding VTK and testing bounds computation again.

[TestExodusWithNaN.py](https://github.com/Kitware/VTK/commit/06d553fb232ac3992b99df47613506f106d2f46d#diff-f59d1c4dd03ca463db9acd13aa1e0c44) is supposed to test range computation with presence of NaN, so you may have a look at this test, too.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 5, 2020, 1:48pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/6 "2020-10-05T13:48:57Z")

</div>

I had a look at the code and the same mistake that was fixed in June 2015 (using a variable that may contain NaN as first argument in comparison, ternary, or min/max operations) was introduced in [new code in vtkBoundingBox class](https://github.com/Kitware/VTK/commit/933371b3b3838caa0ac000da10adcfdf2056c4b1).

I’ve fixed the issue - see necessary changes here: [https://github.com/lassoan/VTK/commit/92b1ee09385c271a88dc4d38d8ce4e4dc5940d9f](https://github.com/lassoan/VTK/commit/92b1ee09385c271a88dc4d38d8ce4e4dc5940d9f)

I don’t have more time to work on this, so you or other VTK developers would need to cherry-pick the fix, add a test, run dashboard tests, get it reviewed, and merged.

On a general note: I would stay away from using NaNs. It could be a convenience for users to assign some meaning to NaN (e.g., it could mean missing data), but if you allow such values then they may show up in completely unexpected places (basically anywhere). To properly handle NaN values, VTK developers would need to add more checks and special case detections everywhere, which could impact performance and would be enormous development and maintenance effort and would require special developer skills.

A potential solution, which would not require a huge effort, could be to describe in VTK documentation that NaN values are only supported in those classes that explicitly state this in their documentation (and add notes to classes that are tested to work well with NaNs).

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [October 7, 2020, 2:51pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/7 "2020-10-07T14:51:25Z")

</div>

For the record, this was posted to GitLab: [https://gitlab.kitware.com/vtk/vtk/-/issues/18019](https://gitlab.kitware.com/vtk/vtk/-/issues/18019)

Thanks for your insight and patch, @lassoan!

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [November 2, 2022, 7:32pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/8 "2022-11-02T19:32:57Z")

</div>

So how to represent missing z-values in a VtkPoints collection, if not with NaN? E.g. I have a sparse topographic dataset with many missing data values. I just insert each point, which may have NaN for z:

```auto
for (row = 0; row < nRows; row++) {
  for (col = 0; col < nCols; col++) {
    # Get x, y, and z. Note that missing z-value is represented as NaN
    getMyData(row, col, &x, &y, &z);
    # Insert point
    vtkIdType id = myPoints->InsertNextPoint(x, y, z);
  }
}

```

The resulting vtkPoints display as expected with visible ‘gaps’ where z-data is missing, but vtkPicker-\>GetPointId() always returns -1 no matter where I click, even where there _is_ z data. So how to properly depict missing data in this case? Thanks!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 2, 2022, 9:02pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/9 "2022-11-02T21:02:06Z")

</div>

According to VTK core developers [NaN point coordinates do not make sense](https://discourse.vtk.org/t/nan-support/8287/6) and the advice is to not add them to the point cloud.

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [November 2, 2022, 9:10pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/10 "2022-11-02T21:10:46Z")

</div>

Thanks @lassoan - what is the proper way to depict a set of points with missing z-data?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 2, 2022, 10:24pm UTC](https://discourse.vtk.org/t/getbounds-fails-with-nan-points-in-vtk9/4351/11 "2022-11-02T22:24:30Z")

</div>

If the coordinates are not known then you cannot display the point. If you don’t know the distance value then you can remove the point from the data set and display all the other points; or you can replace unknown z positions by some distant value.
