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 
39 template class ::IceInternal::Handle<::armarx::Pose>;
40 template class ::IceInternal::Handle<::armarx::Vector2>;
41 template class ::IceInternal::Handle<::armarx::Vector3>;
42 template class ::IceInternal::Handle<::armarx::Quaternion>;
43 
44 namespace 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 
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 
120  {
121  x = m(0, 3);
122  y = m(1, 3);
123  z = m(2, 3);
124  }
125 
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;
178 
180  {
181  Eigen::Matrix3f m3 = m4.block<3, 3>(0, 0);
182  this->init(m3);
183  }
184 
186  {
187  this->init(m3);
188  }
189 
191  {
192  this->init(q);
193  }
194 
196  {
197  this->qw = qw;
198  this->qx = qx;
199  this->qy = qy;
200  this->qz = qz;
201  }
202 
205  {
206  return toEigenQuaternion().toRotationMatrix();
207  }
208 
210  Quaternion::toEigenQuaternion() const
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 
233  Quaternion::slerp(float alpha, const Eigen::Matrix3f& m)
234  {
235  return Quaternion::slerp(alpha, this->toEigen(), m);
236  }
237 
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 
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 
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
327  {
328  this->orientation = new Quaternion(matrix);
329  this->position = new Vector3(matrix);
330  init();
331  }
332 
335  {
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 
383 void
384 armarx::fromIce(const Vector3BasePtr& ice, Eigen::Vector3f& vector)
385 {
386  vector = fromIce(ice);
387 }
388 
389 void
390 armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Quaternionf& quaternion)
391 {
392  quaternion = fromIce(ice);
393 }
394 
395 void
396 armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Matrix3f& rotation)
397 {
398  rotation = fromIce(ice).toRotationMatrix();
399 }
400 
401 void
402 armarx::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 
409 void
410 armarx::fromIce(const PoseBasePtr& ice, Eigen::Matrix4f& pose)
411 {
412  pose = fromIce(ice);
413 }
414 
415 void
417 {
418  Eigen::Matrix4f helper;
419  fromIce(ice, helper);
420  pose = helper;
421 }
422 
423 Eigen::Vector3f
424 armarx::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 
432 armarx::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 
440 armarx::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 
447 void
448 armarx::toIce(Vector3BasePtr& ice, const Eigen::Vector3f& vector)
449 {
450  ice = new Vector3(vector);
451 }
452 
453 void
454 armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix3f& rotation)
455 {
456  ice = new Quaternion(rotation);
457 }
458 
459 void
460 armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>& rotation)
461 {
462  Eigen::Matrix3f helper = rotation;
463  toIce(ice, helper);
464 }
465 
466 void
467 armarx::toIce(QuaternionBasePtr& ice, const Eigen::Quaternionf& quaternion)
468 {
469  ice = new Quaternion(quaternion);
470 }
471 
472 void
473 armarx::toIce(PoseBasePtr& ice, const Eigen::Matrix4f& pose)
474 {
475  ice = new Pose(pose);
476 }
477 
478 void
480 {
481  Eigen::Matrix4f helper = pose;
482  toIce(ice, helper);
483 }
484 
486 armarx::toIce(const Eigen::Vector3f& vector)
487 {
488  return new Vector3(vector);
489 }
490 
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 
512 {
513  return new Pose(pose);
514 }
515 
518 {
519  Eigen::Matrix4f helper = pose;
520  return toIce(helper);
521 }
522 
524 armarx::toIce(const Eigen::Isometry3f& pose)
525 {
526  return armarx::toIce(pose.matrix());
527 }
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::Pose::Pose
Pose()
Definition: Pose.cpp:293
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::conversions::cast
CvT cast(const auto &pt)
Definition: opencv.h:32
Pose.h
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
AbstractObjectSerializer.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::Pose::init
void init()
Definition: Pose.cpp:369
armarx::VariantType::Vector2
const VariantTypeId Vector2
Definition: Pose.h:37
IceUtil
Definition: Instance.h:21
armarx::VariantType::Quaternion
const VariantTypeId Quaternion
Definition: Pose.h:39
armarx::Pose::operator=
Pose & operator=(const Pose &)=default
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ArmarXEigenFormat
const Eigen::IOFormat ArmarXEigenFormat(Eigen::StreamPrecision, Eigen::DontAlignCols, " ", "\n", "", "")
armarx::Quaternion::slerp
Eigen::Matrix3f slerp(float, const Eigen::Matrix3f &)
Definition: Pose.cpp:233
armarx::toIce
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:15
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::Pose::toEigen
virtual Eigen::Matrix4f toEigen() const
Definition: Pose.cpp:334
armarx::Pose::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: Pose.cpp:353
Eigen::Quaternionf
Quaternion< float, 0 > Quaternionf
Definition: EigenForwardDeclarations.h:61
armarx::Pose::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: Pose.cpp:360
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
q
#define q
armarx::fromIce
void fromIce(const std::map< IceKeyT, IceValueT > &iceMap, boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:26
ExpressionException.h
armarx::Pose
The Pose class.
Definition: Pose.h:242
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::Quaternion< float, 0 >
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
armarx::Pose::ice_postUnmarshal
void ice_postUnmarshal() override
Definition: Pose.cpp:376
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
armarx::Pose::output
std::string output(const Ice::Current &=Ice::emptyCurrent) const override
Definition: Pose.cpp:345
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28