43 #include <pcl/features/intensity_gradient.h>
45 #include <pcl/common/point_tests.h>
49 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT,
typename IntensitySelectorT>
void
52 const Eigen::Vector3f &point,
float mean_intensity,
const Eigen::Vector3f &normal, Eigen::Vector3f &gradient)
54 if (indices.size () < 3)
56 gradient[0] = gradient[1] = gradient[2] = std::numeric_limits<float>::quiet_NaN ();
60 Eigen::Matrix3f A = Eigen::Matrix3f::Zero ();
61 Eigen::Vector3f b = Eigen::Vector3f::Zero ();
63 for (
const auto &nn_index : indices)
65 PointInT p = cloud[nn_index];
66 if (!std::isfinite (p.x) ||
67 !std::isfinite (p.y) ||
68 !std::isfinite (p.z) ||
69 !std::isfinite (intensity_ (p)))
75 intensity_.demean (p, mean_intensity);
77 A (0, 0) += p.x * p.x;
78 A (0, 1) += p.x * p.y;
79 A (0, 2) += p.x * p.z;
81 A (1, 1) += p.y * p.y;
82 A (1, 2) += p.y * p.z;
84 A (2, 2) += p.z * p.z;
86 b[0] += p.x * intensity_ (p);
87 b[1] += p.y * intensity_ (p);
88 b[2] += p.z * intensity_ (p);
97 Eigen::Vector3f eigen_values;
98 Eigen::Matrix3f eigen_vectors;
99 eigen33 (A, eigen_vectors, eigen_values);
101 b = eigen_vectors.transpose () * b;
103 if ( eigen_values (0) != 0)
104 b (0) /= eigen_values (0);
108 if ( eigen_values (1) != 0)
109 b (1) /= eigen_values (1);
113 if ( eigen_values (2) != 0)
114 b (2) /= eigen_values (2);
119 Eigen::Vector3f x = eigen_vectors * b;
138 gradient = (Eigen::Matrix3f::Identity () - normal*normal.transpose ()) * x;
142 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT,
typename IntensitySelectorT>
void
148 std::vector<float> nn_dists (k_);
152 if (surface_->is_dense)
154 #pragma omp parallel for \
157 firstprivate(nn_indices, nn_dists) \
158 num_threads(threads_)
160 for (std::ptrdiff_t idx = 0; idx < static_cast<std::ptrdiff_t> (indices_->size ()); ++idx)
162 PointOutT &p_out = output[idx];
164 if (!this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
166 p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
171 Eigen::Vector3f centroid;
172 float mean_intensity = 0;
175 for (
const auto &nn_index : nn_indices)
177 centroid += (*surface_)[nn_index].getVector3fMap ();
178 mean_intensity += intensity_ ((*surface_)[nn_index]);
180 centroid /=
static_cast<float> (nn_indices.size ());
181 mean_intensity /=
static_cast<float> (nn_indices.size ());
183 Eigen::Vector3f normal = Eigen::Vector3f::Map ((*normals_)[(*indices_) [idx]].normal);
184 Eigen::Vector3f gradient;
185 computePointIntensityGradient (*surface_, nn_indices, centroid, mean_intensity, normal, gradient);
187 p_out.gradient[0] = gradient[0];
188 p_out.gradient[1] = gradient[1];
189 p_out.gradient[2] = gradient[2];
194 #pragma omp parallel for \
197 firstprivate(nn_indices, nn_dists) \
198 num_threads(threads_)
200 for (std::ptrdiff_t idx = 0; idx < static_cast<std::ptrdiff_t> (indices_->size ()); ++idx)
202 PointOutT &p_out = output[idx];
203 if (!
isFinite ((*surface_) [(*indices_)[idx]]) ||
204 !this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
206 p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
210 Eigen::Vector3f centroid;
211 float mean_intensity = 0;
215 for (
const auto &nn_index : nn_indices)
218 if (!
isFinite ((*surface_) [nn_index]))
221 centroid += surface_->points [nn_index].getVector3fMap ();
222 mean_intensity += intensity_ (surface_->points [nn_index]);
225 centroid /=
static_cast<float> (cp);
226 mean_intensity /=
static_cast<float> (cp);
227 Eigen::Vector3f normal = Eigen::Vector3f::Map ((*normals_)[(*indices_) [idx]].normal);
228 Eigen::Vector3f gradient;
229 computePointIntensityGradient (*surface_, nn_indices, centroid, mean_intensity, normal, gradient);
231 p_out.gradient[0] = gradient[0];
232 p_out.gradient[1] = gradient[1];
233 p_out.gradient[2] = gradient[2];
238 #define PCL_INSTANTIATE_IntensityGradientEstimation(InT,NT,OutT) template class PCL_EXPORTS pcl::IntensityGradientEstimation<InT,NT,OutT>;