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
transform.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_TRANSFORM_H
39#define COAL_TRANSFORM_H
40
41#include "coal/fwd.hh"
42#include "coal/data_types.h"
43
44namespace coal {
45
46COAL_DEPRECATED typedef Eigen::Quaternion<Scalar> Quaternion3f;
47typedef Eigen::Quaternion<Scalar> Quats;
48
49static inline std::ostream& operator<<(std::ostream& o, const Quats& q) {
50 o << "(" << q.w() << " " << q.x() << " " << q.y() << " " << q.z() << ")";
51 return o;
52}
53
57 Matrix3s R;
58
60 Vec3s T;
61
62 public:
65 setIdentity(); // set matrix_set true
66 }
67
68 static Transform3s Identity() { return Transform3s(); }
69
71 template <typename Matrixx3Like, typename Vector3Like>
72 Transform3s(const Eigen::MatrixBase<Matrixx3Like>& R_,
73 const Eigen::MatrixBase<Vector3Like>& T_)
74 : R(R_), T(T_) {}
75
77 template <typename Vector3Like>
78 Transform3s(const Quats& q_, const Eigen::MatrixBase<Vector3Like>& T_)
79 : R(q_.toRotationMatrix()), T(T_) {}
80
82 Transform3s(const Matrix3s& R_) : R(R_), T(Vec3s::Zero()) {}
83
85 Transform3s(const Quats& q_) : R(q_), T(Vec3s::Zero()) {}
86
88 Transform3s(const Vec3s& T_) : R(Matrix3s::Identity()), T(T_) {}
89
91 Transform3s(const Transform3s& tf) : R(tf.R), T(tf.T) {}
92
95 R = tf.R;
96 T = tf.T;
97 return *this;
98 }
99
101 inline const Vec3s& getTranslation() const { return T; }
102
104 inline const Vec3s& translation() const { return T; }
105
107 inline Vec3s& translation() { return T; }
108
110 inline const Matrix3s& getRotation() const { return R; }
111
113 inline const Matrix3s& rotation() const { return R; }
114
116 inline Matrix3s& rotation() { return R; }
117
119 inline Quats getQuatRotation() const { return Quats(R); }
120
122 template <typename Matrix3Like, typename Vector3Like>
123 inline void setTransform(const Eigen::MatrixBase<Matrix3Like>& R_,
124 const Eigen::MatrixBase<Vector3Like>& T_) {
125 R.noalias() = R_;
126 T.noalias() = T_;
127 }
128
130 inline void setTransform(const Quats& q_, const Vec3s& T_) {
131 R = q_.toRotationMatrix();
132 T = T_;
133 }
134
136 template <typename Derived>
137 inline void setRotation(const Eigen::MatrixBase<Derived>& R_) {
138 R.noalias() = R_;
139 }
140
142 template <typename Derived>
143 inline void setTranslation(const Eigen::MatrixBase<Derived>& T_) {
144 T.noalias() = T_;
145 }
146
148 inline void setQuatRotation(const Quats& q_) { R = q_.toRotationMatrix(); }
149
151 template <typename Derived>
152 inline Vec3s transform(const Eigen::MatrixBase<Derived>& v) const {
153 return R * v + T;
154 }
155
157 template <typename Derived>
158 inline Vec3s inverseTransform(const Eigen::MatrixBase<Derived>& v) const {
159 return R.transpose() * (v - T);
160 }
161
164 R.transposeInPlace();
165 T = -R * T;
166 return *this;
167 }
168
171 return Transform3s(R.transpose(), -R.transpose() * T);
172 }
173
175 inline Transform3s inverseTimes(const Transform3s& other) const {
176 return Transform3s(R.transpose() * other.R, R.transpose() * (other.T - T));
177 }
178
180 inline const Transform3s& operator*=(const Transform3s& other) {
181 T += R * other.T;
182 R *= other.R;
183 return *this;
184 }
185
187 inline Transform3s operator*(const Transform3s& other) const {
188 return Transform3s(R * other.R, R * other.T + T);
189 }
190
192 inline bool isIdentity(
193 const Scalar& prec = Eigen::NumTraits<Scalar>::dummy_precision()) const {
194 return R.isIdentity(prec) && T.isZero(prec);
195 }
196
198 inline void setIdentity() {
199 R.setIdentity();
200 T.setZero();
201 }
202
206 tf.setRandom();
207 return tf;
208 }
209
211 inline void setRandom();
212
213 bool operator==(const Transform3s& other) const {
214 return (R == other.getRotation()) && (T == other.getTranslation());
215 }
216
217 bool operator!=(const Transform3s& other) const { return !(*this == other); }
218
219 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
220};
221
222template <typename Derived>
223inline Quats fromAxisAngle(const Eigen::MatrixBase<Derived>& axis,
224 Scalar angle) {
225 return Quats(Eigen::AngleAxis<Scalar>(angle, axis));
226}
227
231 // Rotational part
232 const Scalar u1 = (Scalar)rand() / Scalar(RAND_MAX);
233 const Scalar u2 = (Scalar)rand() / Scalar(RAND_MAX);
234 const Scalar u3 = (Scalar)rand() / Scalar(RAND_MAX);
235
236 const Scalar mult1 = std::sqrt(Scalar(1.0) - u1);
237 const Scalar mult2 = std::sqrt(u1);
238
239 static const Scalar PI_value = static_cast<Scalar>(EIGEN_PI);
240 Scalar s2 = std::sin(2 * PI_value * u2);
241 Scalar c2 = std::cos(2 * PI_value * u2);
242 Scalar s3 = std::sin(2 * PI_value * u3);
243 Scalar c3 = std::cos(2 * PI_value * u3);
244
245 Quats q;
246 q.w() = mult1 * s2;
247 q.x() = mult1 * c2;
248 q.y() = mult2 * s3;
249 q.z() = mult2 * c3;
250 return q;
251}
252
254 const Quats q = uniformRandomQuaternion();
255 this->rotation() = q.matrix();
256 this->translation().setRandom();
257}
258
262 Matrix3s basis = Matrix3s::Zero();
263 basis.col(2) = vec.normalized();
264 basis.col(1) = -vec.unitOrthogonal();
265 basis.col(0) = basis.col(1).cross(vec);
266 return basis;
267}
268
269} // namespace coal
270
271#endif
Simple transform class used locally by InterpMotion.
Definition transform.h:55
Transform3s(const Vec3s &T_)
Construct transform from translation.
Definition transform.h:88
void setRotation(const Eigen::MatrixBase< Derived > &R_)
set transform from rotation
Definition transform.h:137
Transform3s & inverseInPlace()
inverse transform
Definition transform.h:163
bool operator==(const Transform3s &other) const
Definition transform.h:213
bool operator!=(const Transform3s &other) const
Definition transform.h:217
Transform3s(const Quats &q_)
Construct transform from rotation.
Definition transform.h:85
const Matrix3s & rotation() const
get rotation
Definition transform.h:113
bool isIdentity(const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision()) const
check whether the transform is identity
Definition transform.h:192
static Transform3s Random()
return a random transform
Definition transform.h:204
Transform3s(const Eigen::MatrixBase< Matrixx3Like > &R_, const Eigen::MatrixBase< Vector3Like > &T_)
Construct transform from rotation and translation.
Definition transform.h:72
void setTranslation(const Eigen::MatrixBase< Derived > &T_)
set transform from translation
Definition transform.h:143
Vec3s transform(const Eigen::MatrixBase< Derived > &v) const
transform a given vector by the transform
Definition transform.h:152
const Vec3s & translation() const
get translation
Definition transform.h:104
Vec3s inverseTransform(const Eigen::MatrixBase< Derived > &v) const
transform a given vector by the inverse of the transform
Definition transform.h:158
void setQuatRotation(const Quats &q_)
set transform from rotation
Definition transform.h:148
Matrix3s & rotation()
get rotation
Definition transform.h:116
Transform3s(const Transform3s &tf)
Construct transform from other transform.
Definition transform.h:91
const Matrix3s & getRotation() const
get rotation
Definition transform.h:110
Vec3s & translation()
get translation
Definition transform.h:107
void setTransform(const Eigen::MatrixBase< Matrix3Like > &R_, const Eigen::MatrixBase< Vector3Like > &T_)
set transform from rotation and translation
Definition transform.h:123
static Transform3s Identity()
Definition transform.h:68
Transform3s(const Quats &q_, const Eigen::MatrixBase< Vector3Like > &T_)
Construct transform from rotation and translation.
Definition transform.h:78
Transform3s inverse()
inverse transform
Definition transform.h:170
Transform3s operator*(const Transform3s &other) const
multiply with another transform
Definition transform.h:187
Quats getQuatRotation() const
get quaternion
Definition transform.h:119
const Vec3s & getTranslation() const
get translation
Definition transform.h:101
void setIdentity()
set the transform to be identity transform
Definition transform.h:198
void setTransform(const Quats &q_, const Vec3s &T_)
set transform from rotation and translation
Definition transform.h:130
const Transform3s & operator*=(const Transform3s &other)
multiply with another transform
Definition transform.h:180
Transform3s()
Default transform is no movement.
Definition transform.h:64
void setRandom()
set the transform to a random transform
Definition transform.h:253
Transform3s & operator=(const Transform3s &tf)
operator =
Definition transform.h:94
Transform3s(const Matrix3s &R_)
Construct transform from rotation.
Definition transform.h:82
Transform3s inverseTimes(const Transform3s &other) const
inverse the transform and multiply with another
Definition transform.h:175
#define COAL_DLLAPI
Definition config.hh:88
#define COAL_DEPRECATED
Definition deprecated.hh:37
Main namespace.
Definition broadphase_bruteforce.h:44
Eigen::Quaternion< Scalar > Quats
Definition transform.h:47
Quats uniformRandomQuaternion()
Uniformly random quaternion sphere. Code taken from Pinocchio (https://github.com/stack-of-tasks/pino...
Definition transform.h:230
Matrix3s constructOrthonormalBasisFromVector(const Vec3s &vec)
Construct othonormal basis from vector. The z-axis is the normalized input vector.
Definition transform.h:261
Eigen::Quaternion< Scalar > Quaternion3f
Definition transform.h:46
Quats fromAxisAngle(const Eigen::MatrixBase< Derived > &axis, Scalar angle)
Definition transform.h:223
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition data_types.h:70
double Scalar
Definition data_types.h:68
Eigen::Matrix< Scalar, 3, 3 > Matrix3s
Definition data_types.h:74