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 <VirtualRobot/Robot.h>
28 #include <VirtualRobot/LinkedCoordinate.h>
29 #include <VirtualRobot/LinkedCoordinate.h>
30 #include <VirtualRobot/VirtualRobot.h>
32 
33 #include <Eigen/Core>
34 #include <Eigen/Geometry>
35 
36 #include <sstream>
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 Vector2::toEigen() const
65  {
66  Eigen::Vector2f v;
67  v << this->x, this->y;
68  return v;
69  }
70 
71  void Vector2::operator=(const Eigen::Vector2f& vec)
72  {
73  x = vec[0];
74  y = vec[1];
75  }
76 
77  std::string Vector2::output(const Ice::Current&) const
78  {
79  std::stringstream s;
80  s << toEigen();
81  return s.str();
82  }
83 
84  void Vector2::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
85  {
86  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
87 
88  obj->setFloat("x", x);
89  obj->setFloat("y", y);
90  }
91 
92  void Vector2::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&)
93  {
94  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
95 
96  x = obj->getFloat("x");
97  y = obj->getFloat("y");
98  }
99 
101  {
102  x = 0;
103  y = 0;
104  z = 0;
105  }
106 
107  Vector3::Vector3(const Eigen::Vector3f& v)
108  {
109  x = v(0);
110  y = v(1);
111  z = v(2);
112  }
113 
115  {
116  x = m(0, 3);
117  y = m(1, 3);
118  z = m(2, 3);
119  }
120 
122  {
123  this->x = x;
124  this->y = y;
125  this->z = z;
126  }
127 
128  Eigen::Vector3f Vector3::toEigen() const
129  {
130  Eigen::Vector3f v;
131  v << this->x, this->y, this->z;
132  return v;
133  }
134 
135  void Vector3::operator=(const Eigen::Vector3f& vec)
136  {
137  x = vec[0];
138  y = vec[1];
139  z = vec[2];
140  }
141 
142  std::string Vector3::output(const Ice::Current&) const
143  {
144  std::stringstream s;
145  s << toEigen().format(ArmarXEigenFormat);
146  return s.str();
147  }
148 
149  void Vector3::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
150  {
151  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
152 
153  obj->setFloat("x", x);
154  obj->setFloat("y", y);
155  obj->setFloat("z", z);
156  }
157 
158  void Vector3::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
159  {
160  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
161 
162  x = obj->getFloat("x");
163  y = obj->getFloat("y");
164  z = obj->getFloat("z");
165  }
166 
167 
169  = default;
170 
172  {
173  Eigen::Matrix3f m3 = m4.block<3, 3>(0, 0);
174  this->init(m3);
175  }
176 
178  {
179  this->init(m3);
180  }
181 
183  {
184  this->init(q);
185  }
186 
188  {
189  this->qw = qw;
190  this->qx = qx;
191  this->qy = qy;
192  this->qz = qz;
193  }
194 
196  {
197  return toEigenQuaternion().toRotationMatrix();
198  }
199 
200  Eigen::Quaternionf Quaternion::toEigenQuaternion() const
201  {
202  return {this->qw, this->qx, this->qy, this->qz};
203  }
204 
205  void Quaternion::init(const Eigen::Matrix3f& m3)
206  {
208  q = m3;
209  init(q);
210  }
211 
212  void Quaternion::init(const Eigen::Quaternionf& q)
213  {
214  this->qw = q.w();
215  this->qx = q.x();
216  this->qy = q.y();
217  this->qz = q.z();
218  }
219 
220  Eigen::Matrix3f Quaternion::slerp(float alpha, const Eigen::Matrix3f& m)
221  {
222  return Quaternion::slerp(alpha, this->toEigen(), m);
223  }
224 
225  Eigen::Matrix3f Quaternion::slerp(float alpha, const Eigen::Matrix3f& m1, const Eigen::Matrix3f& m2)
226  {
227  Eigen::Matrix3f result;
228  Eigen::Quaternionf q1, q2;
229  q1 = m1;
230  q2 = m2;
231  result = q1.slerp(alpha, q2);
232  return result;
233  }
234 
235  std::string Quaternion::output(const Ice::Current& c) const
236  {
237  std::stringstream s;
238  s << toEigen()/*.format(ArmarXEigenFormat)*/;
239  return s.str();
240  }
241 
242  void Quaternion::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
243  {
244  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
245 
246  obj->setFloat("qx", qx);
247  obj->setFloat("qy", qy);
248  obj->setFloat("qz", qz);
249  obj->setFloat("qw", qw);
250  }
251 
252  void Quaternion::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
253  {
254  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
255 
256  qx = obj->getFloat("qx");
257  qy = obj->getFloat("qy");
258  qz = obj->getFloat("qz");
259  qw = obj->getFloat("qw");
260  }
261 
262  Pose::Pose(const Eigen::Matrix3f& m, const Eigen::Vector3f& v)
263  {
264  this->orientation = new Quaternion(m);
265  this->position = new Vector3(v);
266  init();
267  }
268 
269  Pose::Pose(const armarx::Vector3BasePtr pos, const armarx::QuaternionBasePtr ori)
270  {
271  this->orientation = new Quaternion(*QuaternionPtr::dynamicCast(ori));
272  this->position = new Vector3(*Vector3Ptr::dynamicCast(pos));
273  init();
274  }
275 
277  {
278  this->orientation = new Quaternion();
279  this->position = new Vector3();
280  init();
281  }
282 
284  IceUtil::Shared(source),
285  armarx::Serializable(source),
286  armarx::VariantDataClass(source),
287  PoseBase(source)
288  {
289  orientation = QuaternionBasePtr::dynamicCast(source.orientation->clone());
290  position = Vector3Ptr::dynamicCast(source.position->clone());
291  init();
292  }
293 
295  {
296  this->orientation = new Quaternion(m);
297  this->position = new Vector3(m);
298  init();
299  }
300 
301  void Pose::operator=(const Eigen::Matrix4f& matrix)
302  {
303  this->orientation = new Quaternion(matrix);
304  this->position = new Vector3(matrix);
305  init();
306  }
307 
309  {
311  ARMARX_CHECK_EXPRESSION(c_orientation);
312  ARMARX_CHECK_EXPRESSION(c_position);
313  m.block<3, 3>(0, 0) = c_orientation->toEigen();
314  m.block<3, 1>(0, 3) = c_position->toEigen();
315  return m;
316  }
317 
318  std::string Pose::output(const Ice::Current&) const
319  {
320  std::stringstream s;
321  s << toEigen()/*.format(ArmarXEigenFormat)*/;
322  return s.str();
323  }
324 
325 
326  void Pose::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c) const
327  {
328  position->serialize(serializer, c);
329  orientation->serialize(serializer, c);
330  }
331 
332  void Pose::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
333  {
334  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
335 
336  position->deserialize(obj);
337  orientation->deserialize(obj);
338  }
339 
340  void Pose::init()
341  {
342  this->c_position = Vector3Ptr::dynamicCast(this->position);
343  this->c_orientation = QuaternionPtr::dynamicCast(this->orientation);
344  }
345 
347  {
348  init();
349  }
350 
351 }
352 
353 
354 void armarx::fromIce(const Vector3BasePtr& ice, Eigen::Vector3f& vector)
355 {
356  vector = fromIce(ice);
357 }
358 
359 void armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Quaternionf& quaternion)
360 {
361  quaternion = fromIce(ice);
362 }
363 
364 void armarx::fromIce(const QuaternionBasePtr& ice, Eigen::Matrix3f& rotation)
365 {
366  rotation = fromIce(ice).toRotationMatrix();
367 }
368 
369 void armarx::fromIce(const PoseBasePtr& ice, Eigen::Matrix4f& pose)
370 {
371  pose = fromIce(ice);
372 }
373 
374 
375 Eigen::Vector3f armarx::fromIce(const Vector3BasePtr& pose)
376 {
377  auto cast = Vector3Ptr::dynamicCast(pose);
378  ARMARX_CHECK_NOT_NULL(cast) << "Vector3BasePtr must be a Vector3Ptr.";
379  return cast->toEigen();
380 }
381 Eigen::Quaternionf armarx::fromIce(const QuaternionBasePtr& pose)
382 {
383  auto cast = QuaternionPtr::dynamicCast(pose);
384  ARMARX_CHECK_NOT_NULL(cast) << "QuaternionBasePtr must be a QuaternionPtr.";
385  return cast->toEigenQuaternion();
386 }
387 Eigen::Matrix4f armarx::fromIce(const PoseBasePtr& pose)
388 {
389  auto cast = PosePtr::dynamicCast(pose);
390  ARMARX_CHECK_NOT_NULL(cast) << "PoseBasePtr must be a PosePtr.";
391  return cast->toEigen();
392 }
393 
394 
395 void armarx::toIce(Vector3BasePtr& ice, const Eigen::Vector3f& vector)
396 {
397  ice = new Vector3(vector);
398 }
399 
400 void armarx::toIce(QuaternionBasePtr& ice, const Eigen::Matrix3f& rotation)
401 {
402  ice = new Quaternion(rotation);
403 }
404 
405 void armarx::toIce(QuaternionBasePtr& ice, const Eigen::Quaternionf& quaternion)
406 {
407  ice = new Quaternion(quaternion);
408 }
409 
410 void armarx::toIce(PoseBasePtr& ice, const Eigen::Matrix4f& pose)
411 {
412  ice = new Pose(pose);
413 }
414 
415 
416 armarx::Vector3Ptr armarx::toIce(const Eigen::Vector3f& vector)
417 {
418  return new Vector3(vector);
419 }
420 
422 {
423  return new Quaternion(rotation);
424 }
425 
427 {
428  return new Quaternion(quaternion);
429 }
430 
432 {
433  return new Pose(pose);
434 }
435 
436 armarx::PosePtr armarx::toIce(const Eigen::Isometry3f& pose)
437 {
438  return armarx::toIce(pose.matrix());
439 }
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::Pose::Pose
Pose()
Definition: Pose.cpp:276
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
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:204
AbstractObjectSerializer.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::Pose::init
void init()
Definition: Pose.cpp:340
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:220
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:308
armarx::Pose::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: Pose.cpp:326
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:332
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:216
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:35
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:70
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:346
armarx::Pose::output
std::string output(const Ice::Current &=Ice::emptyCurrent) const override
Definition: Pose.cpp:318
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:31
armarx::VariantType::Pose
const VariantTypeId Pose
Definition: Pose.h:40
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28