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 
9 using namespace yarp::sig;
10 
11 namespace {
12 YARP_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.
17 const 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
28  { std::make_pair(PCL_INTEREST_POINT_XYZ, PC_INTEREST_DATA), sizeof(yarp::sig::DataXYZ) },
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
35  { std::make_pair(PCL_POINT_XYZ_NORMAL_RGBA, PC_NORMAL_DATA), sizeof(yarp::sig::DataXYZ) },
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
43 const 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} },
50  { PCL_NORMAL, std::vector<int> {PC_NORMAL_DATA, PC_CURVATURE_DATA, PC_PADDING3} },
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
56  { PCL_POINT_XYZ_RGBA, std::vector<int> {PC_XYZ_DATA, PC_RGBA_DATA, PC_PADDING3} },
57  // PCL_POINT_XYZ_I
58  { PCL_POINT_XYZ_I, std::vector<int> {PC_XYZ_DATA, PC_INTENSITY_DATA} },
59  // PCL_INTEREST_POINT_XYZ
60  { PCL_INTEREST_POINT_XYZ, std::vector<int> {PC_XYZ_DATA, PC_INTEREST_DATA} },
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
68 const 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) },
72  { PC_XYZ_DATA, sizeof(yarp::sig::DataXYZ) },
73  { PC_RGBA_DATA, sizeof(yarp::sig::DataRGBA) },
74  { PC_INTENSITY_DATA, sizeof(float) },
75  { PC_INTEREST_DATA, sizeof(float) },
77  { PCL_NORMAL, sizeof(yarp::sig::DataNormal) },
79  { PC_RANGE_DATA, sizeof(yarp::sig::Range) },
86 };
87 
88 
89 size_t PointCloudBase::height() const
90 {
91  return header.height;
92 }
93 
94 size_t PointCloudBase::width() const
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 
114 void 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 
140 std::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 
152 size_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 
164 size_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:169
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
Signal processing.
Definition: Image.h:22
@ PCL_INTEREST_POINT_XYZ
@ PCL_POINT_XYZ_NORMAL
@ PCL_POINT_XYZ_NORMAL_RGBA