Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
rerun_compat.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <rerun.hpp>
11
12#include <cstdint>
13#include <string>
14
15// Rerun SDK version compatibility layer
16// This header provides compatibility between rerun 0.23.x (legacy) and 0.29.x+ (latest) APIs.
17//
18// Minimum supported versions:
19// - rerun-latest: >=0.29.0
20// - rerun-legacy: >=0.23.3,<0.24
21//
22// Key API changes between versions:
23// - 0.24+: Scalar -> Scalars (takes batch of values)
24// - 0.24+: SeriesLine -> SeriesLines
25// - 0.24+: SeriesPoint -> SeriesPoints
26// - 0.26+: set_time_seconds deprecated -> use set_time_duration_secs
27// - 0.28+: Transform3D::with_axis_length removed (use Axes3D archetype instead)
28// - 0.29+: No breaking API changes affecting this codebase
29//
30// The RERUN_VERSION_GE macro is available since rerun 0.18.
31
32namespace momentum {
33
34// Helper function to log a single scalar value
35// In rerun 0.24+, Scalar was renamed to Scalars and takes a batch of values
36// This wrapper provides a consistent API across versions
37template <typename T>
38inline auto makeScalar([[maybe_unused]] T value) {
39#if defined(RERUN_VERSION_GE) && RERUN_VERSION_GE(0, 24, 0)
40 // Rerun 0.24+: Use Scalars with a single-element batch
41 return rerun::Scalars({static_cast<double>(value)});
42#else
43 // Rerun 0.23.x: Use Scalar with a single value
44 return rerun::Scalar(value);
45#endif
46}
47
48// Helper function to create a SeriesLine configuration
49// In rerun 0.24+, SeriesLine was renamed to SeriesLines
50inline auto makeSeriesLine() {
51#if defined(RERUN_VERSION_GE) && RERUN_VERSION_GE(0, 24, 0)
52 return rerun::SeriesLines();
53#else
54 return rerun::SeriesLine();
55#endif
56}
57
58// Helper function to create a SeriesLine with a name
59inline auto makeSeriesLineWithName(const std::string& name) {
60#if defined(RERUN_VERSION_GE) && RERUN_VERSION_GE(0, 24, 0)
61 return rerun::SeriesLines().with_names({name});
62#else
63 return rerun::SeriesLine().with_name(name);
64#endif
65}
66
67// Helper function to set time in seconds on a recording stream
68// In rerun 0.26+, set_time_seconds was deprecated in favor of set_time_duration_secs
69inline void
70setTimeSeconds(const rerun::RecordingStream& rec, const std::string& timelineName, double seconds) {
71#if defined(RERUN_VERSION_GE) && RERUN_VERSION_GE(0, 26, 0)
72 rec.set_time_duration_secs(timelineName, seconds);
73#else
74 rec.set_time_seconds(timelineName, seconds);
75#endif
76}
77
78// Helper function to create a Transform3D with axis visualization
79// In rerun 0.28+, with_axis_length was removed from Transform3D
80// Instead, Axes3D archetype should be used for axis visualization
81// This wrapper returns the transform without axis_length for 0.28+
82// Callers should log a separate Axes3D archetype if axis visualization is needed
83template <typename... Args>
84inline auto makeTransform3D(Args&&... args) {
85 return rerun::Transform3D(std::forward<Args>(args)...);
86}
87
88// Log coordinate axes at a given entity path
89// In rerun 0.28+, Transform3D::with_axis_length was removed
90// The axis visualization is omitted in newer versions
91inline void logAxes3D(
92 [[maybe_unused]] const rerun::RecordingStream& rec,
93 [[maybe_unused]] const std::string& path,
94 [[maybe_unused]] float axisLength) {
95#if !defined(RERUN_VERSION_GE) || !RERUN_VERSION_GE(0, 24, 0)
96 (void)rec;
97 (void)path;
98 (void)axisLength;
99 // In 0.23.x, axis_length is set directly on Transform3D (handled by caller)
100#endif
101 // In 0.24+, axis visualization on transforms is not supported
102 // To visualize axes, users should use Arrows3D archetype manually
103}
104
105// Helper function to create a sequence TimeColumn
106// Used by send_columns-based batch logging functions
107inline rerun::TimeColumn makeSequenceTimeColumn(
108 std::string timelineName,
109 rerun::Collection<int64_t> values,
110 rerun::SortingStatus sortingStatus = rerun::SortingStatus::Unknown) {
111 return rerun::TimeColumn::from_sequence(
112 std::move(timelineName), std::move(values), sortingStatus);
113}
114
115// Helper function to create a duration seconds TimeColumn
116// In rerun 0.26+, from_seconds was deprecated in favor of from_duration_secs
117inline rerun::TimeColumn makeDurationSecondsTimeColumn(
118 std::string timelineName,
119 rerun::Collection<double> values,
120 rerun::SortingStatus sortingStatus = rerun::SortingStatus::Unknown) {
121#if defined(RERUN_VERSION_GE) && RERUN_VERSION_GE(0, 26, 0)
122 return rerun::TimeColumn::from_duration_secs(
123 std::move(timelineName), std::move(values), sortingStatus);
124#else
125 return rerun::TimeColumn::from_seconds(std::move(timelineName), std::move(values), sortingStatus);
126#endif
127}
128
129// Helper function to create scalar ComponentColumns for send_columns
130// In rerun 0.24+, Scalar was renamed to Scalars
131inline rerun::Collection<rerun::ComponentColumn> makeScalarColumns(std::vector<double> values) {
132#if defined(RERUN_VERSION_GE) && RERUN_VERSION_GE(0, 24, 0)
133 return rerun::Scalars(std::move(values)).columns();
134#else
135 return rerun::Scalar().with_many_scalar(std::move(values)).columns();
136#endif
137}
138
145template <typename T>
146inline bool hasComponentData(const T& archetype) {
147 const auto result = rerun::AsComponents<T>().as_batches(archetype);
148 return result.is_ok() && !result.value.empty();
149}
150
156template <typename T>
157inline void
158safeLog(const rerun::RecordingStream& rec, std::string_view entityPath, const T& archetype) {
159 auto result = rerun::AsComponents<T>().as_batches(archetype);
160 if (result.is_err() || result.value.empty()) {
161 return;
162 }
163 rec.try_log_serialized_batches(entityPath, false, std::move(result.value).to_vector()).handle();
164}
165
167template <typename T>
168inline void
169safeLogStatic(const rerun::RecordingStream& rec, std::string_view entityPath, const T& archetype) {
170 auto result = rerun::AsComponents<T>().as_batches(archetype);
171 if (result.is_err() || result.value.empty()) {
172 return;
173 }
174 rec.try_log_serialized_batches(entityPath, true, std::move(result.value).to_vector()).handle();
175}
176
177} // namespace momentum
rerun::TimeColumn makeSequenceTimeColumn(std::string timelineName, rerun::Collection< int64_t > values, rerun::SortingStatus sortingStatus=rerun::SortingStatus::Unknown)
void logAxes3D(const rerun::RecordingStream &rec, const std::string &path, float axisLength)
bool hasComponentData(const T &archetype)
Check whether an archetype has any component data to log.
void setTimeSeconds(const rerun::RecordingStream &rec, const std::string &timelineName, double seconds)
auto makeSeriesLineWithName(const std::string &name)
rerun::TimeColumn makeDurationSecondsTimeColumn(std::string timelineName, rerun::Collection< double > values, rerun::SortingStatus sortingStatus=rerun::SortingStatus::Unknown)
auto makeScalar(T value)
auto makeSeriesLine()
rerun::Collection< rerun::ComponentColumn > makeScalarColumns(std::vector< double > values)
auto makeTransform3D(Args &&... args)
void safeLog(const rerun::RecordingStream &rec, std::string_view entityPath, const T &archetype)
Safe wrapper for RecordingStream::log that skips empty archetypes.
void safeLogStatic(const rerun::RecordingStream &rec, std::string_view entityPath, const T &archetype)
Safe wrapper for RecordingStream::log_static that skips empty archetypes.