Skip to content

liblaf.melon.transfer ยค

Functions:

  • transfer_tet_cell โ€“
  • transfer_tet_cell_to_point โ€“
  • transfer_tri_cell_to_point_category โ€“
  • transfer_tri_point โ€“
  • transfer_tri_point_to_tet โ€“

transfer_tet_cell ยค

transfer_tet_cell(
    source: UnstructuredGrid,
    target: UnstructuredGrid,
    data: str | Iterable[str] | None = None,
    **kwargs,
) -> UnstructuredGrid
Source code in src/liblaf/melon/transfer/_tet_cell.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def transfer_tet_cell(
    source: pv.UnstructuredGrid,
    target: pv.UnstructuredGrid,
    data: str | Iterable[str] | None = None,
    **kwargs,
) -> pv.UnstructuredGrid:
    from liblaf.melon import tet

    data: Collection[str] = utils.as_names(data, source.cell_data)
    output: pv.UnstructuredGrid = transfer_tet_cell_to_point(
        source, target, data, **kwargs
    )
    output = tet.point_data_to_cell_data(output, data)
    for name in data:
        target.cell_data[name] = output.cell_data[name]
    return output

transfer_tet_cell_to_point ยค

transfer_tet_cell_to_point(
    source: UnstructuredGrid,
    target: UnstructuredGrid,
    data: str | Iterable[str] | None = None,
    *,
    categorical: bool = False,
    snap_to_closest_point: bool = True,
    tolerance: float | None = 1e-06,
    **kwargs,
) -> UnstructuredGrid
Source code in src/liblaf/melon/transfer/_tet_cell_to_point.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def transfer_tet_cell_to_point(
    source: pv.UnstructuredGrid,
    target: pv.UnstructuredGrid,
    data: str | Iterable[str] | None = None,
    *,
    categorical: bool = False,
    snap_to_closest_point: bool = True,
    tolerance: float | None = 1e-6,
    **kwargs,
) -> pv.UnstructuredGrid:
    data: Collection[str] = utils.as_names(data, source.cell_data)
    source_filtered: pv.UnstructuredGrid = pv.UnstructuredGrid()
    source_filtered.copy_structure(source)
    source_filtered.cell_data.update(
        {name: source.cell_data[name] for name in data}, copy=False
    )
    output: pv.UnstructuredGrid = target.sample(
        source_filtered,
        categorical=categorical,
        snap_to_closest_point=snap_to_closest_point,
        tolerance=tolerance,
        **kwargs,
    )  # pyright: ignore[reportAssignmentType]
    for name in data:
        target.point_data[name] = output.point_data[name]
    return target

transfer_tri_cell_to_point_category ยค

transfer_tri_cell_to_point_category(
    source: Any,
    target: Any,
    *,
    data: str | Iterable[str] | None = None,
    fill: Any | Mapping[str, Any] | None = None,
    nearest: NearestPointOnSurface | None = None,
) -> Any
Source code in src/liblaf/melon/transfer/_tri_cell_to_point_category.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def transfer_tri_cell_to_point_category(
    source: Any,
    target: Any,
    *,
    data: str | Iterable[str] | None = None,
    fill: Any | Mapping[str, Any] | None = None,
    nearest: NearestPointOnSurface | None = None,
) -> Any:
    source: pv.PolyData = io.as_polydata(source)
    source.triangulate(inplace=True)
    target: pv.PolyData = io.as_polydata(target).copy()
    data: Iterable[str] = _make_data_names(data, source)
    fill = _make_fill_mapping(fill)
    if nearest is None:
        nearest = NearestPointOnSurface()
    prepared: NearestPointOnSurfacePrepared = nearest.prepare(source)
    result: NearestPointOnSurfaceResult = prepared.query(target)
    any_missing: np.bool = np.any(result.missing)
    valid: Bool[np.ndarray, " T"] = ~result.missing
    indices: Integer[np.ndarray, "V 3"] = result.triangle_id[valid]
    for name in data:
        source_data: Integer[np.ndarray, "S ..."] = source.cell_data[name]
        target_data: Integer[np.ndarray, "T ..."]
        if any_missing:
            target_data = np.full(
                (target.n_points, *source_data.shape[1:]), fill[name], source_data.dtype
            )
            target_data[valid] = source_data[indices]
        else:
            target_data = source_data[indices]
        target.point_data[name] = target_data
    return target

transfer_tri_point ยค

transfer_tri_point(
    source: Any,
    target: Any,
    *,
    data: str | Iterable[str] | None = None,
    fill: Any | Mapping[str, Any] | None = None,
    nearest: NearestPointOnSurface | None = None,
) -> Any
Source code in src/liblaf/melon/transfer/_tri_point.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def transfer_tri_point(
    source: Any,
    target: Any,
    *,
    data: str | Iterable[str] | None = None,
    fill: Any | Mapping[str, Any] | None = None,
    nearest: NearestPointOnSurface | None = None,
) -> Any:
    source: pv.PolyData = io.as_polydata(source)
    target: pv.PolyData = io.as_polydata(target)
    data: Iterable[str] = _make_data_names(data, source)
    fill = _make_fill_mapping(fill)
    if nearest is None:
        nearest = NearestPointOnSurface()
    prepared: NearestPointOnSurfacePrepared = nearest.prepare(source)
    result: NearestPointOnSurfaceResult = prepared.query(target)
    any_missing: np.bool = np.any(result.missing)
    valid: Bool[np.ndarray, " T"] = ~result.missing
    indices: Integer[np.ndarray, "V 3"] = source.regular_faces[
        result.triangle_id[valid]
    ]
    barycentric: Float[np.ndarray, "V 3"] = tm.triangles.points_to_barycentric(
        source.points[indices], result.nearest[valid]
    )
    for name in data:
        source_data: Float[np.ndarray, "S ..."] = source.point_data[name]
        target_data_valid: Float[np.ndarray, "V ..."] = einops.einsum(
            barycentric, source_data[indices], "V B, V B ... -> V ..."
        )
        target_data: Float[np.ndarray, "T ..."]
        if any_missing:
            target_data = np.full(
                (target.n_points, *source_data.shape[1:]), fill[name], source_data.dtype
            )
            target_data[valid] = target_data_valid
        else:
            target_data = target_data_valid
        target.point_data[name] = target_data
    return target

transfer_tri_point_to_tet ยค

transfer_tri_point_to_tet(
    source: Any,
    target: Any,
    *,
    data: str | Iterable[str] | None = None,
    fill: Any | Mapping[str, Any] | None = None,
    nearest: NearestPointOnSurface | None = None,
    point_id: str | None = None,
) -> UnstructuredGrid
Source code in src/liblaf/melon/transfer/_tri_point_to_tet.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def transfer_tri_point_to_tet(
    source: Any,
    target: Any,
    *,
    data: str | Iterable[str] | None = None,
    fill: Any | Mapping[str, Any] | None = None,
    nearest: NearestPointOnSurface | None = None,  # noqa: ARG001
    point_id: str | None = None,
) -> pv.UnstructuredGrid:
    source: pv.PolyData = io.as_polydata(source)
    target: pv.UnstructuredGrid = io.as_unstructured_grid(target)
    data: Iterable[str] = _make_data_names(data, source)
    fill: Mapping[str, Any] = _make_fill_mapping(fill)
    result: pv.UnstructuredGrid
    if point_id is None:
        raise NotImplementedError
    result = _transfer_with_point_id(
        source, target, data=data, point_id=point_id, fill=fill
    )
    return result