libvisiontransfer  10.8.0
reconstruct3d-pcl.h
1 /*******************************************************************************
2  * Copyright (c) 2024 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_RECONSTRUCT3D_PCL_H
16 #define VISIONTRANSFER_RECONSTRUCT3D_PCL_H
17 
18 #ifdef PCL_MAJOR_VERSION
19 
20 namespace visiontransfer {
21 
22 /*
23  * PCL-specific implementations that need to be inlined in order to avoid
24  * dependencies for projects that do not make use of PCL
25  */
26 
27 template <typename T>
28 typename pcl::PointCloud<T>::Ptr Reconstruct3D::initPointCloud(const ImageSet& imageSet, const char* frameId) {
29  int sec, microsec;
30  imageSet.getTimestamp(sec, microsec);
31 
32  typename pcl::PointCloud<T>::Ptr ret(
33  new pcl::PointCloud<T>(imageSet.getWidth(), imageSet.getHeight()));
34 
35  ret->header.frame_id = frameId;
36  ret->header.seq = imageSet.getSequenceNumber();
37  ret->header.stamp = sec * 1000000LL + microsec;
38  ret->width = imageSet.getWidth();
39  ret->height = imageSet.getHeight();
40  ret->is_dense = true;
41  return ret;
42 }
43 
44 inline pcl::PointCloud<pcl::PointXYZ>::Ptr Reconstruct3D::createXYZCloud(const ImageSet& imageSet,
45  const char* frameId, unsigned short minDisparity) {
46  float* pointMap = createPointMap(imageSet, minDisparity);
47  pcl::PointCloud<pcl::PointXYZ>::Ptr ret = initPointCloud<pcl::PointXYZ>(imageSet, frameId);
48  memcpy(&ret->points[0].x, pointMap, ret->width*ret->height*sizeof(float)*4);
49  return ret;
50 }
51 
52 inline pcl::PointCloud<pcl::PointXYZI>::Ptr Reconstruct3D::createXYZICloud(const ImageSet& imageSet,
53  const char* frameId, unsigned short minDisparity) {
54  float* pointMap = createPointMap(imageSet, minDisparity);
55  pcl::PointCloud<pcl::PointXYZI>::Ptr ret = initPointCloud<pcl::PointXYZI>(imageSet, frameId);
56 
57  ImageSet::ImageType colImg = ImageSet::IMAGE_LEFT;
58 
59  pcl::PointXYZI* dstPtr = &ret->points[0];
60  if(imageSet.getPixelFormat(colImg) == ImageSet::FORMAT_8_BIT_MONO) {
61  for(int y = 0; y < imageSet.getHeight(); y++) {
62  unsigned char* rowPtr = imageSet.getPixelData(colImg) + y*imageSet.getRowStride(colImg);
63  unsigned char* endPtr = rowPtr + imageSet.getWidth();
64  for(; rowPtr < endPtr; rowPtr++) {
65  dstPtr->intensity = static_cast<float>(*rowPtr)/255.0F;
66  dstPtr->x = *pointMap++;
67  dstPtr->y = *pointMap++;
68  dstPtr->z = *pointMap++;
69 
70  pointMap++;
71  dstPtr++;
72  }
73  }
74  } else if(imageSet.getPixelFormat(colImg) == ImageSet::FORMAT_12_BIT_MONO) {
75  for(int y = 0; y < imageSet.getHeight(); y++) {
76  unsigned short* rowPtr = reinterpret_cast<unsigned short*>(imageSet.getPixelData(colImg) + y*imageSet.getRowStride(colImg));
77  unsigned short* endPtr = rowPtr + imageSet.getWidth();
78  for(; rowPtr < endPtr; rowPtr++) {
79  dstPtr->intensity = static_cast<float>(*rowPtr)/4095.0F;
80  dstPtr->x = *pointMap++;
81  dstPtr->y = *pointMap++;
82  dstPtr->z = *pointMap++;
83 
84  pointMap++;
85  dstPtr++;
86  }
87  }
88  } else {
89  throw std::runtime_error("Left image does not have a valid greyscale format");
90  }
91 
92  return ret;
93 }
94 
95 inline pcl::PointCloud<pcl::PointXYZRGB>::Ptr Reconstruct3D::createXYZRGBCloud(const ImageSet& imageSet,
96  const char* frameId, unsigned short minDisparity) {
97  float* pointMap = createPointMap(imageSet, minDisparity);
98  pcl::PointCloud<pcl::PointXYZRGB>::Ptr ret = initPointCloud<pcl::PointXYZRGB>(imageSet, frameId);
99 
100  ImageSet::ImageType colImg = getColorImage(imageSet, COLOR_AUTO);
101 
102  pcl::PointXYZRGB* dstPtr = &ret->points[0];
103  if(imageSet.getPixelFormat(colImg) != ImageSet::FORMAT_8_BIT_RGB) {
104  throw std::runtime_error("Left image is not an RGB image");
105  }
106 
107  for(int y = 0; y < imageSet.getHeight(); y++) {
108  unsigned char* rowPtr = imageSet.getPixelData(colImg) + y*imageSet.getRowStride(colImg);
109  unsigned char* endPtr = rowPtr + 3*imageSet.getWidth();
110  for(; rowPtr < endPtr;rowPtr +=3) {
111  dstPtr->r = rowPtr[0];
112  dstPtr->g = rowPtr[1];
113  dstPtr->b = rowPtr[2];
114  dstPtr->x = *pointMap++;
115  dstPtr->y = *pointMap++;
116  dstPtr->z = *pointMap++;
117 
118  pointMap++;
119  dstPtr++;
120  }
121  }
122 
123  return ret;
124 }
125 
126 } // namespace
127 
128 #endif
129 #endif
visiontransfer::Reconstruct3D::COLOR_AUTO
@ COLOR_AUTO
Automatically choose the best color channel.
Definition: reconstruct3d.h:81
visiontransfer::ImageSet::FORMAT_8_BIT_RGB
@ FORMAT_8_BIT_RGB
8-bit RGB format
Definition: imageset.h:79
visiontransfer::ImageSet::ImageType
ImageType
Supported image types.
Definition: imageset.h:97
visiontransfer::Reconstruct3D::createXYZICloud
pcl::PointCloud< pcl::PointXYZI >::Ptr createXYZICloud(const ImageSet &imageSet, const char *frameId, unsigned short minDisparity=0)
Projects the given disparity map to a PCL point cloud, including pixel intensities.
Definition: reconstruct3d-pcl.h:64
visiontransfer::ImageSet::FORMAT_12_BIT_MONO
@ FORMAT_12_BIT_MONO
Definition: imageset.h:83
visiontransfer::Reconstruct3D::createXYZCloud
pcl::PointCloud< pcl::PointXYZ >::Ptr createXYZCloud(const ImageSet &imageSet, const char *frameId, unsigned short minDisparity=0)
Projects the given disparity map to a PCL point cloud without pixel intensities.
Definition: reconstruct3d-pcl.h:56
visiontransfer::Reconstruct3D::createXYZRGBCloud
pcl::PointCloud< pcl::PointXYZRGB >::Ptr createXYZRGBCloud(const ImageSet &imageSet, const char *frameId, unsigned short minDisparity=0)
Projects the given disparity map to a PCL point cloud, including pixel RGB data.
Definition: reconstruct3d-pcl.h:107
visiontransfer::ImageSet::FORMAT_8_BIT_MONO
@ FORMAT_8_BIT_MONO
8-bit greyscale format
Definition: imageset.h:76
visiontransfer::Reconstruct3D::createPointMap
float * createPointMap(const ImageSet &imageSet, unsigned short minDisparity=0, unsigned short maxDisparity=0xFFF)
Reconstructs the 3D location of each pixel in the given disparity map.
Definition: reconstruct3d.cpp:97
Allied Vision