Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Rerun 0.11 #18

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/d5153cb/rerun_cpp_sdk.zip) # TODO: 2023-11-24. 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.

89 changes: 89 additions & 0 deletions src/collection_adapters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#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 will 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 will 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
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
);

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

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

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));
}
};

// Adapter for extracting tensor dimensions from an OpenCV matrix.
// TODO(https://github.com/rerun-io/rerun/pull/4331): remove `datatypes::`
template <>
struct rerun::CollectionAdapter<rerun::datatypes::TensorDimension, cv::Mat> {
/// Only overload the const& operator since there is no way of borrowing the dimensions anyways.
Collection<rerun::datatypes::TensorDimension> operator()(const cv::Mat& img) {
return {
static_cast<size_t>(img.rows),
static_cast<size_t>(img.cols),
static_cast<size_t>(img.channels()),
};
}
};
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 5 additions & 16 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 Down Expand Up @@ -51,8 +51,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 @@ -65,20 +65,9 @@ int main() {
}

// Log image to Rerun
// TODO(https://github.com/rerun-io/rerun/pull/4331): remove `datatypes::`
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)
)
);
rec.log("image", rerun::Image(img, rerun::datatypes::TensorBuffer::u8(img)));
Wumpf marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}