Skip to content

Commit

Permalink
Merge pull request #18 from rerun-io/andreas/rerun-0.11
Browse files Browse the repository at this point in the history
Update for Rerun 0.11
  • Loading branch information
Wumpf authored Nov 28, 2023
2 parents b7439e0 + a9d8650 commit 0098d17
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
endif()

include(FetchContent)
FetchContent_Declare(rerun_sdk URL https://github.com/rerun-io/rerun/releases/download/0.10.1/rerun_cpp_sdk.zip)
FetchContent_Declare(rerun_sdk URL https://build.rerun.io/commit/746dbf3/rerun_cpp_sdk.zip) # TODO: 2023-11-28. Update to latest commit.
FetchContent_MakeAvailable(rerun_sdk)

find_package(Eigen3 REQUIRED)
Expand Down
41 changes: 0 additions & 41 deletions src/batch_adapters.hpp

This file was deleted.

75 changes: 75 additions & 0 deletions src/collection_adapters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <rerun.hpp>

#include <eigen3/Eigen/Core>
#include <opencv2/core.hpp>

#include <cassert>

// Adapters so we can log eigen vectors as rerun positions:
template <>
struct rerun::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
/// Borrow for non-temporary.
Collection<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) {
return Collection<rerun::Position3D>::borrow(container.data(), container.size());
}

// Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed).
Collection<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) {
std::vector<rerun::Position3D> positions(container.size());
memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f));
return Collection<rerun::Position3D>::take_ownership(std::move(positions));
}
};

// Adapters so we can log an eigen matrix as rerun positions:
template <>
struct rerun::CollectionAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
// Sanity check that this is binary compatible.
static_assert(
sizeof(rerun::Position3D) ==
sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime
);

/// Borrow for non-temporary.
Collection<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) {
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf::Scalar));
return Collection<rerun::Position3D>::borrow(
// Cast to void because otherwise Rerun will try to do above sanity checks with the wrong type (scalar).
reinterpret_cast<const void*>(matrix.data()),
matrix.cols()
);
}

// Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed).
Collection<rerun::Position3D> operator()(Eigen::Matrix3Xf&& matrix) {
std::vector<rerun::Position3D> positions(matrix.cols());
memcpy(positions.data(), matrix.data(), matrix.size() * sizeof(rerun::Position3D));
return Collection<rerun::Position3D>::take_ownership(std::move(positions));
}
};

// Adapters so we can borrow an OpenCV image easily into Rerun images without copying:
template <>
struct rerun::CollectionAdapter<uint8_t, cv::Mat> {
/// Borrow for non-temporary.
Collection<uint8_t> operator()(const cv::Mat& img) {
assert(
"OpenCV matrix was expected have bit depth CV_U8" && CV_MAT_DEPTH(img.type()) == CV_8U
);

return Collection<uint8_t>::borrow(img.data, img.total() * img.channels());
}

// Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed).
Collection<uint8_t> operator()(cv::Mat&& img) {
assert(
"OpenCV matrix was expected have bit depth CV_U8" && CV_MAT_DEPTH(img.type()) == CV_8U
);

std::vector<uint8_t> img_vec(img.total() * img.channels());
img_vec.assign(img.data, img.data + img.total() * img.channels());
return Collection<uint8_t>::take_ownership(std::move(img_vec));
}
};
34 changes: 16 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <opencv2/imgproc.hpp>
#include <rerun.hpp>

#include "batch_adapters.hpp"
#include "collection_adapters.hpp"

std::vector<Eigen::Vector3f> generate_random_points_vector(int num_points) {
std::vector<Eigen::Vector3f> points(num_points);
Expand All @@ -18,6 +18,10 @@ std::vector<Eigen::Vector3f> generate_random_points_vector(int num_points) {
return points;
}

rerun::Collection<rerun::TensorDimension> tensor_shape(const cv::Mat& img) {
return {img.rows, img.cols, img.channels()};
};

int main() {
const auto rec = rerun::RecordingStream("rerun_example_cpp");
rec.spawn().exit_on_failure();
Expand Down Expand Up @@ -51,8 +55,8 @@ int main() {
rec.log(
"world/camera",
rerun::Transform3D(
rerun::datatypes::Vec3D(camera_position.data()),
rerun::datatypes::Mat3x3(camera_orientation.data())
rerun::Vec3D(camera_position.data()),
rerun::Mat3x3(camera_orientation.data())
)
);

Expand All @@ -64,21 +68,15 @@ int main() {
return 1;
}

// Log image to Rerun
cv::cvtColor(img, img, cv::COLOR_BGR2RGB); // Rerun expects RGB format
// NOTE currently we need to construct a vector to log an image, this will change in the future
// see https://github.com/rerun-io/rerun/issues/3794
std::vector<uint8_t> img_vec(img.total() * img.channels());
img_vec.assign(img.data, img.data + img.total() * img.channels());
rec.log(
"image",
rerun::Image(
{static_cast<size_t>(img.rows),
static_cast<size_t>(img.cols),
static_cast<size_t>(img.channels())},
std::move(img_vec)
)
);
// Rerun expects RGB format
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);

// Log image to rerun using the tensor buffer adapter defined in `collection_adapters.hpp`.
rec.log("image0", rerun::Image(tensor_shape(img), rerun::TensorBuffer::u8(img)));

// Or by passing a pointer to the image data.
// The pointer cast here is redundant since `data` is already uint8_t in this case, but if you have e.g. a float image it may be necessary to cast to float*.
rec.log("image1", rerun::Image(tensor_shape(img), reinterpret_cast<const uint8_t*>(img.data)));

return 0;
}

0 comments on commit 0098d17

Please sign in to comment.