00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 #ifndef PCL_EIGEN_H_
00091 #define PCL_EIGEN_H_
00092
00093 #include <Eigen/Core>
00094 #include <Eigen/Eigenvalues>
00095 #include <Eigen/Geometry>
00096
00097 namespace pcl
00098 {
00099 template<typename Scalar, typename Roots> inline void computeRoots2 (const Scalar& b, const Scalar& c, Roots& roots)
00100 {
00101 roots(0) = Scalar(0);
00102 Scalar d = b * b - 4.0 * c;
00103 if (d < 0.0)
00104 d = 0.0;
00105
00106 Scalar sd = sqrt (d);
00107
00108 roots (2) = 0.5f * (b + sd);
00109 roots (1) = 0.5f * (b - sd);
00110 }
00111
00112 template<typename Matrix, typename Roots> inline void
00113 computeRoots (const Matrix& m, Roots& roots)
00114 {
00115 typedef typename Matrix::Scalar Scalar;
00116
00117
00118
00119
00120 Scalar c0 = m(0,0)*m(1,1)*m(2,2)
00121 + Scalar(2) * m(0,1)*m(0,2)*m(1,2)
00122 - m(0,0)*m(1,2)*m(1,2)
00123 - m(1,1)*m(0,2)*m(0,2)
00124 - m(2,2)*m(0,1)*m(0,1);
00125 Scalar c1 = m(0,0)*m(1,1) -
00126 m(0,1)*m(0,1) +
00127 m(0,0)*m(2,2) -
00128 m(0,2)*m(0,2) +
00129 m(1,1)*m(2,2) -
00130 m(1,2)*m(1,2);
00131 Scalar c2 = m(0,0) + m(1,1) + m(2,2);
00132
00133
00134 if (fabs(c0) < Eigen::NumTraits<Scalar>::epsilon())
00135 computeRoots2 (c2, c1, roots);
00136 else
00137 {
00138 const Scalar s_inv3 = 1.0/3.0;
00139 const Scalar s_sqrt3 = Eigen::internal::sqrt (Scalar (3.0));
00140
00141
00142 Scalar c2_over_3 = c2*s_inv3;
00143 Scalar a_over_3 = (c1 - c2*c2_over_3)*s_inv3;
00144 if (a_over_3 > Scalar (0))
00145 a_over_3 = Scalar (0);
00146
00147 Scalar half_b = Scalar(0.5) * (c0 + c2_over_3 * (Scalar(2) * c2_over_3 * c2_over_3 - c1));
00148
00149 Scalar q = half_b*half_b + a_over_3*a_over_3*a_over_3;
00150 if (q > Scalar(0))
00151 q = Scalar(0);
00152
00153
00154 Scalar rho = Eigen::internal::sqrt (-a_over_3);
00155 Scalar theta = std::atan2 (Eigen::internal::sqrt (-q), half_b)*s_inv3;
00156 Scalar cos_theta = Eigen::internal::cos (theta);
00157 Scalar sin_theta = Eigen::internal::sin (theta);
00158 roots(0) = c2_over_3 + Scalar(2) * rho * cos_theta;
00159 roots(1) = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
00160 roots(2) = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
00161
00162
00163 if (roots (0) >= roots (1))
00164 std::swap (roots (0), roots (1));
00165 if (roots (1) >= roots (2))
00166 {
00167 std::swap (roots (1), roots (2));
00168 if (roots (0) >= roots (1))
00169 std::swap (roots (0), roots (1));
00170 }
00171
00172 if (roots(0) < 0)
00173 computeRoots2 (c2, c1, roots);
00174 }
00175 }
00176
00177 template<typename Matrix, typename Vector> void
00178 eigen33 (const Matrix& mat, Matrix& evecs, Vector& evals)
00179 {
00180 typedef typename Matrix::Scalar Scalar;
00181
00182
00183
00184 Scalar scale = mat.cwiseAbs ().maxCoeff ();
00185 Matrix scaledMat = mat / scale;
00186
00187
00188 computeRoots (scaledMat,evals);
00189
00190 Matrix tmp;
00191 tmp = scaledMat;
00192 tmp.diagonal ().array () -= evals (2);
00193
00194 Vector vec1 = tmp.row (0).cross (tmp.row (1));
00195 Vector vec2 = tmp.row (0).cross (tmp.row (2));
00196 Vector vec3 = tmp.row (1).cross (tmp.row (2));
00197
00198 Scalar len1 = vec1.squaredNorm ();
00199 Scalar len2 = vec2.squaredNorm ();
00200 Scalar len3 = vec3.squaredNorm ();
00201
00202 Scalar *mmax = new Scalar[3];
00203 unsigned int min_el = 2;
00204 unsigned int max_el = 2;
00205
00206 if (len1 >= len2 && len1 >= len3)
00207 {
00208 mmax[2] = len1;
00209 evecs.col (2) = vec1 / Eigen::internal::sqrt (len1);
00210 }
00211 else if (len2 >= len1 && len2 >= len3)
00212 {
00213 mmax[2] = len2;
00214 evecs.col (2) = vec2 / Eigen::internal::sqrt (len2);
00215 }
00216 else
00217 {
00218 mmax[2] = len3;
00219 evecs.col (2) = vec3 / Eigen::internal::sqrt (len3);
00220 }
00221
00222 tmp = scaledMat;
00223 tmp.diagonal ().array () -= evals (1);
00224
00225 vec1 = tmp.row (0).cross (tmp.row (1));
00226 vec2 = tmp.row (0).cross (tmp.row (2));
00227 vec3 = tmp.row (1).cross (tmp.row (2));
00228
00229 len1 = vec1.squaredNorm ();
00230 len2 = vec2.squaredNorm ();
00231 len3 = vec3.squaredNorm ();
00232
00233 if (len1 >= len2 && len1 >= len3)
00234 {
00235 mmax[1] = len1;
00236 evecs.col (1) = vec1 / Eigen::internal::sqrt (len1);
00237 min_el = len1 < mmax[min_el]? 1: min_el;
00238 max_el = len1 > mmax[max_el]? 1: max_el;
00239 }
00240 else if (len2 >= len1 && len2 >= len3)
00241 {
00242 mmax[1] = len2;
00243 evecs.col (1) = vec2 / Eigen::internal::sqrt (len2);
00244 min_el = len2 < mmax[min_el]? 1: min_el;
00245 max_el = len2 > mmax[max_el]? 1: max_el;
00246 }
00247 else
00248 {
00249 mmax[1] = len3;
00250 evecs.col (1) = vec3 / Eigen::internal::sqrt (len3);
00251 min_el = len3 < mmax[min_el]? 1: min_el;
00252 max_el = len3 > mmax[max_el]? 1: max_el;
00253 }
00254
00255 tmp = scaledMat;
00256 tmp.diagonal ().array () -= evals (0);
00257
00258 vec1 = tmp.row (0).cross (tmp.row (1));
00259 vec2 = tmp.row (0).cross (tmp.row (2));
00260 vec3 = tmp.row (1).cross (tmp.row (2));
00261
00262 len1 = vec1.squaredNorm ();
00263 len2 = vec2.squaredNorm ();
00264 len3 = vec3.squaredNorm ();
00265
00266 if (len1 >= len2 && len1 >= len3)
00267 {
00268 mmax[0] = len1;
00269 evecs.col (0) = vec1 / Eigen::internal::sqrt (len1);
00270 min_el = len3 < mmax[min_el]? 0: min_el;
00271 max_el = len3 > mmax[max_el]? 0: max_el;
00272 }
00273 else if (len2 >= len1 && len2 >= len3)
00274 {
00275 mmax[0] = len2;
00276 evecs.col (0) = vec2 / Eigen::internal::sqrt (len2);
00277 min_el = len3 < mmax[min_el]? 0: min_el;
00278 max_el = len3 > mmax[max_el]? 0: max_el;
00279 }
00280 else
00281 {
00282 mmax[0] = len3;
00283 evecs.col (0) = vec3 / Eigen::internal::sqrt (len3);
00284 min_el = len3 < mmax[min_el]? 0: min_el;
00285 max_el = len3 > mmax[max_el]? 0: max_el;
00286 }
00287
00288
00289 if (mmax[min_el] <= std::numeric_limits<Scalar>::min ())
00290 {
00291 unsigned mid_el = 3 - min_el - max_el;
00292 if (mmax[mid_el] <= std::numeric_limits<Scalar>::min ())
00293 evecs.col (mid_el) = evecs.col (max_el).unitOrthogonal ();
00294
00295 evecs.col (min_el) = evecs.col (max_el).cross (evecs.col (mid_el));
00296 }
00297
00298
00299 evals *= scale;
00300
00301 delete [] mmax;
00302 }
00303 }
00304
00305 #endif //#ifndef PCL_EIGEN_H_