Skip to content

liblaf.melon.xfer

Transfer arrays between related triangular and tetrahedral meshes.

Functions:

tri_cell_to_tri_point

tri_cell_to_tri_point(
    source: PolyData,
    target: PolyData,
    names: Iterable[str],
    *,
    tolerance: float | None = None,
    snap_to_closest_point: bool = True,
) -> PolyData

Sample triangle cell arrays onto target surface points.

Parameters:

  • source (PolyData) –

    Surface carrying the source cell-data arrays.

  • target (PolyData) –

    Surface whose points receive sampled point-data arrays.

  • names (Iterable[str]) –

    Cell-data array names to transfer.

  • tolerance (float | None, default: None ) –

    Optional PyVista sampling tolerance.

  • snap_to_closest_point (bool, default: True ) –

    Forwarded to [pyvista.DataSetFilters.sample][].

Returns:

  • PolyData

    The mutated target mesh.

Source code in src/liblaf/melon/xfer/_tri_cell_to_tri_point.py
def tri_cell_to_tri_point(
    source: pv.PolyData,
    target: pv.PolyData,
    names: Iterable[str],
    *,
    tolerance: float | None = None,
    snap_to_closest_point: bool = True,
) -> pv.PolyData:
    """Sample triangle cell arrays onto target surface points.

    Args:
        source: Surface carrying the source cell-data arrays.
        target: Surface whose points receive sampled point-data arrays.
        names: Cell-data array names to transfer.
        tolerance: Optional PyVista sampling tolerance.
        snap_to_closest_point: Forwarded to [`pyvista.DataSetFilters.sample`][].

    Returns:
        The mutated `target` mesh.
    """
    src: pv.PolyData = pv.PolyData()
    src.copy_structure(source)
    src.cell_data.update({name: source.cell_data[name] for name in names})
    result: pv.PolyData = target.sample(
        src, tolerance=tolerance, snap_to_closest_point=snap_to_closest_point
    )
    result.point_data.pop("vtkGhostType", None)
    target.point_data.update(result.point_data)
    return target

tri_point_to_tet_point

tri_point_to_tet_point(
    source: PolyData,
    target: UnstructuredGrid,
    fill_values: Mapping[str, Any],
) -> UnstructuredGrid

Transfer point arrays from a surface to nearest tetrahedral points.

Parameters:

  • source (PolyData) –

    Surface with source point-data arrays.

  • target (UnstructuredGrid) –

    Tetrahedral mesh whose point data should be filled.

  • fill_values (Mapping[str, Any]) –

    Mapping from point-data array name to default value for target points that do not receive a nearest source value.

Returns:

  • UnstructuredGrid

    The mutated target mesh.

Source code in src/liblaf/melon/xfer/_tri_point_to_tet_point.py
def tri_point_to_tet_point(
    source: pv.PolyData, target: pv.UnstructuredGrid, fill_values: Mapping[str, Any]
) -> pv.UnstructuredGrid:
    """Transfer point arrays from a surface to nearest tetrahedral points.

    Args:
        source: Surface with source point-data arrays.
        target: Tetrahedral mesh whose point data should be filled.
        fill_values: Mapping from point-data array name to default value for
            target points that do not receive a nearest source value.

    Returns:
        The mutated `target` mesh.
    """
    kdtree: scipy.spatial.KDTree = scipy.spatial.KDTree(target.points)
    _d, indices = kdtree.query(source.points)
    for name, fill_value in fill_values.items():
        source_data: np.ndarray = source.point_data[name]
        target_data: np.ndarray = np.full(
            (target.n_points, *source_data.shape[1:]), fill_value, source_data.dtype
        )
        target_data[indices] = source_data
        target.point_data[name] = target_data
    return target