Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RotationAveraging.hh
1 #ifndef __RotationAveraging_hh__
2 #define __RotationAveraging_hh__
3 
4 #include <bias_config.h>
5 
6 #include <Base/Debug/Debug.hh>
7 #include <Base/Geometry/Quaternion.hh>
8 #include <Geometry/RMatrix.hh>
9 #include <vector>
10 
11 namespace BIAS {
12 
13 /** @class RotationAveraging
14  @brief Computes mean of rotations due to different measures.
15  @ingroup g_geometry
16  @author esquivel
17  @date 09/2011
18 
19  This tool computes the average rotation of given rotations R_1,...,R_n
20  with respect to different distance measures d: SO(3) x SO(3) -> R
21  on rotations as described in [1].
22  The L2-mean for a given d is defined by the rotation matrix R from
23  mean(R_1,...,R_n) = \argmin_R \sum_{i=1}^N d(R,R_i)^2
24  while the L1-mean is given by \argmin_R \sum_{i=1}^N d(R,R_i).
25 
26  Implemented so far are:
27 
28  - geodesic L2-mean (Karcher mean): d_geod(R,S) = |log(RS')| where
29  the rotation matrix logarithm computes the angle-axis representation
30  theta*r for a rotation matrix R, hence d_geod(R,S) returns the
31  rotation angle theta of the matrix RS'
32 
33  - quaternion L2-mean: d_quat(R,S) = min(|r-s|,|r+s|) where r,s are
34  the unit quaternion representations of rotation matrices R,S,
35  hence d_quat(R,S) returns 2*sin(theta/4)
36 
37  - chordal L2-mean: d_chor(R,S) = |R-S|_F = 2*sqrt(2)*sin(theta/2)
38 
39  - geodesic L1-mean (geodesic median): computed via Weiszfeld algorithm [2]
40 
41  - logarithmic L2-mean: computes R = exp(mean(log(R_1),...,log(R_n)))
42 
43  While closed form solutions exist for the L2-means (except geodesic mean),
44  iterative algorithms are given for the geodesic L1-/L2-mean.
45 
46  The solution is in general only optimal under the assumptions that
47  the rotation angles of all R_i are less than 180 degree resp. all
48  rotations cover less than a hemisphere of the space SO(3).
49 
50  [1] Y. Dai, J. Trumpf, H. Li, N. Barnes & R. Hartley: "Rotation
51  Averaging with Application to Camera-Rig Calibration", 2009.
52  [2] R. Hartley, K. Aftab, J. Trumpf: "L1 Rotation Averaging Using the
53  Weiszfeld Algorithm", 2011.
54 */
55  class BIASGeometry_EXPORT RotationAveraging : public BIAS::Debug
56  {
57  public:
58 
60 
62 
63  /** @brief Set threshold for convergence of iterative geodesic L1-/L2-mean
64  computation. The algorithm stops if the angular update is less.
65  @param[in] epsilon Threshold for convergence in radians */
66  inline void SetTolerance(double epsilon) { tolerance_ = epsilon; }
67 
68  /** @brief Set maximal number of iterations for geodesic L1-/L2-mean
69  computation.
70  @param[in] num Maximal number of iterations */
71  inline void SetMaxIterations(unsigned int num) { maxIters_ = num; }
72 
73  /** @brief Compute geodesic L1-mean (geodesic median) of rotations.
74  @param[in] R Input rotation matrices to compute the average for
75  @param[out] meanR Output average rotation matrix
76  @return Returns 0 on success, < 0 otherwise. */
77  int GeodesicL1Mean(const std::vector<BIAS::RMatrix> &R,
78  BIAS::RMatrix &meanR);
79 
80  /** @brief Compute geodesic L2-mean (Karcher/geometric mean) of rotations.
81  @param[in] R Input rotation matrices to compute the average for
82  @param[out] meanR Output average rotation matrix
83  @return Returns 0 on success, < 0 otherwise. */
84  int GeodesicL2Mean(const std::vector<BIAS::RMatrix> &R,
85  BIAS::RMatrix &meanR);
86 
87  /** @brief Compute quaternion L2-mean of rotations.
88  @param[in] R Input rotation matrices to compute the average for
89  @param[out] meanR Output average rotation matrix
90  @return Returns 0 on success, < 0 otherwise. */
91  int QuaternionL2Mean(const std::vector<BIAS::RMatrix> &R,
92  BIAS::RMatrix &meanR);
93 
94  /** @brief Compute quaternion L2-mean of rotations.
95  @param[in] Q Input unit quaternions to compute the average for
96  @param[out] meanQ Output average rotation as unit quaternion
97  @return Returns 0 on success, < 0 otherwise. */
98  int QuaternionL2Mean(const std::vector<BIAS::Quaternion<double> > &Q,
100 
101  /** @brief Compute matrix logarithm L2-mean of rotations.
102  The matrix logarithm of a rotation matrix is the angle/axis vector
103  phi * r, also known as the modified Rodrigues vector.
104  @param[in] R Input rotation matrices to compute the average for
105  @param[out] meanR Output average rotation matrix
106  @return Returns 0 on success, < 0 otherwise. */
107  int LogarithmicL2Mean(const std::vector<BIAS::RMatrix> &R,
108  BIAS::RMatrix &meanR);
109 
110  /** @brief Compute chordal L2-mean of rotations.
111  @param[in] R Input rotation matrices to compute the average for
112  @param[out] meanR Output average rotation matrix
113  @return Returns 0 on success, < 0 otherwise. */
114  int ChordalL2Mean(const std::vector<BIAS::RMatrix> &R,
115  BIAS::RMatrix &meanR);
116 
117  /** @brief Compute chordal L2-mean of rotations using unit quaternions.
118  @param[in] Q Input unit quaternions to compute the average for
119  @param[out] meanQ Output average rotation as unit quaternion
120  @return Returns 0 on success, < 0 otherwise. */
121  int ChordalL2Mean(const std::vector<BIAS::Quaternion<double> > &Q,
122  BIAS::Quaternion<double> &meanQ);
123 
124  private:
125 
126  /** Threshold for convergence of geodesic L1-/L2-mean computation */
127  double tolerance_;
128 
129  /** Maximal number of iterations for geodesic L1-/L2-mean computation */
130  unsigned int maxIters_;
131 
132  /** @brief Test if given rotations are appropriate for averaging. */
133  bool TestRotations(const std::vector<BIAS::RMatrix> &R);
134 
135  /** @brief Test if unit quaternions are appropriate for averaging. */
136  bool TestRotations(const std::vector<BIAS::Quaternion<double> > &Q);
137 
138  };
139 
140 }
141 
142 #endif //__RotationAveraging_hh__
void SetMaxIterations(unsigned int num)
Set maximal number of iterations for geodesic L1-/L2-mean computation.
void SetTolerance(double epsilon)
Set threshold for convergence of iterative geodesic L1-/L2-mean computation.
3D rotation matrix
Definition: RMatrix.hh:49
Computes mean of rotations due to different measures.