diff --git a/awviz_plugin/include/awviz_plugin/image/collection_adapter.hpp b/awviz_plugin/include/awviz_plugin/collection_adapter.hpp similarity index 50% rename from awviz_plugin/include/awviz_plugin/image/collection_adapter.hpp rename to awviz_plugin/include/awviz_plugin/collection_adapter.hpp index 7267a47..0066987 100644 --- a/awviz_plugin/include/awviz_plugin/image/collection_adapter.hpp +++ b/awviz_plugin/include/awviz_plugin/collection_adapter.hpp @@ -12,13 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef AWVIZ_PLUGIN__IMAGE__COLLECTION_ADAPTER_HPP_ -#define AWVIZ_PLUGIN__IMAGE__COLLECTION_ADAPTER_HPP_ +#ifndef AWVIZ_PLUGIN__COLLECTION_ADAPTER_HPP_ +#define AWVIZ_PLUGIN__COLLECTION_ADAPTER_HPP_ +#include #include #include #include +#include #include #include @@ -30,15 +32,25 @@ namespace rerun template struct CollectionAdapter { - /// Borrow for non-temporary. + /** + * @brief Borrow for non-temporary. + * + * @param img OpenCV matrix. + * @return Collection + */ Collection operator()(const cv::Mat & img) { return Collection::borrow( reinterpret_cast(img.data), img.total() * img.channels()); } - // Do a full copy for temporaries (otherwise the data might be deleted when the temporary is - // destroyed). + /** + * @brief Do a full copy for temporaries (otherwise the data might be deleted when the temporary + * is destroyed). + * + * @param img OpenCV matrix. + * @return Collection + */ Collection operator()(cv::Mat && img) { std::vector img_vec(img.total() * img.channels()); @@ -46,6 +58,43 @@ struct CollectionAdapter return Collection::take_ownership(std::move(img_vec)); } }; + +/** + * @brief An adaptor to be able to borrow an Eigen matrix into Rerun 3D position without copying. + */ +template <> +struct CollectionAdapter> +{ + // Sanity check that this is binary compatible. + static_assert( + sizeof(rerun::Position3D) == + sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime); + + /** + * @brief Borrow for non-temporary. + * + * @param container Eigen 3D float vector. + * @return Collection + */ + Collection operator()(const std::vector & container) + { + return Collection::borrow(container.data(), container.size()); + } + + /** + * @brief Do a full copy for temporaries (otherwise the data might be deleted when the temporary + * is destroyed). + * + * @param container Eigen 3D float vector. + * @return Collection + */ + Collection operator()(std::vector && container) + { + std::vector positions(container.size()); + memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f)); + return Collection::take_ownership(std::move(positions)); + } +}; } // namespace rerun namespace awviz_plugin @@ -64,4 +113,4 @@ inline rerun::Collection tensor_shape(const cv::Mat & im } } // namespace awviz_plugin -#endif // AWVIZ_PLUGIN__IMAGE__IMAGE_COMPRESSED_IMAGE_DISPLAY_HPP_ +#endif diff --git a/awviz_plugin/src/image/compressed_image_display.cpp b/awviz_plugin/src/image/compressed_image_display.cpp index 0fb2f6a..85d6cce 100644 --- a/awviz_plugin/src/image/compressed_image_display.cpp +++ b/awviz_plugin/src/image/compressed_image_display.cpp @@ -14,7 +14,7 @@ #include "awviz_plugin/image/compressed_image_display.hpp" -#include "awviz_plugin/image/collection_adapter.hpp" +#include "awviz_plugin/collection_adapter.hpp" #include diff --git a/awviz_plugin/src/image/image_display.cpp b/awviz_plugin/src/image/image_display.cpp index 41c5af9..7f2df27 100644 --- a/awviz_plugin/src/image/image_display.cpp +++ b/awviz_plugin/src/image/image_display.cpp @@ -14,7 +14,7 @@ #include "awviz_plugin/image/image_display.hpp" -#include "awviz_plugin/image/collection_adapter.hpp" +#include "awviz_plugin/collection_adapter.hpp" #include