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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::datatypes::{TensorData, TensorDimension};

use re_types_core::ArrowString;

use super::Tensor;

impl Tensor {
    /// Accessor to the underlying [`TensorData`].
    pub fn data(&self) -> &TensorData {
        &self.data.0
    }

    /// Try to construct a [`Tensor`] from anything that can be converted into [`TensorData`]
    ///
    /// This is useful for constructing a tensor from an ndarray.
    pub fn try_from<T: TryInto<TensorData>>(data: T) -> Result<Self, T::Error> {
        let data: TensorData = data.try_into()?;

        Ok(Self { data: data.into() })
    }

    /// Update the `names` of the contained [`TensorData`] dimensions.
    ///
    /// Any existing Dimension names will be overwritten.
    ///
    /// If too many, or too few names are provided, this function will warn and only
    /// update the subset of names that it can.
    pub fn with_dim_names(self, names: impl IntoIterator<Item = impl Into<ArrowString>>) -> Self {
        let names: Vec<_> = names.into_iter().map(|x| Some(x.into())).collect();
        if names.len() != self.data.0.shape.len() {
            re_log::warn_once!(
                "Wrong number of names provided for tensor dimension. {} provided but {} expected.",
                names.len(),
                self.data.0.shape.len(),
            );
        }
        Self {
            data: TensorData {
                shape: self
                    .data
                    .0
                    .shape
                    .into_iter()
                    .zip(names.into_iter().chain(std::iter::repeat(None)))
                    .map(|(dim, name)| TensorDimension {
                        size: dim.size,
                        name: name.or(dim.name),
                    })
                    .collect(),
                buffer: self.data.0.buffer,
            }
            .into(),
        }
    }
}

// ----------------------------------------------------------------------------
// Make it possible to create an ArrayView directly from a Tensor.

macro_rules! forward_array_views {
    ($type:ty, $alias:ty) => {
        impl<'a> TryFrom<&'a $alias> for ::ndarray::ArrayViewD<'a, $type> {
            type Error = crate::tensor_data::TensorCastError;

            #[inline]
            fn try_from(value: &'a $alias) -> Result<Self, Self::Error> {
                value.data().try_into()
            }
        }
    };
}

forward_array_views!(u8, Tensor);
forward_array_views!(u16, Tensor);
forward_array_views!(u32, Tensor);
forward_array_views!(u64, Tensor);

forward_array_views!(i8, Tensor);
forward_array_views!(i16, Tensor);
forward_array_views!(i32, Tensor);
forward_array_views!(i64, Tensor);

forward_array_views!(half::f16, Tensor);
forward_array_views!(f32, Tensor);
forward_array_views!(f64, Tensor);