Skip to content

Commit 05bada5

Browse files
authored
Merge pull request #9 from rerun-io/improve-adapters
Improve batch adapters
2 parents 8e11fe7 + 5baccea commit 05bada5

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
99
endif()
1010

1111
include(FetchContent)
12-
FetchContent_Declare(rerun_sdk URL https://build.rerun.io/commit/bc05d11/rerun_cpp_sdk.zip) # 2023-10-26
12+
FetchContent_Declare(rerun_sdk URL https://build.rerun.io/commit/b7f404a/rerun_cpp_sdk.zip) # 2023-10-29
1313
FetchContent_MakeAvailable(rerun_sdk)
1414

1515
find_package(Eigen3 REQUIRED)

src/batch_adapters.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <rerun.hpp>
4+
5+
// Adapters so we can log eigen vectors as rerun positions:
6+
template <>
7+
struct rerun::ComponentBatchAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
8+
ComponentBatch<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) {
9+
return ComponentBatch<rerun::Position3D>::borrow(container.data(), container.size());
10+
}
11+
12+
ComponentBatch<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) {
13+
std::vector<rerun::Position3D> positions(container.size());
14+
memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f));
15+
return ComponentBatch<rerun::Position3D>::take_ownership(std::move(positions));
16+
}
17+
};
18+
19+
// Adapters so we can log an eigen matrix as rerun positions:
20+
template <>
21+
struct rerun::ComponentBatchAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
22+
ComponentBatch<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) {
23+
// Sanity check that this is binary compatible.
24+
static_assert(
25+
sizeof(rerun::Position3D) ==
26+
sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime
27+
);
28+
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf::Scalar));
29+
return ComponentBatch<rerun::Position3D>::borrow(
30+
// Cast to void because otherwise Rerun will try to do above sanity checks with the wrong type (scalar).
31+
reinterpret_cast<const void*>(matrix.data()),
32+
matrix.cols()
33+
);
34+
}
35+
36+
ComponentBatch<rerun::Position3D> operator()(Eigen::Matrix3Xf&& matrix) {
37+
std::vector<rerun::Position3D> positions(matrix.cols());
38+
memcpy(positions.data(), matrix.data(), matrix.size() * sizeof(rerun::Position3D));
39+
return ComponentBatch<rerun::Position3D>::take_ownership(std::move(positions));
40+
}
41+
};

src/main.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,7 @@
88
#include <opencv2/imgproc.hpp>
99
#include <rerun.hpp>
1010

11-
// Adapters so we can log eigen vectors as rerun positions:
12-
template <>
13-
struct rerun::ComponentBatchAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
14-
// Sanity check that this is binary compatible.
15-
static_assert(sizeof(rerun::Position3D) == sizeof(Eigen::Vector3f));
16-
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Vector3f));
17-
18-
ComponentBatch<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) {
19-
return ComponentBatch<rerun::Position3D>::borrow(
20-
reinterpret_cast<const rerun::Position3D*>(container.data()),
21-
container.size()
22-
);
23-
}
24-
25-
ComponentBatch<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) {
26-
throw std::runtime_error("Not implemented for temporaries");
27-
}
28-
};
29-
30-
// Adapters so we can log an eigen matrix as rerun positions:
31-
template <>
32-
struct rerun::ComponentBatchAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
33-
// Sanity check that this is binary compatible.
34-
static_assert(
35-
sizeof(rerun::Position3D) ==
36-
sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime
37-
);
38-
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf));
39-
40-
ComponentBatch<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) {
41-
return ComponentBatch<rerun::Position3D>::borrow(
42-
reinterpret_cast<const rerun::Position3D*>(matrix.data()),
43-
matrix.cols()
44-
);
45-
}
46-
47-
ComponentBatch<rerun::Position3D> operator()(std::vector<Eigen::Matrix3Xf>&& container) {
48-
throw std::runtime_error("Not implemented for temporaries");
49-
}
50-
};
11+
#include "batch_adapters.hpp"
5112

5213
std::vector<Eigen::Vector3f> generate_random_points_vector(int num_points) {
5314
std::vector<Eigen::Vector3f> points(num_points);

0 commit comments

Comments
 (0)