SE2d.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, CNRS-UM LIRMM
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <mc_rbdyn/rpy_utils.h>
31 #include <mc_rtc/Configuration.h>
32 #include <mc_rtc/constants.h>
33 
34 #include <SpaceVecAlg/SpaceVecAlg>
35 
36 namespace lipm_walking
37 {
38 
39 namespace utils
40 {
41 
45 struct SE2d
46 {
47  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
48 
58  SE2d(double x = 0., double y = 0., double theta = 0.) : x(x), y(y), theta(theta) {}
59 
60  SE2d(const Eigen::Vector3d & v) : x(v.x()), y(v.y()), theta(v.z()) {}
61 
68  SE2d(const sva::PTransformd & pose)
69  {
70  x = pose.translation().x();
71  y = pose.translation().y();
72  theta = mc_rbdyn::rpyFromMat(pose.rotation()).z();
73  }
74 
80  inline sva::PTransformd operator*(const sva::PTransformd & X_0_a) const
81  {
82  sva::PTransformd X_a_b = asPTransform();
83  return X_a_b * X_0_a;
84  }
85 
89  inline sva::PTransformd asPTransform() const
90  {
91  return {mc_rbdyn::rpyToMat(0., 0., theta), Eigen::Vector3d{x, y, 0.}};
92  }
93 
99  inline Eigen::Vector2d pos() const
100  {
101  return {x, y};
102  }
103 
109  inline Eigen::Vector2d ori() const
110  {
111  return {std::cos(theta), std::sin(theta)};
112  }
113 
119  inline const Eigen::Vector3d vector() const noexcept
120  {
121  return {x, y, theta};
122  }
123 
129  inline Eigen::Vector3d vectorDegrees() const
130  {
131  return {x, y, mc_rtc::constants::toDeg(theta)};
132  }
133 
134  // This will be called to load your object from a Configuration object
135  static SE2d fromConfiguration(const mc_rtc::Configuration & in)
136  {
137  SE2d res;
138  res.x = in("x", 0);
139  res.y = in("y", 0);
140  res.theta = in("theta", 0);
141  return res;
142  }
143 
144  // This will be called save your object to a Configuration object
145  mc_rtc::Configuration toConfiguration(bool toDeg = false) const
146  {
147  mc_rtc::Configuration res;
148  res.add("x", x);
149  res.add("y", y);
150  if(toDeg)
151  {
152  res.add("theta", mc_rtc::constants::toDeg(theta));
153  }
154  else
155  {
156  res.add("theta", theta);
157  }
158  return res;
159  }
160 
161 public:
162  double x;
163  double y;
164  double theta;
165 };
166 
167 } // namespace utils
168 } // namespace lipm_walking
169 
170 inline std::ostream & operator<<(std::ostream & os, const lipm_walking::utils::SE2d & se2d)
171 {
172  os << se2d.x << ", " << se2d.y << ", " << se2d.theta;
173  return os;
174 }
std::ostream & operator<<(std::ostream &os, const lipm_walking::utils::SE2d &se2d)
Definition: SE2d.h:170
Definition: clamp.h:36
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SE2d(double x=0., double y=0., double theta=0.)
Definition: SE2d.h:58
mc_rtc::Configuration toConfiguration(bool toDeg=false) const
Definition: SE2d.h:145
sva::PTransformd asPTransform() const
Definition: SE2d.h:89
Eigen::Vector3d vectorDegrees() const
Definition: SE2d.h:129
static SE2d fromConfiguration(const mc_rtc::Configuration &in)
Definition: SE2d.h:135
sva::PTransformd operator*(const sva::PTransformd &X_0_a) const
Definition: SE2d.h:80
const Eigen::Vector3d vector() const noexcept
Definition: SE2d.h:119
SE2d(const sva::PTransformd &pose)
Definition: SE2d.h:68
Eigen::Vector2d ori() const
Definition: SE2d.h:109
SE2d(const Eigen::Vector3d &v)
Definition: SE2d.h:60
Eigen::Vector2d pos() const
Definition: SE2d.h:99