|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <rerun.hpp> |
| 4 | + |
| 5 | +#include <eigen3/Eigen/Core> |
| 6 | +#include <opencv2/core.hpp> |
| 7 | + |
| 8 | +#include <cassert> |
| 9 | + |
| 10 | +// Adapters so we can log eigen vectors as rerun positions: |
| 11 | +template <> |
| 12 | +struct rerun::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> { |
| 13 | + /// Borrow for non-temporary. |
| 14 | + Collection<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) { |
| 15 | + return Collection<rerun::Position3D>::borrow(container.data(), container.size()); |
| 16 | + } |
| 17 | + |
| 18 | + // Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed). |
| 19 | + Collection<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) { |
| 20 | + std::vector<rerun::Position3D> positions(container.size()); |
| 21 | + memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f)); |
| 22 | + return Collection<rerun::Position3D>::take_ownership(std::move(positions)); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +// Adapters so we can log an eigen matrix as rerun positions: |
| 27 | +template <> |
| 28 | +struct rerun::CollectionAdapter<rerun::Position3D, Eigen::Matrix3Xf> { |
| 29 | + // Sanity check that this is binary compatible. |
| 30 | + static_assert( |
| 31 | + sizeof(rerun::Position3D) == |
| 32 | + sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime |
| 33 | + ); |
| 34 | + |
| 35 | + /// Borrow for non-temporary. |
| 36 | + Collection<rerun::Position3D> operator()(const Eigen::Matrix3Xf& matrix) { |
| 37 | + static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf::Scalar)); |
| 38 | + return Collection<rerun::Position3D>::borrow( |
| 39 | + // Cast to void because otherwise Rerun will try to do above sanity checks with the wrong type (scalar). |
| 40 | + reinterpret_cast<const void*>(matrix.data()), |
| 41 | + matrix.cols() |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + // Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed). |
| 46 | + Collection<rerun::Position3D> operator()(Eigen::Matrix3Xf&& matrix) { |
| 47 | + std::vector<rerun::Position3D> positions(matrix.cols()); |
| 48 | + memcpy(positions.data(), matrix.data(), matrix.size() * sizeof(rerun::Position3D)); |
| 49 | + return Collection<rerun::Position3D>::take_ownership(std::move(positions)); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +// Adapters so we can borrow an OpenCV image easily into Rerun images without copying: |
| 54 | +template <> |
| 55 | +struct rerun::CollectionAdapter<uint8_t, cv::Mat> { |
| 56 | + /// Borrow for non-temporary. |
| 57 | + Collection<uint8_t> operator()(const cv::Mat& img) { |
| 58 | + assert( |
| 59 | + "OpenCV matrix was expected have bit depth CV_U8" && CV_MAT_DEPTH(img.type()) == CV_8U |
| 60 | + ); |
| 61 | + |
| 62 | + return Collection<uint8_t>::borrow(img.data, img.total() * img.channels()); |
| 63 | + } |
| 64 | + |
| 65 | + // Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed). |
| 66 | + Collection<uint8_t> operator()(cv::Mat&& img) { |
| 67 | + assert( |
| 68 | + "OpenCV matrix was expected have bit depth CV_U8" && CV_MAT_DEPTH(img.type()) == CV_8U |
| 69 | + ); |
| 70 | + |
| 71 | + std::vector<uint8_t> img_vec(img.total() * img.channels()); |
| 72 | + img_vec.assign(img.data, img.data + img.total() * img.channels()); |
| 73 | + return Collection<uint8_t>::take_ownership(std::move(img_vec)); |
| 74 | + } |
| 75 | +}; |
0 commit comments