Mech-Eye API 2.3.3
API reference documentation for Mech-Eye Industrial 3D Camera
Loading...
Searching...
No Matches
PclUtil.h
1/*******************************************************************************
2 *BSD 3-Clause License
3 *
4 *Copyright (c) 2016-2024, Mech-Mind Robotics
5 *All rights reserved.
6 *
7 *Redistribution and use in source and binary forms, with or without
8 *modification, are permitted provided that the following conditions are met:
9 *
10 *1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 *2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 *3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 *AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 *DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 *SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 *CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 *OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 *OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************************************************************/
32
33#pragma once
34#include "MechEyeApi.h"
35#include <pcl/point_types.h>
36#include <pcl/io/ply_io.h>
37#include <pcl/visualization/pcl_visualizer.h>
38#include <vtkOutputWindow.h>
39#include <thread>
40
41inline void toPCL(pcl::PointCloud<pcl::PointXYZ>& pointCloud,
42 const mmind::api::PointXYZMap& pointXYZMap)
43{
44 // write pointcloudXYZ data
45 uint32_t size = pointXYZMap.height() * pointXYZMap.width();
46 pointCloud.resize(size);
47
48 for (size_t i = 0; i < size; i++) {
49 pointCloud[i].x = 0.001 * pointXYZMap[i].x; // mm to m
50 pointCloud[i].y = 0.001 * pointXYZMap[i].y; // mm to m
51 pointCloud[i].z = 0.001 * pointXYZMap[i].z; // mm to m
52 }
53
54 return;
55}
56
57inline void toPCL(pcl::PointCloud<pcl::PointXYZRGB>& colorPointCloud,
58 const mmind::api::PointXYZBGRMap& pointXYZBGRMap)
59{
60 // write pointcloudXYZRGB data
61 uint32_t size = pointXYZBGRMap.height() * pointXYZBGRMap.width();
62 colorPointCloud.resize(size);
63
64 for (size_t i = 0; i < size; i++) {
65 colorPointCloud[i].x = 0.001 * pointXYZBGRMap[i].x; // mm to m
66 colorPointCloud[i].y = 0.001 * pointXYZBGRMap[i].y; // mm to m
67 colorPointCloud[i].z = 0.001 * pointXYZBGRMap[i].z; // mm to m
68
69 colorPointCloud[i].r = pointXYZBGRMap[i].r;
70 colorPointCloud[i].g = pointXYZBGRMap[i].g;
71 colorPointCloud[i].b = pointXYZBGRMap[i].b;
72 }
73
74 return;
75}
76
77inline void viewPCL(const pcl::PointCloud<pcl::PointXYZ>& pointCloud)
78{
79 vtkOutputWindow::SetGlobalWarningDisplay(0);
80 if (pointCloud.empty())
81 return;
82
83 pcl::visualization::PCLVisualizer cloudViewer("Cloud Viewer");
84 cloudViewer.setShowFPS(false);
85 cloudViewer.setBackgroundColor(0, 0, 0);
86 cloudViewer.addPointCloud(pointCloud.makeShared());
87 cloudViewer.addCoordinateSystem(0.01);
88 cloudViewer.addText("Cloud Size: " + std::to_string(pointCloud.size()), 0, 25, 20, 1, 1, 1,
89 "cloudSize");
90 cloudViewer.addText("Press r/R to reset camera view point to center.", 0, 0, 16, 1, 1, 1,
91 "help");
92 cloudViewer.initCameraParameters();
93 while (!cloudViewer.wasStopped()) {
94 cloudViewer.spinOnce(20);
95 std::this_thread::sleep_for(std::chrono::milliseconds(100));
96 }
97}
98
99inline void viewPCL(const pcl::PointCloud<pcl::PointXYZRGB>& colorPointCloud)
100{
101 vtkOutputWindow::SetGlobalWarningDisplay(0);
102 if (colorPointCloud.empty())
103 return;
104
105 pcl::visualization::PCLVisualizer cloudViewer("Cloud Viewer");
106 cloudViewer.setShowFPS(false);
107 cloudViewer.setBackgroundColor(0, 0, 0);
108 cloudViewer.addPointCloud(colorPointCloud.makeShared());
109 cloudViewer.addCoordinateSystem(0.01);
110 cloudViewer.addText("Cloud Size: " + std::to_string(colorPointCloud.size()), 0, 25, 20, 1, 1, 1,
111 "cloudSize");
112 cloudViewer.addText("Press r/R to reset camera view point to center.", 0, 0, 16, 1, 1, 1,
113 "help");
114 cloudViewer.initCameraParameters();
115 while (!cloudViewer.wasStopped()) {
116 cloudViewer.spinOnce(20);
117 std::this_thread::sleep_for(std::chrono::milliseconds(100));
118 }
119}
120
121inline void savePLY(const mmind::api::PointXYZMap& pointXYZMap, const std::string& path)
122{
123 pcl::PointCloud<pcl::PointXYZ> pointCloud(pointXYZMap.width(), pointXYZMap.height());
124 toPCL(pointCloud, pointXYZMap);
125 pcl::PLYWriter writer;
126 writer.write(path, pointCloud, true);
127 std::cout << "PointCloudXYZ has : " << pointCloud.width * pointCloud.height << " data points."
128 << std::endl;
129 std::cout << "PointCloudXYZ saved to: " << path << std::endl;
130 return;
131}
132
133inline void savePLY(const mmind::api::PointXYZBGRMap& pointXYZBGRMap, const std::string& path)
134{
135 pcl::PointCloud<pcl::PointXYZRGB> pointCloud(pointXYZBGRMap.width(), pointXYZBGRMap.height());
136 toPCL(pointCloud, pointXYZBGRMap);
137 pcl::PLYWriter writer;
138 writer.write(path, pointCloud, true);
139 std::cout << "PointCloudXYZRGB has : " << pointCloud.width * pointCloud.height
140 << " data points." << std::endl;
141 std::cout << "PointCloudXYZRGB saved to: " << path << std::endl;
142 return;
143}
Definition of data structure in device capturing image.
uint32_t width() const
uint32_t height() const