Skip to content

Commit 6480a82

Browse files
committed
add assertion, comment improvements
1 parent 516b8f4 commit 6480a82

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/collection_adapters.hpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
#include <eigen3/Eigen/Core>
66
#include <opencv2/core.hpp>
77

8+
#include <cassert>
9+
810
// Adapters so we can log eigen vectors as rerun positions:
911
template <>
1012
struct rerun::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
13+
/// Borrow for non-temporary.
1114
Collection<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f>& container) {
1215
return Collection<rerun::Position3D>::borrow(container.data(), container.size());
1316
}
1417

18+
// Do a full copy for temporaries (otherwise the data will be deleted when the temporary is destroyed).
1519
Collection<rerun::Position3D> operator()(std::vector<Eigen::Vector3f>&& container) {
1620
std::vector<rerun::Position3D> positions(container.size());
1721
memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f));
@@ -22,12 +26,14 @@ struct rerun::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>>
2226
// Adapters so we can log an eigen matrix as rerun positions:
2327
template <>
2428
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.
2536
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-
);
3137
static_assert(alignof(rerun::Position3D) <= alignof(Eigen::Matrix3Xf::Scalar));
3238
return Collection<rerun::Position3D>::borrow(
3339
// Cast to void because otherwise Rerun will try to do above sanity checks with the wrong type (scalar).
@@ -36,6 +42,7 @@ struct rerun::CollectionAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
3642
);
3743
}
3844

45+
// Do a full copy for temporaries (otherwise the data will be deleted when the temporary is destroyed).
3946
Collection<rerun::Position3D> operator()(Eigen::Matrix3Xf&& matrix) {
4047
std::vector<rerun::Position3D> positions(matrix.cols());
4148
memcpy(positions.data(), matrix.data(), matrix.size() * sizeof(rerun::Position3D));
@@ -46,11 +53,21 @@ struct rerun::CollectionAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
4653
// Adapters so we can borrow an OpenCV image easily into Rerun images without copying:
4754
template <>
4855
struct rerun::CollectionAdapter<uint8_t, cv::Mat> {
56+
/// Borrow for non-temporary.
4957
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+
5062
return Collection<uint8_t>::borrow(img.data, img.total() * img.channels());
5163
}
5264

65+
// Do a full copy for temporaries (otherwise the data will be deleted when the temporary is destroyed).
5366
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+
5471
std::vector<uint8_t> img_vec(img.total() * img.channels());
5572
img_vec.assign(img.data, img.data + img.total() * img.channels());
5673
return Collection<uint8_t>::take_ownership(std::move(img_vec));
@@ -61,8 +78,8 @@ struct rerun::CollectionAdapter<uint8_t, cv::Mat> {
6178
// TODO(https://github.com/rerun-io/rerun/pull/4331): remove `datatypes::`
6279
template <>
6380
struct rerun::CollectionAdapter<rerun::datatypes::TensorDimension, cv::Mat> {
81+
/// Only overload the const& operator since there is no way of borrowing the dimensions anyways.
6482
Collection<rerun::datatypes::TensorDimension> operator()(const cv::Mat& img) {
65-
// Only specify the const& operator since there is no way of borrowing the dimensions anyways.
6683
return {
6784
static_cast<size_t>(img.rows),
6885
static_cast<size_t>(img.cols),

0 commit comments

Comments
 (0)