YARP
Yet Another Robot Platform
PointCloudBase.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <yarp/os/Type.h>
8
9using namespace yarp::sig;
10
11namespace {
12YARP_LOG_COMPONENT(POINTCLOUDBASE, "yarp.sig.PointCloudBase")
13}
14
15// Map that contains the offset of the basic types respect the origin of the struct
16// representing the composite types.
17const std::map<std::pair<int, int>, size_t> offsetMap = {
18 // PCL_NORMAL
20
21 // PCL_POINT_XYZ_RGBA
22 { std::make_pair(PCL_POINT_XYZ_RGBA, PC_RGBA_DATA), sizeof(yarp::sig::DataXYZ) },
23
24 // PCL_POINT_XYZ_I
25 { std::make_pair(PCL_POINT_XYZ_I, PC_INTENSITY_DATA), sizeof(yarp::sig::DataXYZ) },
26
27 // PCL_INTEREST_POINT_XYZ
29
30 // PCL_POINT_XYZ_NORMAL
31 { std::make_pair(PCL_POINT_XYZ_NORMAL, PC_NORMAL_DATA), sizeof(yarp::sig::DataXYZ) },
33
34 // PCL_XYZ_NORMAL_RGBA
38
39 // PCL_XYZ_I_NORMAL TBD
40};
41// Map that contains the information about the basic types that form
42// the composite ones and in which order
43const std::map<int, std::vector<int> > compositionMap = {
44 // recipe for basic data
45 { PC_XY_DATA, std::vector<int> {PC_XY_DATA} },
46 { PC_XYZ_DATA, std::vector<int> {PC_XYZ_DATA} },
47 { PC_RGBA_DATA, std::vector<int> {PC_RGBA_DATA} },
48 { PC_INTENSITY_DATA, std::vector<int> {PC_INTENSITY_DATA} },
49 { PC_INTEREST_DATA, std::vector<int> {PC_INTEREST_DATA} },
51 { PC_NORMAL_DATA, std::vector<int> {PC_NORMAL_DATA} },
52 { PC_CURVATURE_DATA, std::vector<int> {PC_CURVATURE_DATA} },
53 { PC_RANGE_DATA, std::vector<int> {PC_RANGE_DATA} },
54 { PC_VIEWPOINT_DATA, std::vector<int> {PC_VIEWPOINT_DATA} },
55 // PCL_POINT_XYZ_RGBA
57 // PCL_POINT_XYZ_I
58 { PCL_POINT_XYZ_I, std::vector<int> {PC_XYZ_DATA, PC_INTENSITY_DATA} },
59 // PCL_INTEREST_POINT_XYZ
61 // PCL_POINT_XYZ_NORMAL
63 // PCL_POINT_XYZ_NORMAL_RGBA
65};
66
67// Map that contains the size of the struct given the enum representing the type
68const std::map<int, size_t> sizeMap = {
69 { PC_PADDING3, 3*sizeof(float) },
70 { PC_PADDING2, 2*sizeof(float) },
71 { PC_XY_DATA, sizeof(yarp::sig::DataXY) },
74 { PC_INTENSITY_DATA, sizeof(float) },
75 { PC_INTEREST_DATA, sizeof(float) },
86};
87
88
89size_t PointCloudBase::height() const
90{
91 return header.height;
92}
93
95{
96 return header.width;
97}
98
100{
101 return header.pointType;
102}
103
105{
106 return yarp::os::Type::byName("yarp/pointCloud");
107}
108
110{
111 return height() > 1;
112}
113
114void PointCloudBase::copyFromRawData(const char* dst, const char* source, std::vector<int>& recipe)
115{
116 char* tmpSrc = const_cast<char*>(source);
117 char* tmpDst = const_cast<char*>(dst);
118 if (recipe.empty()) {
119 return;
120 }
121 yCAssert(POINTCLOUDBASE, tmpSrc && tmpDst);
122
123 size_t sizeDst = pointType2Size(getPointType());
124 const size_t numPts = height() * width();
125 for (size_t i = 0; i < numPts; i++) {
126 for (int j : recipe) {
127 size_t sizeToRead = pointType2Size(j);
128 if ((header.pointType & j)) {
129 size_t offset = getOffset(header.pointType, j);
130 std::memcpy(tmpDst + i * sizeDst + offset, tmpSrc, sizeToRead);
131 }
132
133 // increment anyways, if the field is missing in the destination, simply skip it
134 tmpSrc += sizeToRead;
135 }
136 }
137}
138
139
140std::vector<int> PointCloudBase::getComposition(int type_composite) const
141{
142 //todo probably
143 std::vector<int> ret;
144 auto it = compositionMap.find(type_composite);
145 if (it != compositionMap.end()) {
146 ret = it->second;
147 }
148 return ret;
149}
150
151
152size_t PointCloudBase::pointType2Size(int type) const
153{
154 size_t size = 0;
155
156 auto it = sizeMap.find(type);
157 if (it != sizeMap.end()) {
158 size = it->second;
159 }
160
161 return size;
162}
163
164size_t PointCloudBase::getOffset(int type_composite, int type_basic) const
165{
166 size_t offset = 0;
167 auto it = offsetMap.find(std::make_pair(type_composite, type_basic));
168 if (it != offsetMap.end()) {
169 offset = it->second;
170 }
171 return offset;
172}
bool ret
const std::map< int, std::vector< int > > compositionMap
const std::map< int, size_t > sizeMap
const std::map< std::pair< int, int >, size_t > offsetMap
static Type byName(const char *name)
Definition: Type.cpp:171
virtual size_t size() const =0
yarp::sig::PointCloudNetworkHeader header
virtual std::vector< int > getComposition(int type_composite) const
virtual size_t width() const
virtual void copyFromRawData(const char *dst, const char *source, std::vector< int > &recipe)
virtual bool isOrganized() const
yarp::os::Type getType() const override
virtual int getPointType() const
virtual size_t height() const
virtual size_t pointType2Size(int type) const
virtual size_t getOffset(int type_composite, int type_basic) const
#define yCAssert(component, x)
Definition: LogComponent.h:240
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
@ PCL_INTEREST_POINT_XYZ
@ PCL_POINT_XYZ_NORMAL
@ PCL_POINT_XYZ_NORMAL_RGBA