39 #ifndef PCL_FEATURES_IMPL_GASD_H_
40 #define PCL_FEATURES_IMPL_GASD_H_
42 #include <pcl/features/gasd.h>
43 #include <pcl/common/transforms.h>
48 template <
typename Po
intInT,
typename Po
intOutT>
void
53 output.width = output.height = 0;
54 output.points.clear ();
62 output.header = surface_->header;
63 output.is_dense = surface_->is_dense;
66 computeFeature (output);
72 template <
typename Po
intInT,
typename Po
intOutT>
void
75 Eigen::Vector4f centroid;
76 Eigen::Matrix3f covariance_matrix;
84 Eigen::Matrix3f eigenvectors;
85 Eigen::Vector3f eigenvalues;
88 pcl::eigen33 (covariance_matrix, eigenvectors, eigenvalues);
91 Eigen::Vector3f z_axis = eigenvectors.col (0);
94 if (z_axis.dot (view_direction_) > 0)
100 const Eigen::Vector3f x_axis = eigenvectors.col (2);
103 const Eigen::Vector3f y_axis = z_axis.cross (x_axis);
105 const Eigen::Vector3f centroid_xyz = centroid.head<3> ();
108 transform_ << x_axis.transpose (), -x_axis.dot (centroid_xyz),
109 y_axis.transpose (), -y_axis.dot (centroid_xyz),
110 z_axis.transpose (), -z_axis.dot (centroid_xyz),
111 0.0f, 0.0f, 0.0f, 1.0f;
115 template <
typename Po
intInT,
typename Po
intOutT>
void
117 const float max_coord,
118 const std::size_t half_grid_size,
121 const float hist_incr,
122 std::vector<Eigen::VectorXf> &hists)
124 const std::size_t grid_size = half_grid_size * 2;
127 const Eigen::Vector3f scaled ( (p[0] / max_coord) * half_grid_size, (p[1] / max_coord) * half_grid_size, (p[2] / max_coord) * half_grid_size);
130 Eigen::Vector4f coords (scaled[0] + half_grid_size, scaled[1] + half_grid_size, scaled[2] + half_grid_size, hbin);
135 coords -= Eigen::Vector4f (0.5f, 0.5f, 0.5f, 0.5f);
139 const Eigen::Vector4f bins (std::floor (coords[0]), std::floor (coords[1]), std::floor (coords[2]), std::floor (coords[3]));
142 const std::size_t grid_idx = ( (bins[0] + 1) * (grid_size + 2) + bins[1] + 1) * (grid_size + 2) + bins[2] + 1;
143 const std::size_t h_idx = bins[3] + 1;
148 hists[grid_idx][h_idx] += hist_incr;
153 coords -= Eigen::Vector4f (bins[0], bins[1], bins[2], 0.0f);
155 const float v_x1 = hist_incr * coords[0];
156 const float v_x0 = hist_incr - v_x1;
158 const float v_xy11 = v_x1 * coords[1];
159 const float v_xy10 = v_x1 - v_xy11;
160 const float v_xy01 = v_x0 * coords[1];
161 const float v_xy00 = v_x0 - v_xy01;
163 const float v_xyz111 = v_xy11 * coords[2];
164 const float v_xyz110 = v_xy11 - v_xyz111;
165 const float v_xyz101 = v_xy10 * coords[2];
166 const float v_xyz100 = v_xy10 - v_xyz101;
167 const float v_xyz011 = v_xy01 * coords[2];
168 const float v_xyz010 = v_xy01 - v_xyz011;
169 const float v_xyz001 = v_xy00 * coords[2];
170 const float v_xyz000 = v_xy00 - v_xyz001;
172 if (interp == INTERP_TRILINEAR)
175 hists[grid_idx][h_idx] += v_xyz000;
176 hists[grid_idx + 1][h_idx] += v_xyz001;
177 hists[grid_idx + (grid_size + 2)][h_idx] += v_xyz010;
178 hists[grid_idx + (grid_size + 3)][h_idx] += v_xyz011;
179 hists[grid_idx + (grid_size + 2) * (grid_size + 2)][h_idx] += v_xyz100;
180 hists[grid_idx + (grid_size + 2) * (grid_size + 2) + 1][h_idx] += v_xyz101;
181 hists[grid_idx + (grid_size + 3) * (grid_size + 2)][h_idx] += v_xyz110;
182 hists[grid_idx + (grid_size + 3) * (grid_size + 2) + 1][h_idx] += v_xyz111;
187 coords[3] -= bins[3];
189 const float v_xyzh1111 = v_xyz111 * coords[3];
190 const float v_xyzh1110 = v_xyz111 - v_xyzh1111;
191 const float v_xyzh1101 = v_xyz110 * coords[3];
192 const float v_xyzh1100 = v_xyz110 - v_xyzh1101;
193 const float v_xyzh1011 = v_xyz101 * coords[3];
194 const float v_xyzh1010 = v_xyz101 - v_xyzh1011;
195 const float v_xyzh1001 = v_xyz100 * coords[3];
196 const float v_xyzh1000 = v_xyz100 - v_xyzh1001;
197 const float v_xyzh0111 = v_xyz011 * coords[3];
198 const float v_xyzh0110 = v_xyz011 - v_xyzh0111;
199 const float v_xyzh0101 = v_xyz010 * coords[3];
200 const float v_xyzh0100 = v_xyz010 - v_xyzh0101;
201 const float v_xyzh0011 = v_xyz001 * coords[3];
202 const float v_xyzh0010 = v_xyz001 - v_xyzh0011;
203 const float v_xyzh0001 = v_xyz000 * coords[3];
204 const float v_xyzh0000 = v_xyz000 - v_xyzh0001;
206 hists[grid_idx][h_idx] += v_xyzh0000;
207 hists[grid_idx][h_idx + 1] += v_xyzh0001;
208 hists[grid_idx + 1][h_idx] += v_xyzh0010;
209 hists[grid_idx + 1][h_idx + 1] += v_xyzh0011;
210 hists[grid_idx + (grid_size + 2)][h_idx] += v_xyzh0100;
211 hists[grid_idx + (grid_size + 2)][h_idx + 1] += v_xyzh0101;
212 hists[grid_idx + (grid_size + 3)][h_idx] += v_xyzh0110;
213 hists[grid_idx + (grid_size + 3)][h_idx + 1] += v_xyzh0111;
214 hists[grid_idx + (grid_size + 2) * (grid_size + 2)][h_idx] += v_xyzh1000;
215 hists[grid_idx + (grid_size + 2) * (grid_size + 2)][h_idx + 1] += v_xyzh1001;
216 hists[grid_idx + (grid_size + 2) * (grid_size + 2) + 1][h_idx] += v_xyzh1010;
217 hists[grid_idx + (grid_size + 2) * (grid_size + 2) + 1][h_idx + 1] += v_xyzh1011;
218 hists[grid_idx + (grid_size + 3) * (grid_size + 2)][h_idx] += v_xyzh1100;
219 hists[grid_idx + (grid_size + 3) * (grid_size + 2)][h_idx + 1] += v_xyzh1101;
220 hists[grid_idx + (grid_size + 3) * (grid_size + 2) + 1][h_idx] += v_xyzh1110;
221 hists[grid_idx + (grid_size + 3) * (grid_size + 2) + 1][h_idx + 1] += v_xyzh1111;
227 template <
typename Po
intInT,
typename Po
intOutT>
void
229 const std::size_t hists_size,
230 const std::vector<Eigen::VectorXf> &hists,
231 PointCloudOut &output,
234 for (std::size_t i = 0; i < grid_size; ++i)
236 for (std::size_t j = 0; j < grid_size; ++j)
238 for (std::size_t k = 0; k < grid_size; ++k)
240 const std::size_t idx = ( (i + 1) * (grid_size + 2) + (j + 1)) * (grid_size + 2) + (k + 1);
242 std::copy (hists[idx].data () + 1, hists[idx].data () + hists_size + 1, output[0].histogram + pos);
250 template <
typename Po
intInT,
typename Po
intOutT>
void
254 computeAlignmentTransform ();
259 const std::size_t shape_grid_size = shape_half_grid_size_ * 2;
262 std::vector<Eigen::VectorXf> shape_hists ((shape_grid_size + 2) * (shape_grid_size + 2) * (shape_grid_size + 2),
263 Eigen::VectorXf::Zero (shape_hists_size_ + 2));
265 Eigen::Vector4f centroid_p = Eigen::Vector4f::Zero ();
268 Eigen::Vector4f far_pt;
271 const float distance_normalization_factor = (centroid_p - far_pt).norm ();
274 Eigen::Vector4f min_pt, max_pt;
277 max_coord_ = std::max (min_pt.head<3> ().cwiseAbs ().maxCoeff (), max_pt.head<3> ().cwiseAbs ().maxCoeff ());
280 hist_incr_ = 100.0f /
static_cast<float> (shape_samples_.size () - 1);
283 for (
const auto& sample: shape_samples_)
286 const Eigen::Vector4f p (sample.x, sample.y, sample.z, 0.0f);
287 const float d = p.norm ();
289 const float shape_grid_step = distance_normalization_factor / shape_half_grid_size_;
292 const float dist_hist_val = std::modf(d / shape_grid_step, &integral);
294 const float dbin = dist_hist_val * shape_hists_size_;
297 addSampleToHistograms (p, max_coord_, shape_half_grid_size_, shape_interp_, dbin, hist_incr_, shape_hists);
303 copyShapeHistogramsToOutput (shape_grid_size, shape_hists_size_, shape_hists, output, pos_);
306 std::fill (output[0].histogram + pos_, output[0].histogram + output[0].descriptorSize (), 0.0f);
310 template <
typename Po
intInT,
typename Po
intOutT>
void
312 const std::size_t hists_size,
313 std::vector<Eigen::VectorXf> &hists,
314 PointCloudOut &output,
317 for (std::size_t i = 0; i < grid_size; ++i)
319 for (std::size_t j = 0; j < grid_size; ++j)
321 for (std::size_t k = 0; k < grid_size; ++k)
323 const std::size_t idx = ( (i + 1) * (grid_size + 2) + (j + 1)) * (grid_size + 2) + (k + 1);
325 hists[idx][1] += hists[idx][hists_size + 1];
326 hists[idx][hists_size] += hists[idx][0];
328 std::copy (hists[idx].data () + 1, hists[idx].data () + hists_size + 1, output[0].histogram + pos);
336 template <
typename Po
intInT,
typename Po
intOutT>
void
340 GASDEstimation<PointInT, PointOutT>::computeFeature (output);
342 const std::size_t color_grid_size = color_half_grid_size_ * 2;
345 std::vector<Eigen::VectorXf> color_hists ((color_grid_size + 2) * (color_grid_size + 2) * (color_grid_size + 2),
346 Eigen::VectorXf::Zero (color_hists_size_ + 2));
349 for (
const auto& sample: shape_samples_)
352 const Eigen::Vector4f p (sample.x, sample.y, sample.z, 0.0f);
357 const unsigned char max = std::max (sample.r, std::max (sample.g, sample.b));
358 const unsigned char min = std::min (sample.r, std::min (sample.g, sample.b));
360 const float diff_inv = 1.f /
static_cast <float> (max - min);
362 if (std::isfinite (diff_inv))
366 hue = 60.f * (
static_cast <float> (sample.g - sample.b) * diff_inv);
368 else if (max == sample.g)
370 hue = 60.f * (2.f +
static_cast <float> (sample.b - sample.r) * diff_inv);
374 hue = 60.f * (4.f +
static_cast <float> (sample.r - sample.g) * diff_inv);
384 const float hbin = (hue / 360) * color_hists_size_;
387 GASDEstimation<PointInT, PointOutT>::addSampleToHistograms (p, max_coord_, color_half_grid_size_, color_interp_, hbin, hist_incr_, color_hists);
391 copyColorHistogramsToOutput (color_grid_size, color_hists_size_, color_hists, output, pos_);
394 std::fill (output[0].histogram + pos_, output[0].histogram + output[0].descriptorSize (), 0.0f);
397 #define PCL_INSTANTIATE_GASDEstimation(InT, OutT) template class PCL_EXPORTS pcl::GASDEstimation<InT, OutT>;
398 #define PCL_INSTANTIATE_GASDColorEstimation(InT, OutT) template class PCL_EXPORTS pcl::GASDColorEstimation<InT, OutT>;
400 #endif // PCL_FEATURES_IMPL_GASD_H_