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.
35 #include <vector>
36 #include <list>
37 #include <stdexcept>
38 
39 #ifndef IKFAST_HEADER_COMMON
40 #define IKFAST_HEADER_COMMON
41 
43 #define IKFAST_VERSION 61
44 
45 namespace ikfast
46 {
48 template <typename T>
50 {
51 public:
53  {
54  indices[0] = indices[1] = indices[2] = indices[3] = indices[4] = -1;
55  }
57  signed char freeind;
58  unsigned char jointtype;
59  unsigned char maxsolutions;
60  unsigned char indices[5];
61 };
63 
69 template <typename T>
71 {
72 public:
73  virtual ~IkSolutionBase() {}
78  virtual void GetSolution(T* solution, const T* freevalues) const = 0;
79 
81  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const
82  {
83  solution.resize(GetDOF());
84  GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : NULL);
85  }
86 
90  virtual const std::vector<int>& GetFree() const = 0;
91 
93  virtual const int GetDOF() const = 0;
94 };
95 
97 template <typename T>
99 {
100 public:
101  virtual ~IkSolutionListBase() {}
102 
108  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) = 0;
109 
111  virtual const IkSolutionBase<T>& GetSolution(size_t index) const = 0;
112 
114  virtual size_t GetNumSolutions() const = 0;
115 
118  virtual void Clear() = 0;
119 };
120 
122 template <typename T>
124 {
125 public:
127  : _ComputeIk(NULL)
128  , _ComputeFk(NULL)
129  , _GetNumFreeParameters(NULL)
130  , _GetFreeParameters(NULL)
131  , _GetNumJoints(NULL)
132  , _GetIkRealSize(NULL)
133  , _GetIkFastVersion(NULL)
134  , _GetIkType(NULL)
135  , _GetKinematicsHash(NULL)
136  {
137  }
138  virtual ~IkFastFunctions() {}
139  typedef bool (*ComputeIkFn)(const T*, const T*, const T*, IkSolutionListBase<T>&);
141  typedef void (*ComputeFkFn)(const T*, T*, T*);
143  typedef int (*GetNumFreeParametersFn)();
145  typedef int* (*GetFreeParametersFn)();
147  typedef int (*GetNumJointsFn)();
149  typedef int (*GetIkRealSizeFn)();
151  typedef const char* (*GetIkFastVersionFn)();
153  typedef int (*GetIkTypeFn)();
155  typedef const char* (*GetKinematicsHashFn)();
157 };
158 
159 // Implementations of the abstract classes, user doesn't need to use them
160 
162 template <typename T>
163 class IkSolution : public IkSolutionBase<T>
164 {
165 public:
166  IkSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
167  {
168  _vbasesol = vinfos;
169  _vfree = vfree;
170  }
171 
172  virtual void GetSolution(T* solution, const T* freevalues) const
173  {
174  for (std::size_t i = 0; i < _vbasesol.size(); ++i)
175  {
176  if (_vbasesol[i].freeind < 0)
177  solution[i] = _vbasesol[i].foffset;
178  else
179  {
180  solution[i] = freevalues[_vbasesol[i].freeind] * _vbasesol[i].fmul + _vbasesol[i].foffset;
181  if (solution[i] > T(3.14159265358979))
182  {
183  solution[i] -= T(6.28318530717959);
184  }
185  else if (solution[i] < T(-3.14159265358979))
186  {
187  solution[i] += T(6.28318530717959);
188  }
189  }
190  }
191  }
192 
193  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const
194  {
195  solution.resize(GetDOF());
196  GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : NULL);
197  }
198 
199  virtual const std::vector<int>& GetFree() const { return _vfree; }
200  virtual const int GetDOF() const { return static_cast<int>(_vbasesol.size()); }
201 
202  virtual void Validate() const
203  {
204  for (size_t i = 0; i < _vbasesol.size(); ++i)
205  {
206  if (_vbasesol[i].maxsolutions == (unsigned char)-1)
207  {
208  throw std::runtime_error("max solutions for joint not initialized");
209  }
210  if (_vbasesol[i].maxsolutions > 0)
211  {
212  if (_vbasesol[i].indices[0] >= _vbasesol[i].maxsolutions)
213  {
214  throw std::runtime_error("index >= max solutions for joint");
215  }
216  if (_vbasesol[i].indices[1] != (unsigned char)-1 && _vbasesol[i].indices[1] >= _vbasesol[i].maxsolutions)
217  {
218  throw std::runtime_error("2nd index >= max solutions for joint");
219  }
220  }
221  }
222  }
223 
224  virtual void GetSolutionIndices(std::vector<unsigned int>& v) const
225  {
226  v.resize(0);
227  v.push_back(0);
228  for (int i = (int)_vbasesol.size() - 1; i >= 0; --i)
229  {
230  if (_vbasesol[i].maxsolutions != (unsigned char)-1 && _vbasesol[i].maxsolutions > 1)
231  {
232  for (size_t j = 0; j < v.size(); ++j)
233  {
234  v[j] *= _vbasesol[i].maxsolutions;
235  }
236  size_t orgsize = v.size();
237  if (_vbasesol[i].indices[1] != (unsigned char)-1)
238  {
239  for (size_t j = 0; j < orgsize; ++j)
240  {
241  v.push_back(v[j] + _vbasesol[i].indices[1]);
242  }
243  }
244  if (_vbasesol[i].indices[0] != (unsigned char)-1)
245  {
246  for (size_t j = 0; j < orgsize; ++j)
247  {
248  v[j] += _vbasesol[i].indices[0];
249  }
250  }
251  }
252  }
253  }
254 
255  std::vector<IkSingleDOFSolutionBase<T> > _vbasesol;
256  std::vector<int> _vfree;
257 };
258 
260 template <typename T>
262 {
263 public:
264  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
265  {
266  size_t index = _listsolutions.size();
267  _listsolutions.push_back(IkSolution<T>(vinfos, vfree));
268  return index;
269  }
270 
271  virtual const IkSolutionBase<T>& GetSolution(size_t index) const
272  {
273  if (index >= _listsolutions.size())
274  {
275  throw std::runtime_error("GetSolution index is invalid");
276  }
277  typename std::list<IkSolution<T> >::const_iterator it = _listsolutions.begin();
278  std::advance(it, index);
279  return *it;
280  }
281 
282  virtual size_t GetNumSolutions() const { return _listsolutions.size(); }
283 
284  virtual void Clear() { _listsolutions.clear(); }
285 
286 protected:
287  std::list<IkSolution<T> > _listsolutions;
288 };
289 
290 } // namespace ikfast
291 
292 #endif // OPENRAVE_IKFAST_HEADER
293 
294 // The following code is dependent on the C++ library linking with.
295 #ifdef IKFAST_HAS_LIBRARY
296 
297 // defined when creating a shared object/dll
298 #ifdef IKFAST_CLIBRARY
299 #ifdef _MSC_VER
300 #define IKFAST_API extern "C" __declspec(dllexport)
301 #else
302 #define IKFAST_API extern "C"
303 #endif
304 #else
305 #define IKFAST_API
306 #endif
307 
308 #ifdef IKFAST_NAMESPACE
309 namespace IKFAST_NAMESPACE
310 {
311 #endif
312 
313 #ifdef IKFAST_REAL
314 typedef IKFAST_REAL IkReal;
315 #else
316 typedef double IkReal;
317 #endif
318 
330 IKFAST_API bool ComputeIk(const IkReal* eetrans,
331  const IkReal* eerot,
332  const IkReal* pfree,
334 
336 IKFAST_API void ComputeFk(const IkReal* joints, IkReal* eetrans, IkReal* eerot);
337 
339 IKFAST_API int GetNumFreeParameters();
340 
342 IKFAST_API int* GetFreeParameters();
343 
345 IKFAST_API int GetNumJoints();
346 
348 IKFAST_API int GetIkRealSize();
349 
351 IKFAST_API const char* GetIkFastVersion();
352 
354 IKFAST_API int GetIkType();
355 
357 IKFAST_API const char* GetKinematicsHash();
358 
359 #ifdef IKFAST_NAMESPACE
360 }
361 #endif
362 
363 #endif // IKFAST_HAS_LIBRARY
ikfast::IkSingleDOFSolutionBase::jointtype
unsigned char jointtype
joint type, 0x01 is revolute, 0x11 is slider
Definition: ikfast.h:58
ikfast::IkSolutionList
Default implementation of IkSolutionListBase.
Definition: ikfast.h:261
ikfast::IkSolutionListBase::GetSolution
virtual const IkSolutionBase< T > & GetSolution(size_t index) const =0
returns the solution pointer
ikfast::IkFastFunctions::_GetIkFastVersion
GetIkFastVersionFn _GetIkFastVersion
Definition: ikfast.h:152
ikfast::IkSingleDOFSolutionBase::IkSingleDOFSolutionBase
IkSingleDOFSolutionBase()
Definition: ikfast.h:52
ikfast::IkFastFunctions::ComputeFkFn
void(* ComputeFkFn)(const T *, T *, T *)
Definition: ikfast.h:141
ComputeIk
IKFAST_API bool ComputeIk(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions)
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:3205
ikfast::IkFastFunctions::_GetFreeParameters
GetFreeParametersFn _GetFreeParameters
Definition: ikfast.h:146
ikfast::IkFastFunctions::IkFastFunctions
IkFastFunctions()
Definition: ikfast.h:126
GetIkFastVersion
const IKFAST_API char * GetIkFastVersion()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:3216
ikfast::IkSolutionList::GetNumSolutions
virtual size_t GetNumSolutions() const
returns the number of solutions stored
Definition: ikfast.h:282
ikfast::IkSolution::_vfree
std::vector< int > _vfree
Definition: ikfast.h:256
ikfast::IkSolution::GetSolutionIndices
virtual void GetSolutionIndices(std::vector< unsigned int > &v) const
Definition: ikfast.h:224
ikfast::IkSingleDOFSolutionBase::fmul
T fmul
Definition: ikfast.h:56
ikfast
Definition: ikfast.h:45
ikfast::IkSolutionBase::GetSolution
virtual void GetSolution(T *solution, const T *freevalues) const =0
gets a concrete solution
ikfast::IkSingleDOFSolutionBase::foffset
T foffset
joint value is fmul*sol[freeind]+foffset
Definition: ikfast.h:56
ikfast::IkSingleDOFSolutionBase::maxsolutions
unsigned char maxsolutions
max possible indices, 0 if controlled by free index or a free joint itself
Definition: ikfast.h:59
ikfast::IkSolutionListBase
manages all the solutions
Definition: ikfast.h:98
ikfast::IkSolutionList::AddSolution
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:264
ikfast::IkFastFunctions::GetIkTypeFn
int(* GetIkTypeFn)()
Definition: ikfast.h:153
ikfast::IkFastFunctions::_GetKinematicsHash
GetKinematicsHashFn _GetKinematicsHash
Definition: ikfast.h:156
ikfast::IkSolutionListBase::GetNumSolutions
virtual size_t GetNumSolutions() const =0
returns the number of solutions stored
ikfast::IkFastFunctions::GetIkRealSizeFn
int(* GetIkRealSizeFn)()
Definition: ikfast.h:149
GetNumFreeParameters
IKFAST_API int GetNumFreeParameters()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:374
ikfast::IkFastFunctions::GetNumFreeParametersFn
int(* GetNumFreeParametersFn)()
Definition: ikfast.h:143
ikfast::IkSingleDOFSolutionBase
holds the solution for a single dof
Definition: ikfast.h:49
ikfast::IkFastFunctions
holds function pointers for all the exported functions of ikfast
Definition: ikfast.h:123
ikfast::IkFastFunctions::_GetNumFreeParameters
GetNumFreeParametersFn _GetNumFreeParameters
Definition: ikfast.h:144
ikfast::IkSolution::GetFree
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:199
ikfast::IkSolutionBase::GetDOF
virtual const int GetDOF() const =0
the dof of the solution
ikfast::IkSolutionBase::~IkSolutionBase
virtual ~IkSolutionBase()
Definition: ikfast.h:73
ComputeFk
IKFAST_API void ComputeFk(const IkReal *j, IkReal *eetrans, IkReal *eerot)
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:279
ikfast::IkSolutionListBase::AddSolution
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
ikfast::IkFastFunctions::_GetIkType
GetIkTypeFn _GetIkType
Definition: ikfast.h:154
ikfast::IkSolution::Validate
virtual void Validate() const
Definition: ikfast.h:202
GetNumJoints
IKFAST_API int GetNumJoints()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:376
ikfast::IkFastFunctions::_ComputeIk
ComputeIkFn _ComputeIk
Definition: ikfast.h:140
ikfast::IkSolutionList::GetSolution
virtual const IkSolutionBase< T > & GetSolution(size_t index) const
returns the solution pointer
Definition: ikfast.h:271
ikfast::IkFastFunctions::_ComputeFk
ComputeFkFn _ComputeFk
Definition: ikfast.h:142
ikfast::IkSolution::GetSolution
virtual void GetSolution(T *solution, const T *freevalues) const
gets a concrete solution
Definition: ikfast.h:172
ikfast::IkFastFunctions::GetIkFastVersionFn
const typedef char *(* GetIkFastVersionFn)()
Definition: ikfast.h:151
GetKinematicsHash
const IKFAST_API char * GetKinematicsHash()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:3211
ikfast::IkFastFunctions::_GetNumJoints
GetNumJointsFn _GetNumJoints
Definition: ikfast.h:148
ikfast::IkSolutionBase
The discrete solutions are returned in this structure.
Definition: ikfast.h:70
ikfast::IkSolutionListBase::~IkSolutionListBase
virtual ~IkSolutionListBase()
Definition: ikfast.h:101
ikfast::IkSolutionList::_listsolutions
std::list< IkSolution< T > > _listsolutions
Definition: ikfast.h:287
ikfast::IkSolutionListBase::Clear
virtual void Clear()=0
clears all current solutions, note that any memory addresses returned from GetSolution will be invali...
GetFreeParameters
IKFAST_API int * GetFreeParameters()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:375
ikfast::IkFastFunctions::_GetIkRealSize
GetIkRealSizeFn _GetIkRealSize
Definition: ikfast.h:150
ikfast::IkFastFunctions::GetNumJointsFn
int(* GetNumJointsFn)()
Definition: ikfast.h:147
ikfast::IkFastFunctions::~IkFastFunctions
virtual ~IkFastFunctions()
Definition: ikfast.h:138
ikfast::IkFastFunctions::GetKinematicsHashFn
const typedef char *(* GetKinematicsHashFn)()
Definition: ikfast.h:155
ikfast::IkSolution::GetDOF
virtual const int GetDOF() const
the dof of the solution
Definition: ikfast.h:200
ikfast::IkSolutionBase::GetFree
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...
ikfast::IkSolution::GetSolution
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
std::vector version of GetSolution
Definition: ikfast.h:193
GetIkRealSize
IKFAST_API int GetIkRealSize()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:378
ikfast::IkSolution::_vbasesol
std::vector< IkSingleDOFSolutionBase< T > > _vbasesol
solution and their offsets if joints are mimiced
Definition: ikfast.h:255
ikfast::IkSolution::IkSolution
IkSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
Definition: ikfast.h:166
GetIkType
IKFAST_API int GetIkType()
Definition: abb_irb2400_manipulator_ikfast_solver.cpp:380
ikfast::IkSingleDOFSolutionBase::freeind
signed char freeind
if >= 0, mimics another joint
Definition: ikfast.h:57
ikfast::IkSolution
Default implementation of IkSolutionBase.
Definition: ikfast.h:163
ikfast::IkFastFunctions::ComputeIkFn
bool(* ComputeIkFn)(const T *, const T *, const T *, IkSolutionListBase< T > &)
Definition: ikfast.h:139
ikfast::IkFastFunctions::GetFreeParametersFn
int *(* GetFreeParametersFn)()
Definition: ikfast.h:145
ikfast::IkSolutionBase::GetSolution
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
std::vector version of GetSolution
Definition: ikfast.h:81
ikfast::IkSolutionList::Clear
virtual void Clear()
clears all current solutions, note that any memory addresses returned from GetSolution will be invali...
Definition: ikfast.h:284
ikfast::IkSingleDOFSolutionBase::indices
unsigned char indices[5]
Definition: ikfast.h:60


opw_kinematics
Author(s): Jon Meyer , Jeroen De Maeyer
autogenerated on Thu Jan 16 2025 03:40:37