RobotsIO
Loading...
Searching...
No Matches
TransformYarpPort.cpp
1/*
2 * Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#include <RobotsIO/Utils/TransformYarpPort.h>
9
10#include <opencv2/core/eigen.hpp>
11
12#include <yarp/cv/Cv.h>
13#include <yarp/eigen/Eigen.h>
14#include <yarp/sig/Vector.h>
15
16using namespace Eigen;
17using namespace RobotsIO::Utils;
18using namespace yarp::cv;
19using namespace yarp::eigen;
20using namespace yarp::sig;
21
22
23TransformYarpPort::TransformYarpPort(const std::string& port_prefix, const bool& provide_rgb, const bool& provide_depth_segmentation) :
24 YarpBufferedPort<yarp::sig::Vector>(port_prefix + "/transform:i"),
25 rgb_out_(port_prefix + "/rgb:o"),
26 depth_segmentation_out_(port_prefix + "/depth_segmentation:o"),
27 provide_rgb_(provide_rgb),
28 provide_depth_segmentation_(provide_depth_segmentation)
29{}
30
31
32TransformYarpPort:: ~TransformYarpPort()
33{}
34
35
36Eigen::Transform<double, 3, Affine> TransformYarpPort::transform()
37{
38 return transform_;
39}
40
41
43{
44 return bbox_points_;
45}
46
47
48bool TransformYarpPort::freeze(const bool blocking)
49{
50 yarp::sig::Vector* data_yarp = receive_data(blocking);
51 transform_received_ = (data_yarp != nullptr);
52
53 if (!transform_received_)
54 return false;
55
56 bool invalid_pose = true;
57 for (std::size_t i = 0; i < 7; i++)
58 invalid_pose &= ((*data_yarp)(i) == 0.0);
59 if (invalid_pose)
60 return false;
61
62 transform_ = Translation<double, 3>(toEigen(*data_yarp).head<3>());
63 AngleAxisd rotation((*data_yarp)(6), toEigen(*data_yarp).segment<3>(3));
64 transform_.rotate(rotation);
65
66 // FIXME: this might be moved somewhere else.
67 if (data_yarp->size() > 7)
68 {
69 int offset;
70 /* Check if this stream is also carrying object velocities. */
71 if (data_yarp->size() == (7 + 6 + 8 * 3))
72 offset = 7 + 6;
73 else if (data_yarp->size() == (7 + 8 * 3))
74 offset = 7;
75 else
76 throw(std::runtime_error(log_name_ + "::freeze(). Error: the data stream carries a wrong number of items"));
77
78 Eigen::VectorXd bbox_points_data = toEigen(*data_yarp).segment<24>(offset);
79 bbox_points_.resize(3, 8);
80 for (std::size_t i = 0; i < 8; i++)
81 bbox_points_.col(i) = bbox_points_data.segment<3>(3 * i);
82 }
83
84 return true;
85}
86
87
89{
90 return -1;
91}
92
93
94void TransformYarpPort::set_rgb_image(const cv::Mat& image)
95{
96 if (!provide_rgb_)
97 return;
98
99 cv_rgb_out_ = image.clone();
100 yarp_rgb_out_ = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cv_rgb_out_);
101
102 rgb_out_.send_data(yarp_rgb_out_);
103}
104
105
106void TransformYarpPort::set_depth_segmentation_image(const Eigen::MatrixXf& depth, const cv::Mat& segmentation)
107{
108 if (!provide_depth_segmentation_)
109 return;
110
111 cv::Mat depth_temp;
112 cv::eigen2cv(depth, depth_temp);
113 cv_depth_out_ = depth_temp.clone();
114
115 // cv_depth_out_ = cv::Mat(depth.rows(), depth.cols(), CV_32FC1);
116 // Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> cv_depth_out_eigen(cv_depth_out_.ptr<float>(), depth.rows(), depth.cols());
117 // cv_depth_out_eigen = depth;
118
119 cv_segmentation_out_ = segmentation.clone();
120
121 yarp_depth_segmentation_out_.image_mono = yarp::cv::fromCvMat<PixelMono>(cv_segmentation_out_);
122 yarp_depth_segmentation_out_.image_float = yarp::cv::fromCvMat<PixelFloat>(cv_depth_out_);
123
124 depth_segmentation_out_.send_data(yarp_depth_segmentation_out_);
125}
126
127
129{
130 return transform_received_;
131}
int get_frames_between_iterations() const override
Eigen::MatrixXd bounding_box() override
void set_rgb_image(const cv::Mat &image) override
EIGEN_MAKE_ALIGNED_OPERATOR_NEW TransformYarpPort(const std::string &port_prefix, const bool &provide_rgb, const bool &provide_depth_segmentation)
void set_depth_segmentation_image(const Eigen::MatrixXf &depth, const cv::Mat &segmentation) override