Skip to content

Commit 283fed5

Browse files
committed
use collection adpaters for tensor buffer
1 parent b7439e0 commit 283fed5

File tree

4 files changed

+76
-58
lines changed

4 files changed

+76
-58
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://github.com/rerun-io/rerun/releases/download/0.10.1/rerun_cpp_sdk.zip)
12+
FetchContent_Declare(rerun_sdk URL https://build.rerun.io/commit/d5153cb/rerun_cpp_sdk.zip) # TODO: 2023-11-24. Update to latest commit.
1313
FetchContent_MakeAvailable(rerun_sdk)
1414

1515
find_package(Eigen3 REQUIRED)

src/batch_adapters.hpp

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/collection_adapters.hpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <rerun.hpp>
4+
5+
#include <eigen3/Eigen/Core>
6+
#include <opencv2/core.hpp>
7+
8+
// Adapters so we can log eigen vectors as rerun positions:
9+
template <>
10+
struct rerun::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
11+
Collection<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) {
12+
return Collection<rerun::Position3D>::borrow(container.data(), container.size());
13+
}
14+
15+
Collection<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) {
16+
std::vector<rerun::Position3D> positions(container.size());
17+
memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f));
18+
return Collection<rerun::Position3D>::take_ownership(std::move(positions));
19+
}
20+
};
21+
22+
// Adapters so we can log an eigen matrix as rerun positions:
23+
template <>
24+
struct rerun::CollectionAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
25+
Collection<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) {
26+
// Sanity check that this is binary compatible.
27+
static_assert(
28+
sizeof(rerun::Position3D) ==
29+
sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime
30+
);
31+
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf::Scalar));
32+
return Collection<rerun::Position3D>::borrow(
33+
// Cast to void because otherwise Rerun will try to do above sanity checks with the wrong type (scalar).
34+
reinterpret_cast<const void*>(matrix.data()),
35+
matrix.cols()
36+
);
37+
}
38+
39+
Collection<rerun::Position3D> operator()(Eigen::Matrix3Xf&& matrix) {
40+
std::vector<rerun::Position3D> positions(matrix.cols());
41+
memcpy(positions.data(), matrix.data(), matrix.size() * sizeof(rerun::Position3D));
42+
return Collection<rerun::Position3D>::take_ownership(std::move(positions));
43+
}
44+
};
45+
46+
// Adapters so we can borrow an OpenCV image easily into Rerun images without copying:
47+
template <>
48+
struct rerun::CollectionAdapter<uint8_t, cv::Mat> {
49+
Collection<uint8_t> operator()(const cv::Mat& img) {
50+
return Collection<uint8_t>::borrow(img.data, img.total() * img.channels());
51+
}
52+
53+
Collection<uint8_t> operator()(cv::Mat&& img) {
54+
std::vector<uint8_t> img_vec(img.total() * img.channels());
55+
img_vec.assign(img.data, img.data + img.total() * img.channels());
56+
return Collection<uint8_t>::take_ownership(std::move(img_vec));
57+
}
58+
};
59+
60+
// Adapter for extracting tensor dimensions from an OpenCV matrix.
61+
template <>
62+
struct rerun::CollectionAdapter<rerun::datatypes::TensorDimension, cv::Mat> {
63+
Collection<rerun::datatypes::TensorDimension> operator()(const cv::Mat& img) {
64+
// Only specify the const& operator since there is no way of borrowing the dimensions anyways.
65+
return {
66+
static_cast<size_t>(img.rows),
67+
static_cast<size_t>(img.cols),
68+
static_cast<size_t>(img.channels()),
69+
};
70+
}
71+
};

src/main.cpp

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

11-
#include "batch_adapters.hpp"
11+
#include "collection_adapters.hpp"
1212

1313
std::vector<Eigen::Vector3f> generate_random_points_vector(int num_points) {
1414
std::vector<Eigen::Vector3f> points(num_points);
@@ -51,8 +51,8 @@ int main() {
5151
rec.log(
5252
"world/camera",
5353
rerun::Transform3D(
54-
rerun::datatypes::Vec3D(camera_position.data()),
55-
rerun::datatypes::Mat3x3(camera_orientation.data())
54+
rerun::Vec3D(camera_position.data()),
55+
rerun::Mat3x3(camera_orientation.data())
5656
)
5757
);
5858

@@ -66,19 +66,7 @@ int main() {
6666

6767
// Log image to Rerun
6868
cv::cvtColor(img, img, cv::COLOR_BGR2RGB); // Rerun expects RGB format
69-
// NOTE currently we need to construct a vector to log an image, this will change in the future
70-
// see https://github.com/rerun-io/rerun/issues/3794
71-
std::vector<uint8_t> img_vec(img.total() * img.channels());
72-
img_vec.assign(img.data, img.data + img.total() * img.channels());
73-
rec.log(
74-
"image",
75-
rerun::Image(
76-
{static_cast<size_t>(img.rows),
77-
static_cast<size_t>(img.cols),
78-
static_cast<size_t>(img.channels())},
79-
std::move(img_vec)
80-
)
81-
);
69+
rec.log("image", rerun::Image(img, rerun::datatypes::TensorBuffer::u8(img)));
8270

8371
return 0;
8472
}

0 commit comments

Comments
 (0)