5
5
#include < eigen3/Eigen/Core>
6
6
#include < opencv2/core.hpp>
7
7
8
+ #include < cassert>
9
+
8
10
// Adapters so we can log eigen vectors as rerun positions:
9
11
template <>
10
12
struct rerun ::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>> {
13
+ // / Borrow for non-temporary.
11
14
Collection<rerun::Position3D> operator ()(const std::vector<Eigen::Vector3f>& container) {
12
15
return Collection<rerun::Position3D>::borrow (container.data (), container.size ());
13
16
}
14
17
18
+ // Do a full copy for temporaries (otherwise the data will be deleted when the temporary is destroyed).
15
19
Collection<rerun::Position3D> operator ()(std::vector<Eigen::Vector3f>&& container) {
16
20
std::vector<rerun::Position3D> positions (container.size ());
17
21
memcpy (positions.data (), container.data (), container.size () * sizeof (Eigen::Vector3f));
@@ -22,12 +26,14 @@ struct rerun::CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>>
22
26
// Adapters so we can log an eigen matrix as rerun positions:
23
27
template <>
24
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.
25
36
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
- );
31
37
static_assert (alignof (rerun::Position3D) <= alignof (Eigen::Matrix3Xf::Scalar));
32
38
return Collection<rerun::Position3D>::borrow (
33
39
// 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> {
36
42
);
37
43
}
38
44
45
+ // Do a full copy for temporaries (otherwise the data will be deleted when the temporary is destroyed).
39
46
Collection<rerun::Position3D> operator ()(Eigen::Matrix3Xf&& matrix) {
40
47
std::vector<rerun::Position3D> positions (matrix.cols ());
41
48
memcpy (positions.data (), matrix.data (), matrix.size () * sizeof (rerun::Position3D));
@@ -46,11 +53,21 @@ struct rerun::CollectionAdapter<rerun::Position3D, Eigen::Matrix3Xf> {
46
53
// Adapters so we can borrow an OpenCV image easily into Rerun images without copying:
47
54
template <>
48
55
struct rerun ::CollectionAdapter<uint8_t , cv::Mat> {
56
+ // / Borrow for non-temporary.
49
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
+
50
62
return Collection<uint8_t >::borrow (img.data , img.total () * img.channels ());
51
63
}
52
64
65
+ // Do a full copy for temporaries (otherwise the data will be deleted when the temporary is destroyed).
53
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
+
54
71
std::vector<uint8_t > img_vec (img.total () * img.channels ());
55
72
img_vec.assign (img.data , img.data + img.total () * img.channels ());
56
73
return Collection<uint8_t >::take_ownership (std::move (img_vec));
@@ -61,8 +78,8 @@ struct rerun::CollectionAdapter<uint8_t, cv::Mat> {
61
78
// TODO(https://github.com/rerun-io/rerun/pull/4331): remove `datatypes::`
62
79
template <>
63
80
struct rerun ::CollectionAdapter<rerun::datatypes::TensorDimension, cv::Mat> {
81
+ // / Only overload the const& operator since there is no way of borrowing the dimensions anyways.
64
82
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.
66
83
return {
67
84
static_cast <size_t >(img.rows ),
68
85
static_cast <size_t >(img.cols ),
0 commit comments