forked from patmarion/PointCloudLibraryPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkPCLSACSegmentationPlane.cxx
206 lines (170 loc) · 7.24 KB
/
vtkPCLSACSegmentationPlane.cxx
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPCLSACSegmentationPlane.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPCLSACSegmentationPlane.h"
#include "vtkPCLConversions.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkPlane.h"
#include "vtkNew.h"
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
//----------------------------------------------------------------------------
namespace {
void ComputeSACSegmentationLine(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud,
double distanceThreshold,
int maxIterations,
pcl::ModelCoefficients::Ptr &modelCoefficients,
pcl::PointIndices::Ptr &inliers)
{
pcl::SACSegmentation<pcl::PointXYZ> seg;
inliers = pcl::PointIndices::Ptr(new pcl::PointIndices);
modelCoefficients = pcl::ModelCoefficients::Ptr(new pcl::ModelCoefficients);
seg.setOptimizeCoefficients (true);
seg.setModelType(pcl::SACMODEL_LINE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(maxIterations);
seg.setDistanceThreshold(distanceThreshold);
seg.setInputCloud(cloud);
seg.segment(*inliers, *modelCoefficients);
}
void ComputeSACSegmentationPlane(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud,
double distanceThreshold,
int maxIterations,
pcl::ModelCoefficients::Ptr &modelCoefficients,
pcl::PointIndices::Ptr &inliers)
{
pcl::SACSegmentation<pcl::PointXYZ> seg;
inliers = pcl::PointIndices::Ptr(new pcl::PointIndices);
modelCoefficients = pcl::ModelCoefficients::Ptr(new pcl::ModelCoefficients);
seg.setOptimizeCoefficients (true);
seg.setModelType(pcl::SACMODEL_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(maxIterations);
seg.setDistanceThreshold(distanceThreshold);
seg.setInputCloud(cloud);
seg.segment(*inliers, *modelCoefficients);
}
void ComputeSACSegmentationPerpendicularPlane(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud,
double distanceThreshold,
const Eigen::Vector3f& perpendicularAxis,
double angleEpsilon,
int maxIterations,
pcl::ModelCoefficients::Ptr &modelCoefficients,
pcl::PointIndices::Ptr &inliers)
{
pcl::SACSegmentation<pcl::PointXYZ> seg;
inliers = pcl::PointIndices::Ptr(new pcl::PointIndices);
modelCoefficients = pcl::ModelCoefficients::Ptr(new pcl::ModelCoefficients);
seg.setOptimizeCoefficients (true);
seg.setModelType(pcl::SACMODEL_PERPENDICULAR_PLANE);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(maxIterations);
seg.setDistanceThreshold(distanceThreshold);
seg.setAxis(perpendicularAxis);
seg.setEpsAngle(angleEpsilon);
seg.setInputCloud(cloud);
seg.segment(*inliers, *modelCoefficients);
}
}
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPCLSACSegmentationPlane);
//----------------------------------------------------------------------------
vtkPCLSACSegmentationPlane::vtkPCLSACSegmentationPlane()
{
this->DistanceThreshold = 0.05;
this->MaxIterations = 200;
this->PlaneCoefficients[0] = 0.0;
this->PlaneCoefficients[1] = 0.0;
this->PlaneCoefficients[2] = 0.0;
this->PlaneCoefficients[3] = 0.0;
this->PerpendicularConstraintEnabled = false;
this->AngleEpsilon = 0.2;
this->PerpendicularAxis[0] = 1.0;
this->PerpendicularAxis[1] = 0.0;
this->PerpendicularAxis[2] = 0.0;
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
}
//----------------------------------------------------------------------------
vtkPCLSACSegmentationPlane::~vtkPCLSACSegmentationPlane()
{
}
//----------------------------------------------------------------------------
int vtkPCLSACSegmentationPlane::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get input and output data objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkPolyData *input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
// perform plane model fit
pcl::PointIndices::Ptr inlierIndices;
pcl::ModelCoefficients::Ptr modelCoefficients;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = vtkPCLConversions::PointCloudFromPolyData(input);
if (this->PerpendicularConstraintEnabled)
{
ComputeSACSegmentationPerpendicularPlane(
cloud,
this->DistanceThreshold,
Eigen::Vector3f(this->PerpendicularAxis[0],
this->PerpendicularAxis[1],
this->PerpendicularAxis[2]),
this->AngleEpsilon,
this->MaxIterations,
modelCoefficients,
inlierIndices);
}
else
{
ComputeSACSegmentationPlane(cloud,
this->DistanceThreshold,
this->MaxIterations,
modelCoefficients,
inlierIndices);
}
// store plane coefficients
for (size_t i = 0; i < modelCoefficients->values.size(); ++i)
{
this->PlaneCoefficients[i] = modelCoefficients->values[i];
}
// compute origin and normal
Eigen::Vector4d coeffs(this->PlaneCoefficients);
if (coeffs[2] < 0)
{
coeffs *= -1.0;
}
Eigen::Vector3d normal(coeffs.data());
vtkNew<vtkPlane> plane;
plane->SetNormal(normal.data());
plane->Push(-coeffs[3]/normal.norm());
plane->GetOrigin(this->PlaneOrigin);
plane->GetNormal(this->PlaneNormal);
// pass thru input add labels
vtkSmartPointer<vtkIntArray> labels = vtkPCLConversions::NewLabelsArray(inlierIndices, input->GetNumberOfPoints());
labels->SetName("ransac_labels");
output->ShallowCopy(input);
output->GetPointData()->AddArray(labels);
return 1;
}
//----------------------------------------------------------------------------
void vtkPCLSACSegmentationPlane::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}