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();
94 Eigen::Vector2f uvn_current;
95 Eigen::Vector2f uvn_prev;
96 Eigen::Vector2f uvn_next;
99 already_connected_ =
false;
107 part_.resize(indices_->size (), -1);
108 state_.resize(indices_->size (), FREE);
109 source_.resize(indices_->size (), NONE);
110 ffn_.resize(indices_->size (), NONE);
111 sfn_.resize(indices_->size (), NONE);
112 fringe_queue_.clear ();
116 if (!input_->is_dense)
119 for (
const auto& idx : (*indices_))
120 if (!std::isfinite ((*input_)[idx].x) ||
121 !std::isfinite ((*input_)[idx].y) ||
122 !std::isfinite ((*input_)[idx].z))
128 coords_.reserve (indices_->size ());
129 std::vector<int> point2index (input_->size (), -1);
130 for (
int cp = 0; cp < static_cast<int> (indices_->size ()); ++
cp)
132 coords_.push_back((*input_)[(*indices_)[
cp]].getVector3fMap());
133 point2index[(*indices_)[
cp]] =
cp;
137 int is_free=0, nr_parts=0, increase_nnn4fn=0, increase_nnn4s=0, increase_dist=0, nr_touched = 0;
138 angles_.resize(nnn_);
139 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > uvn_nn (nnn_);
140 Eigen::Vector2f uvn_s;
143 while (is_free != NONE)
146 if (state_[R_] == FREE)
149 part_[R_] = part_index++;
154 tree_->nearestKSearch (indices_->at (R_), nnn_, nnIdx, sqrDists);
155 double sqr_dist_threshold = (std::min)(sqr_max_edge, sqr_mu * sqrDists[1]);
158 for (
int i = 1; i < nnn_; i++)
162 nnIdx[i] = point2index[nnIdx[i]];
166 const Eigen::Vector3f nc = (*input_)[(*indices_)[R_]].getNormalVector3fMap ();
169 v_ = nc.unitOrthogonal ();
173 float dist = nc.dot (coords_[R_]);
174 proj_qp_ = coords_[R_] - dist * nc;
178 std::vector<doubleEdge> doubleEdges;
179 for (
int i = 1; i < nnn_; i++)
182 tmp_ = coords_[nnIdx[i]] - proj_qp_;
183 uvn_nn[i][0] = tmp_.dot(u_);
184 uvn_nn[i][1] = tmp_.dot(v_);
186 angles_[i].angle = std::atan2(uvn_nn[i][1], uvn_nn[i][0]);
188 angles_[i].index = nnIdx[i];
190 (state_[nnIdx[i]] == COMPLETED) || (state_[nnIdx[i]] == BOUNDARY)
191 || (state_[nnIdx[i]] == NONE) || (nnIdx[i] == -1)
192 || (sqrDists[i] > sqr_dist_threshold)
194 angles_[i].visible =
false;
196 angles_[i].visible =
true;
198 if ((state_[nnIdx[i]] == FRINGE) || (state_[nnIdx[i]] == BOUNDARY))
203 tmp_ = coords_[ffn_[nnIdx[i]]] - proj_qp_;
204 e.first[0] = tmp_.dot(u_);
205 e.first[1] = tmp_.dot(v_);
206 tmp_ = coords_[sfn_[nnIdx[i]]] - proj_qp_;
207 e.second[0] = tmp_.dot(u_);
208 e.second[1] = tmp_.dot(v_);
209 doubleEdges.push_back(e);
212 angles_[0].visible =
false;
215 for (
int i = 1; i < nnn_; i++)
216 if ((angles_[i].visible) && (ffn_[R_] != nnIdx[i]) && (sfn_[R_] != nnIdx[i]))
218 bool visibility =
true;
219 for (
int j = 0; j < nr_edge; j++)
221 if (ffn_[nnIdx[doubleEdges[j].index]] != nnIdx[i])
222 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].first, Eigen::Vector2f::Zero());
225 if (sfn_[nnIdx[doubleEdges[j].index]] != nnIdx[i])
226 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].second, Eigen::Vector2f::Zero());
230 angles_[i].visible = visibility;
234 bool not_found =
true;
238 while ((left < nnn_) && ((!angles_[left].visible) || (state_[nnIdx[left]] > FREE))) left++;
244 while ((right < nnn_) && ((!angles_[right].visible) || (state_[nnIdx[right]] > FREE))) right++;
247 if ((coords_[nnIdx[left]] - coords_[nnIdx[right]]).squaredNorm () > sqr_max_edge)
251 addFringePoint (nnIdx[right], R_);
252 addFringePoint (nnIdx[left], nnIdx[right]);
253 addFringePoint (R_, nnIdx[left]);
254 state_[R_] = state_[nnIdx[left]] = state_[nnIdx[right]] = FRINGE;
255 ffn_[R_] = nnIdx[left];
256 sfn_[R_] = nnIdx[right];
257 ffn_[nnIdx[left]] = nnIdx[right];
258 sfn_[nnIdx[left]] = R_;
259 ffn_[nnIdx[right]] = R_;
260 sfn_[nnIdx[right]] = nnIdx[left];
261 addTriangle (R_, nnIdx[left], nnIdx[right], polygons);
274 for (std::size_t temp = 0; temp < indices_->size (); temp++)
276 if (state_[temp] == FREE)
283 bool is_fringe =
true;
288 int fqSize =
static_cast<int> (fringe_queue_.size ());
289 while ((fqIdx < fqSize) && (state_[fringe_queue_[fqIdx]] != FRINGE))
298 R_ = fringe_queue_[fqIdx];
301 if (ffn_[R_] == sfn_[R_])
303 state_[R_] = COMPLETED;
308 tree_->nearestKSearch (indices_->at (R_), nnn_, nnIdx, sqrDists);
311 for (
int i = 1; i < nnn_; i++)
315 nnIdx[i] = point2index[nnIdx[i]];
319 double sqr_source_dist = (coords_[R_] - coords_[source_[R_]]).squaredNorm ();
320 double sqr_ffn_dist = (coords_[R_] - coords_[ffn_[R_]]).squaredNorm ();
321 double sqr_sfn_dist = (coords_[R_] - coords_[sfn_[R_]]).squaredNorm ();
322 double max_sqr_fn_dist = (std::max)(sqr_ffn_dist, sqr_sfn_dist);
323 double sqr_dist_threshold = (std::min)(sqr_max_edge, sqr_mu * sqrDists[1]);
324 if (max_sqr_fn_dist > sqrDists[nnn_-1])
326 if (0 == increase_nnn4fn)
327 PCL_WARN(
"Not enough neighbors are considered: ffn or sfn out of range! Consider increasing nnn_... Setting R=%d to be BOUNDARY!\n", R_);
329 state_[R_] = BOUNDARY;
332 double max_sqr_fns_dist = (std::max)(sqr_source_dist, max_sqr_fn_dist);
333 if (max_sqr_fns_dist > sqrDists[nnn_-1])
335 if (0 == increase_nnn4s)
336 PCL_WARN(
"Not enough neighbors are considered: source of R=%d is out of range! Consider increasing nnn_...\n", R_);
341 const Eigen::Vector3f nc = (*input_)[(*indices_)[R_]].getNormalVector3fMap ();
344 v_ = nc.unitOrthogonal ();
348 float dist = nc.dot (coords_[R_]);
349 proj_qp_ = coords_[R_] - dist * nc;
353 std::vector<doubleEdge> doubleEdges;
354 for (
int i = 1; i < nnn_; i++)
356 tmp_ = coords_[nnIdx[i]] - proj_qp_;
357 uvn_nn[i][0] = tmp_.dot(u_);
358 uvn_nn[i][1] = tmp_.dot(v_);
361 angles_[i].angle = std::atan2(uvn_nn[i][1], uvn_nn[i][0]);
363 angles_[i].index = nnIdx[i];
364 angles_[i].nnIndex = i;
366 (state_[nnIdx[i]] == COMPLETED) || (state_[nnIdx[i]] == BOUNDARY)
367 || (state_[nnIdx[i]] == NONE) || (nnIdx[i] == -1)
368 || (sqrDists[i] > sqr_dist_threshold)
370 angles_[i].visible =
false;
372 angles_[i].visible =
true;
373 if ((ffn_[R_] == nnIdx[i]) || (sfn_[R_] == nnIdx[i]))
374 angles_[i].visible =
true;
375 bool same_side =
true;
376 const Eigen::Vector3f neighbor_normal = (*input_)[(*indices_)[nnIdx[i]]].getNormalVector3fMap ();
377 double cosine = nc.dot (neighbor_normal);
378 if (cosine > 1) cosine = 1;
379 if (cosine < -1) cosine = -1;
380 double angle = std::acos (cosine);
381 if ((!consistent_) && (angle >
M_PI/2))
382 angle =
M_PI - angle;
383 if (angle > eps_angle_)
385 angles_[i].visible =
false;
389 if ((i!=0) && (same_side) && ((state_[nnIdx[i]] == FRINGE) || (state_[nnIdx[i]] == BOUNDARY)))
394 tmp_ = coords_[ffn_[nnIdx[i]]] - proj_qp_;
395 e.first[0] = tmp_.dot(u_);
396 e.first[1] = tmp_.dot(v_);
397 tmp_ = coords_[sfn_[nnIdx[i]]] - proj_qp_;
398 e.second[0] = tmp_.dot(u_);
399 e.second[1] = tmp_.dot(v_);
400 doubleEdges.push_back(e);
402 if ((state_[nnIdx[i]] == FRINGE) && (ffn_[R_] != nnIdx[i]) && (sfn_[R_] != nnIdx[i]))
404 double angle1 = std::atan2(e.first[1] - uvn_nn[i][1], e.first[0] - uvn_nn[i][0]);
405 double angle2 = std::atan2(e.second[1] - uvn_nn[i][1], e.second[0] - uvn_nn[i][0]);
406 double angleMin, angleMax;
417 double angleR = angles_[i].angle +
M_PI;
420 if ((source_[nnIdx[i]] == ffn_[nnIdx[i]]) || (source_[nnIdx[i]] == sfn_[nnIdx[i]]))
422 if ((angleMax - angleMin) <
M_PI)
424 if ((angleMin < angleR) && (angleR < angleMax))
425 angles_[i].visible =
false;
429 if ((angleR < angleMin) || (angleMax < angleR))
430 angles_[i].visible =
false;
435 tmp_ = coords_[source_[nnIdx[i]]] - proj_qp_;
436 uvn_s[0] = tmp_.dot(u_);
437 uvn_s[1] = tmp_.dot(v_);
438 double angleS = std::atan2(uvn_s[1] - uvn_nn[i][1], uvn_s[0] - uvn_nn[i][0]);
439 if ((angleMin < angleS) && (angleS < angleMax))
441 if ((angleMin < angleR) && (angleR < angleMax))
442 angles_[i].visible =
false;
446 if ((angleR < angleMin) || (angleMax < angleR))
447 angles_[i].visible =
false;
453 angles_[0].visible =
false;
456 for (
int i = 1; i < nnn_; i++)
457 if ((angles_[i].visible) && (ffn_[R_] != nnIdx[i]) && (sfn_[R_] != nnIdx[i]))
459 bool visibility =
true;
460 for (
int j = 0; j < nr_edge; j++)
462 if (doubleEdges[j].index != i)
464 int f = ffn_[nnIdx[doubleEdges[j].index]];
465 if ((f != nnIdx[i]) && (f != R_))
466 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].first, Eigen::Vector2f::Zero());
470 int s = sfn_[nnIdx[doubleEdges[j].index]];
471 if ((s != nnIdx[i]) && (s != R_))
472 visibility =
isVisible(uvn_nn[i], uvn_nn[doubleEdges[j].index], doubleEdges[j].second, Eigen::Vector2f::Zero());
477 angles_[i].visible = visibility;
481 std::sort (angles_.begin (), angles_.end (), GreedyProjectionTriangulation<PointInT>::nnAngleSortAsc);
484 if (angles_[2].visible ==
false)
486 if ( !( (angles_[0].index == ffn_[R_] && angles_[1].index == sfn_[R_]) || (angles_[0].index == sfn_[R_] && angles_[1].index == ffn_[R_]) ) )
488 state_[R_] = BOUNDARY;
492 if ((source_[R_] == angles_[0].index) || (source_[R_] == angles_[1].index))
493 state_[R_] = BOUNDARY;
496 if (sqr_max_edge < (coords_[ffn_[R_]] - coords_[sfn_[R_]]).squaredNorm ())
498 state_[R_] = BOUNDARY;
502 tmp_ = coords_[source_[R_]] - proj_qp_;
503 uvn_s[0] = tmp_.dot(u_);
504 uvn_s[1] = tmp_.dot(v_);
505 double angleS = std::atan2(uvn_s[1], uvn_s[0]);
506 double dif = angles_[1].angle - angles_[0].angle;
507 if ((angles_[0].angle < angleS) && (angleS < angles_[1].angle))
509 if (dif < 2*
M_PI - maximum_angle_)
510 state_[R_] = BOUNDARY;
512 closeTriangle (polygons);
516 if (dif >= maximum_angle_)
517 state_[R_] = BOUNDARY;
519 closeTriangle (polygons);
528 int start = -1, end = -1;
529 for (
int i=0; i<nnn_; i++)
531 if (ffn_[R_] == angles_[i].index)
534 if (sfn_[R_] == angles_[i+1].index)
539 for (i = i+2; i < nnn_; i++)
540 if (sfn_[R_] == angles_[i].index)
546 for (i = i+2; i < nnn_; i++)
547 if (sfn_[R_] == angles_[i].index)
553 if (sfn_[R_] == angles_[i].index)
556 if (ffn_[R_] == angles_[i+1].index)
561 for (i = i+2; i < nnn_; i++)
562 if (ffn_[R_] == angles_[i].index)
568 for (i = i+2; i < nnn_; i++)
569 if (ffn_[R_] == angles_[i].index)
578 if ((start < 0) || (end < 0) || (end == nnn_) || (!angles_[start].visible) || (!angles_[end].visible))
580 state_[R_] = BOUNDARY;
585 int last_visible = end;
586 while ((last_visible+1<nnn_) && (angles_[last_visible+1].visible)) last_visible++;
589 bool need_invert =
false;
590 if ((source_[R_] == ffn_[R_]) || (source_[R_] == sfn_[R_]))
592 if ((angles_[end].angle - angles_[start].angle) <
M_PI)
598 for (sourceIdx=0; sourceIdx<nnn_; sourceIdx++)
599 if (angles_[sourceIdx].index == source_[R_])
601 if (sourceIdx == nnn_)
603 int vis_free = NONE, nnCB = NONE;
604 for (
int i = 1; i < nnn_; i++)
607 if ((state_[nnIdx[i]] == COMPLETED) || (state_[nnIdx[i]] == BOUNDARY))
612 if (vis_free != NONE)
617 if (state_[angles_[i].index] <= FREE)
619 if (i <= last_visible)
630 while (angles_[nCB].index != nnIdx[nnCB]) nCB++;
634 if (vis_free != NONE)
636 if ((vis_free < start) || (vis_free > end))
643 if ((nCB == start) || (nCB == end))
645 bool inside_CB =
false;
646 bool outside_CB =
false;
647 for (
int i=0; i<nnn_; i++)
650 ((state_[angles_[i].index] == COMPLETED) || (state_[angles_[i].index] == BOUNDARY))
651 && (i != start) && (i != end)
654 if ((angles_[start].angle <= angles_[i].angle) && (angles_[i].angle <= angles_[end].angle))
668 if (inside_CB && !outside_CB)
670 else if (!(!inside_CB && outside_CB))
672 if ((angles_[end].angle - angles_[start].angle) <
M_PI)
678 if ((angles_[nCB].angle > angles_[start].angle) && (angles_[nCB].angle < angles_[end].angle))
689 else if ((angles_[start].angle < angles_[sourceIdx].angle) && (angles_[sourceIdx].angle < angles_[end].angle))
703 bool is_boundary =
false, is_skinny =
false;
704 std::vector<bool> gaps (nnn_,
false);
705 std::vector<bool> skinny (nnn_,
false);
706 std::vector<double> dif (nnn_);
707 std::vector<int> angleIdx; angleIdx.reserve (nnn_);
710 for (
int j=start; j<last_visible; j++)
712 dif[j] = (angles_[j+1].angle - angles_[j].angle);
713 if (dif[j] < minimum_angle_)
715 skinny[j] = is_skinny =
true;
717 else if (maximum_angle_ <= dif[j])
719 gaps[j] = is_boundary =
true;
721 if ((!gaps[j]) && (sqr_max_edge < (coords_[angles_[j+1].index] - coords_[angles_[j].index]).squaredNorm ()))
723 gaps[j] = is_boundary =
true;
725 angleIdx.push_back(j);
728 dif[last_visible] = (2*
M_PI + angles_[0].angle - angles_[last_visible].angle);
729 if (dif[last_visible] < minimum_angle_)
731 skinny[last_visible] = is_skinny =
true;
733 else if (maximum_angle_ <= dif[last_visible])
735 gaps[last_visible] = is_boundary =
true;
737 if ((!gaps[last_visible]) && (sqr_max_edge < (coords_[angles_[0].index] - coords_[angles_[last_visible].index]).squaredNorm ()))
739 gaps[last_visible] = is_boundary =
true;
741 angleIdx.push_back(last_visible);
743 for (
int j=0; j<end; j++)
745 dif[j] = (angles_[j+1].angle - angles_[j].angle);
746 if (dif[j] < minimum_angle_)
748 skinny[j] = is_skinny =
true;
750 else if (maximum_angle_ <= dif[j])
752 gaps[j] = is_boundary =
true;
754 if ((!gaps[j]) && (sqr_max_edge < (coords_[angles_[j+1].index] - coords_[angles_[j].index]).squaredNorm ()))
756 gaps[j] = is_boundary =
true;
758 angleIdx.push_back(j);
760 angleIdx.push_back(end);
765 for (
int j=start; j<end; j++)
767 dif[j] = (angles_[j+1].angle - angles_[j].angle);
768 if (dif[j] < minimum_angle_)
770 skinny[j] = is_skinny =
true;
772 else if (maximum_angle_ <= dif[j])
774 gaps[j] = is_boundary =
true;
776 if ((!gaps[j]) && (sqr_max_edge < (coords_[angles_[j+1].index] - coords_[angles_[j].index]).squaredNorm ()))
778 gaps[j] = is_boundary =
true;
780 angleIdx.push_back(j);
782 angleIdx.push_back(end);
786 state_[R_] = is_boundary ? BOUNDARY : COMPLETED;
788 std::vector<int>::iterator first_gap_after = angleIdx.end ();
789 std::vector<int>::iterator last_gap_before = angleIdx.begin ();
791 for (std::vector<int>::iterator it = angleIdx.begin (); it != angleIdx.end () - 1; ++it)
796 if (first_gap_after == angleIdx.end())
797 first_gap_after = it;
798 last_gap_before = it+1;
803 angleIdx.erase(first_gap_after+1, last_gap_before);
809 double angle_so_far = 0, angle_would_be;
810 double max_combined_angle = (std::min)(maximum_angle_,
M_PI-2*minimum_angle_);
814 std::vector<int> to_erase;
815 for (std::vector<int>::iterator it = angleIdx.begin()+1; it != angleIdx.end()-1; ++it)
820 angle_so_far += dif[*(it-1)];
822 angle_would_be = angle_so_far;
824 angle_would_be = angle_so_far + dif[*it];
826 (skinny[*it] || skinny[*(it-1)]) &&
827 ((state_[angles_[*it].index] <= FREE) || (state_[angles_[*(it-1)].index] <= FREE)) &&
828 ((!gaps[*it]) || (angles_[*it].nnIndex > angles_[*(it-1)].nnIndex)) &&
829 ((!gaps[*(it-1)]) || (angles_[*it].nnIndex > angles_[*(it+1)].nnIndex)) &&
830 (angle_would_be < max_combined_angle)
836 to_erase.push_back(*it);
840 gaps[*(it-1)] =
true;
841 to_erase.push_back(*it);
845 std::vector<int>::iterator prev_it;
846 int erased_idx =
static_cast<int> (to_erase.size ()) -1;
847 for (prev_it = it-1; (erased_idx != -1) && (it != angleIdx.begin()); --it)
848 if (*it == to_erase[erased_idx])
852 bool can_delete =
true;
853 for (std::vector<int>::iterator curr_it = prev_it+1; curr_it != it+1; ++curr_it)
855 tmp_ = coords_[angles_[*curr_it].index] - proj_qp_;
858 tmp_ = coords_[angles_[*prev_it].index] - proj_qp_;
859 S1[0] = tmp_.dot(u_);
860 S1[1] = tmp_.dot(v_);
861 tmp_ = coords_[angles_[*(it+1)].index] - proj_qp_;
862 S2[0] = tmp_.dot(u_);
863 S2[1] = tmp_.dot(v_);
874 to_erase.push_back(*it);
881 for (
const auto &idx : to_erase)
883 for (std::vector<int>::iterator iter = angleIdx.begin(); iter != angleIdx.end(); ++iter)
886 angleIdx.erase(iter);
893 changed_1st_fn_ =
false;
894 changed_2nd_fn_ =
false;
895 new2boundary_ = NONE;
896 for (std::vector<int>::iterator it = angleIdx.begin()+1; it != angleIdx.end()-1; ++it)
898 current_index_ = angles_[*it].index;
900 is_current_free_ =
false;
901 if (state_[current_index_] <= FREE)
903 state_[current_index_] = FRINGE;
904 is_current_free_ =
true;
906 else if (!already_connected_)
908 prev_is_ffn_ = (ffn_[current_index_] == angles_[*(it-1)].index) && (!gaps[*(it-1)]);
909 prev_is_sfn_ = (sfn_[current_index_] == angles_[*(it-1)].index) && (!gaps[*(it-1)]);
910 next_is_ffn_ = (ffn_[current_index_] == angles_[*(it+1)].index) && (!gaps[*it]);
911 next_is_sfn_ = (sfn_[current_index_] == angles_[*(it+1)].index) && (!gaps[*it]);
912 if (!prev_is_ffn_ && !next_is_sfn_ && !prev_is_sfn_ && !next_is_ffn_)
921 if (is_current_free_)
922 state_[current_index_] = NONE;
927 addTriangle (current_index_, angles_[*(it-1)].index, R_, polygons);
928 addFringePoint (current_index_, R_);
929 new2boundary_ = current_index_;
930 if (!already_connected_)
931 connectPoint (polygons, angles_[*(it-1)].index, R_,
932 angles_[*(it+1)].index,
933 uvn_nn[angles_[*it].nnIndex], uvn_nn[angles_[*(it-1)].nnIndex], uvn_nn_qp_zero);
934 else already_connected_ =
false;
935 if (ffn_[R_] == angles_[*(angleIdx.begin())].index)
937 ffn_[R_] = new2boundary_;
939 else if (sfn_[R_] == angles_[*(angleIdx.begin())].index)
941 sfn_[R_] = new2boundary_;
948 addFringePoint (current_index_, R_);
949 new2boundary_ = current_index_;
950 if (!already_connected_) connectPoint (polygons, R_, angles_[*(it+1)].index,
951 (it+2) == angleIdx.end() ? -1 : angles_[*(it+2)].index,
952 uvn_nn[angles_[*it].nnIndex], uvn_nn_qp_zero,
953 uvn_nn[angles_[*(it+1)].nnIndex]);
954 else already_connected_ =
false;
955 if (ffn_[R_] == angles_[*(angleIdx.end()-1)].index)
957 ffn_[R_] = new2boundary_;
959 else if (sfn_[R_] == angles_[*(angleIdx.end()-1)].index)
961 sfn_[R_] = new2boundary_;
967 addTriangle (current_index_, angles_[*(it-1)].index, R_, polygons);
968 addFringePoint (current_index_, R_);
969 if (!already_connected_) connectPoint (polygons, angles_[*(it-1)].index, angles_[*(it+1)].index,
970 (it+2) == angleIdx.end() ? -1 : gaps[*(it+1)] ? R_ : angles_[*(it+2)].index,
971 uvn_nn[angles_[*it].nnIndex],
972 uvn_nn[angles_[*(it-1)].nnIndex],
973 uvn_nn[angles_[*(it+1)].nnIndex]);
974 else already_connected_ =
false;
979 if (ffn_[R_] == sfn_[R_])
981 state_[R_] = COMPLETED;
983 if (!gaps[*(angleIdx.end()-2)])
985 addTriangle (angles_[*(angleIdx.end()-2)].index, angles_[*(angleIdx.end()-1)].index, R_, polygons);
986 addFringePoint (angles_[*(angleIdx.end()-2)].index, R_);
987 if (R_ == ffn_[angles_[*(angleIdx.end()-1)].index])
989 if (angles_[*(angleIdx.end()-2)].index == sfn_[angles_[*(angleIdx.end()-1)].index])
991 state_[angles_[*(angleIdx.end()-1)].index] = COMPLETED;
995 ffn_[angles_[*(angleIdx.end()-1)].index] = angles_[*(angleIdx.end()-2)].index;
998 else if (R_ == sfn_[angles_[*(angleIdx.end()-1)].index])
1000 if (angles_[*(angleIdx.end()-2)].index == ffn_[angles_[*(angleIdx.end()-1)].index])
1002 state_[angles_[*(angleIdx.end()-1)].index] = COMPLETED;
1006 sfn_[angles_[*(angleIdx.end()-1)].index] = angles_[*(angleIdx.end()-2)].index;
1010 if (!gaps[*(angleIdx.begin())])
1012 if (R_ == ffn_[angles_[*(angleIdx.begin())].index])
1014 if (angles_[*(angleIdx.begin()+1)].index == sfn_[angles_[*(angleIdx.begin())].index])
1016 state_[angles_[*(angleIdx.begin())].index] = COMPLETED;
1020 ffn_[angles_[*(angleIdx.begin())].index] = angles_[*(angleIdx.begin()+1)].index;
1023 else if (R_ == sfn_[angles_[*(angleIdx.begin())].index])
1025 if (angles_[*(angleIdx.begin()+1)].index == ffn_[angles_[*(angleIdx.begin())].index])
1027 state_[angles_[*(angleIdx.begin())].index] = COMPLETED;
1031 sfn_[angles_[*(angleIdx.begin())].index] = angles_[*(angleIdx.begin()+1)].index;
1037 PCL_DEBUG (
"Number of triangles: %lu\n", polygons.size());
1038 PCL_DEBUG (
"Number of unconnected parts: %d\n", nr_parts);
1039 if (increase_nnn4fn > 0)
1040 PCL_WARN (
"Number of neighborhood size increase requests for fringe neighbors: %d\n", increase_nnn4fn);
1041 if (increase_nnn4s > 0)
1042 PCL_WARN (
"Number of neighborhood size increase requests for source: %d\n", increase_nnn4s);
1043 if (increase_dist > 0)
1044 PCL_WARN (
"Number of automatic maximum distance increases: %d\n", increase_dist);
1047 std::sort (fringe_queue_.begin (), fringe_queue_.end ());
1048 fringe_queue_.erase (std::unique (fringe_queue_.begin (), fringe_queue_.end ()), fringe_queue_.end ());
1049 PCL_DEBUG (
"Number of processed points: %lu / %lu\n", fringe_queue_.size(), indices_->size ());
1054 template <
typename Po
intInT>
void
1057 state_[R_] = COMPLETED;
1058 addTriangle (angles_[0].index, angles_[1].index, R_, polygons);
1059 for (
int aIdx=0; aIdx<2; aIdx++)
1061 if (ffn_[angles_[aIdx].index] == R_)
1063 if (sfn_[angles_[aIdx].index] == angles_[(aIdx+1)%2].index)
1065 state_[angles_[aIdx].index] = COMPLETED;
1069 ffn_[angles_[aIdx].index] = angles_[(aIdx+1)%2].index;
1072 else if (sfn_[angles_[aIdx].index] == R_)
1074 if (ffn_[angles_[aIdx].index] == angles_[(aIdx+1)%2].index)
1076 state_[angles_[aIdx].index] = COMPLETED;
1080 sfn_[angles_[aIdx].index] = angles_[(aIdx+1)%2].index;
1087 template <
typename Po
intInT>
void
1089 std::vector<pcl::Vertices> &polygons,
1090 const int prev_index,
const int next_index,
const int next_next_index,
1091 const Eigen::Vector2f &uvn_current,
1092 const Eigen::Vector2f &uvn_prev,
1093 const Eigen::Vector2f &uvn_next)
1095 if (is_current_free_)
1097 ffn_[current_index_] = prev_index;
1098 sfn_[current_index_] = next_index;
1102 if ((prev_is_ffn_ && next_is_sfn_) || (prev_is_sfn_ && next_is_ffn_))
1103 state_[current_index_] = COMPLETED;
1104 else if (prev_is_ffn_ && !next_is_sfn_)
1105 ffn_[current_index_] = next_index;
1106 else if (next_is_ffn_ && !prev_is_sfn_)
1107 ffn_[current_index_] = prev_index;
1108 else if (prev_is_sfn_ && !next_is_ffn_)
1109 sfn_[current_index_] = next_index;
1110 else if (next_is_sfn_ && !prev_is_ffn_)
1111 sfn_[current_index_] = prev_index;
1114 bool found_triangle =
false;
1115 if ((prev_index != R_) && ((ffn_[current_index_] == ffn_[prev_index]) || (ffn_[current_index_] == sfn_[prev_index])))
1117 found_triangle =
true;
1118 addTriangle (current_index_, ffn_[current_index_], prev_index, polygons);
1119 state_[prev_index] = COMPLETED;
1120 state_[ffn_[current_index_]] = COMPLETED;
1121 ffn_[current_index_] = next_index;
1123 else if ((prev_index != R_) && ((sfn_[current_index_] == ffn_[prev_index]) || (sfn_[current_index_] == sfn_[prev_index])))
1125 found_triangle =
true;
1126 addTriangle (current_index_, sfn_[current_index_], prev_index, polygons);
1127 state_[prev_index] = COMPLETED;
1128 state_[sfn_[current_index_]] = COMPLETED;
1129 sfn_[current_index_] = next_index;
1131 else if (state_[next_index] > FREE)
1133 if ((ffn_[current_index_] == ffn_[next_index]) || (ffn_[current_index_] == sfn_[next_index]))
1135 found_triangle =
true;
1136 addTriangle (current_index_, ffn_[current_index_], next_index, polygons);
1138 if (ffn_[current_index_] == ffn_[next_index])
1140 ffn_[next_index] = current_index_;
1144 sfn_[next_index] = current_index_;
1146 state_[ffn_[current_index_]] = COMPLETED;
1147 ffn_[current_index_] = prev_index;
1149 else if ((sfn_[current_index_] == ffn_[next_index]) || (sfn_[current_index_] == sfn_[next_index]))
1151 found_triangle =
true;
1152 addTriangle (current_index_, sfn_[current_index_], next_index, polygons);
1154 if (sfn_[current_index_] == ffn_[next_index])
1156 ffn_[next_index] = current_index_;
1160 sfn_[next_index] = current_index_;
1162 state_[sfn_[current_index_]] = COMPLETED;
1163 sfn_[current_index_] = prev_index;
1172 tmp_ = coords_[ffn_[current_index_]] - proj_qp_;
1173 uvn_ffn_[0] = tmp_.dot(u_);
1174 uvn_ffn_[1] = tmp_.dot(v_);
1175 tmp_ = coords_[sfn_[current_index_]] - proj_qp_;
1176 uvn_sfn_[0] = tmp_.dot(u_);
1177 uvn_sfn_[1] = tmp_.dot(v_);
1178 bool prev_ffn =
isVisible(uvn_prev, uvn_next, uvn_current, uvn_ffn_) &&
isVisible(uvn_prev, uvn_sfn_, uvn_current, uvn_ffn_);
1179 bool prev_sfn =
isVisible(uvn_prev, uvn_next, uvn_current, uvn_sfn_) &&
isVisible(uvn_prev, uvn_ffn_, uvn_current, uvn_sfn_);
1180 bool next_ffn =
isVisible(uvn_next, uvn_prev, uvn_current, uvn_ffn_) &&
isVisible(uvn_next, uvn_sfn_, uvn_current, uvn_ffn_);
1181 bool next_sfn =
isVisible(uvn_next, uvn_prev, uvn_current, uvn_sfn_) &&
isVisible(uvn_next, uvn_ffn_, uvn_current, uvn_sfn_);
1183 if (prev_ffn && next_sfn && prev_sfn && next_ffn)
1186 double prev2f = (coords_[ffn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1187 double next2s = (coords_[sfn_[current_index_]] - coords_[next_index]).squaredNorm ();
1188 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1189 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1190 if (prev2f < prev2s)
1192 if (prev2f < next2f)
1194 if (prev2f < next2s)
1201 if (next2f < next2s)
1209 if (prev2s < next2f)
1211 if (prev2s < next2s)
1218 if (next2f < next2s)
1225 else if (prev_ffn && next_sfn)
1228 double prev2f = (coords_[ffn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1229 double next2s = (coords_[sfn_[current_index_]] - coords_[next_index]).squaredNorm ();
1230 if (prev2f < next2s)
1235 else if (prev_sfn && next_ffn)
1238 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1239 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1240 if (prev2s < next2f)
1246 else if (prev_ffn && !next_sfn && !prev_sfn && !next_ffn)
1248 else if (!prev_ffn && !next_sfn && prev_sfn && !next_ffn)
1250 else if (!prev_ffn && !next_sfn && !prev_sfn && next_ffn)
1252 else if (!prev_ffn && next_sfn && !prev_sfn && !next_ffn)
1257 double prev2f = (coords_[ffn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1260 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1261 if (prev2s < prev2f)
1268 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1269 if (next2f < prev2f)
1277 double next2s = (coords_[sfn_[current_index_]] - coords_[next_index]).squaredNorm ();
1280 double prev2s = (coords_[sfn_[current_index_]] - coords_[prev_index]).squaredNorm ();
1281 if (prev2s < next2s)
1288 double next2f = (coords_[ffn_[current_index_]] - coords_[next_index]).squaredNorm ();
1289 if (next2f < next2s)
1299 addTriangle (current_index_, ffn_[current_index_], prev_index, polygons);
1302 if (ffn_[prev_index] == current_index_)
1304 ffn_[prev_index] = ffn_[current_index_];
1306 else if (sfn_[prev_index] == current_index_)
1308 sfn_[prev_index] = ffn_[current_index_];
1310 else if (ffn_[prev_index] == R_)
1312 changed_1st_fn_ =
true;
1313 ffn_[prev_index] = ffn_[current_index_];
1315 else if (sfn_[prev_index] == R_)
1317 changed_1st_fn_ =
true;
1318 sfn_[prev_index] = ffn_[current_index_];
1320 else if (prev_index == R_)
1322 new2boundary_ = ffn_[current_index_];
1326 if (ffn_[ffn_[current_index_]] == current_index_)
1328 ffn_[ffn_[current_index_]] = prev_index;
1330 else if (sfn_[ffn_[current_index_]] == current_index_)
1332 sfn_[ffn_[current_index_]] = prev_index;
1336 ffn_[current_index_] = next_index;
1342 addTriangle (current_index_, sfn_[current_index_], prev_index, polygons);
1345 if (ffn_[prev_index] == current_index_)
1347 ffn_[prev_index] = sfn_[current_index_];
1349 else if (sfn_[prev_index] == current_index_)
1351 sfn_[prev_index] = sfn_[current_index_];
1353 else if (ffn_[prev_index] == R_)
1355 changed_1st_fn_ =
true;
1356 ffn_[prev_index] = sfn_[current_index_];
1358 else if (sfn_[prev_index] == R_)
1360 changed_1st_fn_ =
true;
1361 sfn_[prev_index] = sfn_[current_index_];
1363 else if (prev_index == R_)
1365 new2boundary_ = sfn_[current_index_];
1369 if (ffn_[sfn_[current_index_]] == current_index_)
1371 ffn_[sfn_[current_index_]] = prev_index;
1373 else if (sfn_[sfn_[current_index_]] == current_index_)
1375 sfn_[sfn_[current_index_]] = prev_index;
1379 sfn_[current_index_] = next_index;
1385 addTriangle (current_index_, ffn_[current_index_], next_index, polygons);
1386 int neighbor_update = next_index;
1389 if (state_[next_index] <= FREE)
1391 state_[next_index] = FRINGE;
1392 ffn_[next_index] = current_index_;
1393 sfn_[next_index] = ffn_[current_index_];
1397 if (ffn_[next_index] == R_)
1399 changed_2nd_fn_ =
true;
1400 ffn_[next_index] = ffn_[current_index_];
1402 else if (sfn_[next_index] == R_)
1404 changed_2nd_fn_ =
true;
1405 sfn_[next_index] = ffn_[current_index_];
1407 else if (next_index == R_)
1409 new2boundary_ = ffn_[current_index_];
1410 if (next_next_index == new2boundary_)
1411 already_connected_ =
true;
1413 else if (ffn_[next_index] == next_next_index)
1415 already_connected_ =
true;
1416 ffn_[next_index] = ffn_[current_index_];
1418 else if (sfn_[next_index] == next_next_index)
1420 already_connected_ =
true;
1421 sfn_[next_index] = ffn_[current_index_];
1425 tmp_ = coords_[ffn_[next_index]] - proj_qp_;
1426 uvn_next_ffn_[0] = tmp_.dot(u_);
1427 uvn_next_ffn_[1] = tmp_.dot(v_);
1428 tmp_ = coords_[sfn_[next_index]] - proj_qp_;
1429 uvn_next_sfn_[0] = tmp_.dot(u_);
1430 uvn_next_sfn_[1] = tmp_.dot(v_);
1432 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_);
1433 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_);
1435 int connect2ffn = -1;
1436 if (ffn_next_ffn && sfn_next_ffn)
1438 double fn2f = (coords_[ffn_[current_index_]] - coords_[ffn_[next_index]]).squaredNorm ();
1439 double sn2f = (coords_[ffn_[current_index_]] - coords_[sfn_[next_index]]).squaredNorm ();
1440 if (fn2f < sn2f) connect2ffn = 0;
1441 else connect2ffn = 1;
1443 else if (ffn_next_ffn) connect2ffn = 0;
1444 else if (sfn_next_ffn) connect2ffn = 1;
1446 switch (connect2ffn)
1450 addTriangle (next_index, ffn_[current_index_], ffn_[next_index], polygons);
1451 neighbor_update = ffn_[next_index];
1454 if ((ffn_[ffn_[next_index]] == ffn_[current_index_]) || (sfn_[ffn_[next_index]] == ffn_[current_index_]))
1456 state_[ffn_[next_index]] = COMPLETED;
1458 else if (ffn_[ffn_[next_index]] == next_index)
1460 ffn_[ffn_[next_index]] = ffn_[current_index_];
1462 else if (sfn_[ffn_[next_index]] == next_index)
1464 sfn_[ffn_[next_index]] = ffn_[current_index_];
1467 ffn_[next_index] = current_index_;
1473 addTriangle (next_index, ffn_[current_index_], sfn_[next_index], polygons);
1474 neighbor_update = sfn_[next_index];
1477 if ((ffn_[sfn_[next_index]] == ffn_[current_index_]) || (sfn_[sfn_[next_index]] == ffn_[current_index_]))
1479 state_[sfn_[next_index]] = COMPLETED;
1481 else if (ffn_[sfn_[next_index]] == next_index)
1483 ffn_[sfn_[next_index]] = ffn_[current_index_];
1485 else if (sfn_[sfn_[next_index]] == next_index)
1487 sfn_[sfn_[next_index]] = ffn_[current_index_];
1490 sfn_[next_index] = current_index_;
1500 if ((ffn_[ffn_[current_index_]] == neighbor_update) || (sfn_[ffn_[current_index_]] == neighbor_update))
1502 state_[ffn_[current_index_]] = COMPLETED;
1504 else if (ffn_[ffn_[current_index_]] == current_index_)
1506 ffn_[ffn_[current_index_]] = neighbor_update;
1508 else if (sfn_[ffn_[current_index_]] == current_index_)
1510 sfn_[ffn_[current_index_]] = neighbor_update;
1514 ffn_[current_index_] = prev_index;
1520 addTriangle (current_index_, sfn_[current_index_], next_index, polygons);
1521 int neighbor_update = next_index;
1524 if (state_[next_index] <= FREE)
1526 state_[next_index] = FRINGE;
1527 ffn_[next_index] = current_index_;
1528 sfn_[next_index] = sfn_[current_index_];
1532 if (ffn_[next_index] == R_)
1534 changed_2nd_fn_ =
true;
1535 ffn_[next_index] = sfn_[current_index_];
1537 else if (sfn_[next_index] == R_)
1539 changed_2nd_fn_ =
true;
1540 sfn_[next_index] = sfn_[current_index_];
1542 else if (next_index == R_)
1544 new2boundary_ = sfn_[current_index_];
1545 if (next_next_index == new2boundary_)
1546 already_connected_ =
true;
1548 else if (ffn_[next_index] == next_next_index)
1550 already_connected_ =
true;
1551 ffn_[next_index] = sfn_[current_index_];
1553 else if (sfn_[next_index] == next_next_index)
1555 already_connected_ =
true;
1556 sfn_[next_index] = sfn_[current_index_];
1560 tmp_ = coords_[ffn_[next_index]] - proj_qp_;
1561 uvn_next_ffn_[0] = tmp_.dot(u_);
1562 uvn_next_ffn_[1] = tmp_.dot(v_);
1563 tmp_ = coords_[sfn_[next_index]] - proj_qp_;
1564 uvn_next_sfn_[0] = tmp_.dot(u_);
1565 uvn_next_sfn_[1] = tmp_.dot(v_);
1567 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_);
1568 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_);
1570 int connect2sfn = -1;
1571 if (ffn_next_sfn && sfn_next_sfn)
1573 double fn2s = (coords_[sfn_[current_index_]] - coords_[ffn_[next_index]]).squaredNorm ();
1574 double sn2s = (coords_[sfn_[current_index_]] - coords_[sfn_[next_index]]).squaredNorm ();
1575 if (fn2s < sn2s) connect2sfn = 0;
1576 else connect2sfn = 1;
1578 else if (ffn_next_sfn) connect2sfn = 0;
1579 else if (sfn_next_sfn) connect2sfn = 1;
1581 switch (connect2sfn)
1585 addTriangle (next_index, sfn_[current_index_], ffn_[next_index], polygons);
1586 neighbor_update = ffn_[next_index];
1589 if ((ffn_[ffn_[next_index]] == sfn_[current_index_]) || (sfn_[ffn_[next_index]] == sfn_[current_index_]))
1591 state_[ffn_[next_index]] = COMPLETED;
1593 else if (ffn_[ffn_[next_index]] == next_index)
1595 ffn_[ffn_[next_index]] = sfn_[current_index_];
1597 else if (sfn_[ffn_[next_index]] == next_index)
1599 sfn_[ffn_[next_index]] = sfn_[current_index_];
1602 ffn_[next_index] = current_index_;
1608 addTriangle (next_index, sfn_[current_index_], sfn_[next_index], polygons);
1609 neighbor_update = sfn_[next_index];
1612 if ((ffn_[sfn_[next_index]] == sfn_[current_index_]) || (sfn_[sfn_[next_index]] == sfn_[current_index_]))
1614 state_[sfn_[next_index]] = COMPLETED;
1616 else if (ffn_[sfn_[next_index]] == next_index)
1618 ffn_[sfn_[next_index]] = sfn_[current_index_];
1620 else if (sfn_[sfn_[next_index]] == next_index)
1622 sfn_[sfn_[next_index]] = sfn_[current_index_];
1625 sfn_[next_index] = current_index_;
1635 if ((ffn_[sfn_[current_index_]] == neighbor_update) || (sfn_[sfn_[current_index_]] == neighbor_update))
1637 state_[sfn_[current_index_]] = COMPLETED;
1639 else if (ffn_[sfn_[current_index_]] == current_index_)
1641 ffn_[sfn_[current_index_]] = neighbor_update;
1643 else if (sfn_[sfn_[current_index_]] == current_index_)
1645 sfn_[sfn_[current_index_]] = neighbor_update;
1648 sfn_[current_index_] = prev_index;
1660 template <
typename Po
intInT> std::vector<std::vector<std::size_t> >
1665 for (std::size_t i=0; i < input.
polygons.size (); ++i)
1666 for (std::size_t j=0; j < input.
polygons[i].vertices.size (); ++j)
1667 triangleList[input.
polygons[i].vertices[j]].push_back (i);
1668 return (triangleList);
1671 #define PCL_INSTANTIATE_GreedyProjectionTriangulation(T) \
1672 template class PCL_EXPORTS pcl::GreedyProjectionTriangulation<T>;
1674 #endif // PCL_SURFACE_IMPL_GP3_H_