-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgeo_normal_3d_omp.h
251 lines (218 loc) · 10.4 KB
/
geo_normal_3d_omp.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2011, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
/*
* Modified by Xin Wang
* Email : [email protected]
*/
#pragma once
#include "geo_normal_3d.h"
/** \brief GeoNormalEstimationOMP estimates local surface properties at each 3D point, such as surface normals and
* curvatures, in parallel, using the OpenMP standard.
* \author Radu Bogdan Rusu
* \ingroup features
*/
template <typename PointInT, typename PointOutT>
class GeoNormalEstimationOMP: public GeoNormalEstimation<PointInT, PointOutT>
{
public:
using GeoNormalEstimation<PointInT, PointOutT>::feature_name_;
using GeoNormalEstimation<PointInT, PointOutT>::getClassName;
using GeoNormalEstimation<PointInT, PointOutT>::indices_;
using GeoNormalEstimation<PointInT, PointOutT>::input_;
using GeoNormalEstimation<PointInT, PointOutT>::k_;
using GeoNormalEstimation<PointInT, PointOutT>::search_parameter_;
using GeoNormalEstimation<PointInT, PointOutT>::surface_;
using GeoNormalEstimation<PointInT, PointOutT>::getViewPoint;
typedef typename GeoNormalEstimation<PointInT, PointOutT>::PointCloudOut PointCloudOut;
public:
/** \brief Empty constructor. */
GeoNormalEstimationOMP () : threads_ (1)
{
feature_name_ = "GeoNormalEstimationOMP";
};
/** \brief Initialize the scheduler and set the number of threads to use.
* \param nr_threads the number of hardware threads to use (-1 sets the value back to automatic)
*/
GeoNormalEstimationOMP (unsigned int nr_threads) : threads_ (1)
{
setNumberOfThreads (nr_threads);
feature_name_ = "GeoNormalEstimationOMP";
}
/** \brief Initialize the scheduler and set the number of threads to use.
* \param nr_threads the number of hardware threads to use (-1 sets the value back to automatic)
*/
inline void
setNumberOfThreads (unsigned int nr_threads)
{
if (nr_threads == 0)
nr_threads = 1;
threads_ = nr_threads;
}
protected:
/** \brief The number of threads the scheduler should use. */
unsigned int threads_;
private:
/** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
* setSearchSurface () and the spatial locator in setSearchMethod ()
* \param output the resultant point cloud model dataset that contains surface normals and curvatures
*/
void
computeFeature (PointCloudOut &output);
/** \brief Make the computeFeature (&Eigen::MatrixXf); inaccessible from outside the class
* \param[out] output the output point cloud
*/
void
computeFeatureEigen (pcl::PointCloud<Eigen::MatrixXf> &) {}
};
/** \brief GeoNormalEstimationOMP estimates local surface properties at each 3D point, such as surface normals and
* curvatures, in parallel, using the OpenMP standard.
* \author Radu Bogdan Rusu
* \ingroup features
*/
template <typename PointInT>
class GeoNormalEstimationOMP<PointInT, Eigen::MatrixXf>: public GeoNormalEstimationOMP<PointInT, pcl::Normal>
{
public:
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::indices_;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::search_parameter_;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::k_;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::input_;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::surface_;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::getViewPoint;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::threads_;
using GeoNormalEstimationOMP<PointInT, pcl::Normal>::compute;
/** \brief Default constructor.
*/
GeoNormalEstimationOMP () : GeoNormalEstimationOMP<PointInT, pcl::Normal> () {}
/** \brief Initialize the scheduler and set the number of threads to use.
* \param nr_threads the number of hardware threads to use (-1 sets the value back to automatic)
*/
GeoNormalEstimationOMP (unsigned int nr_threads) : GeoNormalEstimationOMP<PointInT, pcl::Normal> (nr_threads) {}
private:
/** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
* setSearchSurface () and the spatial locator in setSearchMethod ()
* \param output the resultant point cloud model dataset that contains surface normals and curvatures
*/
void
computeFeatureEigen (pcl::PointCloud<Eigen::MatrixXf> &output);
/** \brief Make the compute (&PointCloudOut); inaccessible from outside the class
* \param[out] output the output point cloud
*/
void
compute (pcl::PointCloud<pcl::Normal> &) {}
};
//////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointInT> void
GeoNormalEstimationOMP<PointInT, Eigen::MatrixXf>::computeFeatureEigen (pcl::PointCloud<Eigen::MatrixXf> &output)
{
float vpx, vpy, vpz;
getViewPoint (vpx, vpy, vpz);
output.is_dense = true;
// Resize the output dataset
output.points.resize (indices_->size (), 4);
// GCC 4.2.x seems to segfault with "internal compiler error" on MacOS X here
#if defined(_WIN32) || ((__GNUC__ > 4) && (__GNUC_MINOR__ > 2))
#pragma omp parallel for schedule (dynamic, threads_)
#endif
// Iterating over the entire index vector
for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
{
// Allocate enough space to hold the results
// \note This resize is irrelevant for a radiusSearch ().
std::vector<int> nn_indices (k_);
std::vector<float> nn_dists (k_);
if (!isFinite ((*input_)[(*indices_)[idx]]) ||
this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
{
output.points (idx, 0) = output.points (idx, 1) = output.points (idx, 2) = output.points (idx, 3) = std::numeric_limits<float>::quiet_NaN ();
output.is_dense = false;
continue;
}
// 16-bytes aligned placeholder for the XYZ centroid of a surface patch
Eigen::Vector4f xyz_centroid;
// Estimate the XYZ centroid
compute3DCentroid (*surface_, nn_indices, xyz_centroid);
// Placeholder for the 3x3 covariance matrix at each surface patch
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
// Compute the 3x3 covariance matrix
computeCovarianceMatrix (*surface_, nn_indices, xyz_centroid, covariance_matrix);
// Get the plane normal and surface curvature
pcl::solvePlaneParameters (covariance_matrix,
output.points (idx, 0), output.points (idx, 1), output.points (idx, 2), output.points (idx, 3));
flipNormalTowardsViewpoint (input_->points[(*indices_)[idx]], vpx, vpy, vpz,
output.points (idx, 0), output.points (idx, 1), output.points (idx, 2));
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointInT, typename PointOutT> void
GeoNormalEstimationOMP<PointInT, PointOutT>::computeFeature (PointCloudOut &output)
{
float vpx, vpy, vpz;
getViewPoint (vpx, vpy, vpz);
output.is_dense = true;
// Iterating over the entire index vector
#pragma omp parallel for schedule (dynamic, threads_)
for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
{
// Allocate enough space to hold the results
// \note This resize is irrelevant for a radiusSearch ().
std::vector<int> nn_indices (k_);
std::vector<float> nn_dists (k_);
if (!isFinite ((*input_)[(*indices_)[idx]]) ||
this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
{
output.points[idx].normal[0] = output.points[idx].normal[1] = output.points[idx].normal[2] = output.points[idx].curvature = std::numeric_limits<float>::quiet_NaN ();
output.is_dense = false;
continue;
}
// 16-bytes aligned placeholder for the XYZ centroid of a surface patch
Eigen::Vector4f xyz_centroid;
// Estimate the XYZ centroid
compute3DCentroid (*surface_, nn_indices, xyz_centroid);
// Placeholder for the 3x3 covariance matrix at each surface patch
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
// Compute the 3x3 covariance matrix
computeCovarianceMatrix (*surface_, nn_indices, xyz_centroid, covariance_matrix);
// Get the plane normal and surface curvature
pcl::solvePlaneParameters (covariance_matrix,
output.points[idx].normal[0], output.points[idx].normal[1], output.points[idx].normal[2], output.points[idx].curvature);
flipNormalTowardsViewpoint (input_->points[(*indices_)[idx]], vpx, vpy, vpz,
output.points[idx].normal[0], output.points[idx].normal[1], output.points[idx].normal[2]);
}
}