Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cast_to_unstructured_grid does not maintain dtype for RectilinearGrids #7931

@WesleyTheGeolien

Description

@WesleyTheGeolien

Describe the bug, what's wrong, and what you expected.

RectilinearGrids that have float64 Coordinates get cast to float32 coordinates if passed to cast_to_unstructured_grid. Float64 coordinates are "common" if using geographic coordinates. This causes downstream issue in processing.

I believe this is due to this line inside the vtkAppendFilter: https://github.com/Kitware/VTK/blob/845cd2a1687ef6b77982d1b7e592c81a15d23829/Filters/Core/vtkAppendFilter.cxx#L254 which tries to cast the RectilinearGrid to a PointSet, but RectilinearGrids don't inherit from PointSet meaning this returns a null pointer and the Point data type isn't retrieved.

Casting to StructuredGrid then UnstructuredGrid appears to maintain coordinate precision. (I didn't dive into why). Also it looks like the vtkAppendFilter has a SetOutputPointsPrecision which defaults to trying to determine the value (2) I wonder if it would be possible to expose this inside cast_to_unstructured_grid or build in the determination logic for objects that don't inherit from vtkPointSet.

I may have some scope to make a PR for this issue just unsure on the desired approach.

I can also see this potentially as a vtk issue? but that being said here pyvista is implementing the steps necessary to run the vtk filter.

Steps to reproduce the bug.

import pyvista as pv

xrng = np.array([45.402827])
yrng = np.array([6.232620])
zrng = np.arange(0, 1000, 25)
grid = pv.RectilinearGrid(xrng, yrng, zrng)

print(f"{xrng.dtype=}")

print(grid.GetXCoordinates().GetValue(0))

print(grid.cast_to_unstructured_grid().GetPoints().GetDataType())
print(grid.cast_to_structured_grid().cast_to_unstructured_grid().GetPoints().GetDataType())

System Information

--------------------------------------------------------------------------------
  Date: Thu Sep 04 13:34:43 2025 CEST

                OS : Linux (Pop!_OS 22.04)
            CPU(s) : 24
           Machine : x86_64
      Architecture : 64bit
               RAM : 62.7 GiB
       Environment : Jupyter
       File system : ext4
        GPU Vendor : NVIDIA Corporation
      GPU Renderer : NVIDIA GeForce RTX 3060/PCIe/SSE2
       GPU Version : 4.5.0 NVIDIA 470.256.02
  MathText Support : True

  Python 3.12.8 | packaged by conda-forge | (main, Dec  5 2024, 14:24:40) [GCC
  13.3.0]

           pyvista : 0.44.2
               vtk : 9.3.1
             numpy : 1.26.4
        matplotlib : 3.10.0
            scooby : 0.10.0
             pooch : 1.8.2
            pillow : 10.4.0
           imageio : 2.36.1
           IPython : 8.31.0
        ipywidgets : 8.0.6
             scipy : 1.14.1
              tqdm : 4.67.1
        jupyterlab : 4.3.4
      nest_asyncio : 1.6.0

  Intel(R) oneAPI Math Kernel Library Version 2024.2.2-Product Build 20240823
  for Intel(R) 64 architecture applications
--------------------------------------------------------------------------------

Screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUh-oh! Something isn't working as expected.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions