AWViz-ROS
Loading...
Searching...
No Matches
collection_adapter.hpp
Go to the documentation of this file.
1// Copyright 2024 Kotaro Uetake.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef AWVIZ_PLUGIN__COLLECTION_ADAPTER_HPP_
16#define AWVIZ_PLUGIN__COLLECTION_ADAPTER_HPP_
17
18#include <eigen3/Eigen/Core>
19#include <opencv2/opencv.hpp>
20#include <rerun.hpp>
21#include <rerun/collection.hpp>
22
23#include <cstring>
24#include <utility>
25#include <vector>
26
27namespace rerun
28{
32template <typename TElement>
33struct CollectionAdapter<TElement, cv::Mat>
34{
41 Collection<TElement> operator()(const cv::Mat & img)
42 {
43 return Collection<TElement>::borrow(
44 reinterpret_cast<TElement *>(img.data), img.total() * img.channels());
45 }
46
54 Collection<TElement> operator()(cv::Mat && img)
55 {
56 std::vector<TElement> img_vec(img.total() * img.channels());
57 img_vec.assign(img.data, img.data + img.total() * img.channels());
58 return Collection<TElement>::take_ownership(std::move(img_vec));
59 }
60};
61
65template <>
66struct CollectionAdapter<rerun::Position3D, std::vector<Eigen::Vector3f>>
67{
68 // Sanity check that this is binary compatible.
69 static_assert(
70 sizeof(rerun::Position3D) ==
71 sizeof(Eigen::Matrix3Xf::Scalar) * Eigen::Matrix3Xf::RowsAtCompileTime);
72
79 Collection<rerun::Position3D> operator()(const std::vector<Eigen::Vector3f> & container)
80 {
81 return Collection<rerun::Position3D>::borrow(container.data(), container.size());
82 }
83
91 Collection<rerun::Position3D> operator()(std::vector<Eigen::Vector3f> && container)
92 {
93 std::vector<rerun::Position3D> positions(container.size());
94 memcpy(positions.data(), container.data(), container.size() * sizeof(Eigen::Vector3f));
95 return Collection<rerun::Position3D>::take_ownership(std::move(positions));
96 }
97};
98} // namespace rerun
99
100#endif
Definition: collection_adapter.hpp:28
Collection< TElement > operator()(cv::Mat &&img)
Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed).
Definition: collection_adapter.hpp:54
Collection< TElement > operator()(const cv::Mat &img)
Borrow for non-temporary.
Definition: collection_adapter.hpp:41
Collection< rerun::Position3D > operator()(std::vector< Eigen::Vector3f > &&container)
Do a full copy for temporaries (otherwise the data might be deleted when the temporary is destroyed).
Definition: collection_adapter.hpp:91
Collection< rerun::Position3D > operator()(const std::vector< Eigen::Vector3f > &container)
Borrow for non-temporary.
Definition: collection_adapter.hpp:79