moveit2
The MoveIt Motion Planning Framework for ROS 2.
ikfast.h
Go to the documentation of this file.
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2012 Rosen Diankov <rosen.diankov@gmail.com>
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
36 #pragma once
37 
38 #include <vector>
39 #include <list>
40 #include <stdexcept>
41 
42 #pragma once
43 
45 #define IKFAST_VERSION 61
46 
47 namespace ikfast
48 {
50 template <typename T>
52 {
53 public:
55  {
56  indices[0] = indices[1] = indices[2] = indices[3] = indices[4] = -1;
57  }
59  signed char freeind;
60  unsigned char jointtype;
61  unsigned char maxsolutions;
62  unsigned char indices[5];
64 };
65 
71 template <typename T>
73 {
74 public:
75  virtual ~IkSolutionBase()
76  {
77  }
82  virtual void GetSolution(T* solution, const T* freevalues) const = 0;
83 
85  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const
86  {
87  solution.resize(GetDOF());
88  GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : nullptr);
89  }
90 
94  virtual const std::vector<int>& GetFree() const = 0;
95 
97  virtual int GetDOF() const = 0;
98 };
99 
101 template <typename T>
103 {
104 public:
106  {
107  }
108 
114  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) = 0;
115 
117  virtual const IkSolutionBase<T>& GetSolution(size_t index) const = 0;
118 
120  virtual size_t GetNumSolutions() const = 0;
121 
124  virtual void Clear() = 0;
125 };
126 
128 template <typename T>
130 {
131 public:
133  : _ComputeIk(nullptr)
134  , _ComputeFk(nullptr)
135  , _GetNumFreeParameters(nullptr)
136  , _GetFreeParameters(nullptr)
137  , _GetNumJoints(nullptr)
138  , _GetIkRealSize(nullptr)
139  , _GetIkFastVersion(nullptr)
140  , _GetIkType(nullptr)
141  , _GetKinematicsHash(nullptr)
142  {
143  }
145  {
146  }
147  typedef bool (*ComputeIkFn)(const T*, const T*, const T*, IkSolutionListBase<T>&);
149  typedef void (*ComputeFkFn)(const T*, T*, T*);
151  typedef int (*GetNumFreeParametersFn)();
153  typedef int* (*GetFreeParametersFn)();
155  typedef int (*GetNumJointsFn)();
157  typedef int (*GetIkRealSizeFn)();
159  typedef const char* (*GetIkFastVersionFn)();
161  typedef int (*GetIkTypeFn)();
163  typedef const char* (*GetKinematicsHashFn)();
165 };
166 
167 // Implementations of the abstract classes, user doesn't need to use them
168 
170 template <typename T>
171 class IkSolution : public IkSolutionBase<T>
172 {
173 public:
174  IkSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
175  {
176  _vbasesol = vinfos;
177  _vfree = vfree;
178  }
179 
180  virtual void GetSolution(T* solution, const T* freevalues) const
181  {
182  for (std::size_t i = 0; i < _vbasesol.size(); ++i)
183  {
184  if (_vbasesol[i].freeind < 0)
185  solution[i] = _vbasesol[i].foffset;
186  else
187  {
188  solution[i] = freevalues[_vbasesol[i].freeind] * _vbasesol[i].fmul + _vbasesol[i].foffset;
189  if (solution[i] > T(3.14159265358979))
190  {
191  solution[i] -= T(6.28318530717959);
192  }
193  else if (solution[i] < T(-3.14159265358979))
194  {
195  solution[i] += T(6.28318530717959);
196  }
197  }
198  }
199  }
200 
201  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const
202  {
203  solution.resize(GetDOF());
204  GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : nullptr);
205  }
206 
207  virtual const std::vector<int>& GetFree() const
208  {
209  return _vfree;
210  }
211  virtual int GetDOF() const
212  {
213  return static_cast<int>(_vbasesol.size());
214  }
215 
216  virtual void Validate() const
217  {
218  for (size_t i = 0; i < _vbasesol.size(); ++i)
219  {
220  if (_vbasesol[i].maxsolutions == (unsigned char)-1)
221  {
222  throw std::runtime_error("max solutions for joint not initialized");
223  }
224  if (_vbasesol[i].maxsolutions > 0)
225  {
226  if (_vbasesol[i].indices[0] >= _vbasesol[i].maxsolutions)
227  {
228  throw std::runtime_error("index >= max solutions for joint");
229  }
230  if (_vbasesol[i].indices[1] != (unsigned char)-1 && _vbasesol[i].indices[1] >= _vbasesol[i].maxsolutions)
231  {
232  throw std::runtime_error("2nd index >= max solutions for joint");
233  }
234  }
235  }
236  }
237 
238  virtual void GetSolutionIndices(std::vector<unsigned int>& v) const
239  {
240  v.resize(0);
241  v.push_back(0);
242  for (int i = static_cast<int>(_vbasesol.size()) - 1; i >= 0; --i)
243  {
244  if (_vbasesol[i].maxsolutions != (unsigned char)-1 && _vbasesol[i].maxsolutions > 1)
245  {
246  for (size_t j = 0; j < v.size(); ++j)
247  {
248  v[j] *= _vbasesol[i].maxsolutions;
249  }
250  size_t orgsize = v.size();
251  if (_vbasesol[i].indices[1] != (unsigned char)-1)
252  {
253  for (size_t j = 0; j < orgsize; ++j)
254  {
255  v.push_back(v[j] + _vbasesol[i].indices[1]);
256  }
257  }
258  if (_vbasesol[i].indices[0] != (unsigned char)-1)
259  {
260  for (size_t j = 0; j < orgsize; ++j)
261  {
262  v[j] += _vbasesol[i].indices[0];
263  }
264  }
265  }
266  }
267  }
268 
269  std::vector<IkSingleDOFSolutionBase<T> > _vbasesol;
270  std::vector<int> _vfree;
271 };
272 
274 template <typename T>
276 {
277 public:
278  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
279  {
280  size_t index = _listsolutions.size();
281  _listsolutions.push_back(IkSolution<T>(vinfos, vfree));
282  return index;
283  }
284 
285  virtual const IkSolutionBase<T>& GetSolution(size_t index) const
286  {
287  if (index >= _listsolutions.size())
288  {
289  throw std::runtime_error("GetSolution index is invalid");
290  }
291  typename std::list<IkSolution<T> >::const_iterator it = _listsolutions.begin();
292  std::advance(it, index);
293  return *it;
294  }
295 
296  virtual size_t GetNumSolutions() const
297  {
298  return _listsolutions.size();
299  }
300 
301  virtual void Clear()
302  {
303  _listsolutions.clear();
304  }
305 
306 protected:
307  std::list<IkSolution<T> > _listsolutions;
308 };
309 } // namespace ikfast
310 
311 // The following code is dependent on the C++ library linking with.
312 #ifdef IKFAST_HAS_LIBRARY
313 
314 // defined when creating a shared object/dll
315 #ifdef IKFAST_CLIBRARY
316 #ifdef _MSC_VER
317 #define IKFAST_API extern "C" __declspec(dllexport)
318 #else
319 #define IKFAST_API extern "C"
320 #endif
321 #else
322 #define IKFAST_API
323 #endif
324 
325 #ifdef IKFAST_NAMESPACE
326 namespace IKFAST_NAMESPACE
327 {
328 #endif
329 
330 #ifdef IKFAST_REAL
331 typedef IKFAST_REAL IkReal;
332 #else
333 typedef double IkReal;
334 #endif
335 
347 IKFAST_API bool ComputeIk(const IkReal* eetrans, const IkReal* eerot, const IkReal* pfree,
349 
351 IKFAST_API void ComputeFk(const IkReal* joints, IkReal* eetrans, IkReal* eerot);
352 
354 IKFAST_API int GetNumFreeParameters();
355 
357 IKFAST_API int* GetFreeParameters();
358 
360 IKFAST_API int GetNumJoints();
361 
363 IKFAST_API int GetIkRealSize();
364 
366 IKFAST_API const char* GetIkFastVersion();
367 
369 IKFAST_API int GetIkType();
370 
372 IKFAST_API const char* GetKinematicsHash();
373 
374 #ifdef IKFAST_NAMESPACE
375 }
376 #endif
377 
378 #endif // IKFAST_HAS_LIBRARY
holds function pointers for all the exported functions of ikfast
Definition: ikfast.h:130
int(* GetNumJointsFn)()
Definition: ikfast.h:155
GetIkFastVersionFn _GetIkFastVersion
Definition: ikfast.h:160
GetIkTypeFn _GetIkType
Definition: ikfast.h:162
GetFreeParametersFn _GetFreeParameters
Definition: ikfast.h:154
int(* GetIkRealSizeFn)()
Definition: ikfast.h:157
const char *(* GetKinematicsHashFn)()
Definition: ikfast.h:163
const char *(* GetIkFastVersionFn)()
Definition: ikfast.h:159
int(* GetNumFreeParametersFn)()
Definition: ikfast.h:151
ComputeFkFn _ComputeFk
Definition: ikfast.h:150
void(* ComputeFkFn)(const T *, T *, T *)
Definition: ikfast.h:149
GetIkRealSizeFn _GetIkRealSize
Definition: ikfast.h:158
ComputeIkFn _ComputeIk
Definition: ikfast.h:148
virtual ~IkFastFunctions()
Definition: ikfast.h:144
GetKinematicsHashFn _GetKinematicsHash
Definition: ikfast.h:164
int(* GetIkTypeFn)()
Definition: ikfast.h:161
GetNumJointsFn _GetNumJoints
Definition: ikfast.h:156
int *(* GetFreeParametersFn)()
Definition: ikfast.h:153
GetNumFreeParametersFn _GetNumFreeParameters
Definition: ikfast.h:152
bool(* ComputeIkFn)(const T *, const T *, const T *, IkSolutionListBase< T > &)
Definition: ikfast.h:147
holds the solution for a single dof
Definition: ikfast.h:52
T foffset
joint value is fmul*sol[freeind]+foffset
Definition: ikfast.h:58
unsigned char jointtype
joint type, 0x01 is revolute, 0x11 is slider
Definition: ikfast.h:60
unsigned char maxsolutions
max possible indices, 0 if controlled by free index or a free joint itself
Definition: ikfast.h:61
unsigned char indices[5]
Definition: ikfast.h:62
signed char freeind
if >= 0, mimics another joint
Definition: ikfast.h:59
The discrete solutions are returned in this structure.
Definition: ikfast.h:73
virtual const std::vector< int > & GetFree() const =0
Gets the indices of the configuration space that have to be preset before a full solution can be retu...
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
std::vector version of GetSolution
Definition: ikfast.h:85
virtual void GetSolution(T *solution, const T *freevalues) const =0
gets a concrete solution
virtual int GetDOF() const =0
the dof of the solution
virtual ~IkSolutionBase()
Definition: ikfast.h:75
manages all the solutions
Definition: ikfast.h:103
virtual ~IkSolutionListBase()
Definition: ikfast.h:105
virtual const IkSolutionBase< T > & GetSolution(size_t index) const =0
returns the solution pointer
virtual size_t GetNumSolutions() const =0
returns the number of solutions stored
virtual void Clear()=0
clears all current solutions, note that any memory addresses returned from GetSolution will be invali...
virtual size_t AddSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)=0
add one solution and return its index for later retrieval
Default implementation of IkSolutionListBase.
Definition: ikfast.h:276
virtual const IkSolutionBase< T > & GetSolution(size_t index) const
returns the solution pointer
Definition: ikfast.h:285
std::list< IkSolution< T > > _listsolutions
Definition: ikfast.h:307
virtual size_t AddSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
add one solution and return its index for later retrieval
Definition: ikfast.h:278
virtual size_t GetNumSolutions() const
returns the number of solutions stored
Definition: ikfast.h:296
virtual void Clear()
clears all current solutions, note that any memory addresses returned from GetSolution will be invali...
Definition: ikfast.h:301
Default implementation of IkSolutionBase.
Definition: ikfast.h:172
virtual void Validate() const
Definition: ikfast.h:216
virtual int GetDOF() const
the dof of the solution
Definition: ikfast.h:211
virtual void GetSolutionIndices(std::vector< unsigned int > &v) const
Definition: ikfast.h:238
virtual const std::vector< int > & GetFree() const
Gets the indices of the configuration space that have to be preset before a full solution can be retu...
Definition: ikfast.h:207
IkSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
Definition: ikfast.h:174
std::vector< int > _vfree
Definition: ikfast.h:270
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
std::vector version of GetSolution
Definition: ikfast.h:201
std::vector< IkSingleDOFSolutionBase< T > > _vbasesol
solution and their offsets if joints are mimiced
Definition: ikfast.h:269
virtual void GetSolution(T *solution, const T *freevalues) const
gets a concrete solution
Definition: ikfast.h:180
Definition: ikfast.h:48
IKFAST_API int * GetFreeParameters()
IKFAST_API int GetNumJoints()
IKFAST_API int GetIkRealSize()
IKFAST_API int GetIkType()
IKFAST_API void ComputeFk(const IkReal *j, IkReal *eetrans, IkReal *eerot)
IKFAST_API bool ComputeIk(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions)
IKFAST_API const char * GetIkFastVersion()
IKFAST_API int GetNumFreeParameters()
IKFAST_API const char * GetKinematicsHash()