-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvtkPCLSACSegmentationCylinder.cxx
148 lines (121 loc) · 5.3 KB
/
vtkPCLSACSegmentationCylinder.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPCLSACSegmentationCylinder.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 "vtkPCLSACSegmentationCylinder.h"
#include "vtkPCLConversions.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkSmartPointer.h"
#include "vtkNew.h"
#include "vtkAlgorithmOutput.h"
#include "vtkIdTypeArray.h"
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
//----------------------------------------------------------------------------
namespace {
void ComputeSACSegmentationCylinder(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud,
double distanceThreshold,
double radiusLimit,
double searchRadius,
double normalDistanceWeight,
int maxIterations,
pcl::ModelCoefficients::Ptr &modelCoefficients,
pcl::PointIndices::Ptr &inliers)
{
// Data structs for surface normals, search tree, etc.
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
// Compute surface normals
ne.setSearchMethod (tree);
ne.setInputCloud (cloud);
ne.setRadiusSearch (searchRadius);
ne.compute (*cloud_normals);
// Cylinder RANSAC fitting only works with the SACSegmentation from normal
pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg;
inliers = pcl::PointIndices::Ptr(new pcl::PointIndices);
modelCoefficients = pcl::ModelCoefficients::Ptr(new pcl::ModelCoefficients);
Eigen::Vector3f axis(0,0,1.0);
// Perform RANSAC fitting
seg.setOptimizeCoefficients (true);
seg.setModelType(pcl::SACMODEL_CYLINDER);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setMaxIterations(maxIterations);
seg.setNormalDistanceWeight(normalDistanceWeight);
seg.setRadiusLimits(0,radiusLimit);
seg.setDistanceThreshold(distanceThreshold);
//seg.setAxis(axis);
//seg.setEpsAngle(0.2);
seg.setInputCloud(cloud);
seg.setInputNormals(cloud_normals);
seg.segment(*inliers, *modelCoefficients);
}
}
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPCLSACSegmentationCylinder);
//----------------------------------------------------------------------------
vtkPCLSACSegmentationCylinder::vtkPCLSACSegmentationCylinder()
{
this->RadiusLimit = 0.1;
this->DistanceThreshold = 0.05;
this->MaxIterations = 200;
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
}
//----------------------------------------------------------------------------
vtkPCLSACSegmentationCylinder::~vtkPCLSACSegmentationCylinder()
{
}
//----------------------------------------------------------------------------
int vtkPCLSACSegmentationCylinder::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 cylinder model fit
pcl::PointIndices::Ptr inlierIndices;
pcl::ModelCoefficients::Ptr modelCoefficients;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = vtkPCLConversions::PointCloudFromPolyData(input);
ComputeSACSegmentationCylinder(cloud,
this->DistanceThreshold,
this->RadiusLimit,
this->SearchRadius,
this->NormalDistanceWeight,
this->MaxIterations,
modelCoefficients,
inlierIndices);
// cData[0..2] contains the coordinates of a point on
// the medial axis of the cylinder, cData[3..5] is a
// direction vector and cData[6] is the radius of the
// fitted cylinder.
//const std::vector<float> &cData = modelCoefficients->values;
// 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 vtkPCLSACSegmentationCylinder::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}