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

Improve batch adapters #9

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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://build.rerun.io/commit/bc05d11/rerun_cpp_sdk.zip) # 2023-10-26
FetchContent_Declare(rerun_sdk URL https://build.rerun.io/commit/b7f404a/rerun_cpp_sdk.zip) # 2023-10-29
FetchContent_MakeAvailable(rerun_sdk)

find_package(Eigen3 REQUIRED)
Expand Down
41 changes: 41 additions & 0 deletions src/batch_adapters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <rerun.hpp>

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

ComponentBatch<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 ComponentBatch<rerun::Position3D>::take_ownership(std::move(positions));
}
};

// Adapters so we can log an eigen matrix as rerun positions:
template <>
struct rerun::ComponentBatchAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
ComponentBatch<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) {
// Sanity check that this is binary compatible.
static_assert(
sizeof(rerun::Position3D) ==
sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime
);
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf::Scalar));
return ComponentBatch<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()
);
}

ComponentBatch<rerun::Position3D> operator()(Eigen::Matrix3Xf&& matrix) {
std::vector<rerun::Position3D> positions(matrix.cols());
memcpy(positions.data(), matrix.data(), matrix.size() * sizeof(rerun::Position3D));
return ComponentBatch<rerun::Position3D>::take_ownership(std::move(positions));
}
};
41 changes: 1 addition & 40 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,7 @@
#include <opencv2/imgproc.hpp>
#include <rerun.hpp>

// Adapters so we can log eigen vectors as rerun positions:
template <>
struct rerun::ComponentBatchAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
// Sanity check that this is binary compatible.
static_assert(sizeof(rerun::Position3D) == sizeof(Eigen::Vector3f));
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Vector3f));

ComponentBatch<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) {
return ComponentBatch<rerun::Position3D>::borrow(
reinterpret_cast<const rerun::Position3D*>(container.data()),
container.size()
);
}

ComponentBatch<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) {
throw std::runtime_error("Not implemented for temporaries");
}
};

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

ComponentBatch<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) {
return ComponentBatch<rerun::Position3D>::borrow(
reinterpret_cast<const rerun::Position3D*>(matrix.data()),
matrix.cols()
);
}

ComponentBatch<rerun::Position3D> operator()(std::vector<Eigen::Matrix3Xf>&& container) {
throw std::runtime_error("Not implemented for temporaries");
}
};
#include "batch_adapters.hpp"

std::vector<Eigen::Vector3f> generate_random_points_vector(int num_points) {
std::vector<Eigen::Vector3f> points(num_points);
Expand Down