Rerun C++ SDK
Loading...
Searching...
No Matches
time_column.hpp
1#pragma once
2
3#include <cassert>
4#include <chrono>
5#include <cmath>
6#include <memory> // shared_ptr
7
8#include "collection.hpp"
9#include "error.hpp"
10#include "timeline.hpp"
11
12// X.h (of X11) has a macro called `Unsorted`
13// See <https://gitlab.freedesktop.org/xorg/proto/xorgproto/-/blob/eb28f0378fadd0d143aad7ec16f7b91814faae9a/include/X11/X.h#L540>
14// and <https://github.com/rerun-io/rerun/issues/7846>.
15#ifdef Unsorted
16#error \
17 "Found a macro 'Unsorted' (probably from X11), conflicting with `rerun::SortingStatus::Unsorted`. Add '#undef Unsorted' before '#include <rerun.hpp>' to work around this."
18#endif
19
20struct rr_time_column;
21
22namespace arrow {
23 class Array;
24}
25
26namespace rerun {
27 /// Describes whether an array is known to be sorted or not.
28 enum class SortingStatus {
29 /// It's not known whether the array is sorted or not.
30 Unknown = 0,
31
32 /// The array is known to be sorted.
33 Sorted = 1,
34
35 /// The array is known to be unsorted.
36 Unsorted = 2,
37 };
38
39 /// Arrow-encoded data for a column of time points.
40 ///
41 /// \see `rerun::RecordingStream::send_columns`
42 struct TimeColumn {
43 /// The timeline this column belongs to.
45
46 /// Time points as a primitive array of i64.
47 std::shared_ptr<arrow::Array> array;
48
49 /// The sorting order of the `times` array.
51
52 public:
53 /// Creates a time column from an array of time points.
54 ///
55 /// \param timeline The timeline this column belongs to.
56 /// \param times The time values.
57 /// Depending on the `TimeType` of the timeline this may be either sequence numbers, durations, or timestamps.
58 /// Make sure the sorting status is correctly specified.
59 /// \param sorting_status The sorting status of the time points.
60 /// Already sorted time points may perform better.
64 );
65
66 /// Creates a time column from an array of sequence points.
67 ///
68 /// \param timeline_name The name of the timeline this column belongs to.
69 /// \param sequence_points The sequence points.
70 /// Make sure the sorting status is correctly specified.
71 /// \param sorting_status The sorting status of the sequence points.
72 /// Already sorted time points may perform better.
73 ///
74 /// \deprecated Use `from_sequence` instead.
75 [[deprecated("Use `from_sequence` instead.")]] static TimeColumn from_sequence_points(
76 std::string timeline_name, Collection<int64_t> sequence_points,
78 ) {
79 return TimeColumn(
80 Timeline(std::move(timeline_name), TimeType::Sequence),
81 std::move(sequence_points),
83 );
84 }
85
86 /// Creates a column from an array of sequence points, e.g. frame numbers.
87 ///
88 /// \param timeline_name The name of the timeline this column belongs to.
89 /// \param sequence_points The sequence points.
90 /// Make sure the sorting status is correctly specified.
91 /// \param sorting_status The sorting status of the sequence points.
92 /// Already sorted time points may perform better.
94 std::string timeline_name, Collection<int64_t> sequence_points,
96 ) {
97 return TimeColumn(
98 Timeline(std::move(timeline_name), TimeType::Sequence),
99 std::move(sequence_points),
101 );
102 }
103
104 /// Creates a time column from an array of nanoseconds.
105 ///
106 /// \param timeline_name The name of the timeline this column belongs to.
107 /// \param times_in_nanoseconds Time values in nanoseconds.
108 /// Make sure the sorting status is correctly specified.
109 /// \param sorting_status The sorting status of the time points.
110 /// Already sorted time points may perform better.
111 [[deprecated("Use 'from_duration_nanos' or `from_nanos_since_epoch' instead"
112 )]] static TimeColumn
114 std::string timeline_name, Collection<int64_t> times_in_nanoseconds,
116 ) {
117 return TimeColumn(
118 Timeline(std::move(timeline_name), TimeType::Duration),
119 std::move(times_in_nanoseconds),
121 );
122 }
123
124 /// Creates a time column from an array of seconds.
125 ///
126 /// \param timeline_name The name of the timeline this column belongs to.
127 /// \param times_in_secs Time values in seconds.
128 /// Make sure the sorting status is correctly specified.
129 /// \param sorting_status The sorting status of the time points.
130 /// Already sorted time points may perform better.
131 [[deprecated("Use 'from_duration_secs' or `from_secs_since_epoch' instead"
132 )]] static TimeColumn
134 std::string timeline_name, Collection<double> times_in_secs,
136 );
137
138 // -----------
139 // Durations:
140
141 /// Creates a time column from an array of arbitrary std::chrono durations.
142 ///
143 /// \param timeline_name The name of the timeline this column belongs to.
144 /// \param durations Time values as chrono durations.
145 /// Make sure the sorting status is correctly specified.
146 /// \param sorting_status The sorting status of the time points.
147 /// Already sorted time points may perform better.
148 template <typename TRep, typename TPeriod>
150 std::string timeline_name,
151 const Collection<std::chrono::duration<TRep, TPeriod>>& durations,
153 ) {
154 std::vector<int64_t> times(durations.size());
155 for (size_t i = 0; i < durations.size(); i++) {
156 times[i] =
157 std::chrono::duration_cast<std::chrono::nanoseconds>(durations[i]).count();
158 }
159 return TimeColumn(
160 Timeline(std::move(timeline_name), TimeType::Duration),
161 std::move(times),
163 );
164 }
165
166 /// \deprecated Use `from_durations` instead.
167 template <typename TRep, typename TPeriod>
168 [[deprecated("Use `from_durations` instead.")]] static TimeColumn from_times(
169 std::string timeline_name,
170 const Collection<std::chrono::duration<TRep, TPeriod>>& chrono_times,
172 ) {
173 return from_durations<TRep, TPeriod>(timeline_name, chrono_times, sorting_status);
174 }
175
176 /// Creates a duration column from an array of nanoseconds.
177 ///
178 /// \param timeline_name The name of the timeline this column belongs to.
179 /// \param duration_in_nanos Duration values in nanoseconds.
180 /// Make sure the sorting status is correctly specified.
181 /// \param sorting_status The sorting status of the time points.
182 /// Already sorted time points may perform better.
184 std::string timeline_name, Collection<int64_t> duration_in_nanos,
186 ) {
187 return TimeColumn(
188 Timeline(std::move(timeline_name), TimeType::Duration),
189 std::move(duration_in_nanos),
191 );
192 }
193
194 /// Creates a duration column from an array of seconds.
195 ///
196 /// \param timeline_name The name of the timeline this column belongs to.
197 /// \param duration_in_secs Duration values in seconds.
198 /// Make sure the sorting status is correctly specified.
199 /// \param sorting_status The sorting status of the time points.
200 /// Already sorted time points may perform better.
202 std::string timeline_name, Collection<double> duration_in_secs,
204 ) {
205 std::vector<int64_t> duration_in_nanos;
206 duration_in_nanos.reserve(duration_in_secs.size());
207 for (auto time_in_secs : duration_in_secs) {
208 duration_in_nanos.push_back(std::llround(time_in_secs * 1.0e9));
209 }
210 return TimeColumn(
211 Timeline(std::move(timeline_name), TimeType::Duration),
212 std::move(duration_in_nanos),
214 );
215 }
216
217 // -----------
218 // Timestamps:
219
220 template <typename TClock>
221 static TimeColumn from_time_points(
222 std::string timeline_name,
223 const Collection<std::chrono::time_point<TClock>>& time_points,
225 ) {
226 std::vector<int64_t> nanos_since_epoch;
227 nanos_since_epoch.reserve(time_points.size());
228 for (auto timepoint : time_points) {
229 auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(
230 timepoint.time_since_epoch()
231 );
232 nanos_since_epoch.push_back(nanos.count());
233 }
235 std::move(timeline_name),
236 nanos_since_epoch,
238 );
239 }
240
241 /// Creates a timestamp column from an array of nanoseconds since Unix Epoch (1970-01-01 00:00:00 UTC).
242 ///
243 /// \param timeline_name The name of the timeline this column belongs to.
244 /// \param timestamp_in_nanos Timestamp values in nanoseconds.
245 /// Make sure the sorting status is correctly specified.
246 /// \param sorting_status The sorting status of the time points.
247 /// Already sorted time points may perform better.
249 std::string timeline_name, Collection<int64_t> timestamp_in_nanos,
251 ) {
252 return TimeColumn(
253 Timeline(std::move(timeline_name), TimeType::Timestamp),
254 std::move(timestamp_in_nanos),
256 );
257 }
258
259 /// Creates a duration column from an array of seconds since Unix Epoch (1970-01-01 00:00:00 UTC).
260 ///
261 /// \param timeline_name The name of the timeline this column belongs to.
262 /// \param timestamp_in_secs Timestamp values in seconds.
263 /// Make sure the sorting status is correctly specified.
264 /// \param sorting_status The sorting status of the time points.
265 /// Already sorted time points may perform better.
267 std::string timeline_name, Collection<double> timestamp_in_secs,
269 ) {
270 std::vector<int64_t> timestamp_in_nanos;
271 timestamp_in_nanos.reserve(timestamp_in_secs.size());
272 for (auto time_in_secs : timestamp_in_secs) {
273 timestamp_in_nanos.push_back(std::llround(time_in_secs * 1.0e9));
274 }
276 std::move(timeline_name),
277 std::move(timestamp_in_nanos),
279 );
280 }
281
282 // -----------------------------------------------------------------------------
283
284 /// To rerun C API component batch.
285 ///
286 /// The resulting `rr_time_column` keeps the `arrow::Array` alive until it is released.
287 Error to_c_ffi_struct(rr_time_column& out_column) const;
288 };
289} // 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:295
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:103
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
@ Sequence
Used e.g. for frames in a film.
@ Timestamp
Nanoseconds since Unix epoch (1970-01-01 00:00:00 UTC).
@ Duration
Nanoseconds.
SortingStatus
Describes whether an array is known to be sorted or not.
Definition time_column.hpp:28
@ Sorted
The array is known to be sorted.
@ Unsorted
The array is known to be unsorted.
@ Unknown
It's not known whether the array is sorted or not.
Arrow-encoded data for a column of time points.
Definition time_column.hpp:42
Timeline timeline
The timeline this column belongs to.
Definition time_column.hpp:44
TimeColumn(Timeline timeline, Collection< int64_t > times, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a time column from an array of time points.
std::shared_ptr< arrow::Array > array
Time points as a primitive array of i64.
Definition time_column.hpp:47
static TimeColumn from_times(std::string timeline_name, const Collection< std::chrono::duration< TRep, TPeriod > > &chrono_times, SortingStatus sorting_status=SortingStatus::Unknown)
Definition time_column.hpp:168
static TimeColumn from_nanoseconds(std::string timeline_name, Collection< int64_t > times_in_nanoseconds, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a time column from an array of nanoseconds.
Definition time_column.hpp:113
static TimeColumn from_duration_secs(std::string timeline_name, Collection< double > duration_in_secs, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a duration column from an array of seconds.
Definition time_column.hpp:201
static TimeColumn from_seconds(std::string timeline_name, Collection< double > times_in_secs, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a time column from an array of seconds.
Error to_c_ffi_struct(rr_time_column &out_column) const
To rerun C API component batch.
static TimeColumn from_duration_nanoseconds(std::string timeline_name, Collection< int64_t > duration_in_nanos, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a duration column from an array of nanoseconds.
Definition time_column.hpp:183
static TimeColumn from_sequence(std::string timeline_name, Collection< int64_t > sequence_points, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a column from an array of sequence points, e.g.
Definition time_column.hpp:93
static TimeColumn from_durations(std::string timeline_name, const Collection< std::chrono::duration< TRep, TPeriod > > &durations, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a time column from an array of arbitrary std::chrono durations.
Definition time_column.hpp:149
static TimeColumn from_sequence_points(std::string timeline_name, Collection< int64_t > sequence_points, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a time column from an array of sequence points.
Definition time_column.hpp:75
static TimeColumn from_secs_since_epoch(std::string timeline_name, Collection< double > timestamp_in_secs, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a duration column from an array of seconds since Unix Epoch (1970-01-01 00:00:00 UTC).
Definition time_column.hpp:266
SortingStatus sorting_status
The sorting order of the times array.
Definition time_column.hpp:50
static TimeColumn from_nanos_since_epoch(std::string timeline_name, Collection< int64_t > timestamp_in_nanos, SortingStatus sorting_status=SortingStatus::Unknown)
Creates a timestamp column from an array of nanoseconds since Unix Epoch (1970-01-01 00:00:00 UTC).
Definition time_column.hpp:248
Definition of a timeline.
Definition timeline.hpp:23