Aikido
Rn-impl.hpp
Go to the documentation of this file.
1 #include <type_traits>
2 
4 
5 namespace aikido {
6 namespace statespace {
7 
8 //==============================================================================
9 extern template class R<0>;
10 
11 extern template class R<1>;
12 
13 extern template class R<2>;
14 
15 extern template class R<3>;
16 
17 extern template class R<6>;
18 
19 extern template class R<Eigen::Dynamic>;
20 
21 //==============================================================================
26 template <class _QualifiedState>
28  : public statespace::
29  StateHandle<R<_QualifiedState::DimensionAtCompileTime>, _QualifiedState>
30 {
31 public:
32  static constexpr int DimensionAtCompileTime
33  = _QualifiedState::DimensionAtCompileTime;
34 
36 
37  using typename statespace::
38  StateHandle<R<DimensionAtCompileTime>, _QualifiedState>::State;
39  using typename statespace::
40  StateHandle<R<DimensionAtCompileTime>, _QualifiedState>::StateSpace;
41  using typename statespace::
42  StateHandle<R<DimensionAtCompileTime>, _QualifiedState>::QualifiedState;
43 
44  using ValueType = std::conditional<
45  std::is_const<QualifiedState>::value,
46  const VectorNd,
48 
51  {
52  // Do nothing
53  }
54 
59  RStateHandle(const StateSpace* _space, QualifiedState* _state)
60  : statespace::StateHandle<StateSpace, QualifiedState>(_space, _state)
61  {
62  // Do nothing
63  }
64 
68  Eigen::Map<const VectorNd> getValue()
69  {
70  return this->getStateSpace()->getValue(this->getState());
71  }
72 
76  void setValue(const VectorNd& _value)
77  {
78  return this->getStateSpace()->setValue(this->getState(), _value);
79  }
80 };
81 
82 //==============================================================================
83 template <int N>
85 {
86  static_assert(
87  N >= 0 || N == Eigen::Dynamic,
88  "Invalid dimension. The dimension should be non-negative.");
89 
90  if (N == Eigen::Dynamic)
91  {
92  std::stringstream msg;
93  msg << "Invalid template parameter. Either pass a non-negative dimension "
94  << "as the template parameter (e.g., R<3>()).";
95  throw std::invalid_argument(msg.str());
96  }
97 }
98 
99 //==============================================================================
100 template <int N>
101 R<N>::R(int dimension) : mDimension(dimension)
102 {
103  static_assert(
104  N >= 0 || N == Eigen::Dynamic,
105  "Invalid dimension. The dimension should be non-negative.");
106 
107  if (N != Eigen::Dynamic && N != dimension)
108  {
109  std::stringstream msg;
110  msg << "Invalid dimension argument. Either pass Eigen::Dynamic as the "
111  << "template parameter (e.g., Rn(3)) or pass the same dimension with "
112  << "the template parameter as the constructor parameter "
113  << "(e.g., R<3>(3)).";
114  throw std::invalid_argument(msg.str());
115  }
116 }
117 
118 //==============================================================================
119 template <int N>
121 {
122  return ScopedState(this);
123 }
124 
125 //==============================================================================
126 template <int N>
127 auto R<N>::cloneState(const StateSpace::State* stateIn) const -> ScopedState
128 {
129  auto newState = createState();
130  copyState(stateIn, newState);
131 
132  return newState;
133 }
134 
135 //==============================================================================
136 template <int N>
137 auto R<N>::getMutableValue(State* _state) const -> Eigen::Map<VectorNd>
138 {
139  auto valueBuffer
140  = reinterpret_cast<double*>(reinterpret_cast<unsigned char*>(_state));
141 
142  return Eigen::Map<VectorNd>(valueBuffer, getDimension());
143 }
144 
145 //==============================================================================
146 template <int N>
147 auto R<N>::getValue(const State* _state) const -> Eigen::Map<const VectorNd>
148 {
149  auto valueBuffer = reinterpret_cast<const double*>(
150  reinterpret_cast<const unsigned char*>(_state));
151 
152  return Eigen::Map<const VectorNd>(valueBuffer, getDimension());
153 }
154 
155 //==============================================================================
156 template <int N>
157 void R<N>::setValue(State* _state, const typename R<N>::VectorNd& _value) const
158 {
159  // TODO: Skip this check in release mode.
160  if (static_cast<std::size_t>(_value.size()) != getDimension())
161  {
162  std::stringstream msg;
163  msg << "Value has incorrect size: expected " << getDimension() << ", got "
164  << _value.size() << ".";
165  throw std::invalid_argument(msg.str());
166  }
167 
168  getMutableValue(_state) = _value;
169 }
170 
171 //==============================================================================
172 template <int N>
173 std::size_t R<N>::getStateSizeInBytes() const
174 {
175  return getDimension() * sizeof(double);
176 }
177 
178 //==============================================================================
179 template <int N>
181 {
182  auto state = reinterpret_cast<State*>(_buffer);
183  getMutableValue(state).setZero();
184  return state;
185 }
186 
187 //==============================================================================
188 template <int N>
190 {
191  // Do nothing.
192 }
193 
194 //==============================================================================
195 template <int N>
197  const StateSpace::State* _state1,
198  const StateSpace::State* _state2,
199  StateSpace::State* _out) const
200 {
201  // TODO: Disable this in release mode.
202  if (_state1 == _out || _state2 == _out)
203  throw std::invalid_argument("Output aliases input.");
204 
205  auto state1 = static_cast<const State*>(_state1);
206  auto state2 = static_cast<const State*>(_state2);
207  auto out = static_cast<State*>(_out);
208 
209  setValue(out, getValue(state1) + getValue(state2));
210 }
211 
212 //==============================================================================
213 template <int N>
214 std::size_t R<N>::getDimension() const
215 {
216  if (N == Eigen::Dynamic)
217  return mDimension;
218  else
219  return N;
220 }
221 
222 //==============================================================================
223 template <int N>
225 {
226  auto out = static_cast<State*>(_out);
227 
228  setValue(out, VectorNd::Zero(getDimension()));
229 }
230 
231 //==============================================================================
232 template <int N>
234  const StateSpace::State* _in, StateSpace::State* _out) const
235 {
236  // TODO: Disable this in release mode.
237  if (_out == _in)
238  throw std::invalid_argument("Output aliases input.");
239 
240  auto in = static_cast<const State*>(_in);
241  auto out = static_cast<State*>(_out);
242 
243  setValue(out, -getValue(in));
244 }
245 
246 //==============================================================================
247 template <int N>
249  const StateSpace::State* _source, StateSpace::State* _destination) const
250 {
251  auto destination = static_cast<State*>(_destination);
252  auto source = static_cast<const State*>(_source);
253  setValue(destination, getValue(source));
254 }
255 
256 //==============================================================================
257 template <int N>
259  const Eigen::VectorXd& _tangent, StateSpace::State* _out) const
260 {
261  // TODO: Skip this check in release mode.
262  if (static_cast<std::size_t>(_tangent.size()) != getDimension())
263  {
264  std::stringstream msg;
265  msg << "Tangent vector has incorrect size: expected " << getDimension()
266  << ", got " << _tangent.size() << ".";
267  throw std::invalid_argument(msg.str());
268  }
269 
270  auto out = static_cast<State*>(_out);
271  setValue(out, _tangent);
272 }
273 
274 //==============================================================================
275 template <int N>
276 void R<N>::logMap(const StateSpace::State* _in, Eigen::VectorXd& _tangent) const
277 {
278  if (static_cast<std::size_t>(_tangent.size()) != getDimension())
279  _tangent.resize(getDimension());
280 
281  auto in = static_cast<const State*>(_in);
282  _tangent = getValue(in);
283 }
284 
285 //==============================================================================
286 template <int N>
287 void R<N>::print(const StateSpace::State* _state, std::ostream& _os) const
288 {
289  auto val = getValue(static_cast<const State*>(_state));
290 
291  Eigen::IOFormat cleanFmt(3, Eigen::DontAlignCols, ",", ",", "", "", "[", "]");
292  _os << val.format(cleanFmt);
293 }
294 
295 } // namespace statespace
296 } // namespace aikido
aikido::statespace::R::freeStateInBuffer
void freeStateInBuffer(StateSpace::State *_state) const override
Free a state previously created by allocateStateInBuffer.
Definition: Rn-impl.hpp:189
aikido::statespace::R::createState
ScopedState createState() const
Helper function to create a ScopedState.
Definition: Rn-impl.hpp:120
aikido::statespace::RStateHandle::VectorNd
typename R< DimensionAtCompileTime >::VectorNd VectorNd
Definition: Rn-impl.hpp:35
aikido
Format of serialized trajectory in YAML.
Definition: algorithm.hpp:4
aikido::statespace::RStateHandle::getValue
Eigen::Map< const VectorNd > getValue()
Gets the real vector stored in this state.
Definition: Rn-impl.hpp:68
aikido::statespace::R::allocateStateInBuffer
StateSpace::State * allocateStateInBuffer(void *_buffer) const override
Create a new state in a pre-allocated buffer.
Definition: Rn-impl.hpp:180
aikido::statespace::R::getMutableValue
Eigen::Map< VectorNd > getMutableValue(State *_state) const
Gets the mutable value stored in a Rn::State.
Definition: Rn-impl.hpp:137
aikido::statespace::R::getValue
Eigen::Map< const VectorNd > getValue(const State *_state) const
Gets the real vector stored in a State.
Definition: Rn-impl.hpp:147
aikido::statespace::R::print
void print(const StateSpace::State *_state, std::ostream &_os) const override
Print the n-dimensional vector represented by the state Format: [x_1, x_2, ..., x_n].
Definition: Rn-impl.hpp:287
aikido::statespace::R::logMap
void logMap(const StateSpace::State *_in, Eigen::VectorXd &_tangent) const override
Log mapping of Lie group element to a Lie algebra element.
Definition: Rn-impl.hpp:276
aikido::statespace::R::compose
void compose(const StateSpace::State *_state1, const StateSpace::State *_state2, StateSpace::State *_out) const override
Lie group operation for this StateSpace.
Definition: Rn-impl.hpp:196
aikido::statespace::R::getIdentity
void getIdentity(StateSpace::State *_out) const override
Gets the identity element for this Lie group, such that:
Definition: Rn-impl.hpp:224
aikido::statespace::RStateHandle::ValueType
std::conditional< std::is_const< QualifiedState >::value, const VectorNd, VectorNd > ValueType
Definition: Rn-impl.hpp:47
aikido::statespace::RStateHandle::RStateHandle
RStateHandle(const StateSpace *_space, QualifiedState *_state)
Construct a handle for _state in _space.
Definition: Rn-impl.hpp:59
aikido::statespace::StateHandle
Wrap a State with its StateSpace to provide convenient accessor methods.
Definition: StateHandle.hpp:16
aikido::statespace::R::R
R()
Constructs a N dimensional real vector space only when the dimension is can be known in compile time.
Definition: Rn-impl.hpp:84
aikido::statespace::StateSpace
Represents a Lie group and its associated Lie algebra, i.e.
Definition: StateSpace.hpp:33
aikido::statespace::RStateHandle::RStateHandle
RStateHandle()
Construct and initialize to nullptr.
Definition: Rn-impl.hpp:50
aikido::statespace::R::copyState
void copyState(const StateSpace::State *_source, StateSpace::State *_destination) const override
Copy a state.
Definition: Rn-impl.hpp:248
aikido::statespace::R::getDimension
std::size_t getDimension() const override
Get the dimension of this Lie group.
Definition: Rn-impl.hpp:214
aikido::statespace::ScopedState< StateHandle >
aikido::statespace::R::cloneState
ScopedState cloneState(const StateSpace::State *stateIn) const
Creates an identical clone of stateIn.
Definition: Rn-impl.hpp:127
Rn.hpp
aikido::statespace::RStateHandle::setValue
void setValue(const VectorNd &_value)
Sets the real vector stored in this state.
Definition: Rn-impl.hpp:76
aikido::statespace::R::State
Point in a R<N>.
Definition: Rn.hpp:22
aikido::statespace::R::getInverse
void getInverse(const StateSpace::State *_in, StateSpace::State *_out) const override
Gets the inverse of _in in this Lie group, such that:
Definition: Rn-impl.hpp:233
aikido::statespace::StateSpace::State
Definition: StateSpace.hpp:167
aikido::statespace::R::expMap
void expMap(const Eigen::VectorXd &_tangent, StateSpace::State *_out) const override
Exponential mapping of Lie algebra element to a Lie group element.
Definition: Rn-impl.hpp:258
aikido::statespace::R::getStateSizeInBytes
std::size_t getStateSizeInBytes() const override
Gets the size of a State, in bytes.
Definition: Rn-impl.hpp:173
aikido::statespace::RStateHandle
StateHandle for a Rn.
Definition: Rn-impl.hpp:27
aikido::statespace::R::VectorNd
Eigen::Matrix< double, N, 1 > VectorNd
Definition: Rn.hpp:37
aikido::statespace::R::setValue
void setValue(State *_state, const VectorNd &_value) const
Sets the real vector stored in a State.
Definition: Rn-impl.hpp:157