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 ARMARX_CHECK_NOT_NULL(source.orientation) << "Pose being copied has a null orientation.";
307 ARMARX_CHECK_NOT_NULL(source.position) << "Pose being copied has a null position.";
308 orientation = QuaternionBasePtr::dynamicCast(source.orientation->clone());
309 position = Vector3Ptr::dynamicCast(source.position->clone());
310 ARMARX_CHECK_NOT_NULL(orientation) << "Cloned orientation is not a QuaternionBasePtr.";
311 ARMARX_CHECK_NOT_NULL(position) << "Cloned position is not a Vector3Ptr.";
312 init();
313 }
314
315 Pose::Pose(const Eigen::Matrix4f& m)
316 {
317 this->orientation = new Quaternion(m);
318 this->position = new Vector3(m);
319 init();
320 }
321
322 Pose::Pose(const Eigen::Vector3f& position, const Eigen::Quaternionf& quaternion)
323 {
324 this->position = new Vector3(position);
325 this->orientation = new Quaternion(quaternion);
326 init();
327 }
328
329 void
330 Pose::operator=(const Eigen::Matrix4f& matrix)
331 {
332 this->orientation = new Quaternion(matrix);
333 this->position = new Vector3(matrix);
334 init();
335 }
336
337 Eigen::Matrix4f
339 {
340 Eigen::Matrix4f m = Eigen::Matrix4f::Identity();
341 ARMARX_CHECK_EXPRESSION(c_orientation);
342 ARMARX_CHECK_EXPRESSION(c_position);
343 m.block<3, 3>(0, 0) = c_orientation->toEigen();
344 m.block<3, 1>(0, 3) = c_position->toEigen();
345 return m;
346 }
347
348 std::string
349 Pose::output(const Ice::Current&) const
350 {
351 std::stringstream s;
352 s << toEigen() /*.format(ArmarXEigenFormat)*/;
353 return s.str();
354 }
355
356 void
357 Pose::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c) const
358 {
359 position->serialize(serializer, c);
360 orientation->serialize(serializer, c);
361 }
362
363 void
364 Pose::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
365 {
366 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
367
368 position->deserialize(obj);
369 orientation->deserialize(obj);
370 }
371
372 void
374 {
375 this->c_position = Vector3Ptr::dynamicCast(this->position);
376 this->c_orientation = QuaternionPtr::dynamicCast(this->orientation);
377 }
378
379 void
381 {
382 init();
383 }
384
385} // namespace armarx
386
387void
388armarx::fromIce(const Vector3BasePtr& ice, Eigen::Vector3f& vector)
389{
390 vector = fromIce(ice);
391}
392
393void
394armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Quaternionf& quaternion)
395{
396 quaternion = fromIce(ice);
397}
398
399void
400armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Matrix3f& rotation)
401{
402 rotation = fromIce(ice).toRotationMatrix();
403}
404
405void
406armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Matrix<float, 3, 3, Eigen::RowMajor>& rotation)
407{
408 Eigen::Matrix3f helper;
409 fromIce(ice, helper);
410 rotation = helper;
411}
412
413void
414armarx::fromIce(const PoseBasePtr& ice, Eigen::Matrix4f& pose)
415{
416 pose = fromIce(ice);
417}
418
419void
421{
422 Eigen::Matrix4f helper;
423 fromIce(ice, helper);
424 pose = helper;
425}
426
427Eigen::Vector3f
428armarx::fromIce(const Vector3BasePtr& pose)
429{
430 auto cast = Vector3Ptr::dynamicCast(pose);
431 ARMARX_CHECK_NOT_NULL(cast) << "Vector3BasePtr must be a Vector3Ptr.";
432 return cast->toEigen();
433}
434
436armarx::fromIce(const QuaternionBasePtr& pose)
437{
438 auto cast = QuaternionPtr::dynamicCast(pose);
439 ARMARX_CHECK_NOT_NULL(cast) << "QuaternionBasePtr must be a QuaternionPtr.";
440 return cast->toEigenQuaternion();
441}
442
443Eigen::Matrix4f
444armarx::fromIce(const PoseBasePtr& pose)
445{
446 auto cast = PosePtr::dynamicCast(pose);
447 ARMARX_CHECK_NOT_NULL(cast) << "PoseBasePtr must be a PosePtr.";
448 return cast->toEigen();
449}
450
451void
452armarx::toIce(Vector3BasePtr& ice, const Eigen::Vector3f& vector)
453{
454 ice = new Vector3(vector);
455}
456
457void
458armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix3f& rotation)
459{
460 ice = new Quaternion(rotation);
461}
462
463void
464armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>& rotation)
465{
466 Eigen::Matrix3f helper = rotation;
467 toIce(ice, helper);
468}
469
470void
471armarx::toIce(QuaternionBasePtr& ice, const Eigen::Quaternionf& quaternion)
472{
473 ice = new Quaternion(quaternion);
474}
475
476void
477armarx::toIce(PoseBasePtr& ice, const Eigen::Matrix4f& pose)
478{
479 ice = new Pose(pose);
480}
481
482void
484{
485 Eigen::Matrix4f helper = pose;
486 toIce(ice, helper);
487}
488
490armarx::toIce(const Eigen::Vector3f& vector)
491{
492 return new Vector3(vector);
493}
494
496armarx::toIce(const Eigen::Matrix3f& rotation)
497{
498 return new Quaternion(rotation);
499}
500
503{
504 Eigen::Matrix3f helper = rotation;
505 return toIce(helper);
506}
507
510{
511 return new Quaternion(quaternion);
512}
513
515armarx::toIce(const Eigen::Matrix4f& pose)
516{
517 return new Pose(pose);
518}
519
522{
523 Eigen::Matrix4f helper = pose;
524 return toIce(helper);
525}
526
528armarx::toIce(const Eigen::Isometry3f& pose)
529{
530 return armarx::toIce(pose.matrix());
531}
constexpr T c
The Pose class.
Definition Pose.h:243
void init()
Definition Pose.cpp:373
void ice_postUnmarshal() override
Definition Pose.cpp:380
Pose & operator=(const Pose &)=default
std::string output(const Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:349
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition Pose.cpp:364
virtual Eigen::Matrix4f toEigen() const
Definition Pose.cpp:338
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition Pose.cpp:357
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)