pairCreationFunctor.h
Go to the documentation of this file.
1 #ifndef _OPENGR_ALGO_PAIRCREATIONFUNCTOR_H
2 #define _OPENGR_ALGO_PAIRCREATIONFUNCTOR_H
3 
4 #include <iostream>
5 #include <vector>
6 #include "gr/utils/shared.h"
7 
8 #include "gr/accelerators/pairExtraction/bruteForceFunctor.h"
9 #include "gr/accelerators/pairExtraction/intersectionFunctor.h"
10 #include "gr/accelerators/pairExtraction/intersectionPrimitive.h"
11 #include "gr/algorithms/match4pcsBase.h"
12 
13 namespace gr {
14 
15 template <typename PointType, typename _Scalar, typename FilterFunctor, typename Options>
17 
18 public:
19  using Scalar = _Scalar;
20  using PairsVector = std::vector<std::pair<int, int>>;
21  using VectorType = typename PointType::VectorType;
22  using BaseCoordinates = typename Traits4pcs<PointType>::Coordinates;
23  using OptionType = Options;
24 
25  // Shared data
26  OptionType options_;
27  double pair_distance;
30  const std::vector<PointType>& Q_;
31 
33 
34  std::vector<unsigned int> ids;
35 
36 
37  // Internal data
38  typedef Eigen::Matrix<Scalar, 3, 1> Point;
39  typedef HyperSphere< typename PairCreationFunctor::Point, 3, Scalar> Primitive;
40 
41  std::vector< /*Eigen::Map<*/typename PairCreationFunctor::Point/*>*/ > points;
43 
44 private:
45  VectorType segment1;
46  BaseCoordinates base_3D_;
47  int base_point1_, base_point2_;
48 
49  typename PairCreationFunctor::Point _gcenter;
50  Scalar _ratio;
51  static const typename PairCreationFunctor::Point half;
52 
53 public:
55  const OptionType& options,
56  const std::vector<PointType>& Q)
57  :options_(options), Q_(Q),
58  pairs(NULL), _ratio(1.f)
59  { }
60 
61 private:
62  inline Point worldToUnit(
63  const Eigen::MatrixBase<typename PairCreationFunctor::Point> &p) const {
64  static const Point half = Point::Ones() * Scalar(0.5f);
65  return (p-_gcenter) / _ratio + half;
66  }
67 
68 
69 public:
71  const Eigen::MatrixBase<typename PairCreationFunctor::Point> &p) const {
72  static const Point half = Point::Ones() * Scalar(0.5f);
73  return (p - half) * _ratio + _gcenter;
74  }
75 
76 
77  inline Scalar unitToWorld( Scalar d) const {
78  return d * _ratio;
79  }
80 
81 
82  inline Point getPointInWorldCoord(int i) const {
83  return unitToWorld(points[i]);
84  }
85 
86 
87  inline void synch3DContent(){
88  points.clear();
89  primitives.clear();
90 
91  Eigen::AlignedBox<_Scalar, 3> bbox;
92 
93  unsigned int nSamples = Q_.size();
94 
95  points.reserve(nSamples);
96  primitives.reserve(nSamples);
97 
98  // Compute bounding box on fine data to be SURE to have all points in the
99  // unit bounding box
100  for (unsigned int i = 0; i < nSamples; ++i) {
101  const VectorType &q = Q_[i].pos();
102  points.push_back(q);
103  bbox.extend(q);
104  }
105 
106  _gcenter = bbox.center();
107  // add a delta to avoid to have elements with coordinate = 1
108  _ratio = bbox.diagonal().maxCoeff() + 0.001;
109 
110  // update point cloud (worldToUnit use the ratio and gravity center
111  // previously computed)
112  // Generate primitives
113  for (unsigned int i = 0; i < nSamples; ++i) {
114  points[i] = worldToUnit(points[i]);
115 
116  primitives.emplace_back(points[i], Scalar(1.));
117  ids.push_back(i);
118  }
119  }
120 
121  inline void setRadius(Scalar radius) {
122  const Scalar nRadius = radius/_ratio;
123  for(typename std::vector< Primitive >::iterator it = primitives.begin();
124  it != primitives.end(); ++it)
125  (*it).radius() = nRadius;
126  }
127 
128  inline Scalar getNormalizedEpsilon(Scalar eps){
129  return eps/_ratio;
130  }
131 
132  inline void setBase( int base_point1, int base_point2,
133  const BaseCoordinates& base_3D){
134  base_3D_ = base_3D;
135  base_point1_ = base_point1;
136  base_point2_ = base_point2;
137 
138  segment1 = (base_3D_[base_point2_]->pos() -
139  base_3D_[base_point1_]->pos()).normalized();
140  }
141 
142 
143  inline void beginPrimitiveCollect(int /*primId*/){ }
144  inline void endPrimitiveCollect(int /*primId*/){ }
145 
146 
147  inline void process(int i, int j){
148  if (i>j){
149  const PointType& p = Q_[j];
150  const PointType& q = Q_[i];
151 
152  // Compute the distance and two normal angles to ensure working with
153  // wrong orientation. We want to verify that the angle between the
154  // normals is close to the angle between normals in the base. This can be
155  // checked independent of the full rotation angles which are not yet
156  // defined by segment matching alone..
157  const Scalar distance = (q.pos() - p.pos()).norm();
158 #ifndef MULTISCALE
159  if (std::abs(distance - pair_distance) > pair_distance_epsilon) return;
160 #endif
161  FilterFunctor fun;
162  std::pair<bool,bool> res = fun(p,q, pair_normals_angle, *base_3D_[base_point1_], *base_3D_[base_point2_], options_);
163  if (res.first)
164  pairs->emplace_back(i, j);
165  if (res.second)
166  pairs->emplace_back(j, i);
167  }
168  }
169 };
170 
171 } // namespace gr
172 
173 #endif // PAIRCREATIONFUNCTOR_H
PairCreationFunctor(const OptionType &options, const std::vector< PointType > &Q)
Definition: pairCreationFunctor.h:54
double pair_normals_angle
Definition: pairCreationFunctor.h:28
void endPrimitiveCollect(int)
Definition: pairCreationFunctor.h:144
const std::vector< PointType > & Q_
Definition: pairCreationFunctor.h:30
Point getPointInWorldCoord(int i) const
Definition: pairCreationFunctor.h:82
Definition: pairCreationFunctor.h:16
Scalar unitToWorld(Scalar d) const
Definition: pairCreationFunctor.h:77
void synch3DContent()
Definition: pairCreationFunctor.h:87
OptionType options_
Definition: pairCreationFunctor.h:26
std::vector< typename PairCreationFunctor::Point > points
Definition: pairCreationFunctor.h:41
std::vector< unsigned int > ids
Definition: pairCreationFunctor.h:34
static constexpr int size()
Definition: match4pcsBase.h:27
std::vector< Primitive > primitives
Definition: pairCreationFunctor.h:42
void process(int i, int j)
Definition: pairCreationFunctor.h:147
void setRadius(Scalar radius)
Definition: pairCreationFunctor.h:121
Definition: intersectionPrimitive.h:60
void setBase(int base_point1, int base_point2, const BaseCoordinates &base_3D)
Definition: pairCreationFunctor.h:132
void beginPrimitiveCollect(int)
Definition: pairCreationFunctor.h:143
Eigen::Matrix< Scalar, 3, 1 > Point
Definition: pairCreationFunctor.h:38
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
double pair_distance
Definition: pairCreationFunctor.h:27
Scalar getNormalizedEpsilon(Scalar eps)
Definition: pairCreationFunctor.h:128
PairsVector * pairs
Definition: pairCreationFunctor.h:32
Point unitToWorld(const Eigen::MatrixBase< typename PairCreationFunctor::Point > &p) const
Definition: pairCreationFunctor.h:70
HyperSphere< typename PairCreationFunctor::Point, 3, Scalar > Primitive
Definition: pairCreationFunctor.h:39
double pair_distance_epsilon
Definition: pairCreationFunctor.h:29