shared.h
Go to the documentation of this file.
1 // Copyright 2012 Dror Aiger
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -------------------------------------------------------------------------- //
16 //
17 // Authors: Dror Aiger, Yoni Weill, Nicolas Mellado
18 //
19 // An implementation of the 4-points Congruent Sets (4PCS) algorithm presented
20 // in:
21 //
22 // 4-points Congruent Sets for Robust Surface Registration
23 // Dror Aiger, Niloy J. Mitra, Daniel Cohen-Or
24 // ACM SIGGRAPH 2008 and ACM Transaction of Graphics.
25 //
26 // Given two sets of points in 3-space, P and Q, the algorithm applies RANSAC
27 // in roughly O(n^2) time instead of O(n^3) for standard RANSAC, using an
28 // efficient method based on invariants, to find the set of all 4-points in Q
29 // that can be matched by rigid transformation to a given set of 4-points in P
30 // called a base. This avoids the need to examine all sets of 3-points in Q
31 // against any base of 3-points in P as in standard RANSAC.
32 // The algorithm can use colors and normals to speed-up the matching
33 // and to improve the quality. It can be easily extended to affine/similarity
34 // transformation but then the speed-up is smaller because of the large number
35 // of congruent sets. The algorithm can also limit the range of transformations
36 // when the application knows something on the initial pose but this is not
37 // necessary in general (though can speed the runtime significantly).
38 
39 // Home page of the 4PCS project (containing the paper, presentations and a
40 // demo): http://graphics.stanford.edu/~niloy/research/fpcs/fpcs_sig_08.html
41 // Use google search on "4-points congruent sets" to see many related papers
42 // and applications.
43 
44 #ifndef _SHARED_4PCS_H_
45 #define _SHARED_4PCS_H_
46 
47 #include "gr/utils/disablewarnings.h"
48 
49 #include <Eigen/Core>
50 
51 #include <vector>
52 #include <iostream>
53 #include <fstream>
54 #include <array>
55 #include <random>
56 
57 namespace gr {
58 
59 #ifdef PARSED_BY_DOXYGEN
60 struct ExternalPoint;
61 
62 struct PointConcept {
63  public:
64  /*! \brief Defines the ambient space dimension */
65  enum {Dim = 3};
66 
67  /*! \brief Defines the type used ton encode scalar values */
68  typedef float Scalar;
69 
70  /*! \brief Defines type used to encode vector values */
71  typedef Eigen::Matrix<Scalar, Dim, 1> VectorType;
72 
73  /*! \brief Constructor using external point type that is wrapped */
74  inline PointConcept(const ExternalPoint&) { }
75 
76  /*! \brief Read access to the position property */
77  inline const VectorType& pos() const { }
78 };
79 #endif
80 
81 /// The basic 3D point structure. A point potentially contains also directional
82 /// information and color.
83 /// \implements PointConcept
84 template<typename _Scalar>
85 class Point3D
86 #ifdef PARSED_BY_DOXYGEN
87  : public PointConcept
88 #endif
89 {
90  public:
91  using Scalar = _Scalar;
92  using VectorType = Eigen::Matrix<Scalar, 3, 1>;
93 
94  inline Point3D(Scalar x, Scalar y, Scalar z) : pos_({ x, y, z}) {}
95  inline Point3D(const Point3D& other):
96  pos_(other.pos_),
98  rgb_(other.rgb_) {}
99  template<typename Scalar>
100  explicit inline Point3D(const Eigen::Matrix<Scalar, 3, 1>& other):
101  pos_({ other(0), other(1), other(2) }){
102  }
103 
104  inline Point3D() {}
105  inline VectorType& pos() { return pos_ ; }
106  inline const VectorType& pos() const { return pos_ ; }
107  inline const VectorType& rgb() const { return rgb_; }
108 
109  inline const VectorType& normal() const { return normal_; }
110  inline void set_rgb(const VectorType& rgb) {
111  rgb_ = rgb;
112  }
113  inline void set_normal(const VectorType& normal) {
114  normal_ = normal.normalized();
115  }
116 
117  inline void normalize() {
118  normal_.normalize();
119  }
120  inline bool hasColor() const { return rgb_.squaredNorm() > Scalar(0.001); }
121 
122  Scalar& x() { return pos_.coeffRef(0); }
123  Scalar& y() { return pos_.coeffRef(1); }
124  Scalar& z() { return pos_.coeffRef(2); }
125 
126  Scalar x() const { return pos_.coeff(0); }
127  Scalar y() const { return pos_.coeff(1); }
128  Scalar z() const { return pos_.coeff(2); }
129 
130 
131 
132  private:
133  /// Normal.
134  VectorType pos_ { Scalar(0.0f), Scalar(0.0f), Scalar(0.0f) };
135  /// Normal.
136  VectorType normal_{ Scalar(0.0f), Scalar(0.0f), Scalar(0.0f) };
137  /// Color.
138  VectorType rgb_ { Scalar(-1.0f), Scalar(-1.0f), Scalar(-1.0f) };
139 };
140 
141 
142 //// ----- MatchBase Options -----
143 ///// delta and overlap_estimation are the application parameters. All other
144 ///// parameters are more likely to keep fixed and they can be set via the setters.
145 // struct MatchOptions {
146 // using Scalar = typename Point3D::Scalar;
147 // MatchOptions() {}
148 
149 // /// The delta for the LCP (see the paper).
150 // Scalar delta = 5.0;
151 
152 // /// Maximum rotation angle. Set negative to ignore
153 // Scalar max_angle = -1;
154 // /// The number of points in the sample. We sample this number of points
155 // /// uniformly from P and Q.
156 // size_t sample_size = 200;
157 // /// Maximum time we allow the computation to take. This makes the algorithm
158 // /// an ANY TIME algorithm that can be stopped at any time, producing the best
159 // /// solution so far.
160 // int max_time_seconds = 60;
161 // /// use a constant default seed by default
162 // unsigned int randomSeed = std::mt19937::default_seed;
163 
164 // inline bool configureOverlap(Scalar overlap_, Scalar terminate_threshold_ = Scalar(1)) {
165 // if(terminate_threshold_ < overlap_) return false;
166 // overlap_estimation = overlap_;
167 // terminate_threshold = terminate_threshold_;
168 // return true;
169 // }
170 // inline Scalar getTerminateThreshold() const { return terminate_threshold; }
171 // inline Scalar getOverlapEstimation() const { return overlap_estimation; }
172 
173 // private:
174 // /// Threshold on the value of the target function (LCP, see the paper).
175 // /// It is used to terminate the process once we reached this value.
176 // Scalar terminate_threshold = 1.0;
177 // /// Estimated overlap between P and Q. This is the fraction of points in P that
178 // /// may have corresponding point in Q. It's being used to estimate the number
179 // /// of RANSAC iterations needed to guarantee small failure probability.
180 // Scalar overlap_estimation = 0.2;
181 // };
182 
183 } /// namespace gr
184 
185 
186 
187 #endif //_SHARED_4PCS_H_
Point3D(Scalar x, Scalar y, Scalar z)
Definition: shared.h:94
const VectorType & rgb() const
Definition: shared.h:107
Point3D(const Point3D &other)
Definition: shared.h:95
Scalar x() const
Definition: shared.h:126
Point3D()
Definition: shared.h:104
Scalar y() const
Definition: shared.h:127
bool hasColor() const
Definition: shared.h:120
Scalar z() const
Definition: shared.h:128
Scalar & z()
Definition: shared.h:124
const VectorType & pos() const
Definition: shared.h:106
void normalize()
Definition: shared.h:117
void set_rgb(const VectorType &rgb)
Definition: shared.h:110
VectorType & pos()
Definition: shared.h:105
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
void set_normal(const VectorType &normal)
Definition: shared.h:113
const VectorType & normal() const
Definition: shared.h:109
Scalar & x()
Definition: shared.h:122
Scalar & y()
Definition: shared.h:123