coal 3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
tools.h
Go to the documentation of this file.
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011-2014, Willow Garage, Inc.
5 * Copyright (c) 2014-2015, Open Source Robotics Foundation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of Open Source Robotics Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
38#ifndef COAL_INTERNAL_TOOLS_H
39#define COAL_INTERNAL_TOOLS_H
40
41#include "coal/fwd.hh"
42
43#include <cmath>
44#include <iostream>
45#include <limits>
46
47#include "coal/data_types.h"
48
49namespace coal {
50
51template <typename Derived>
52static 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()));
56}
57
58template <typename Derived1, typename Derived2, typename Derived3>
59void generateCoordinateSystem(const Eigen::MatrixBase<Derived1>& _w,
60 const Eigen::MatrixBase<Derived2>& _u,
61 const Eigen::MatrixBase<Derived3>& _v) {
62 typedef typename Derived1::Scalar T;
63
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);
67
68 T inv_length;
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;
72 u[1] = (T)0;
73 u[2] = w[0] * inv_length;
74 v[0] = w[1] * u[2];
75 v[1] = w[2] * u[0] - w[0] * u[2];
76 v[2] = -w[1] * u[0];
77 } else {
78 inv_length = (T)1.0 / sqrt(w[1] * w[1] + w[2] * w[2]);
79 u[0] = (T)0;
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];
83 v[1] = -w[0] * u[2];
84 v[2] = w[0] * u[1];
85 }
86}
87
88/* ----- Start Matrices ------ */
89template <typename Derived, typename OtherDerived>
90void relativeTransform(const Eigen::MatrixBase<Derived>& R1,
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);
98}
99
102template <typename Derived, typename Vector>
103void 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());
107 int n = 3;
108 int j, iq, ip, i;
109 S tresh, theta, tau, t, sm, s, h, g, c;
110
111 S b[3];
112 S z[3];
113 S v[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
114 S d[3];
115
116 for (ip = 0; ip < n; ++ip) {
117 b[ip] = d[ip] = R(ip, ip);
118 z[ip] = 0;
119 }
120
121 for (i = 0; i < 50; ++i) {
122 sm = 0;
123 for (ip = 0; ip < n; ++ip)
124 for (iq = ip + 1; iq < n; ++iq) sm += std::abs(R(ip, iq));
125 if (sm == 0.0) {
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];
129 dout[0] = d[0];
130 dout[1] = d[1];
131 dout[2] = d[2];
132 return;
133 }
134
135 if (i < 3)
136 tresh = Scalar(0.2 * sm / (n * n));
137 else
138 tresh = 0.0;
139
140 for (ip = 0; ip < n; ++ip) {
141 for (iq = ip + 1; iq < n; ++iq) {
142 g = Scalar(100) * 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]))
145 R(ip, iq) = 0.0;
146 else if (std::abs(R(ip, iq)) > tresh) {
147 h = d[iq] - d[ip];
148 if (std::abs(h) + g == std::abs(h))
149 t = (R(ip, iq)) / h;
150 else {
151 theta = Scalar(0.5) * h / (R(ip, iq));
152 t = 1 / (std::abs(theta) + std::sqrt(1 + theta * theta));
153 if (theta < 0.0) t = -t;
154 }
155 c = 1 / std::sqrt(1 + t * t);
156 s = t * c;
157 tau = s / (1 + c);
158 h = t * R(ip, iq);
159 z[ip] -= h;
160 z[iq] += h;
161 d[ip] -= h;
162 d[iq] += h;
163 R(ip, iq) = 0.0;
164 for (j = 0; j < ip; ++j) {
165 g = R(j, ip);
166 h = R(j, iq);
167 R(j, ip) = g - s * (h + g * tau);
168 R(j, iq) = h + s * (g - h * tau);
169 }
170 for (j = ip + 1; j < iq; ++j) {
171 g = R(ip, j);
172 h = R(j, iq);
173 R(ip, j) = g - s * (h + g * tau);
174 R(j, iq) = h + s * (g - h * tau);
175 }
176 for (j = iq + 1; j < n; ++j) {
177 g = R(ip, j);
178 h = R(iq, j);
179 R(ip, j) = g - s * (h + g * tau);
180 R(iq, j) = h + s * (g - h * tau);
181 }
182 for (j = 0; j < n; ++j) {
183 g = v[j][ip];
184 h = v[j][iq];
185 v[j][ip] = g - s * (h + g * tau);
186 v[j][iq] = h + s * (g - h * tau);
187 }
188 }
189 }
190 }
191 for (ip = 0; ip < n; ++ip) {
192 b[ip] += z[ip];
193 d[ip] = b[ip];
194 z[ip] = 0.0;
195 }
196 }
197
198 std::cerr << "eigen: too many iterations in Jacobi transform." << std::endl;
199
200 return;
201}
202
203template <typename Derived, typename OtherDerived>
204bool isEqual(const Eigen::MatrixBase<Derived>& lhs,
205 const Eigen::MatrixBase<OtherDerived>& rhs,
206 const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 100) {
207 return ((lhs - rhs).array().abs() < tol).all();
208}
209
210} // namespace coal
211
212#endif
Main namespace.
Definition broadphase_bruteforce.h:44
void generateCoordinateSystem(const Eigen::MatrixBase< Derived1 > &_w, const Eigen::MatrixBase< Derived2 > &_u, const Eigen::MatrixBase< Derived3 > &_v)
Definition tools.h:59
void eigen(const Eigen::MatrixBase< Derived > &m, typename Derived::Scalar dout[3], Vector *vout)
compute the eigen vector and eigen vector of a matrix. dout is the eigen values, vout is the eigen ve...
Definition tools.h:103
double Scalar
Definition data_types.h:68
bool isEqual(const Eigen::MatrixBase< Derived > &lhs, const Eigen::MatrixBase< OtherDerived > &rhs, const Scalar tol=std::numeric_limits< Scalar >::epsilon() *100)
Definition tools.h:204
void relativeTransform(const Eigen::MatrixBase< Derived > &R1, const Eigen::MatrixBase< OtherDerived > &t1, const Eigen::MatrixBase< Derived > &R2, const Eigen::MatrixBase< OtherDerived > &t2, const Eigen::MatrixBase< Derived > &R, const Eigen::MatrixBase< OtherDerived > &t)
Definition tools.h:90