-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPointCloudObject.cpp
More file actions
142 lines (114 loc) · 3.36 KB
/
Copy pathPointCloudObject.cpp
File metadata and controls
142 lines (114 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
*
*
**/
#include<iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include "PointCloudObject.h"
/**
* PointCloudObject(pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud)
*
* Constructor which creates an object from the provided point cloud.
**/
PointCloudObject::PointCloudObject(pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud)
{
this->cloud = point_cloud;
}
/**
* PointCloudObject(const char* filename)
*
* Creates a new object and loads the point clouds from a file. Assumes the file
* is a PCD file.
**/
PointCloudObject::PointCloudObject(const char* filename)
{
this->cloud = PointCloudObject::load_PCD(filename);
}
/**
* PointCloudObject(const char* filename, PCL_Filetype filetype)
*
* Creates a new object and loads the point clouds from a file.
**/
PointCloudObject::PointCloudObject(const char* filename, PCL_Filetype filetype)
{
switch(filetype)
{
case PCD:
this->cloud = PointCloudObject::load_PCD(filename);
break;
case PLY:
this->cloud = PointCloudObject::load_PLY(filename);
break;
}
}
/**
* pcl::PointCloud<pcl::PointXYZ>::Ptr load_PCD(const char* filename)
*
* Load a cloud from a PCD file.
**/
pcl::PointCloud<pcl::PointXYZ>::Ptr PointCloudObject::load_PCD(const char* filename)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile(filename, *cloud);
return cloud;
}
/**
* pcl::PointCloud<pcl::PointXYZ>::Ptr load_PLY(const char* filename)
*
* Load a cloud from a PLY file.
**/
pcl::PointCloud<pcl::PointXYZ>::Ptr PointCloudObject::load_PLY(const char* filename)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPLYFile(filename, *cloud);
return cloud;
}
/**
* void show()
*
* Display the point cloud of the object.
**/
void PointCloudObject::show()
{
pcl::visualization::CloudViewer* viewer = new pcl::visualization::CloudViewer("Object Point Cloud");
viewer->showCloud(this->cloud);
while(!viewer->wasStopped()) { }
delete viewer;
}
/**
* pcl::PointCloud<pcl::PointXYZ>::Ptr extractPartPointCloud()
*
* Pull out a part from the remaining object
**/
pcl::PointCloud<pcl::PointXYZ>::Ptr PointCloudObject::extractPartPointCloud(double distanceThreshold)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr partCloud(new pcl::PointCloud<pcl::PointXYZ>);
// The model coefficients and inliers the segmentation object will write to
pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
// Set up the SAC Segmentation object
pcl::SACSegmentation<pcl::PointXYZ> seg;
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setDistanceThreshold(distanceThreshold);
seg.setInputCloud(this->cloud);
seg.segment(*inliers, *coefficients);
// Put the inlier points into the partCloud
return partCloud;
}
/**
* std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr getPart()
*
* Give the vector of parts extracted so far.
**/
std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> PointCloudObject::getParts()
{
return this->parts;
}