41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PLANE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_NORMAL_PLANE_H_
44 #include <pcl/sample_consensus/sac_model_normal_plane.h>
48 template <
typename Po
intT,
typename Po
intNT>
void
50 const Eigen::VectorXf &model_coefficients,
const double threshold,
Indices &inliers)
54 PCL_ERROR (
"[pcl::SampleConsensusModelNormalPlane::selectWithinDistance] No input dataset containing normals was given!\n");
60 if (!isModelValid (model_coefficients))
67 Eigen::Vector4f coeff = model_coefficients;
71 error_sqr_dists_.clear ();
72 inliers.reserve (indices_->size ());
73 error_sqr_dists_.reserve (indices_->size ());
76 for (std::size_t i = 0; i < indices_->size (); ++i)
78 const PointT &pt = (*input_)[(*indices_)[i]];
79 const PointNT &nt = (*normals_)[(*indices_)[i]];
82 Eigen::Vector4f p (pt.x, pt.y, pt.z, 0.0f);
83 Eigen::Vector4f n (nt.normal_x, nt.normal_y, nt.normal_z, 0.0f);
84 double d_euclid = std::abs (coeff.dot (p) + model_coefficients[3]);
87 double d_normal = std::abs (
getAngle3D (n, coeff));
88 d_normal = (std::min) (d_normal,
M_PI - d_normal);
91 double weight = normal_distance_weight_ * (1.0 - nt.curvature);
93 double distance = std::abs (weight * d_normal + (1.0 - weight) * d_euclid);
97 inliers.push_back ((*indices_)[i]);
98 error_sqr_dists_.push_back (
distance);
104 template <
typename Po
intT,
typename Po
intNT> std::size_t
106 const Eigen::VectorXf &model_coefficients,
const double threshold)
const
110 PCL_ERROR (
"[pcl::SampleConsensusModelNormalPlane::countWithinDistance] No input dataset containing normals was given!\n");
115 if (!isModelValid (model_coefficients))
118 #if defined (__AVX__) && defined (__AVX2__)
119 return countWithinDistanceAVX (model_coefficients, threshold);
120 #elif defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
121 return countWithinDistanceSSE (model_coefficients, threshold);
123 return countWithinDistanceStandard (model_coefficients, threshold);
128 template <
typename Po
intT,
typename Po
intNT> std::size_t
130 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
132 std::size_t nr_p = 0;
135 Eigen::Vector4f coeff = model_coefficients;
139 for (; i < indices_->size (); ++i)
141 const PointT &pt = (*input_)[(*indices_)[i]];
142 const PointNT &nt = (*normals_)[(*indices_)[i]];
145 const Eigen::Vector4f p (pt.x, pt.y, pt.z, 0.0f);
146 const Eigen::Vector4f n (nt.normal_x, nt.normal_y, nt.normal_z, 0.0f);
147 const double d_euclid = std::abs (coeff.dot (p) + model_coefficients[3]);
150 double d_normal = std::abs (
getAngle3D (n, coeff));
151 d_normal = (std::min) (d_normal,
M_PI - d_normal);
154 const double weight = normal_distance_weight_ * (1.0 - nt.curvature);
156 if (std::abs (weight * d_normal + (1.0 - weight) * d_euclid) < threshold)
165 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
166 template <
typename Po
intT,
typename Po
intNT> std::size_t
168 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
170 std::size_t nr_p = 0;
171 const __m128 a_vec = _mm_set1_ps (model_coefficients[0]);
172 const __m128 b_vec = _mm_set1_ps (model_coefficients[1]);
173 const __m128 c_vec = _mm_set1_ps (model_coefficients[2]);
174 const __m128 d_vec = _mm_set1_ps (model_coefficients[3]);
175 const __m128 threshold_vec = _mm_set1_ps (threshold);
176 const __m128 normal_distance_weight_vec = _mm_set1_ps (normal_distance_weight_);
177 const __m128 abs_help = _mm_set1_ps (-0.0F);
178 __m128i res = _mm_set1_epi32(0);
179 for (; (i + 4) <= indices_->size (); i += 4)
183 const __m128 d_normal_vec = getAcuteAngle3DSSE (
184 _mm_set_ps ((*normals_)[(*indices_)[i ]].normal_x,
185 (*normals_)[(*indices_)[i+1]].normal_x,
186 (*normals_)[(*indices_)[i+2]].normal_x,
187 (*normals_)[(*indices_)[i+3]].normal_x),
188 _mm_set_ps ((*normals_)[(*indices_)[i ]].normal_y,
189 (*normals_)[(*indices_)[i+1]].normal_y,
190 (*normals_)[(*indices_)[i+2]].normal_y,
191 (*normals_)[(*indices_)[i+3]].normal_y),
192 _mm_set_ps ((*normals_)[(*indices_)[i ]].normal_z,
193 (*normals_)[(*indices_)[i+1]].normal_z,
194 (*normals_)[(*indices_)[i+2]].normal_z,
195 (*normals_)[(*indices_)[i+3]].normal_z),
196 a_vec, b_vec, c_vec);
197 const __m128 weight_vec = _mm_mul_ps (normal_distance_weight_vec, _mm_sub_ps (_mm_set1_ps (1.0f),
198 _mm_set_ps ((*normals_)[(*indices_)[i ]].curvature,
199 (*normals_)[(*indices_)[i+1]].curvature,
200 (*normals_)[(*indices_)[i+2]].curvature,
201 (*normals_)[(*indices_)[i+3]].curvature)));
202 const __m128 dist = _mm_andnot_ps (abs_help, _mm_add_ps (_mm_mul_ps (weight_vec, d_normal_vec), _mm_mul_ps (_mm_sub_ps (_mm_set1_ps (1.0f), weight_vec), d_euclid_vec)));
203 const __m128 mask = _mm_cmplt_ps (dist, threshold_vec);
204 res = _mm_add_epi32 (res, _mm_and_si128 (_mm_set1_epi32 (1), _mm_castps_si128 (mask)));
206 nr_p += _mm_extract_epi32 (res, 0);
207 nr_p += _mm_extract_epi32 (res, 1);
208 nr_p += _mm_extract_epi32 (res, 2);
209 nr_p += _mm_extract_epi32 (res, 3);
212 nr_p += countWithinDistanceStandard(model_coefficients, threshold, i);
218 #if defined (__AVX__) && defined (__AVX2__)
219 template <
typename Po
intT,
typename Po
intNT> std::size_t
221 const Eigen::VectorXf &model_coefficients,
const double threshold, std::size_t i)
const
223 std::size_t nr_p = 0;
224 const __m256 a_vec = _mm256_set1_ps (model_coefficients[0]);
225 const __m256 b_vec = _mm256_set1_ps (model_coefficients[1]);
226 const __m256 c_vec = _mm256_set1_ps (model_coefficients[2]);
227 const __m256 d_vec = _mm256_set1_ps (model_coefficients[3]);
228 const __m256 threshold_vec = _mm256_set1_ps (threshold);
229 const __m256 normal_distance_weight_vec = _mm256_set1_ps (normal_distance_weight_);
230 const __m256 abs_help = _mm256_set1_ps (-0.0F);
231 __m256i res = _mm256_set1_epi32(0);
232 for (; (i + 8) <= indices_->size (); i += 8)
236 const __m256 d_normal_vec = getAcuteAngle3DAVX (
237 _mm256_set_ps ((*normals_)[(*indices_)[i ]].normal_x,
238 (*normals_)[(*indices_)[i+1]].normal_x,
239 (*normals_)[(*indices_)[i+2]].normal_x,
240 (*normals_)[(*indices_)[i+3]].normal_x,
241 (*normals_)[(*indices_)[i+4]].normal_x,
242 (*normals_)[(*indices_)[i+5]].normal_x,
243 (*normals_)[(*indices_)[i+6]].normal_x,
244 (*normals_)[(*indices_)[i+7]].normal_x),
245 _mm256_set_ps ((*normals_)[(*indices_)[i ]].normal_y,
246 (*normals_)[(*indices_)[i+1]].normal_y,
247 (*normals_)[(*indices_)[i+2]].normal_y,
248 (*normals_)[(*indices_)[i+3]].normal_y,
249 (*normals_)[(*indices_)[i+4]].normal_y,
250 (*normals_)[(*indices_)[i+5]].normal_y,
251 (*normals_)[(*indices_)[i+6]].normal_y,
252 (*normals_)[(*indices_)[i+7]].normal_y),
253 _mm256_set_ps ((*normals_)[(*indices_)[i ]].normal_z,
254 (*normals_)[(*indices_)[i+1]].normal_z,
255 (*normals_)[(*indices_)[i+2]].normal_z,
256 (*normals_)[(*indices_)[i+3]].normal_z,
257 (*normals_)[(*indices_)[i+4]].normal_z,
258 (*normals_)[(*indices_)[i+5]].normal_z,
259 (*normals_)[(*indices_)[i+6]].normal_z,
260 (*normals_)[(*indices_)[i+7]].normal_z),
261 a_vec, b_vec, c_vec);
262 const __m256 weight_vec = _mm256_mul_ps (normal_distance_weight_vec, _mm256_sub_ps (_mm256_set1_ps (1.0f),
263 _mm256_set_ps ((*normals_)[(*indices_)[i ]].curvature,
264 (*normals_)[(*indices_)[i+1]].curvature,
265 (*normals_)[(*indices_)[i+2]].curvature,
266 (*normals_)[(*indices_)[i+3]].curvature,
267 (*normals_)[(*indices_)[i+4]].curvature,
268 (*normals_)[(*indices_)[i+5]].curvature,
269 (*normals_)[(*indices_)[i+6]].curvature,
270 (*normals_)[(*indices_)[i+7]].curvature)));
271 const __m256 dist = _mm256_andnot_ps (abs_help, _mm256_add_ps (_mm256_mul_ps (weight_vec, d_normal_vec), _mm256_mul_ps (_mm256_sub_ps (_mm256_set1_ps (1.0f), weight_vec), d_euclid_vec)));
272 const __m256 mask = _mm256_cmp_ps (dist, threshold_vec, _CMP_LT_OQ);
273 res = _mm256_add_epi32 (res, _mm256_and_si256 (_mm256_set1_epi32 (1), _mm256_castps_si256 (mask)));
275 nr_p += _mm256_extract_epi32 (res, 0);
276 nr_p += _mm256_extract_epi32 (res, 1);
277 nr_p += _mm256_extract_epi32 (res, 2);
278 nr_p += _mm256_extract_epi32 (res, 3);
279 nr_p += _mm256_extract_epi32 (res, 4);
280 nr_p += _mm256_extract_epi32 (res, 5);
281 nr_p += _mm256_extract_epi32 (res, 6);
282 nr_p += _mm256_extract_epi32 (res, 7);
285 nr_p += countWithinDistanceStandard(model_coefficients, threshold, i);
291 template <
typename Po
intT,
typename Po
intNT>
void
293 const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
const
297 PCL_ERROR (
"[pcl::SampleConsensusModelNormalPlane::getDistancesToModel] No input dataset containing normals was given!\n");
302 if (!isModelValid (model_coefficients))
309 Eigen::Vector4f coeff = model_coefficients;
312 distances.resize (indices_->size ());
315 for (std::size_t i = 0; i < indices_->size (); ++i)
317 const PointT &pt = (*input_)[(*indices_)[i]];
318 const PointNT &nt = (*normals_)[(*indices_)[i]];
321 Eigen::Vector4f p (pt.x, pt.y, pt.z, 0.0f);
322 Eigen::Vector4f n (nt.normal_x, nt.normal_y, nt.normal_z, 0.0f);
323 double d_euclid = std::abs (coeff.dot (p) + model_coefficients[3]);
326 double d_normal = std::abs (
getAngle3D (n, coeff));
327 d_normal = (std::min) (d_normal,
M_PI - d_normal);
330 double weight = normal_distance_weight_ * (1.0 - nt.curvature);
332 distances[i] = std::abs (weight * d_normal + (1.0 - weight) * d_euclid);
336 #define PCL_INSTANTIATE_SampleConsensusModelNormalPlane(PointT, PointNT) template class PCL_EXPORTS pcl::SampleConsensusModelNormalPlane<PointT, PointNT>;
SampleConsensusModelNormalPlane defines a model for 3D plane segmentation using additional surface no...
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given plane model.
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
SampleConsensusModelPlane defines a model for 3D plane segmentation.
Define standard C methods and C++ classes that are common to all methods.
double getAngle3D(const Eigen::Vector4f &v1, const Eigen::Vector4f &v2, const bool in_degree=false)
Compute the smallest angle between two 3D vectors in radians (default) or degree.
float distance(const PointT &p1, const PointT &p2)
IndicesAllocator<> Indices
Type used for indices in PCL.
A point structure representing Euclidean xyz coordinates, and the RGB color.