Rerun C++ SDK
Loading...
Searching...
No Matches
component_batch.hpp
1#pragma once
2
3#include <memory> // shared_ptr
4#include <optional>
5#include <unordered_map>
6
7#include "collection.hpp"
8#include "component_descriptor.hpp"
9#include "component_type.hpp"
10#include "error.hpp"
11#include "loggable.hpp"
12
13namespace arrow {
14 class Array;
15 class DataType;
16} // namespace arrow
17
18struct rr_component_batch;
19
20namespace rerun {
21 struct ComponentColumn;
22}
23
24namespace rerun {
25 /// Arrow-encoded data of a single batch of components together with a component descriptor.
26 ///
27 /// Component descriptors are registered when first encountered.
29 /// Arrow-encoded data of the component instances.
30 std::shared_ptr<arrow::Array> array;
31
32 /// The type of the component instances in array.
34
35 public:
36 /// Creates a new empty component batch with a given descriptor.
37 template <typename T>
39 return from_loggable(Collection<T>(), descriptor);
40 }
41
42 /// Creates a new component batch from a collection of component instances.
43 ///
44 /// Automatically registers the descriptor the first time it is encountered.
45 template <typename T>
47 const rerun::Collection<T>& components,
49 ) {
50 static_assert(
51 rerun::is_loggable<T>,
52 "The given type does not implement the rerun::Loggable trait."
53 );
54
55 /// TODO(#4257) should take a rerun::Collection instead of pointer and size.
56 auto array = Loggable<T>::to_arrow(components.data(), components.size());
57 RR_RETURN_NOT_OK(array.error);
58
59 return from_arrow_array(std::move(array.value), descriptor);
60 }
61
62 /// Creates a new component batch from a single component instance.
63 ///
64 /// Automatically registers the descriptor the first time it is encountered.
65 template <typename T>
67 const T& component,
69 ) {
70 // Collection adapter will automatically borrow for single elements, but let's do this explicitly, avoiding the extra hoop.
71 const auto collection = Collection<T>::borrow(&component, 1);
72 return from_loggable(collection, descriptor);
73 }
74
75 /// Creates a new data cell from a single optional component instance.
76 ///
77 /// None is represented as a data cell with 0 instances.
78 ///
79 /// Automatically registers the descriptor the first time it is encountered.
80 template <typename T>
82 const std::optional<T>& component,
84 ) {
85 if (component.has_value()) {
86 return from_loggable(component.value(), descriptor);
87 } else {
88 return from_loggable(Collection<T>(), descriptor);
89 }
90 }
91
92 /// Creates a new data cell from an optional collection of component instances.
93 ///
94 /// None is represented as a data cell with 0 instances.
95 ///
96 /// Automatically registers the descriptor the first time it is encountered.
97 template <typename T>
99 const std::optional<rerun::Collection<T>>& components,
101 ) {
102 if (components.has_value()) {
103 return from_loggable(components.value(), descriptor);
104 } else {
105 return from_loggable(Collection<T>(), descriptor);
106 }
107 }
108
109 /// Creates a new component batch for an archetype indicator.
110 template <typename Archetype>
113 typename Archetype::IndicatorComponent(),
115 );
116 }
117
118 /// Creates a new component batch from an already existing arrow array.
119 ///
120 /// Automatically registers the descriptor the first time it is encountered.
122 std::shared_ptr<arrow::Array> array, const ComponentDescriptor& descriptor
123 );
124
125 /// Partitions the component data into multiple sub-batches.
126 ///
127 /// Specifically, this transforms the existing `ComponentBatch` data into a `ComponentColumn`.
128 ///
129 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
130 ///
131 /// \param lengths The number of components in each run. for `rerun::RecordingStream::send_columns`,
132 /// this specifies the number of components at each time point.
133 /// The sum of the lengths must be equal to the number of components in the batch.
135
136 /// Partitions the component data into unit-length sub-batches.
137 ///
138 /// Specifically, this transforms the existing `ComponentBatch` data into a `ComponentColumn`.
139 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
141
142 /// Partitions the component data into multiple sub-batches.
143 ///
144 /// Specifically, this transforms the existing `ComponentBatch` data into a `ComponentColumn`.
145 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
146 ///
147 /// \param lengths The number of components in each run. for `rerun::RecordingStream::send_columns`,
148 /// this specifies the number of components at each time point.
149 /// The sum of the lengths must be equal to the number of components in the batch.
151
152 /// Partitions the component data into unit-length sub-batches.
153 ///
154 /// Specifically, this transforms the existing `ComponentBatch` data into a `ComponentColumn`.
155 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
157
158 /// Size in the number of elements the underlying arrow array contains.
159 size_t length() const;
160
161 /// To rerun C API component batch.
162 ///
163 /// The resulting `rr_component_batch` keeps the `arrow::Array` alive until it is released.
164 Error to_c_ffi_struct(rr_component_batch& out_component_batch) const;
165 };
166} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
static Collection< TElement > borrow(const T *data, size_t num_instances=1)
Borrows binary compatible data into the collection from a typed pointer.
Definition collection.hpp:154
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:291
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:327
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:96
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 of components together with a component descriptor.
Definition component_batch.hpp:28
Result< ComponentColumn > partitioned() const &
Partitions the component data into unit-length sub-batches.
size_t length() const
Size in the number of elements the underlying arrow array contains.
static Result< ComponentBatch > empty(const ComponentDescriptor &descriptor)
Creates a new empty component batch with a given descriptor.
Definition component_batch.hpp:38
static Result< ComponentBatch > from_loggable(const std::optional< T > &component, const ComponentDescriptor &descriptor=rerun::Loggable< T >::Descriptor)
Creates a new data cell from a single optional component instance.
Definition component_batch.hpp:81
Error to_c_ffi_struct(rr_component_batch &out_component_batch) const
To rerun C API component batch.
static Result< ComponentBatch > from_loggable(const std::optional< rerun::Collection< T > > &components, const ComponentDescriptor &descriptor=rerun::Loggable< T >::Descriptor)
Creates a new data cell from an optional collection of component instances.
Definition component_batch.hpp:98
Result< ComponentColumn > partitioned(const Collection< uint32_t > &lengths) &&
Partitions the component data into multiple sub-batches.
ComponentTypeHandle component_type
The type of the component instances in array.
Definition component_batch.hpp:33
static Result< ComponentBatch > from_loggable(const T &component, const ComponentDescriptor &descriptor=rerun::Loggable< T >::Descriptor)
Creates a new component batch from a single component instance.
Definition component_batch.hpp:66
static Result< ComponentBatch > from_arrow_array(std::shared_ptr< arrow::Array > array, const ComponentDescriptor &descriptor)
Creates a new component batch from an already existing arrow array.
std::shared_ptr< arrow::Array > array
Arrow-encoded data of the component instances.
Definition component_batch.hpp:30
static Result< ComponentBatch > from_indicator()
Creates a new component batch for an archetype indicator.
Definition component_batch.hpp:111
Result< ComponentColumn > partitioned() &&
Partitions the component data into unit-length sub-batches.
Result< ComponentColumn > partitioned(const Collection< uint32_t > &lengths) const &
Partitions the component data into multiple sub-batches.
static Result< ComponentBatch > from_loggable(const rerun::Collection< T > &components, const ComponentDescriptor &descriptor=rerun::Loggable< T >::Descriptor)
Creates a new component batch from a collection of component instances.
Definition component_batch.hpp:46
A ComponentDescriptor fully describes the semantics of a column of data.
Definition component_descriptor.hpp:14
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11