Pose.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#define EIGEN_NO_STATIC_ASSERT
25#include "Pose.h"
26
27#include <sstream>
28
29#include <Eigen/Core>
30#include <Eigen/Geometry>
31
32#include <VirtualRobot/LinkedCoordinate.h>
33#include <VirtualRobot/Robot.h>
34#include <VirtualRobot/VirtualRobot.h>
35
38
39template class ::IceInternal::Handle<::armarx::Pose>;
40template class ::IceInternal::Handle<::armarx::Vector2>;
41template class ::IceInternal::Handle<::armarx::Vector3>;
42template class ::IceInternal::Handle<::armarx::Quaternion>;
43
44namespace armarx
45{
47 {
48 x = 0;
49 y = 0;
50 }
51
52 Vector2::Vector2(const Eigen::Vector2f& v)
53 {
54 x = v(0);
55 y = v(1);
56 }
57
58 Vector2::Vector2(::Ice::Float x, ::Ice::Float y)
59 {
60 this->x = x;
61 this->y = y;
62 }
63
64 Eigen::Vector2f
66 {
67 Eigen::Vector2f v;
68 v << this->x, this->y;
69 return v;
70 }
71
72 void
73 Vector2::operator=(const Eigen::Vector2f& vec)
74 {
75 x = vec[0];
76 y = vec[1];
77 }
78
79 std::string
80 Vector2::output(const Ice::Current&) const
81 {
82 std::stringstream s;
83 s << toEigen();
84 return s.str();
85 }
86
87 void
88 Vector2::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
89 {
90 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
91
92 obj->setFloat("x", x);
93 obj->setFloat("y", y);
94 }
95
96 void
97 Vector2::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&)
98 {
99 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
100
101 x = obj->getFloat("x");
102 y = obj->getFloat("y");
103 }
104
106 {
107 x = 0;
108 y = 0;
109 z = 0;
110 }
111
112 Vector3::Vector3(const Eigen::Vector3f& v)
113 {
114 x = v(0);
115 y = v(1);
116 z = v(2);
117 }
118
119 Vector3::Vector3(const Eigen::Matrix4f& m)
120 {
121 x = m(0, 3);
122 y = m(1, 3);
123 z = m(2, 3);
124 }
125
126 Vector3::Vector3(::Ice::Float x, ::Ice::Float y, ::Ice::Float z)
127 {
128 this->x = x;
129 this->y = y;
130 this->z = z;
131 }
132
133 Eigen::Vector3f
135 {
136 Eigen::Vector3f v;
137 v << this->x, this->y, this->z;
138 return v;
139 }
140
141 void
142 Vector3::operator=(const Eigen::Vector3f& vec)
143 {
144 x = vec[0];
145 y = vec[1];
146 z = vec[2];
147 }
148
149 std::string
150 Vector3::output(const Ice::Current&) const
151 {
152 std::stringstream s;
153 s << toEigen().format(ArmarXEigenFormat);
154 return s.str();
155 }
156
157 void
158 Vector3::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
159 {
160 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
161
162 obj->setFloat("x", x);
163 obj->setFloat("y", y);
164 obj->setFloat("z", z);
165 }
166
167 void
168 Vector3::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
169 {
170 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
171
172 x = obj->getFloat("x");
173 y = obj->getFloat("y");
174 z = obj->getFloat("z");
175 }
176
177 Quaternion::Quaternion() = default;
179 Quaternion::Quaternion(const Eigen::Matrix4f& m4)
181 Eigen::Matrix3f m3 = m4.block<3, 3>(0, 0);
182 this->init(m3);
185 Quaternion::Quaternion(const Eigen::Matrix3f& m3)
186 {
187 this->init(m3);
188 }
189
191 {
192 this->init(q);
193 }
194
195 Quaternion::Quaternion(::Ice::Float qw, ::Ice::Float qx, ::Ice::Float qy, ::Ice::Float qz)
196 {
197 this->qw = qw;
198 this->qx = qx;
199 this->qy = qy;
200 this->qz = qz;
201 }
203 Eigen::Matrix3f
205 {
206 return toEigenQuaternion().toRotationMatrix();
207 }
208
211 {
212 return {this->qw, this->qx, this->qy, this->qz};
213 }
214
215 void
216 Quaternion::init(const Eigen::Matrix3f& m3)
217 {
219 q = m3;
220 init(q);
221 }
222
223 void
224 Quaternion::init(const Eigen::Quaternionf& q)
225 {
226 this->qw = q.w();
227 this->qx = q.x();
228 this->qy = q.y();
229 this->qz = q.z();
230 }
231
232 Eigen::Matrix3f
233 Quaternion::slerp(float alpha, const Eigen::Matrix3f& m)
234 {
235 return Quaternion::slerp(alpha, this->toEigen(), m);
236 }
237
238 Eigen::Matrix3f
239 Quaternion::slerp(float alpha, const Eigen::Matrix3f& m1, const Eigen::Matrix3f& m2)
240 {
241 Eigen::Matrix3f result;
242 Eigen::Quaternionf q1, q2;
243 q1 = m1;
244 q2 = m2;
245 result = q1.slerp(alpha, q2);
246 return result;
247 }
248
249 std::string
250 Quaternion::output(const Ice::Current& c) const
251 {
252 std::stringstream s;
253 s << toEigen() /*.format(ArmarXEigenFormat)*/;
254 return s.str();
255 }
256
257 void
258 Quaternion::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
259 {
260 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
261
262 obj->setFloat("qx", qx);
263 obj->setFloat("qy", qy);
264 obj->setFloat("qz", qz);
265 obj->setFloat("qw", qw);
266 }
267
268 void
269 Quaternion::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
270 {
271 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
272
273 qx = obj->getFloat("qx");
274 qy = obj->getFloat("qy");
275 qz = obj->getFloat("qz");
276 qw = obj->getFloat("qw");
277 }
278
279 Pose::Pose(const Eigen::Matrix3f& m, const Eigen::Vector3f& v)
280 {
281 this->orientation = new Quaternion(m);
282 this->position = new Vector3(v);
283 init();
284 }
285
286 Pose::Pose(const armarx::Vector3BasePtr pos, const armarx::QuaternionBasePtr ori)
287 {
288 this->orientation = new Quaternion(*QuaternionPtr::dynamicCast(ori));
289 this->position = new Vector3(*Vector3Ptr::dynamicCast(pos));
290 init();
291 }
292
294 {
295 this->orientation = new Quaternion();
296 this->position = new Vector3();
297 init();
298 }
299
300 Pose::Pose(const Pose& source) :
301 IceUtil::Shared(source),
302 armarx::Serializable(source),
303 armarx::VariantDataClass(source),
304 PoseBase(source)
305 {
306 orientation = QuaternionBasePtr::dynamicCast(source.orientation->clone());
307 position = Vector3Ptr::dynamicCast(source.position->clone());
308 init();
309 }
310
311 Pose::Pose(const Eigen::Matrix4f& m)
312 {
313 this->orientation = new Quaternion(m);
314 this->position = new Vector3(m);
315 init();
316 }
317
318 Pose::Pose(const Eigen::Vector3f& position, const Eigen::Quaternionf& quaternion)
319 {
320 this->position = new Vector3(position);
321 this->orientation = new Quaternion(quaternion);
322 init();
323 }
324
325 void
326 Pose::operator=(const Eigen::Matrix4f& matrix)
327 {
328 this->orientation = new Quaternion(matrix);
329 this->position = new Vector3(matrix);
330 init();
331 }
332
333 Eigen::Matrix4f
335 {
336 Eigen::Matrix4f m = Eigen::Matrix4f::Identity();
337 ARMARX_CHECK_EXPRESSION(c_orientation);
338 ARMARX_CHECK_EXPRESSION(c_position);
339 m.block<3, 3>(0, 0) = c_orientation->toEigen();
340 m.block<3, 1>(0, 3) = c_position->toEigen();
341 return m;
342 }
343
344 std::string
345 Pose::output(const Ice::Current&) const
346 {
347 std::stringstream s;
348 s << toEigen() /*.format(ArmarXEigenFormat)*/;
349 return s.str();
350 }
351
352 void
353 Pose::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c) const
354 {
355 position->serialize(serializer, c);
356 orientation->serialize(serializer, c);
357 }
358
359 void
360 Pose::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
361 {
362 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
363
364 position->deserialize(obj);
365 orientation->deserialize(obj);
366 }
367
368 void
370 {
371 this->c_position = Vector3Ptr::dynamicCast(this->position);
372 this->c_orientation = QuaternionPtr::dynamicCast(this->orientation);
373 }
374
375 void
377 {
378 init();
379 }
380
381} // namespace armarx
382
383void
384armarx::fromIce(const Vector3BasePtr& ice, Eigen::Vector3f& vector)
385{
386 vector = fromIce(ice);
387}
388
389void
390armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Quaternionf& quaternion)
391{
392 quaternion = fromIce(ice);
393}
394
395void
396armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Matrix3f& rotation)
397{
398 rotation = fromIce(ice).toRotationMatrix();
399}
400
401void
402armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Matrix<float, 3, 3, Eigen::RowMajor>& rotation)
403{
404 Eigen::Matrix3f helper;
405 fromIce(ice, helper);
406 rotation = helper;
407}
408
409void
410armarx::fromIce(const PoseBasePtr& ice, Eigen::Matrix4f& pose)
411{
412 pose = fromIce(ice);
413}
414
415void
417{
418 Eigen::Matrix4f helper;
419 fromIce(ice, helper);
420 pose = helper;
421}
422
423Eigen::Vector3f
424armarx::fromIce(const Vector3BasePtr& pose)
425{
426 auto cast = Vector3Ptr::dynamicCast(pose);
427 ARMARX_CHECK_NOT_NULL(cast) << "Vector3BasePtr must be a Vector3Ptr.";
428 return cast->toEigen();
429}
430
432armarx::fromIce(const QuaternionBasePtr& pose)
433{
434 auto cast = QuaternionPtr::dynamicCast(pose);
435 ARMARX_CHECK_NOT_NULL(cast) << "QuaternionBasePtr must be a QuaternionPtr.";
436 return cast->toEigenQuaternion();
437}
438
439Eigen::Matrix4f
440armarx::fromIce(const PoseBasePtr& pose)
441{
442 auto cast = PosePtr::dynamicCast(pose);
443 ARMARX_CHECK_NOT_NULL(cast) << "PoseBasePtr must be a PosePtr.";
444 return cast->toEigen();
445}
446
447void
448armarx::toIce(Vector3BasePtr& ice, const Eigen::Vector3f& vector)
449{
450 ice = new Vector3(vector);
451}
452
453void
454armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix3f& rotation)
455{
456 ice = new Quaternion(rotation);
457}
458
459void
460armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>& rotation)
461{
462 Eigen::Matrix3f helper = rotation;
463 toIce(ice, helper);
464}
465
466void
467armarx::toIce(QuaternionBasePtr& ice, const Eigen::Quaternionf& quaternion)
468{
469 ice = new Quaternion(quaternion);
470}
471
472void
473armarx::toIce(PoseBasePtr& ice, const Eigen::Matrix4f& pose)
474{
475 ice = new Pose(pose);
476}
477
478void
480{
481 Eigen::Matrix4f helper = pose;
482 toIce(ice, helper);
483}
484
486armarx::toIce(const Eigen::Vector3f& vector)
487{
488 return new Vector3(vector);
489}
490
492armarx::toIce(const Eigen::Matrix3f& rotation)
493{
494 return new Quaternion(rotation);
495}
496
499{
500 Eigen::Matrix3f helper = rotation;
501 return toIce(helper);
502}
503
506{
507 return new Quaternion(quaternion);
508}
509
511armarx::toIce(const Eigen::Matrix4f& pose)
512{
513 return new Pose(pose);
514}
515
518{
519 Eigen::Matrix4f helper = pose;
520 return toIce(helper);
521}
522
524armarx::toIce(const Eigen::Isometry3f& pose)
525{
526 return armarx::toIce(pose.matrix());
527}
constexpr T c
The Pose class.
Definition Pose.h:243
void init()
Definition Pose.cpp:369
void ice_postUnmarshal() override
Definition Pose.cpp:376
Pose & operator=(const Pose &)=default
std::string output(const Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:345
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition Pose.cpp:360
virtual Eigen::Matrix4f toEigen() const
Definition Pose.cpp:334
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:353
The Quaternion class.
Definition Pose.h:174
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition Pose.cpp:250
Eigen::Matrix3f toEigen() const
Definition Pose.cpp:204
Quaternion()
Construct an identity quaternion.
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition Pose.cpp:269
Eigen::Quaternionf toEigenQuaternion() const
Definition Pose.cpp:210
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:258
Eigen::Matrix3f slerp(float, const Eigen::Matrix3f &)
Definition Pose.cpp:233
virtual Eigen::Vector2f toEigen() const
Definition Pose.cpp:65
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition Pose.cpp:80
void operator=(const Eigen::Vector2f &ves)
Definition Pose.cpp:73
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition Pose.cpp:97
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:88
The Vector3 class.
Definition Pose.h:113
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition Pose.cpp:150
void operator=(const Eigen::Vector3f &vec)
Definition Pose.cpp:142
virtual Eigen::Vector3f toEigen() const
Definition Pose.cpp:134
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition Pose.cpp:168
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:158
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define q
Quaternion< float, 0 > Quaternionf
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Vector3 > Vector3Ptr
Definition Pose.h:165
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
const Eigen::IOFormat ArmarXEigenFormat(Eigen::StreamPrecision, Eigen::DontAlignCols, " ", "\n", "", "")
IceInternal::Handle< AbstractObjectSerializer > AbstractObjectSerializerPtr
IceInternal::Handle< Quaternion > QuaternionPtr
Definition Pose.h:234
void fromIce(const std::map< IceKeyT, IceValueT > &iceMap, boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)