PointPairFilter.h
Go to the documentation of this file.
1 //
2 // Created by Sandra Alfaro on 26/04/18.
3 //
4 
5 #ifndef OPENGR_FUNCTORFEATUREPOINTTEST_H
6 #define OPENGR_FUNCTORFEATUREPOINTTEST_H
7 
8 #include "gr/utils/shared.h"
9 #include <vector>
10 
11 namespace gr {
12 
13 #ifdef PARSED_BY_DOXYGEN
15 
16  template < class Derived, class TBase>
17  struct Options : public TBase { };
18 
19  /// Verify that the 2 points found in Q are similar to 2 of the points in the base.
20  /// Return a pair of bool, according of the right addition of the pair (p,q) or (q,p) in the congruent set.
21  template <typename PointType, typename WantedOptionsAndMore>
22  inline std::pair<bool,bool> operator() (const PointType& /*p*/,
23  const PointType& /*q*/,
24  typename PointType::Scalar /*pair_normals_angle*/,
25  const PointType& /*b0*/,
26  const PointType& /*b1*/,
27  const WantedOptionsAndMore& /*options*/) { }
28 };
29 #endif
30 
31  /// \brief Functor used in n-pcs algorithm to filters pairs of points according
32  /// to the exploration basis,
33  /// \tparam
34  /// \implements PairFilterConcept
35  ///
37  template < class Derived, class TBase>
38  struct Options : public TBase {
40  enum { IS_DUMMYPOINTFILTER_OPTIONS = true };
41  };
42  template <typename PointType, typename WantedOptionsAndMore>
43  inline std::pair<bool,bool> operator() (const PointType& /*p*/,
44  const PointType& /*q*/,
45  typename PointType::Scalar /*pair_normals_angle*/,
46  const PointType& /*b0*/,
47  const PointType& /*b1*/,
48  const WantedOptionsAndMore &options) {
49  return std::make_pair(options.dummyFilteringResponse, options.dummyFilteringResponse);
50  }
51  };
52 
53  /// \brief Functor used in n-pcs algorithm to filters pairs of points according
54  /// to the exploration basis. Uses normal, colors and max motion when
55  /// available
56  ///
57  /// \implements PairFilterConcept
58  ///
60  template < class Derived, class TBase>
61  struct Options : public TBase {
62  using Scalar = typename TBase::Scalar;
63 
64  /// Maximum normal difference.
65  Scalar max_normal_difference = -1;
66  /// Maximum color RGB distance between corresponding vertices. Set negative to ignore
67  Scalar max_color_distance = -1;
68 
70  };
71 
72  /// Verify that the 2 points found in Q are similar to 2 of the points in the base.
73  /// A filter by point feature : normal, distance, translation distance, angle and color.
74  /// Return a pair of bool, according of the right addition of the pair (p,q) or (q,p) in the congruent set.
75  template <typename PointType, typename WantedOptionsAndMore>
76  inline std::pair<bool,bool> operator() (const PointType& p,
77  const PointType& q,
78  typename PointType::Scalar pair_normals_angle,
79  const PointType& b0,
80  const PointType& b1,
81  const WantedOptionsAndMore &options) {
82  static_assert( WantedOptionsAndMore::IS_ADAPTIVEPOINTFILTER_OPTIONS,
83  "Options passed to AdaptivePointFilter must inherit AdaptivePointFilter::Options" );
84  using Scalar = typename PointType::Scalar;
85  using VectorType = typename PointType::VectorType;
86 
87 
88  std::pair<bool,bool> res;
89  res.first = false;
90  res.second = false;
91 
92  VectorType segment1 = (b1.pos() - b0.pos()).normalized();
93 
94  if ( options.max_normal_difference > 0 &&
95  q.normal().squaredNorm() > 0 &&
96  p.normal().squaredNorm() > 0) {
97  const Scalar norm_threshold =
98  0.5 * options.max_normal_difference * M_PI / 180.0;
99  const double first_normal_angle = (q.normal() - p.normal()).norm();
100  const double second_normal_angle = (q.normal() + p.normal()).norm();
101  // Take the smaller normal distance.
102  const Scalar first_norm_distance =
103  std::min(std::abs(first_normal_angle - pair_normals_angle),
104  std::abs(second_normal_angle - pair_normals_angle));
105  // Verify appropriate angle between normals and distance.
106 
107  if (first_norm_distance > norm_threshold) return res;
108  }
109  // Verify restriction on the rotation angle, translation and colors.
110  if (options.max_color_distance > 0) {
111  const bool use_rgb = (p.rgb()[0] >= 0 && q.rgb()[0] >= 0 &&
112  b0.rgb()[0] >= 0 &&
113  b1.rgb()[0] >= 0);
114  bool color_good = (p.rgb() - b0.rgb()).norm() <
115  options.max_color_distance &&
116  (q.rgb() - b1.rgb()).norm() <
117  options.max_color_distance;
118 
119  if (use_rgb && ! color_good) return res;
120  }
121 
122  if (options.max_translation_distance > 0) {
123  const bool dist_good = (p.pos() - b0.pos()).norm() <
124  options.max_translation_distance &&
125  (q.pos() - b1.pos()).norm() <
126  options.max_translation_distance;
127  if (! dist_good) return res;
128  }
129 
130  // need cleaning here
131  if (options.max_angle > 0){
132  VectorType segment2 = (q.pos() - p.pos()).normalized();
133  if (std::acos(segment1.dot(segment2)) <= options.max_angle * M_PI / 180.0) {
134  res.second = true;
135  }
136 
137  if (std::acos(segment1.dot(- segment2)) <= options.max_angle * M_PI / 180.0) {
138  // Add ordered pair.
139  res.first = true;
140  }
141  }else {
142  res.first = true;
143  res.second = true;
144  }
145  return res;
146  }
147  };
148 }
149 
150 #endif //OPENGR_FUNCTORFEATUREPOINTTEST_H
std::pair< bool, bool > operator()(const PointType &p, const PointType &q, typename PointType::Scalar pair_normals_angle, const PointType &b0, const PointType &b1, const WantedOptionsAndMore &options)
Verify that the 2 points found in Q are similar to 2 of the points in the base. A filter by point fea...
Definition: PointPairFilter.h:76
Functor used in n-pcs algorithm to filters pairs of points according to the exploration basis...
Definition: PointPairFilter.h:59
Definition: PointPairFilter.h:38
Scalar max_color_distance
Maximum color RGB distance between corresponding vertices. Set negative to ignore.
Definition: PointPairFilter.h:67
#define M_PI
Definition: utils.h:57
Functor used in n-pcs algorithm to filters pairs of points according to the exploration basis...
Definition: PointPairFilter.h:36
CongruentSetExplorationBase< Traits, PointType, TransformVisitor, PairFilteringFunctor, OptExts... >::Scalar ComputeTransformation(const InputRange1 &P, const InputRange2 &Q, Eigen::Ref< typename CongruentSetExplorationBase< Traits, PointType, TransformVisitor, PairFilteringFunctor, OptExts... >::MatrixType > transformation, const Sampler< PointType > &sampler, TransformVisitor &v)
Definition: congruentSetExplorationBase.hpp:61
bool dummyFilteringResponse
Definition: PointPairFilter.h:39
Definition: PointPairFilter.h:61
Scalar max_normal_difference
Maximum normal difference.
Definition: PointPairFilter.h:65
std::pair< bool, bool > operator()(const PointType &, const PointType &, typename PointType::Scalar, const PointType &, const PointType &, const WantedOptionsAndMore &options)
Definition: PointPairFilter.h:43