Rerun C++ SDK
Loading...
Searching...
No Matches
component_column.hpp
1#pragma once
2
3#include <memory> // shared_ptr
4
5#include "collection.hpp"
6#include "component_batch.hpp"
7#include "component_type.hpp"
8#include "error.hpp"
9#include "loggable.hpp"
10
11struct rr_component_column;
12
13namespace rerun {
14 /// Arrow-encoded data of a column of components.
15 ///
16 /// This is essentially an array of `rerun::ComponentBatch` with all batches
17 /// continuously in a single array.
18 ///
19 /// \see `rerun::RecordingStream::send_columns`
21 /// Arrow-encoded list array of component batches.
22 std::shared_ptr<arrow::Array> array;
23
24 /// The type of the component instances in array.
26
27 public:
28 /// Creates a new component column from a collection of component instances.
29 ///
30 /// Automatically registers the component type the first time this type is encountered.
31 ///
32 /// \param components Continuous collection of components which is about to be partitioned.
33 /// \param lengths The number of components in each run. for `rerun::RecordingStream::send_columns`,
34 /// this specifies the number of components at each time point.
35 /// The sum of the lengths must be equal to the number of components in the batch.
36 template <typename T>
38 const Collection<T>& components, const Collection<uint32_t>& lengths
39 ) {
40 auto component_batch_result = ComponentBatch::from_loggable(components);
41 if (component_batch_result.is_err()) {
42 return component_batch_result.error;
43 }
45 component_batch_result.value,
46 lengths,
47 list_array_type_for<T>()
48 );
49 }
50
51 /// Creates a new component column from a collection of component instances where each run has a length of one.
52 ///
53 /// When used with `rerun::RecordingStream::send_columns`, this is equivalent to `from_loggable(components, std::vector{1, 1, ...})`.
54 /// I.e. there's a single component for each time point.
55 ///
56 /// Automatically registers the component type the first time this type is encountered.
57 ///
58 /// \param components Continuous collection of components which is about to be partitioned into runs of length one.
59 template <typename T>
62 components,
63 Collection<uint32_t>::take_ownership(std::vector<uint32_t>(components.size(), 1))
64 );
65 }
66
67 /// Creates a new component column with a given number of archetype indicators for a given archetype type.
68 template <typename Archetype>
69 static Result<ComponentColumn> from_indicators(uint32_t num_indicators) {
70 return ComponentColumn::from_loggable<typename Archetype::IndicatorComponent>(
71 std::vector<typename Archetype::IndicatorComponent>(num_indicators)
72 );
73 }
74
75 /// Creates a new component batch partition from a batch and a collection of run lengths.
76 ///
77 /// \param batch A batch of components which is about to be partitioned.
78 /// \param lengths The number of components in each run. for `rerun::RecordingStream::send_columns`,
79 /// this specifies the number of components at each time point.
80 /// The sum of the lengths must be equal to the number of components in the batch.
81 /// \param list_array_type The type of the list array to use for the component column.
82 /// Can be retrieved using `list_array_type_for<T>()`.
84 ComponentBatch batch, const Collection<uint32_t>& lengths,
85 std::shared_ptr<arrow::DataType> list_array_type
86 );
87
88 /// Creates a new component batch partition from a batch and a collection of component offsets.
89 ///
90 /// \param batch A batch of components which is about to be partitioned.
91 /// \param offsets An offset within `batch` for each array of components.
92 /// The last offset is the total number of components in the batch. Meaning that this array has to be
93 /// one element longer than the number of component runs.
94 /// E.g. a `ParitionedComponentBatch` with a single component would have an offset array of `[0, 1]`.
95 /// A `ComponentColumn` with 5 components divided into runs of length 2 and 3
96 // would have an offset array of `[0, 2, 5]`.
97 /// \param list_array_type The type of the list array to use for the component column.
98 /// Can be retrieved using `list_array_type_for<T>()`.
101 std::shared_ptr<arrow::DataType> list_array_type
102 );
103
104 /// Returns the list array type for the given loggable type.
105 ///
106 /// Lazily creates the type on first call and then returns a reference to it.
107 template <typename T>
108 static const std::shared_ptr<arrow::DataType>& list_array_type_for() {
109 static_assert(
110 rerun::is_loggable<T>,
111 "The given type does not implement the rerun::Loggable trait."
112 );
113 static std::shared_ptr<arrow::DataType> data_type =
115 return data_type;
116 }
117
118 /// Creates a new arrow::Datatype for an underlying type.
119 ///
120 /// To avoid repeated allocation, use the templated version of this method.
121 static std::shared_ptr<arrow::DataType> list_array_type_for(
122 std::shared_ptr<arrow::DataType> inner_type
123 );
124
125 /// To rerun C API component batch.
126 ///
127 /// The resulting `rr_component_column` keeps the `arrow::Array` alive until it is released.
128 Error to_c_ffi_struct(rr_component_column& out_component_batch) const;
129 };
130} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:291
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:95
A class for representing either a usable value, or an error.
Definition result.hpp:14
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
uint32_t ComponentTypeHandle
Handle to a registered component types.
Definition component_type.hpp:14
Arrow-encoded data of a single batch components for a single entity.
Definition component_batch.hpp:24
static Result< ComponentBatch > from_loggable(const rerun::Collection< T > &components)
Creates a new component batch from a collection of component instances.
Definition component_batch.hpp:36
Arrow-encoded data of a column of components.
Definition component_column.hpp:20
static Result< ComponentColumn > from_loggable(const Collection< T > &components)
Creates a new component column from a collection of component instances where each run has a length o...
Definition component_column.hpp:60
static const std::shared_ptr< arrow::DataType > & list_array_type_for()
Returns the list array type for the given loggable type.
Definition component_column.hpp:108
ComponentTypeHandle component_type
The type of the component instances in array.
Definition component_column.hpp:25
static Result< ComponentColumn > from_batch_with_offsets(ComponentBatch batch, Collection< uint32_t > offsets, std::shared_ptr< arrow::DataType > list_array_type)
Creates a new component batch partition from a batch and a collection of component offsets.
std::shared_ptr< arrow::Array > array
Arrow-encoded list array of component batches.
Definition component_column.hpp:22
static Result< ComponentColumn > from_batch_with_lengths(ComponentBatch batch, const Collection< uint32_t > &lengths, std::shared_ptr< arrow::DataType > list_array_type)
Creates a new component batch partition from a batch and a collection of run lengths.
static std::shared_ptr< arrow::DataType > list_array_type_for(std::shared_ptr< arrow::DataType > inner_type)
Creates a new arrow::Datatype for an underlying type.
static Result< ComponentColumn > from_loggable_with_lengths(const Collection< T > &components, const Collection< uint32_t > &lengths)
Creates a new component column from a collection of component instances.
Definition component_column.hpp:37
Error to_c_ffi_struct(rr_component_column &out_component_batch) const
To rerun C API component batch.
static Result< ComponentColumn > from_indicators(uint32_t num_indicators)
Creates a new component column with a given number of archetype indicators for a given archetype type...
Definition component_column.hpp:69
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11