code: def voxelize(mesh, density=None, check_surface=True):
      """Voxelize mesh to UnstructuredGrid.
  
      Parameters
      ----------
      mesh : pyvista.DataSet
          Mesh to voxelize.
  
      density : float | array_like[float]
          The uniform size of the voxels when single float passed.
          A list of densities along x,y,z directions.
          Defaults to 1/100th of the mesh length.
  
      check_surface : bool, default: True
          Specify whether to check the surface for closure. If on, then the
          algorithm first checks to see if the surface is closed and
          manifold. If the surface is not closed and manifold, a runtime
          error is raised.
  
      Returns
      -------
      pyvista.UnstructuredGrid
          Voxelized unstructured grid of the original mesh.
  
      Notes
      -----
      Prior to version 0.39.0, this method improperly handled the order of
      structured coordinates.
  
      Examples
      --------
      Create an equal density voxelized mesh.
  
      >>> import pyvista as pv
      >>> from pyvista import examples
      >>> mesh = examples.download_bunny_coarse().clean()
      >>> vox = pv.voxelize(mesh, density=0.01)
      >>> vox.plot(show_edges=True)
  
      Create a voxelized mesh using unequal density dimensions.
  
      >>> vox = pv.voxelize(mesh, density=[0.01, 0.005, 0.002])
      >>> vox.plot(show_edges=True)
  
      """
      if not pyvista.is_pyvista_dataset(mesh):
          mesh = wrap(mesh)
      if density is None:
          density = mesh.length / 100
      if isinstance(density, (int, float, np.number)):
          density_x, density_y, density_z = [density] * 3
      elif isinstance(density, (collections.abc.Sequence, np.ndarray)):
          density_x, density_y, density_z = density
      else:
          raise TypeError(
              f"Invalid density {density!r}, expected number or array-like."
          )
  
      # check and pre-process input mesh
      surface = mesh.extract_geometry()  # filter preserves topology
      if not surface.faces.size:
          # we have a point cloud or an empty mesh
          raise ValueError(
              "Input mesh must have faces for voxelization."
          )
      if not surface.is_all_triangles:
          # reduce chance for artifacts, see gh-1743
          surface.triangulate(inplace=True)
  
      x_min, x_max, y_min, y_max, z_min, z_max = mesh.bounds
      x = np.arange(x_min, x_max, density_x)
      y = np.arange(y_min, y_max, density_y)
      z = np.arange(z_min, z_max, density_z)
      x, y, z = np.meshgrid(x, y, z, indexing="ij")
      # indexing='ij' is used here in order to make grid and ugrid with x-y-z ordering, not y-x-z ordering
      # see https://github.com/pyvista/pyvista/pull/4365
  
      # Create unstructured grid from the structured grid
      grid = pyvista.StructuredGrid(x, y, z)
      ugrid = pyvista.UnstructuredGrid(grid)
  
      # get part of the mesh within the mesh's bounding surface.
      selection = ugrid.select_enclosed_points(
          surface, tolerance=0.0, check_surface=check_surface
      )
      mask = selection.point_data["SelectedPoints"].view(np.bool_)
  
      # extract cells from point indices
      vox = ugrid.extract_points(mask)
      return vox
  
file_path: pyvista/pyvista/core/utilities/features.py
imports: import collections.abc
  import os
  import sys
  from typing import Sequence
  import numpy
  import pyvista
  from pyvista.core import _vtk_core
  from .helpers import wrap
label_desc: Use image node_id 1 for calling a global function with example usage: voxelize(mesh, density, check_surface) and returns: vox