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