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 }
yarp::sig::PointCloudBase::getComposition
virtual std::vector< int > getComposition(int type_composite) const
Definition: PointCloudBase.cpp:143
yarp::sig::PointCloudBase::getPointType
virtual int getPointType() const
Definition: PointCloudBase.cpp:102
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::sig::Range
float Range
Definition: PointCloudTypes.h:418
yarp::sig::PC_VIEWPOINT_DATA
@ PC_VIEWPOINT_DATA
Definition: PointCloudTypes.h:43
yarp::os::Type
Definition: Type.h:24
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::sig::DataCurvature
Definition: PointCloudTypes.h:407
yarp::sig::PC_RANGE_DATA
@ PC_RANGE_DATA
Definition: PointCloudTypes.h:42
yarp::sig::DataRGBA
Definition: PointCloudTypes.h:235
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::sig::PCL_INTEREST_POINT_XYZ
@ PCL_INTEREST_POINT_XYZ
Definition: PointCloudTypes.h:71
yarp::sig::PCL_POINT_XYZ_NORMAL_RGBA
@ PCL_POINT_XYZ_NORMAL_RGBA
Definition: PointCloudTypes.h:74
yarp::sig::PointCloudBase::size
virtual size_t size() const =0
yarp::sig::DataXYZ
Definition: PointCloudTypes.h:162
yarp::sig::PointCloudNetworkHeader::pointType
yarp::os::NetInt32 pointType
Definition: PointCloudNetworkHeader.h:37
yarp::sig::PC_XYZ_DATA
@ PC_XYZ_DATA
Definition: PointCloudTypes.h:36
sizeMap
const std::map< int, size_t > sizeMap
Definition: PointCloudBase.cpp:71
yarp::sig::PC_NORMAL_DATA
@ PC_NORMAL_DATA
Definition: PointCloudTypes.h:40
yarp::sig::PCL_POINT_XYZ_NORMAL
@ PCL_POINT_XYZ_NORMAL
Definition: PointCloudTypes.h:73
yarp::sig::DataXYZNormal
Definition: PointCloudTypes.h:705
yarp::sig::PointCloudBase::getType
yarp::os::Type getType() const override
Definition: PointCloudBase.cpp:107
yarp::os::Type::byName
static Type byName(const char *name)
Definition: Type.cpp:174
yarp::sig::PC_PADDING2
@ PC_PADDING2
Definition: PointCloudTypes.h:59
yarp::sig::PC_PADDING3
@ PC_PADDING3
Definition: PointCloudTypes.h:60
Type.h
yarp::sig::PC_CURVATURE_DATA
@ PC_CURVATURE_DATA
Definition: PointCloudTypes.h:41
yarp::sig::PC_RGBA_DATA
@ PC_RGBA_DATA
Definition: PointCloudTypes.h:37
yarp::sig::PC_INTEREST_DATA
@ PC_INTEREST_DATA
Definition: PointCloudTypes.h:39
PointCloudBase.h
offsetMap
const std::map< std::pair< int, int >, size_t > offsetMap
Definition: PointCloudBase.cpp:20
yarp::sig::PointCloudBase::header
yarp::sig::PointCloudNetworkHeader header
Definition: PointCloudBase.h:106
yarp::sig::PointCloudNetworkHeader::height
yarp::os::NetInt32 height
Definition: PointCloudNetworkHeader.h:36
yarp::sig::DataXY
Definition: PointCloudTypes.h:116
yarp::sig::DataNormalNoCurvature
Definition: PointCloudTypes.h:350
yarp::sig::DataXYZRGBA
Definition: PointCloudTypes.h:486
yarp::sig::DataViewpoint
Definition: PointCloudTypes.h:423
yarp::sig::PointCloudBase::copyFromRawData
virtual void copyFromRawData(const char *dst, const char *source, std::vector< int > &recipe)
Definition: PointCloudBase.cpp:117
yarp::sig::PointCloudBase::pointType2Size
virtual size_t pointType2Size(int type) const
Definition: PointCloudBase.cpp:155
yarp::sig::PC_INTENSITY_DATA
@ PC_INTENSITY_DATA
Definition: PointCloudTypes.h:38
yarp::sig::PointCloudNetworkHeader::width
yarp::os::NetInt32 width
Definition: PointCloudNetworkHeader.h:35
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yarp::sig::DataNormal
Definition: PointCloudTypes.h:282
yarp::sig::PCL_POINT_XYZ_RGBA
@ PCL_POINT_XYZ_RGBA
Definition: PointCloudTypes.h:69
yarp::sig::PointCloudBase::getOffset
virtual size_t getOffset(int type_composite, int type_basic) const
Definition: PointCloudBase.cpp:167
yarp::sig::PointCloudBase::height
virtual size_t height() const
Definition: PointCloudBase.cpp:92
yarp::sig::PointCloudBase::isOrganized
virtual bool isOrganized() const
Definition: PointCloudBase.cpp:112
yarp::sig::DataXYZI
Definition: PointCloudTypes.h:566
yarp::sig::PC_XY_DATA
@ PC_XY_DATA
Definition: PointCloudTypes.h:35
yarp::sig::DataXYZNormalRGBA
Definition: PointCloudTypes.h:798
yarp::sig::PCL_POINT_XYZ_I
@ PCL_POINT_XYZ_I
Definition: PointCloudTypes.h:70
yarp::sig::PointCloudBase::width
virtual size_t width() const
Definition: PointCloudBase.cpp:97
compositionMap
const std::map< int, std::vector< int > > compositionMap
Definition: PointCloudBase.cpp:46
yarp::sig::DataInterestPointXYZ
Definition: PointCloudTypes.h:635
yarp::sig::PCL_NORMAL
@ PCL_NORMAL
Definition: PointCloudTypes.h:72