38 #ifndef COAL_INTERNAL_TOOLS_H
39 #define COAL_INTERNAL_TOOLS_H
51 template <
typename Derived>
52 static inline typename Derived::Scalar
triple(
53 const Eigen::MatrixBase<Derived>& x,
const Eigen::MatrixBase<Derived>& y,
54 const Eigen::MatrixBase<Derived>& z) {
55 return x.derived().dot(
y.derived().cross(z.derived()));
58 template <
typename Derived1,
typename Derived2,
typename Derived3>
60 const Eigen::MatrixBase<Derived2>& _u,
61 const Eigen::MatrixBase<Derived3>& _v) {
62 typedef typename Derived1::Scalar T;
64 Eigen::MatrixBase<Derived1>& w =
const_cast<Eigen::MatrixBase<Derived1>&
>(_w);
65 Eigen::MatrixBase<Derived2>& u =
const_cast<Eigen::MatrixBase<Derived2>&
>(_u);
66 Eigen::MatrixBase<Derived3>&
v =
const_cast<Eigen::MatrixBase<Derived3>&
>(_v);
69 if (std::abs(w[0]) >= std::abs(w[1])) {
70 inv_length = (T)1.0 / sqrt(w[0] * w[0] + w[2] * w[2]);
71 u[0] = -w[2] * inv_length;
73 u[2] = w[0] * inv_length;
75 v[1] = w[2] * u[0] - w[0] * u[2];
78 inv_length = (T)1.0 / sqrt(w[1] * w[1] + w[2] * w[2]);
80 u[1] = w[2] * inv_length;
81 u[2] = -w[1] * inv_length;
82 v[0] = w[1] * u[2] - w[2] * u[1];
89 template <
typename Derived,
typename OtherDerived>
91 const Eigen::MatrixBase<OtherDerived>& t1,
92 const Eigen::MatrixBase<Derived>& R2,
93 const Eigen::MatrixBase<OtherDerived>& t2,
94 const Eigen::MatrixBase<Derived>& R,
95 const Eigen::MatrixBase<OtherDerived>& t) {
96 const_cast<Eigen::MatrixBase<Derived>&
>(
R) = R1.transpose() * R2;
97 const_cast<Eigen::MatrixBase<OtherDerived>&
>(
t) = R1.transpose() * (
t2 - t1);
102 template <
typename Derived,
typename Vector>
103 void eigen(
const Eigen::MatrixBase<Derived>&
m,
104 typename Derived::Scalar dout[3], Vector* vout) {
105 typedef typename Derived::Scalar S;
106 Derived
R(
m.derived());
109 S tresh, theta, tau,
t, sm, s, h, g,
c;
113 S
v[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
116 for (ip = 0; ip < n; ++ip) {
117 b[ip] =
d[ip] =
R(ip, ip);
121 for (i = 0; i < 50; ++i) {
123 for (ip = 0; ip < n; ++ip)
124 for (iq = ip + 1; iq < n; ++iq) sm += std::abs(
R(ip, iq));
126 vout[0] <<
v[0][0],
v[0][1],
v[0][2];
127 vout[1] <<
v[1][0],
v[1][1],
v[1][2];
128 vout[2] <<
v[2][0],
v[2][1],
v[2][2];
136 tresh = 0.2 * sm / (n * n);
140 for (ip = 0; ip < n; ++ip) {
141 for (iq = ip + 1; iq < n; ++iq) {
142 g = 100.0 * std::abs(
R(ip, iq));
143 if (i > 3 && std::abs(
d[ip]) + g == std::abs(
d[ip]) &&
144 std::abs(
d[iq]) + g == std::abs(
d[iq]))
146 else if (std::abs(
R(ip, iq)) > tresh) {
148 if (std::abs(h) + g == std::abs(h))
151 theta = 0.5 * h / (
R(ip, iq));
152 t = 1.0 / (std::abs(theta) + std::sqrt(1.0 + theta * theta));
153 if (theta < 0.0)
t = -
t;
155 c = 1.0 / std::sqrt(1 +
t *
t);
164 for (j = 0; j < ip; ++j) {
167 R(j, ip) = g - s * (h + g * tau);
168 R(j, iq) = h + s * (g - h * tau);
170 for (j = ip + 1; j < iq; ++j) {
173 R(ip, j) = g - s * (h + g * tau);
174 R(j, iq) = h + s * (g - h * tau);
176 for (j = iq + 1; j < n; ++j) {
179 R(ip, j) = g - s * (h + g * tau);
180 R(iq, j) = h + s * (g - h * tau);
182 for (j = 0; j < n; ++j) {
185 v[j][ip] = g - s * (h + g * tau);
186 v[j][iq] = h + s * (g - h * tau);
191 for (ip = 0; ip < n; ++ip) {
198 std::cerr <<
"eigen: too many iterations in Jacobi transform." << std::endl;
203 template <
typename Derived,
typename OtherDerived>
204 bool isEqual(
const Eigen::MatrixBase<Derived>& lhs,
205 const Eigen::MatrixBase<OtherDerived>& rhs,
208 return ((lhs - rhs).array().abs() < tol).all();