YARP
Yet Another Robot Platform
PointCloudBase.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 #include <yarp/os/Type.h>
11 
12 using namespace yarp::sig;
13 
14 namespace {
15 YARP_LOG_COMPONENT(POINTCLOUDBASE, "yarp.sig.PointCloudBase")
16 }
17 
18 // Map that contains the offset of the basic types respect the origin of the struct
19 // representing the composite types.
20 const std::map<std::pair<int, int>, size_t> offsetMap = {
21  // PCL_NORMAL
23 
24  // PCL_POINT_XYZ_RGBA
25  { std::make_pair(PCL_POINT_XYZ_RGBA, PC_RGBA_DATA), sizeof(yarp::sig::DataXYZ) },
26 
27  // PCL_POINT_XYZ_I
28  { std::make_pair(PCL_POINT_XYZ_I, PC_INTENSITY_DATA), sizeof(yarp::sig::DataXYZ) },
29 
30  // PCL_INTEREST_POINT_XYZ
31  { std::make_pair(PCL_INTEREST_POINT_XYZ, PC_INTEREST_DATA), sizeof(yarp::sig::DataXYZ) },
32 
33  // PCL_POINT_XYZ_NORMAL
34  { std::make_pair(PCL_POINT_XYZ_NORMAL, PC_NORMAL_DATA), sizeof(yarp::sig::DataXYZ) },
36 
37  // PCL_XYZ_NORMAL_RGBA
38  { std::make_pair(PCL_POINT_XYZ_NORMAL_RGBA, PC_NORMAL_DATA), sizeof(yarp::sig::DataXYZ) },
41 
42  // PCL_XYZ_I_NORMAL TBD
43 };
44 // Map that contains the information about the basic types that form
45 // the composite ones and in which order
46 const std::map<int, std::vector<int> > compositionMap = {
47  // recipe for basic data
48  { PC_XY_DATA, std::vector<int> {PC_XY_DATA} },
49  { PC_XYZ_DATA, std::vector<int> {PC_XYZ_DATA} },
50  { PC_RGBA_DATA, std::vector<int> {PC_RGBA_DATA} },
51  { PC_INTENSITY_DATA, std::vector<int> {PC_INTENSITY_DATA} },
52  { PC_INTEREST_DATA, std::vector<int> {PC_INTEREST_DATA} },
53  { PCL_NORMAL, std::vector<int> {PC_NORMAL_DATA, PC_CURVATURE_DATA, PC_PADDING3} },
54  { PC_NORMAL_DATA, std::vector<int> {PC_NORMAL_DATA} },
55  { PC_CURVATURE_DATA, std::vector<int> {PC_CURVATURE_DATA} },
56  { PC_RANGE_DATA, std::vector<int> {PC_RANGE_DATA} },
57  { PC_VIEWPOINT_DATA, std::vector<int> {PC_VIEWPOINT_DATA} },
58  // PCL_POINT_XYZ_RGBA
59  { PCL_POINT_XYZ_RGBA, std::vector<int> {PC_XYZ_DATA, PC_RGBA_DATA, PC_PADDING3} },
60  // PCL_POINT_XYZ_I
61  { PCL_POINT_XYZ_I, std::vector<int> {PC_XYZ_DATA, PC_INTENSITY_DATA} },
62  // PCL_INTEREST_POINT_XYZ
63  { PCL_INTEREST_POINT_XYZ, std::vector<int> {PC_XYZ_DATA, PC_INTEREST_DATA} },
64  // PCL_POINT_XYZ_NORMAL
66  // PCL_POINT_XYZ_NORMAL_RGBA
68 };
69 
70 // Map that contains the size of the struct given the enum representing the type
71 const std::map<int, size_t> sizeMap = {
72  { PC_PADDING3, 3*sizeof(float) },
73  { PC_PADDING2, 2*sizeof(float) },
74  { PC_XY_DATA, sizeof(yarp::sig::DataXY) },
75  { PC_XYZ_DATA, sizeof(yarp::sig::DataXYZ) },
76  { PC_RGBA_DATA, sizeof(yarp::sig::DataRGBA) },
77  { PC_INTENSITY_DATA, sizeof(float) },
78  { PC_INTEREST_DATA, sizeof(float) },
80  { PCL_NORMAL, sizeof(yarp::sig::DataNormal) },
82  { PC_RANGE_DATA, sizeof(yarp::sig::Range) },
89 };
90 
91 
92 size_t PointCloudBase::height() const
93 {
94  return header.height;
95 }
96 
97 size_t PointCloudBase::width() const
98 {
99  return header.width;
100 }
101 
103 {
104  return header.pointType;
105 }
106 
108 {
109  return yarp::os::Type::byName("yarp/pointCloud");
110 }
111 
113 {
114  return height() > 1;
115 }
116 
117 void PointCloudBase::copyFromRawData(const char* dst, const char* source, std::vector<int>& recipe)
118 {
119  char* tmpSrc = const_cast<char*>(source);
120  char* tmpDst = const_cast<char*>(dst);
121  if (recipe.empty()) {
122  return;
123  }
124  yCAssert(POINTCLOUDBASE, tmpSrc && tmpDst);
125 
126  size_t sizeDst = pointType2Size(getPointType());
127  const size_t numPts = height() * width();
128  for (size_t i = 0; i < numPts; i++) {
129  for (int j : recipe) {
130  size_t sizeToRead = pointType2Size(j);
131  if ((header.pointType & j)) {
132  size_t offset = getOffset(header.pointType, j);
133  std::memcpy(tmpDst + i * sizeDst + offset, tmpSrc, sizeToRead);
134  }
135 
136  // increment anyways, if the field is missing in the destination, simply skip it
137  tmpSrc += sizeToRead;
138  }
139  }
140 }
141 
142 
143 std::vector<int> PointCloudBase::getComposition(int type_composite) const
144 {
145  //todo probably
146  std::vector<int> ret;
147  auto it = compositionMap.find(type_composite);
148  if (it != compositionMap.end()) {
149  ret = it->second;
150  }
151  return ret;
152 }
153 
154 
155 size_t PointCloudBase::pointType2Size(int type) const
156 {
157  size_t size = 0;
158 
159  auto it = sizeMap.find(type);
160  if (it != sizeMap.end()) {
161  size = it->second;
162  }
163 
164  return size;
165 }
166 
167 size_t PointCloudBase::getOffset(int type_composite, int type_basic) const
168 {
169  size_t offset = 0;
170  auto it = offsetMap.find(std::make_pair(type_composite, type_basic));
171  if (it != offsetMap.end()) {
172  offset = it->second;
173  }
174  return offset;
175 }
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:174
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:172
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
Signal processing.
Definition: Image.h:25
@ PCL_INTEREST_POINT_XYZ
@ PCL_POINT_XYZ_NORMAL
@ PCL_POINT_XYZ_NORMAL_RGBA