vision
Loading...
Searching...
No Matches
KinectFusionImpl.hpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#ifndef __KINECT_FUSION_IMPL_HPP__
4#define __KINECT_FUSION_IMPL_HPP__
5
6#include "KinectFusion.hpp"
7
8#include <mutex>
9#include <type_traits>
10
11#include <opencv2/core.hpp>
12
13#include <yarp/os/LogStream.h>
14#include <yarp/cv/Cv.h>
15
16#include "LogComponent.hpp"
17
18namespace roboticslab
19{
20
21template <typename T>
23{
24public:
25 KinectFusionImpl(const cv::Ptr<T> & other) : handle(other)
26 {
27 cv::setUseOptimized(true);
28 }
29
30 void getCloud(yarp::sig::PointCloudXYZNormalRGBA & cloudWithNormals) const override
31 {
32 cv::UMat points, normals;
33
34 mtx.lock();
35 handle->getCloud(points, normals);
36 mtx.unlock();
37
38 cv::Mat _points = points.getMat(cv::ACCESS_FAST); // no memcpy
39 cv::Mat _normals = normals.getMat(cv::ACCESS_FAST); // no memcpy
40
41 cloudWithNormals.resize(points.rows);
42
43 for (auto i = 0; i < points.rows; i++)
44 {
45 const auto & point = _points.at<cv::Vec4f>(i);
46 const auto & normal = _normals.at<cv::Vec4f>(i);
47 cloudWithNormals(i) = {{point[0], point[1], point[2]}, {normal[0], normal[1], normal[2]}, 0};
48 }
49 }
50
51 void getPoints(yarp::sig::PointCloudXYZ & cloud) const override
52 {
53 cv::UMat points;
54
55 mtx.lock();
56 handle->getPoints(points);
57 mtx.unlock();
58
59 cv::Mat _points = points.getMat(cv::ACCESS_FAST); // no memcpy
60 auto data = const_cast<const char *>(reinterpret_cast<char *>(_points.data));
61
62 cloud.fromExternalPC(data, yarp::sig::PointCloudBasicType::PC_XYZ_DATA, _points.rows, 1);
63 }
64
65 void getPose(yarp::sig::Matrix & pose) const override
66 {
67 mtx.lock();
68 const auto & affine = handle->getPose().matrix;
69 mtx.unlock();
70
71 pose.resize(4, 4);
72
73 for (int i = 0; i < 4; i++)
74 {
75 for (int j = 0; j < 4; j++)
76 {
77 pose(i, j) = affine(i, j);
78 }
79 }
80 }
81
82 bool update(const yarp::sig::ImageOf<yarp::sig::PixelFloat> & depthFrame, const yarp::sig::FlexImage & colorFrame) override
83 {
84 // Cast away constness so that toCvMat accepts the YARP image. This function
85 // does not alter the inner structure of PixelFloat images anyway.
86 auto & nonConstDepthFrame = const_cast<yarp::sig::ImageOf<yarp::sig::PixelFloat> &>(depthFrame);
87 cv::Mat mat = yarp::cv::toCvMat(nonConstDepthFrame);
88
89 cv::UMat umat;
90 mat.convertTo(umat, mat.type(), 1000.0); // OpenCV uses milimeters
91
92 std::lock_guard lock(mtx);
93 return handle->update(umat);
94 }
95
96 void reset() override
97 {
98 std::lock_guard lock(mtx);
99 handle->reset();
100 }
101
102 void render(yarp::sig::FlexImage & image) const override
103 {
104 cv::UMat umat;
105
106 mtx.lock();
107 handle->render(umat);
108 mtx.unlock();
109
110 cv::Mat mat = umat.getMat(cv::ACCESS_FAST); // no memcpy
111 const auto & bgr = yarp::cv::fromCvMat<yarp::sig::PixelBgra>(mat); // no conversion
112 image.copy(bgr); // bgra to grayscale/rgb (single step convert+assign)
113 }
114
115private:
116 cv::Ptr<T> handle;
117 mutable std::mutex mtx;
118};
119
120template <typename T>
121T getValue(const yarp::os::Value & v)
122{
123 if constexpr (std::is_integral_v<T>)
124 {
125 return v.asInt32();
126 }
127 else if constexpr (std::is_floating_point_v<T>)
128 {
129 return v.asFloat64();
130 }
131 else
132 {
133 // https://stackoverflow.com/a/64354296/10404307
134 static_assert(!sizeof(T), "Unsupported type");
135 }
136}
137
138template <typename TParams, typename TRet>
139void updateParam(TParams & params, TRet TParams::* param, const yarp::os::Searchable & config,
140 const std::string & name, const std::string & description)
141{
142 auto && log = yCInfo(KINFU);
143 log << name + ":";
144
145 if (config.check(name, description))
146 {
147 params.*param = getValue<TRet>(config.find(name));
148 }
149 else
150 {
151 log << "(DEFAULT):";
152 }
153
154 log << params.*param;
155}
156
157} // namespace roboticslab
158
159#endif // __KINECT_FUSION_IMPL_HPP__
Definition KinectFusionImpl.hpp:23
Definition KinectFusion.hpp:18
The main, catch-all namespace for Robotics Lab UC3M.
Definition groups.dox:5