38 #ifndef PCL_SURFACE_IMPL_GP3_H_
39 #define PCL_SURFACE_IMPL_GP3_H_
41 #include <pcl/surface/gp3.h>
44 template <
typename Po
intInT>
void
48 output.
polygons.reserve (2 * indices_->size ());
49 if (!reconstructPolygons (output.
polygons))
51 PCL_ERROR (
"[pcl::%s::performReconstruction] Reconstruction failed. Check parameters: search radius (%f) or mu (%f) before continuing.\n", getClassName ().c_str (), search_radius_, mu_);
59 template <
typename Po
intInT>
void
63 polygons.reserve (2 * indices_->size ());
64 if (!reconstructPolygons (polygons))
66 PCL_ERROR (
"[pcl::%s::performReconstruction] Reconstruction failed. Check parameters: search radius (%f) or mu (%f) before continuing.\n", getClassName ().c_str (), search_radius_, mu_);
72 template <
typename Po
intInT>
bool
75 if (search_radius_ <= 0 || mu_ <= 0)
80 const double sqr_mu = mu_*mu_;
81 const double sqr_max_edge = search_radius_*search_radius_;
82 if (nnn_ >
static_cast<int> (indices_->size ()))
83 nnn_ =
static_cast<int> (indices_->size ());
87 std::vector<float> sqrDists (nnn_);
93 const Eigen::Vector2f uvn_nn_qp_zero = Eigen::Vector2f::Zero();
96 already_connected_ =
false;
104 part_.resize(indices_->size (), -1);
105 state_.resize(indices_->size (), FREE);
106 source_.resize(indices_->size (), NONE);
107 ffn_.resize(indices_->size (), NONE);
108 sfn_.resize(indices_->size (), NONE);
109 fringe_queue_.clear ();
113 if (!input_->is_dense)
116 for (
const auto& idx : (*indices_))
117 if (!std::isfinite ((*input_)[idx].x) ||
118 !std::isfinite ((*input_)[idx].y) ||
119 !std::isfinite ((*input_)[idx].z))
125 coords_.reserve (indices_->size ());
126 std::vector<int> point2index (input_->size (), -1);
127 for (
int cp = 0; cp < static_cast<int> (indices_->size ()); ++
cp)
129 coords_.push_back((*input_)[(*indices_)[
cp]].getVector3fMap());
130 point2index[(*indices_)[
cp]] =
cp;
134 int is_free=0, nr_parts=0, increase_nnn4fn=0, increase_nnn4s=0, increase_dist=0;
135 angles_.resize(nnn_);
136 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > uvn_nn (nnn_);
137 Eigen::Vector2f uvn_s;
140 while (is_free != NONE)
143 if (state_[R_] == FREE)
146 part_[R_] = part_index++;
151 tree_->nearestKSearch (indices_->at (R_), nnn_, nnIdx, sqrDists);
152 double sqr_dist_threshold = (std::min)(sqr_max_edge, sqr_mu * sqrDists[1]);
155 for (
int i = 1; i < nnn_; i++)
159 nnIdx[i] = point2index[nnIdx[i]];
163 const Eigen::Vector3f nc = (*input_)[(*indices_)[R_]].getNormalVector3fMap ();
166 v_ = nc.unitOrthogonal ();
170 float dist = nc.dot (coords_[R_]);
171 proj_qp_ = coords_[R_] - dist * nc;
175 std::vector<doubleEdge> doubleEdges;
176 for (
int i = 1; i < nnn_; i++)
179 tmp_ = coords_[nnIdx[i]] - proj_qp_;
180 uvn_nn[i][0] = tmp_.dot(u_);
181 uvn_nn[i][1] = tmp_.dot(v_);
183 angles_[i].angle = std::atan2(uvn_nn[i][1], uvn_nn[i][0]);
185 angles_[i].index = nnIdx[i];
187 (state_[nnIdx[i]] == COMPLETED) || (state_[nnIdx[i]] == BOUNDARY)
188 || (state_[nnIdx[i]] == NONE) || (nnIdx[i] == UNAVAILABLE)
189 || (sqrDists[i] > sqr_dist_threshold)
191 angles_[i].visible =
false;
193 angles_[i].visible =
true;
195 if ((state_[nnIdx[i]] == FRINGE) || (state_[nnIdx[i]] == BOUNDARY))
200 tmp_ = coords_[ffn_[nnIdx[i]]] - proj_qp_;
201 e.first[0] = tmp_.dot(u_);
202 e.first[1] = tmp_.dot(v_);
203 tmp_ = coords_[sfn_[nnIdx[i]]] - proj_qp_;
204 e.second[0] = tmp_.dot(u_);
205 e.second[1] = tmp_.dot(v_);
206 doubleEdges.push_back(e);
209 angles_[0].visible =
false;
212 for (
int i = 1; i < nnn_; i++)
213 if ((angles_[i].visible) && (ffn_[R_] != nnIdx[i]) && (sfn_[R_] != nnIdx[i]))
215 bool visibility =
true;
216 for (
int j = 0; j < nr_edge; j++)
218 if (ffn_[nnIdx[doubleEdges[j].index]] != nnIdx[i])
219 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].first, Eigen::Vector2f::Zero());
222 if (sfn_[nnIdx[doubleEdges[j].index]] != nnIdx[i])
223 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].second, Eigen::Vector2f::Zero());
227 angles_[i].visible = visibility;
231 bool not_found =
true;
235 while ((left < nnn_) && ((!angles_[left].visible) || (state_[nnIdx[left]] > FREE))) left++;
241 while ((right < nnn_) && ((!angles_[right].visible) || (state_[nnIdx[right]] > FREE))) right++;
244 if ((coords_[nnIdx[left]] - coords_[nnIdx[right]]).squaredNorm () > sqr_max_edge)
248 addFringePoint (nnIdx[right], R_);
249 addFringePoint (nnIdx[left], nnIdx[right]);
250 addFringePoint (R_, nnIdx[left]);
251 state_[R_] = state_[nnIdx[left]] = state_[nnIdx[right]] = FRINGE;
252 ffn_[R_] = nnIdx[left];
253 sfn_[R_] = nnIdx[right];
254 ffn_[nnIdx[left]] = nnIdx[right];
255 sfn_[nnIdx[left]] = R_;
256 ffn_[nnIdx[right]] = R_;
257 sfn_[nnIdx[right]] = nnIdx[left];
258 addTriangle (R_, nnIdx[left], nnIdx[right], polygons);
271 for (std::size_t temp = 0; temp < indices_->size (); temp++)
273 if (state_[temp] == FREE)
280 bool is_fringe =
true;
285 int fqSize =
static_cast<int> (fringe_queue_.size ());
286 while ((fqIdx < fqSize) && (state_[fringe_queue_[fqIdx]] != FRINGE))
295 R_ = fringe_queue_[fqIdx];
298 if (ffn_[R_] == sfn_[R_])
300 state_[R_] = COMPLETED;
305 tree_->nearestKSearch (indices_->at (R_), nnn_, nnIdx, sqrDists);
308 for (
int i = 1; i < nnn_; i++)
312 nnIdx[i] = point2index[nnIdx[i]];
316 double sqr_source_dist = (coords_[R_] - coords_[source_[R_]]).squaredNorm ();
317 double sqr_ffn_dist = (coords_[R_] - coords_[ffn_[R_]]).squaredNorm ();
318 double sqr_sfn_dist = (coords_[R_] - coords_[sfn_[R_]]).squaredNorm ();
319 double max_sqr_fn_dist = (std::max)(sqr_ffn_dist, sqr_sfn_dist);
320 double sqr_dist_threshold = (std::min)(sqr_max_edge, sqr_mu * sqrDists[1]);
321 if (max_sqr_fn_dist > sqrDists[nnn_-1])
323 if (0 == increase_nnn4fn)
324 PCL_WARN(
"Not enough neighbors are considered: ffn or sfn out of range! Consider increasing nnn_... Setting R=%d to be BOUNDARY!\n", R_);
326 state_[R_] = BOUNDARY;
329 double max_sqr_fns_dist = (std::max)(sqr_source_dist, max_sqr_fn_dist);
330 if (max_sqr_fns_dist > sqrDists[nnn_-1])
332 if (0 == increase_nnn4s)
333 PCL_WARN(
"Not enough neighbors are considered: source of R=%d is out of range! Consider increasing nnn_...\n", R_);
338 const Eigen::Vector3f nc = (*input_)[(*indices_)[R_]].getNormalVector3fMap ();
341 v_ = nc.unitOrthogonal ();
345 float dist = nc.dot (coords_[R_]);
346 proj_qp_ = coords_[R_] - dist * nc;
350 std::vector<doubleEdge> doubleEdges;
351 for (
int i = 1; i < nnn_; i++)
353 tmp_ = coords_[nnIdx[i]] - proj_qp_;
354 uvn_nn[i][0] = tmp_.dot(u_);
355 uvn_nn[i][1] = tmp_.dot(v_);
358 angles_[i].angle = std::atan2(uvn_nn[i][1], uvn_nn[i][0]);
360 angles_[i].index = nnIdx[i];
361 angles_[i].nnIndex = i;
363 (state_[nnIdx[i]] == COMPLETED) || (state_[nnIdx[i]] == BOUNDARY)
364 || (state_[nnIdx[i]] == NONE) || (nnIdx[i] == UNAVAILABLE)
365 || (sqrDists[i] > sqr_dist_threshold)
367 angles_[i].visible =
false;
369 angles_[i].visible =
true;
370 if ((ffn_[R_] == nnIdx[i]) || (sfn_[R_] == nnIdx[i]))
371 angles_[i].visible =
true;
372 bool same_side =
true;
373 const Eigen::Vector3f neighbor_normal = (*input_)[(*indices_)[nnIdx[i]]].getNormalVector3fMap ();
374 double cosine = nc.dot (neighbor_normal);
375 if (cosine > 1) cosine = 1;
376 if (cosine < -1) cosine = -1;
377 double angle = std::acos (cosine);
378 if ((!consistent_) && (angle >
M_PI/2))
379 angle =
M_PI - angle;
380 if (angle > eps_angle_)
382 angles_[i].visible =
false;
386 if ((i!=0) && (same_side) && ((state_[nnIdx[i]] == FRINGE) || (state_[nnIdx[i]] == BOUNDARY)))
391 tmp_ = coords_[ffn_[nnIdx[i]]] - proj_qp_;
392 e.first[0] = tmp_.dot(u_);
393 e.first[1] = tmp_.dot(v_);
394 tmp_ = coords_[sfn_[nnIdx[i]]] - proj_qp_;
395 e.second[0] = tmp_.dot(u_);
396 e.second[1] = tmp_.dot(v_);
397 doubleEdges.push_back(e);
399 if ((state_[nnIdx[i]] == FRINGE) && (ffn_[R_] != nnIdx[i]) && (sfn_[R_] != nnIdx[i]))
401 double angle1 = std::atan2(e.first[1] - uvn_nn[i][1], e.first[0] - uvn_nn[i][0]);
402 double angle2 = std::atan2(e.second[1] - uvn_nn[i][1], e.second[0] - uvn_nn[i][0]);
403 double angleMin, angleMax;
414 double angleR = angles_[i].angle +
M_PI;
417 if ((source_[nnIdx[i]] == ffn_[nnIdx[i]]) || (source_[nnIdx[i]] == sfn_[nnIdx[i]]))
419 if ((angleMax - angleMin) <
M_PI)
421 if ((angleMin < angleR) && (angleR < angleMax))
422 angles_[i].visible =
false;
426 if ((angleR < angleMin) || (angleMax < angleR))
427 angles_[i].visible =
false;
432 tmp_ = coords_[source_[nnIdx[i]]] - proj_qp_;
433 uvn_s[0] = tmp_.dot(u_);
434 uvn_s[1] = tmp_.dot(v_);
435 double angleS = std::atan2(uvn_s[1] - uvn_nn[i][1], uvn_s[0] - uvn_nn[i][0]);
436 if ((angleMin < angleS) && (angleS < angleMax))
438 if ((angleMin < angleR) && (angleR < angleMax))
439 angles_[i].visible =
false;
443 if ((angleR < angleMin) || (angleMax < angleR))
444 angles_[i].visible =
false;
450 angles_[0].visible =
false;
453 for (
int i = 1; i < nnn_; i++)
454 if ((angles_[i].visible) && (ffn_[R_] != nnIdx[i]) && (sfn_[R_] != nnIdx[i]))
456 bool visibility =
true;
457 for (
int j = 0; j < nr_edge; j++)
459 if (doubleEdges[j].index != i)
461 const auto& f = ffn_[nnIdx[doubleEdges[j].index]];
462 if ((f != nnIdx[i]) && (f != R_))
463 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].first, Eigen::Vector2f::Zero());
467 const auto& s = sfn_[nnIdx[doubleEdges[j].index]];
468 if ((s != nnIdx[i]) && (s != R_))
469 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].second, Eigen::Vector2f::Zero());
474 angles_[i].visible = visibility;
478 std::sort (angles_.begin (), angles_.end (), GreedyProjectionTriangulation<PointInT>::nnAngleSortAsc);
481 if (angles_[2].visible ==
false)
483 if ( !( (angles_[0].index == ffn_[R_] && angles_[1].index == sfn_[R_]) || (angles_[0].index == sfn_[R_] && angles_[1].index == ffn_[R_]) ) )
485 state_[R_] = BOUNDARY;
489 if ((source_[R_] == angles_[0].index) || (source_[R_] == angles_[1].index))
490 state_[R_] = BOUNDARY;
493 if (sqr_max_edge < (coords_[ffn_[R_]] - coords_[sfn_[R_]]).squaredNorm ())
495 state_[R_] = BOUNDARY;
499 tmp_ = coords_[source_[R_]] - proj_qp_;
500 uvn_s[0] = tmp_.dot(u_);
501 uvn_s[1] = tmp_.dot(v_);
502 double angleS = std::atan2(uvn_s[1], uvn_s[0]);
503 double dif = angles_[1].angle - angles_[0].angle;
504 if ((angles_[0].angle < angleS) && (angleS < angles_[1].angle))
506 if (dif < 2*
M_PI - maximum_angle_)
507 state_[R_] = BOUNDARY;
509 closeTriangle (polygons);
513 if (dif >= maximum_angle_)
514 state_[R_] = BOUNDARY;
516 closeTriangle (polygons);
525 int start = -1, end = -1;
526 for (
int i=0; i<nnn_; i++)
528 if (ffn_[R_] == angles_[i].index)
531 if (sfn_[R_] == angles_[i+1].index)
536 for (i = i+2; i < nnn_; i++)
537 if (sfn_[R_] == angles_[i].index)
543 for (i = i+2; i < nnn_; i++)
544 if (sfn_[R_] == angles_[i].index)
550 if (sfn_[R_] == angles_[i].index)
553 if (ffn_[R_] == angles_[i+1].index)
558 for (i = i+2; i < nnn_; i++)
559 if (ffn_[R_] == angles_[i].index)
565 for (i = i+2; i < nnn_; i++)
566 if (ffn_[R_] == angles_[i].index)
575 if ((start < 0) || (end < 0) || (end == nnn_) || (!angles_[start].visible) || (!angles_[end].visible))
577 state_[R_] = BOUNDARY;
582 int last_visible = end;
583 while ((last_visible+1<nnn_) && (angles_[last_visible+1].visible)) last_visible++;
586 bool need_invert =
false;
587 if ((source_[R_] == ffn_[R_]) || (source_[R_] == sfn_[R_]))
589 if ((angles_[end].angle - angles_[start].angle) <
M_PI)
595 for (sourceIdx=0; sourceIdx<nnn_; sourceIdx++)
596 if (angles_[sourceIdx].index == source_[R_])
598 if (sourceIdx == nnn_)
600 int vis_free = NONE, nnCB = NONE;
601 for (
int i = 1; i < nnn_; i++)
604 if ((state_[nnIdx[i]] == COMPLETED) || (state_[nnIdx[i]] == BOUNDARY))
609 if (vis_free != NONE)
614 if (state_[angles_[i].index] <= FREE)
616 if (i <= last_visible)
627 while (angles_[nCB].index != nnIdx[nnCB]) nCB++;
631 if (vis_free != NONE)
633 if ((vis_free < start) || (vis_free > end))
640 if ((nCB == start) || (nCB == end))
642 bool inside_CB =
false;
643 bool outside_CB =
false;
644 for (
int i=0; i<nnn_; i++)
647 ((state_[angles_[i].index] == COMPLETED) || (state_[angles_[i].index] == BOUNDARY))
648 && (i != start) && (i != end)
651 if ((angles_[start].angle <= angles_[i].angle) && (angles_[i].angle <= angles_[end].angle))
665 if (inside_CB && !outside_CB)
667 else if (inside_CB || !outside_CB)
669 if ((angles_[end].angle - angles_[start].angle) <
M_PI)
675 if ((angles_[nCB].angle > angles_[start].angle) && (angles_[nCB].angle < angles_[end].angle))
686 else if ((angles_[start].angle < angles_[sourceIdx].angle) && (angles_[sourceIdx].angle < angles_[end].angle))
700 bool is_boundary =
false, is_skinny =
false;
701 std::vector<bool> gaps (nnn_,
false);
702 std::vector<bool> skinny (nnn_,
false);
703 std::vector<double> dif (nnn_);
704 std::vector<int> angleIdx; angleIdx.reserve (nnn_);
707 for (
int j=start; j<last_visible; j++)
709 dif[j] = (angles_[j+1].angle - angles_[j].angle);
710 if (dif[j] < minimum_angle_)
712 skinny[j] = is_skinny =
true;
714 else if (maximum_angle_ <= dif[j])
716 gaps[j] = is_boundary =
true;
718 if ((!gaps[j]) && (sqr_max_edge < (coords_[angles_[j+1].index] - coords_[angles_[j].index]).squaredNorm ()))
720 gaps[j] = is_boundary =
true;
722 angleIdx.push_back(j);
725 dif[last_visible] = (2*
M_PI + angles_[0].angle - angles_[last_visible].angle);
726 if (dif[last_visible] < minimum_angle_)
728 skinny[last_visible] = is_skinny =
true;
730 else if (maximum_angle_ <= dif[last_visible])
732 gaps[last_visible] = is_boundary =
true;
734 if ((!gaps[last_visible]) && (sqr_max_edge < (coords_[angles_[0].index] - coords_[angles_[last_visible].index]).squaredNorm ()))
736 gaps[last_visible] = is_boundary =
true;
738 angleIdx.push_back(last_visible);
740 for (
int j=0; j<end; j++)
742 dif[j] = (angles_[j+1].angle - angles_[j].angle);
743 if (dif[j] < minimum_angle_)
745 skinny[j] = is_skinny =
true;
747 else if (maximum_angle_ <= dif[j])
749 gaps[j] = is_boundary =
true;
751 if ((!gaps[j]) && (sqr_max_edge < (coords_[angles_[j+1].index] - coords_[angles_[j].index]).squaredNorm ()))
753 gaps[j] = is_boundary =
true;
755 angleIdx.push_back(j);
757 angleIdx.push_back(end);
762 for (
int j=start; j<end; j++)
764 dif[j] = (angles_[j+1].angle - angles_[j].angle);
765 if (dif[j] < minimum_angle_)
767 skinny[j] = is_skinny =
true;
769 else if (maximum_angle_ <= dif[j])
771 gaps[j] = is_boundary =
true;
773 if ((!gaps[j]) && (sqr_max_edge < (coords_[angles_[j+1].index] - coords_[angles_[j].index]).squaredNorm ()))
775 gaps[j] = is_boundary =
true;
777 angleIdx.push_back(j);
779 angleIdx.push_back(end);
783 state_[R_] = is_boundary ? BOUNDARY : COMPLETED;
785 auto first_gap_after = angleIdx.end ();
786 auto last_gap_before = angleIdx.begin ();
788 for (
auto it = angleIdx.begin (); it != angleIdx.end () - 1; ++it)
793 if (first_gap_after == angleIdx.end())
794 first_gap_after = it;
795 last_gap_before = it+1;
800 angleIdx.erase(first_gap_after+1, last_gap_before);
806 double angle_so_far = 0, angle_would_be;
807 double max_combined_angle = (std::min)(maximum_angle_,
M_PI-2*minimum_angle_);
811 std::vector<int> to_erase;
812 for (
auto it = angleIdx.begin()+1; it != angleIdx.end()-1; ++it)
817 angle_so_far += dif[*(it-1)];
819 angle_would_be = angle_so_far;
821 angle_would_be = angle_so_far + dif[*it];
823 (skinny[*it] || skinny[*(it-1)]) &&
824 ((state_[angles_[*it].index] <= FREE) || (state_[angles_[*(it-1)].index] <= FREE)) &&
825 ((!gaps[*it]) || (angles_[*it].nnIndex > angles_[*(it-1)].nnIndex)) &&
826 ((!gaps[*(it-1)]) || (angles_[*it].nnIndex > angles_[*(it+1)].nnIndex)) &&
827 (angle_would_be < max_combined_angle)
833 to_erase.push_back(*it);
837 gaps[*(it-1)] =
true;
838 to_erase.push_back(*it);
842 std::vector<int>::iterator prev_it;
843 int erased_idx =
static_cast<int> (to_erase.size ()) -1;
844 for (prev_it = it-1; (erased_idx != -1) && (it != angleIdx.begin()); --it)
845 if (*it == to_erase[erased_idx])
849 bool can_delete =
true;
850 for (
auto curr_it = prev_it+1; curr_it != it+1; ++curr_it)
852 tmp_ = coords_[angles_[*curr_it].index] - proj_qp_;
855 tmp_ = coords_[angles_[*prev_it].index] - proj_qp_;
856 S1[0] = tmp_.dot(u_);
857 S1[1] = tmp_.dot(v_);
858 tmp_ = coords_[angles_[*(it+1)].index] - proj_qp_;
859 S2[0] = tmp_.dot(u_);
860 S2[1] = tmp_.dot(v_);
871 to_erase.push_back(*it);
878 for (
const auto &idx : to_erase)
880 for (
auto iter = angleIdx.begin(); iter != angleIdx.end(); ++iter)
883 angleIdx.erase(iter);
890 changed_1st_fn_ =
false;
891 changed_2nd_fn_ =
false;
892 new2boundary_ = NONE;
893 for (
auto it = angleIdx.begin()+1; it != angleIdx.end()-1; ++it)
895 current_index_ = angles_[*it].index;
897 is_current_free_ =
false;
898 if (state_[current_index_] <= FREE)
900 state_[current_index_] = FRINGE;
901 is_current_free_ =
true;
903 else if (!already_connected_)
905 prev_is_ffn_ = (ffn_[current_index_] == angles_[*(it-1)].index) && (!gaps[*(it-1)]);
906 prev_is_sfn_ = (sfn_[current_index_] == angles_[*(it-1)].index) && (!gaps[*(it-1)]);
907 next_is_ffn_ = (ffn_[current_index_] == angles_[*(it+1)].index) && (!gaps[*it]);
908 next_is_sfn_ = (sfn_[current_index_] == angles_[*(it+1)].index) && (!gaps[*it]);
914 if (is_current_free_)
915 state_[current_index_] = NONE;
920 addTriangle (current_index_, angles_[*(it-1)].index, R_, polygons);
921 addFringePoint (current_index_, R_);
922 new2boundary_ = current_index_;
923 if (!already_connected_)
924 connectPoint (polygons, angles_[*(it-1)].index, R_,
925 angles_[*(it+1)].index,
926 uvn_nn[angles_[*it].nnIndex], uvn_nn[angles_[*(it-1)].nnIndex], uvn_nn_qp_zero);
927 else already_connected_ =
false;
928 if (ffn_[R_] == angles_[*(angleIdx.begin())].index)
930 ffn_[R_] = new2boundary_;
932 else if (sfn_[R_] == angles_[*(angleIdx.begin())].index)
934 sfn_[R_] = new2boundary_;
941 addFringePoint (current_index_, R_);
942 new2boundary_ = current_index_;
943 if (!already_connected_) connectPoint (polygons, R_, angles_[*(it+1)].index,
944 (it+2) == angleIdx.end() ? -1 : angles_[*(it+2)].index,
945 uvn_nn[angles_[*it].nnIndex], uvn_nn_qp_zero,
946 uvn_nn[angles_[*(it+1)].nnIndex]);
947 else already_connected_ =
false;
948 if (ffn_[R_] == angles_[*(angleIdx.end()-1)].index)
950 ffn_[R_] = new2boundary_;
952 else if (sfn_[R_] == angles_[*(angleIdx.end()-1)].index)
954 sfn_[R_] = new2boundary_;
960 addTriangle (current_index_, angles_[*(it-1)].index, R_, polygons);
961 addFringePoint (current_index_, R_);
962 if (!already_connected_) connectPoint (polygons, angles_[*(it-1)].index, angles_[*(it+1)].index,
963 (it+2) == angleIdx.end() ? -1 : gaps[*(it+1)] ? R_ : angles_[*(it+2)].index,
964 uvn_nn[angles_[*it].nnIndex],
965 uvn_nn[angles_[*(it-1)].nnIndex],
966 uvn_nn[angles_[*(it+1)].nnIndex]);
967 else already_connected_ =
false;
972 if (ffn_[R_] == sfn_[R_])
974 state_[R_] = COMPLETED;
976 if (!gaps[*(angleIdx.end()-2)])
978 addTriangle (angles_[*(angleIdx.end()-2)].index, angles_[*(angleIdx.end()-1)].index, R_, polygons);
979 addFringePoint (angles_[*(angleIdx.end()-2)].index, R_);
980 if (R_ == ffn_[angles_[*(angleIdx.end()-1)].index])
982 if (angles_[*(angleIdx.end()-2)].index == sfn_[angles_[*(angleIdx.end()-1)].index])
984 state_[angles_[*(angleIdx.end()-1)].index] = COMPLETED;
988 ffn_[angles_[*(angleIdx.end()-1)].index] = angles_[*(angleIdx.end()-2)].index;
991 else if (R_ == sfn_[angles_[*(angleIdx.end()-1)].index])
993 if (angles_[*(angleIdx.end()-2)].index == ffn_[angles_[*(angleIdx.end()-1)].index])
995 state_[angles_[*(angleIdx.end()-1)].index] = COMPLETED;
999 sfn_[angles_[*(angleIdx.end()-1)].index] = angles_[*(angleIdx.end()-2)].index;
1003 if (!gaps[*(angleIdx.begin())])
1005 if (R_ == ffn_[angles_[*(angleIdx.begin())].index])
1007 if (angles_[*(angleIdx.begin()+1)].index == sfn_[angles_[*(angleIdx.begin())].index])
1009 state_[angles_[*(angleIdx.begin())].index] = COMPLETED;
1013 ffn_[angles_[*(angleIdx.begin())].index] = angles_[*(angleIdx.begin()+1)].index;
1016 else if (R_ == sfn_[angles_[*(angleIdx.begin())].index])
1018 if (angles_[*(angleIdx.begin()+1)].index == ffn_[angles_[*(angleIdx.begin())].index])
1020 state_[angles_[*(angleIdx.begin())].index] = COMPLETED;
1024 sfn_[angles_[*(angleIdx.begin())].index] = angles_[*(angleIdx.begin()+1)].index;
1030 PCL_DEBUG (
"Number of triangles: %lu\n", polygons.size());
1031 PCL_DEBUG (
"Number of unconnected parts: %d\n", nr_parts);
1032 if (increase_nnn4fn > 0)
1033 PCL_WARN (
"Number of neighborhood size increase requests for fringe neighbors: %d\n", increase_nnn4fn);
1034 if (increase_nnn4s > 0)
1035 PCL_WARN (
"Number of neighborhood size increase requests for source: %d\n", increase_nnn4s);
1036 if (increase_dist > 0)
1037 PCL_WARN (
"Number of automatic maximum distance increases: %d\n", increase_dist);
1040 std::sort (fringe_queue_.begin (), fringe_queue_.end ());
1041 fringe_queue_.erase (std::unique (fringe_queue_.begin (), fringe_queue_.end ()), fringe_queue_.end ());
1042 PCL_DEBUG (
"Number of processed points: %lu / %lu\n", fringe_queue_.size(), indices_->size ());
1047 template <
typename Po
intInT>
void
1050 state_[R_] = COMPLETED;
1051 addTriangle (angles_[0].index, angles_[1].index, R_, polygons);
1052 for (
int aIdx=0; aIdx<2; aIdx++)
1054 if (ffn_[angles_[aIdx].index] == R_)
1056 if (sfn_[angles_[aIdx].index] == angles_[(aIdx+1)%2].index)
1058 state_[angles_[aIdx].index] = COMPLETED;
1062 ffn_[angles_[aIdx].index] = angles_[(aIdx+1)%2].index;
1065 else if (sfn_[angles_[aIdx].index] == R_)
1067 if (ffn_[angles_[aIdx].index] == angles_[(aIdx+1)%2].index)
1069 state_[angles_[aIdx].index] = COMPLETED;
1073 sfn_[angles_[aIdx].index] = angles_[(aIdx+1)%2].index;
1080 template <
typename Po
intInT>
void
1082 std::vector<pcl::Vertices> &polygons,
1084 const Eigen::Vector2f &uvn_current,
1085 const Eigen::Vector2f &uvn_prev,
1086 const Eigen::Vector2f &uvn_next)
1088 if (is_current_free_)
1090 ffn_[current_index_] = prev_index;
1091 sfn_[current_index_] = next_index;
1095 if ((prev_is_ffn_ && next_is_sfn_) || (prev_is_sfn_ && next_is_ffn_))
1096 state_[current_index_] = COMPLETED;
1097 else if (prev_is_ffn_ && !next_is_sfn_)
1098 ffn_[current_index_] = next_index;
1099 else if (next_is_ffn_ && !prev_is_sfn_)
1100 ffn_[current_index_] = prev_index;
1101 else if (prev_is_sfn_ && !next_is_ffn_)
1102 sfn_[current_index_] = next_index;
1103 else if (next_is_sfn_ && !prev_is_ffn_)
1104 sfn_[current_index_] = prev_index;
1107 bool found_triangle =
false;
1108 if ((prev_index != R_) && ((ffn_[current_index_] == ffn_[prev_index]) || (ffn_[current_index_] == sfn_[prev_index])))
1110 found_triangle =
true;
1111 addTriangle (current_index_, ffn_[current_index_], prev_index, polygons);
1112 state_[prev_index] = COMPLETED;
1113 state_[ffn_[current_index_]] = COMPLETED;
1114 ffn_[current_index_] = next_index;
1116 else if ((prev_index != R_) && ((sfn_[current_index_] == ffn_[prev_index]) || (sfn_[current_index_] == sfn_[prev_index])))
1118 found_triangle =
true;
1119 addTriangle (current_index_, sfn_[current_index_], prev_index, polygons);
1120 state_[prev_index] = COMPLETED;
1121 state_[sfn_[current_index_]] = COMPLETED;
1122 sfn_[current_index_] = next_index;
1124 else if (state_[next_index] > FREE)
1126 if ((ffn_[current_index_] == ffn_[next_index]) || (ffn_[current_index_] == sfn_[next_index]))
1128 found_triangle =
true;
1129 addTriangle (current_index_, ffn_[current_index_], next_index, polygons);
1131 if (ffn_[current_index_] == ffn_[next_index])
1133 ffn_[next_index] = current_index_;
1137 sfn_[next_index] = current_index_;
1139 state_[ffn_[current_index_]] = COMPLETED;
1140 ffn_[current_index_] = prev_index;
1142 else if ((sfn_[current_index_] == ffn_[next_index]) || (sfn_[current_index_] == sfn_[next_index]))
1144 found_triangle =
true;
1145 addTriangle (current_index_, sfn_[current_index_], next_index, polygons);
1147 if (sfn_[current_index_] == ffn_[next_index])
1149 ffn_[next_index] = current_index_;
1153 sfn_[next_index] = current_index_;
1155 state_[sfn_[current_index_]] = COMPLETED;
1156 sfn_[current_index_] = prev_index;
1165 tmp_ = coords_[ffn_[current_index_]] - proj_qp_;
1166 uvn_ffn_[0] = tmp_.dot(u_);
1167 uvn_ffn_[1] = tmp_.dot(v_);
1168 tmp_ = coords_[sfn_[current_index_]] - proj_qp_;
1169 uvn_sfn_[0] = tmp_.dot(u_);
1170 uvn_sfn_[1] = tmp_.dot(v_);
1171 bool prev_ffn =
isVisible(uvn_prev, uvn_next, uvn_current, uvn_ffn_) &&
isVisible(uvn_prev, uvn_sfn_, uvn_current, uvn_ffn_);
1172 bool prev_sfn =
isVisible(uvn_prev, uvn_next, uvn_current, uvn_sfn_) &&
isVisible(uvn_prev, uvn_ffn_, uvn_current, uvn_sfn_);
1173 bool next_ffn =
isVisible(uvn_next, uvn_prev, uvn_current, uvn_ffn_) &&
isVisible(uvn_next, uvn_sfn_, uvn_current, uvn_ffn_);
1174 bool next_sfn =
isVisible(uvn_next, uvn_prev, uvn_current, uvn_sfn_) &&
isVisible(uvn_next, uvn_ffn_, uvn_current, uvn_sfn_);
1176 if (prev_ffn && next_sfn && prev_sfn && next_ffn)
1179 double prev2f = (coords_[ffn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1180 double next2s = (coords_[sfn_[current_index_]] - coords_[next_index]).squaredNorm ();
1181 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1182 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1183 if (prev2f < prev2s)
1185 if (prev2f < next2f)
1187 if (prev2f < next2s)
1194 if (next2f < next2s)
1202 if (prev2s < next2f)
1204 if (prev2s < next2s)
1211 if (next2f < next2s)
1218 else if (prev_ffn && next_sfn)
1221 double prev2f = (coords_[ffn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1222 double next2s = (coords_[sfn_[current_index_]] - coords_[next_index]).squaredNorm ();
1223 if (prev2f < next2s)
1228 else if (prev_sfn && next_ffn)
1231 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1232 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1233 if (prev2s < next2f)
1239 else if (prev_ffn && !next_sfn && !prev_sfn && !next_ffn)
1241 else if (!prev_ffn && !next_sfn && prev_sfn && !next_ffn)
1243 else if (!prev_ffn && !next_sfn && !prev_sfn && next_ffn)
1245 else if (!prev_ffn && next_sfn && !prev_sfn && !next_ffn)
1250 double prev2f = (coords_[ffn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1253 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1254 if (prev2s < prev2f)
1261 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1262 if (next2f < prev2f)
1270 double next2s = (coords_[sfn_[current_index_]] - coords_[next_index]).squaredNorm ();
1273 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1274 if (prev2s < next2s)
1281 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1282 if (next2f < next2s)
1292 addTriangle (current_index_, ffn_[current_index_], prev_index, polygons);
1295 if (ffn_[prev_index] == current_index_)
1297 ffn_[prev_index] = ffn_[current_index_];
1299 else if (sfn_[prev_index] == current_index_)
1301 sfn_[prev_index] = ffn_[current_index_];
1303 else if (ffn_[prev_index] == R_)
1305 changed_1st_fn_ =
true;
1306 ffn_[prev_index] = ffn_[current_index_];
1308 else if (sfn_[prev_index] == R_)
1310 changed_1st_fn_ =
true;
1311 sfn_[prev_index] = ffn_[current_index_];
1313 else if (prev_index == R_)
1315 new2boundary_ = ffn_[current_index_];
1319 if (ffn_[ffn_[current_index_]] == current_index_)
1321 ffn_[ffn_[current_index_]] = prev_index;
1323 else if (sfn_[ffn_[current_index_]] == current_index_)
1325 sfn_[ffn_[current_index_]] = prev_index;
1329 ffn_[current_index_] = next_index;
1335 addTriangle (current_index_, sfn_[current_index_], prev_index, polygons);
1338 if (ffn_[prev_index] == current_index_)
1340 ffn_[prev_index] = sfn_[current_index_];
1342 else if (sfn_[prev_index] == current_index_)
1344 sfn_[prev_index] = sfn_[current_index_];
1346 else if (ffn_[prev_index] == R_)
1348 changed_1st_fn_ =
true;
1349 ffn_[prev_index] = sfn_[current_index_];
1351 else if (sfn_[prev_index] == R_)
1353 changed_1st_fn_ =
true;
1354 sfn_[prev_index] = sfn_[current_index_];
1356 else if (prev_index == R_)
1358 new2boundary_ = sfn_[current_index_];
1362 if (ffn_[sfn_[current_index_]] == current_index_)
1364 ffn_[sfn_[current_index_]] = prev_index;
1366 else if (sfn_[sfn_[current_index_]] == current_index_)
1368 sfn_[sfn_[current_index_]] = prev_index;
1372 sfn_[current_index_] = next_index;
1378 addTriangle (current_index_, ffn_[current_index_], next_index, polygons);
1379 auto neighbor_update = next_index;
1382 if (state_[next_index] <= FREE)
1384 state_[next_index] = FRINGE;
1385 ffn_[next_index] = current_index_;
1386 sfn_[next_index] = ffn_[current_index_];
1390 if (ffn_[next_index] == R_)
1392 changed_2nd_fn_ =
true;
1393 ffn_[next_index] = ffn_[current_index_];
1395 else if (sfn_[next_index] == R_)
1397 changed_2nd_fn_ =
true;
1398 sfn_[next_index] = ffn_[current_index_];
1400 else if (next_index == R_)
1402 new2boundary_ = ffn_[current_index_];
1403 if (next_next_index == new2boundary_)
1404 already_connected_ =
true;
1406 else if (ffn_[next_index] == next_next_index)
1408 already_connected_ =
true;
1409 ffn_[next_index] = ffn_[current_index_];
1411 else if (sfn_[next_index] == next_next_index)
1413 already_connected_ =
true;
1414 sfn_[next_index] = ffn_[current_index_];
1418 tmp_ = coords_[ffn_[next_index]] - proj_qp_;
1419 uvn_next_ffn_[0] = tmp_.dot(u_);
1420 uvn_next_ffn_[1] = tmp_.dot(v_);
1421 tmp_ = coords_[sfn_[next_index]] - proj_qp_;
1422 uvn_next_sfn_[0] = tmp_.dot(u_);
1423 uvn_next_sfn_[1] = tmp_.dot(v_);
1425 bool ffn_next_ffn =
isVisible(uvn_next_ffn_, uvn_next, uvn_current, uvn_ffn_) &&
isVisible(uvn_next_ffn_, uvn_next, uvn_next_sfn_, uvn_ffn_);
1426 bool sfn_next_ffn =
isVisible(uvn_next_sfn_, uvn_next, uvn_current, uvn_ffn_) &&
isVisible(uvn_next_sfn_, uvn_next, uvn_next_ffn_, uvn_ffn_);
1428 int connect2ffn = -1;
1429 if (ffn_next_ffn && sfn_next_ffn)
1431 double fn2f = (coords_[ffn_[current_index_]] - coords_[ffn_[next_index]]).squaredNorm ();
1432 double sn2f = (coords_[ffn_[current_index_]] - coords_[sfn_[next_index]]).squaredNorm ();
1433 if (fn2f < sn2f) connect2ffn = 0;
1434 else connect2ffn = 1;
1436 else if (ffn_next_ffn) connect2ffn = 0;
1437 else if (sfn_next_ffn) connect2ffn = 1;
1439 switch (connect2ffn)
1443 addTriangle (next_index, ffn_[current_index_], ffn_[next_index], polygons);
1444 neighbor_update = ffn_[next_index];
1447 if ((ffn_[ffn_[next_index]] == ffn_[current_index_]) || (sfn_[ffn_[next_index]] == ffn_[current_index_]))
1449 state_[ffn_[next_index]] = COMPLETED;
1451 else if (ffn_[ffn_[next_index]] == next_index)
1453 ffn_[ffn_[next_index]] = ffn_[current_index_];
1455 else if (sfn_[ffn_[next_index]] == next_index)
1457 sfn_[ffn_[next_index]] = ffn_[current_index_];
1460 ffn_[next_index] = current_index_;
1466 addTriangle (next_index, ffn_[current_index_], sfn_[next_index], polygons);
1467 neighbor_update = sfn_[next_index];
1470 if ((ffn_[sfn_[next_index]] == ffn_[current_index_]) || (sfn_[sfn_[next_index]] == ffn_[current_index_]))
1472 state_[sfn_[next_index]] = COMPLETED;
1474 else if (ffn_[sfn_[next_index]] == next_index)
1476 ffn_[sfn_[next_index]] = ffn_[current_index_];
1478 else if (sfn_[sfn_[next_index]] == next_index)
1480 sfn_[sfn_[next_index]] = ffn_[current_index_];
1483 sfn_[next_index] = current_index_;
1493 if ((ffn_[ffn_[current_index_]] == neighbor_update) || (sfn_[ffn_[current_index_]] == neighbor_update))
1495 state_[ffn_[current_index_]] = COMPLETED;
1497 else if (ffn_[ffn_[current_index_]] == current_index_)
1499 ffn_[ffn_[current_index_]] = neighbor_update;
1501 else if (sfn_[ffn_[current_index_]] == current_index_)
1503 sfn_[ffn_[current_index_]] = neighbor_update;
1507 ffn_[current_index_] = prev_index;
1513 addTriangle (current_index_, sfn_[current_index_], next_index, polygons);
1514 auto neighbor_update = next_index;
1517 if (state_[next_index] <= FREE)
1519 state_[next_index] = FRINGE;
1520 ffn_[next_index] = current_index_;
1521 sfn_[next_index] = sfn_[current_index_];
1525 if (ffn_[next_index] == R_)
1527 changed_2nd_fn_ =
true;
1528 ffn_[next_index] = sfn_[current_index_];
1530 else if (sfn_[next_index] == R_)
1532 changed_2nd_fn_ =
true;
1533 sfn_[next_index] = sfn_[current_index_];
1535 else if (next_index == R_)
1537 new2boundary_ = sfn_[current_index_];
1538 if (next_next_index == new2boundary_)
1539 already_connected_ =
true;
1541 else if (ffn_[next_index] == next_next_index)
1543 already_connected_ =
true;
1544 ffn_[next_index] = sfn_[current_index_];
1546 else if (sfn_[next_index] == next_next_index)
1548 already_connected_ =
true;
1549 sfn_[next_index] = sfn_[current_index_];
1553 tmp_ = coords_[ffn_[next_index]] - proj_qp_;
1554 uvn_next_ffn_[0] = tmp_.dot(u_);
1555 uvn_next_ffn_[1] = tmp_.dot(v_);
1556 tmp_ = coords_[sfn_[next_index]] - proj_qp_;
1557 uvn_next_sfn_[0] = tmp_.dot(u_);
1558 uvn_next_sfn_[1] = tmp_.dot(v_);
1560 bool ffn_next_sfn =
isVisible(uvn_next_ffn_, uvn_next, uvn_current, uvn_sfn_) &&
isVisible(uvn_next_ffn_, uvn_next, uvn_next_sfn_, uvn_sfn_);
1561 bool sfn_next_sfn =
isVisible(uvn_next_sfn_, uvn_next, uvn_current, uvn_sfn_) &&
isVisible(uvn_next_sfn_, uvn_next, uvn_next_ffn_, uvn_sfn_);
1563 int connect2sfn = -1;
1564 if (ffn_next_sfn && sfn_next_sfn)
1566 double fn2s = (coords_[sfn_[current_index_]] - coords_[ffn_[next_index]]).squaredNorm ();
1567 double sn2s = (coords_[sfn_[current_index_]] - coords_[sfn_[next_index]]).squaredNorm ();
1568 if (fn2s < sn2s) connect2sfn = 0;
1569 else connect2sfn = 1;
1571 else if (ffn_next_sfn) connect2sfn = 0;
1572 else if (sfn_next_sfn) connect2sfn = 1;
1574 switch (connect2sfn)
1578 addTriangle (next_index, sfn_[current_index_], ffn_[next_index], polygons);
1579 neighbor_update = ffn_[next_index];
1582 if ((ffn_[ffn_[next_index]] == sfn_[current_index_]) || (sfn_[ffn_[next_index]] == sfn_[current_index_]))
1584 state_[ffn_[next_index]] = COMPLETED;
1586 else if (ffn_[ffn_[next_index]] == next_index)
1588 ffn_[ffn_[next_index]] = sfn_[current_index_];
1590 else if (sfn_[ffn_[next_index]] == next_index)
1592 sfn_[ffn_[next_index]] = sfn_[current_index_];
1595 ffn_[next_index] = current_index_;
1601 addTriangle (next_index, sfn_[current_index_], sfn_[next_index], polygons);
1602 neighbor_update = sfn_[next_index];
1605 if ((ffn_[sfn_[next_index]] == sfn_[current_index_]) || (sfn_[sfn_[next_index]] == sfn_[current_index_]))
1607 state_[sfn_[next_index]] = COMPLETED;
1609 else if (ffn_[sfn_[next_index]] == next_index)
1611 ffn_[sfn_[next_index]] = sfn_[current_index_];
1613 else if (sfn_[sfn_[next_index]] == next_index)
1615 sfn_[sfn_[next_index]] = sfn_[current_index_];
1618 sfn_[next_index] = current_index_;
1628 if ((ffn_[sfn_[current_index_]] == neighbor_update) || (sfn_[sfn_[current_index_]] == neighbor_update))
1630 state_[sfn_[current_index_]] = COMPLETED;
1632 else if (ffn_[sfn_[current_index_]] == current_index_)
1634 ffn_[sfn_[current_index_]] = neighbor_update;
1636 else if (sfn_[sfn_[current_index_]] == current_index_)
1638 sfn_[sfn_[current_index_]] = neighbor_update;
1641 sfn_[current_index_] = prev_index;
1653 template <
typename Po
intInT> std::vector<std::vector<std::size_t> >
1658 for (std::size_t i=0; i < input.
polygons.size (); ++i)
1659 for (std::size_t j=0; j < input.
polygons[i].vertices.size (); ++j)
1660 triangleList[input.
polygons[i].vertices[j]].push_back (i);
1661 return (triangleList);
1664 #define PCL_INSTANTIATE_GreedyProjectionTriangulation(T) \
1665 template class PCL_EXPORTS pcl::GreedyProjectionTriangulation<T>;
GreedyProjectionTriangulation is an implementation of a greedy triangulation algorithm for 3D points ...
bool isVisible(const Eigen::Vector2f &X, const Eigen::Vector2f &S1, const Eigen::Vector2f &S2, const Eigen::Vector2f &R=Eigen::Vector2f::Zero())
Returns if a point X is visible from point R (or the origin) when taking into account the segment bet...
int cp(int from, int to)
Returns field copy operation code.
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
IndicesAllocator<> Indices
Type used for indices in PCL.
std::vector< std::uint8_t > data
std::vector< ::pcl::Vertices > polygons
::pcl::PCLPointCloud2 cloud