File size: 2,332 Bytes
9ff4bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
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
59
60
61
from __future__ import annotations

from typing import TYPE_CHECKING, Any

import numpy as np
import pyarrow as pa

from rerun.error_utils import _send_warning_or_raise

if TYPE_CHECKING:
    from . import Mat4x4ArrayLike, Mat4x4Like


class Mat4x4Ext:
    """Extension for [Mat4x4][rerun.encodings.Mat4x4]."""

    def __init__(self: Any, rows: Mat4x4Like | None = None, *, columns: Mat4x4Like | None = None) -> None:
        from . import Mat4x4

        if rows is not None:
            if columns is not None:
                _send_warning_or_raise("Can't specify both columns and rows of matrix.", 1, recording=None)

            if isinstance(rows, Mat4x4):
                flat_columns = rows.flat_columns
            else:
                arr = np.asarray(rows, dtype=np.float32).reshape(4, 4)
                flat_columns = arr.flatten("F")
        elif columns is not None:
            # Equalize the format of the columns to a 4x4 matrix.
            # Numpy expects rows _and_ stores row-major. Therefore the flattened list will have flat columns.
            arr = np.asarray(columns, dtype=np.float32).reshape(4, 4)
            flat_columns = arr.flatten()
        else:
            _send_warning_or_raise("Need to specify either columns or columns of matrix.", 1, recording=None)
            flat_columns = np.identity(4, dtype=np.float32).flatten()
        self.__attrs_init__(
            flat_columns=flat_columns,
        )

    @staticmethod
    def native_to_pa_array_override(data: Mat4x4ArrayLike, data_type: pa.DataType) -> pa.Array:
        from . import Mat4x4

        if isinstance(data, Mat4x4):
            matrices = [data]
        elif len(data) == 0:
            matrices = []
        else:
            try:
                # Try to convert it to a single Mat4x4
                # Will raise ValueError if the wrong shape
                matrices = [Mat4x4(data)]  # type: ignore[arg-type]
            except ValueError:
                # Otherwise try to convert it to a sequence of Mat4x4
                # Let this value error propagate as the fallback
                matrices = [Mat4x4(d) for d in data]

        float_arrays = np.asarray([matrix.flat_columns for matrix in matrices], dtype=np.float32).reshape(-1)
        return pa.FixedSizeListArray.from_arrays(float_arrays, type=data_type)