File size: 2,782 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
62
63
64
65
66
67
from __future__ import annotations

import numbers
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 Mat3x3ArrayLike, Mat3x3Like


class Mat3x3Ext:
    """Extension for [Mat3x3][rerun.encodings.Mat3x3]."""

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

        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, Mat3x3):
                flat_columns = rows.flat_columns
            else:
                arr = np.asarray(rows, dtype=np.float32).reshape(3, 3)
                flat_columns = arr.ravel("F")
        elif columns is not None:
            # Equalize the format of the columns to a 3x3 matrix.
            # Numpy expects rows _and_ stores row-major. Therefore the flattened list will have flat columns.
            arr = np.asarray(columns, dtype=np.float32).reshape(3, 3)
            flat_columns = arr.ravel("C")
        else:
            _send_warning_or_raise("Need to specify either columns or columns of matrix.", 1, recording=None)
            flat_columns = np.identity(3, dtype=np.float32).ravel()
        self.__attrs_init__(
            flat_columns=flat_columns,
        )

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

        if isinstance(data, Mat3x3):
            float_arrays = data.flat_columns
        elif len(data) == 0:  # type: ignore[arg-type]
            float_arrays = np.empty((0,), dtype=np.float32)
        else:
            try:
                # Try to convert it to a single Mat3x3
                # Will raise ValueError if the wrong shape
                float_arrays = Mat3x3(data).flat_columns  # type: ignore[arg-type]
            except ValueError:
                # If the data can't be possibly more than one Mat3x3, raise the original ValueError.
                if isinstance(data[0], numbers.Number):  # type: ignore[arg-type, index]  # ty: ignore[not-subscriptable]
                    raise

                # Otherwise try to convert it to a sequence of Mat3x3s
                # Let this value error propagate as the fallback
                result = [Mat3x3(d).flat_columns for d in data]  # type: ignore[arg-type, union-attr, call-overload]  # ty: ignore[not-iterable]
                float_arrays = np.hstack(result).ravel()

        float_arrays = np.ascontiguousarray(float_arrays)
        return pa.FixedSizeListArray.from_arrays(float_arrays, type=data_type)