- 3.0.2 rigid body dynamics module.
ikfast.h
Go to the documentation of this file.
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2012-2014 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.
33 #include <vector>
34 #include <list>
35 #include <stdexcept>
36 #include <cmath>
37 
38 #ifndef IKFAST_HEADER_COMMON
39 #define IKFAST_HEADER_COMMON
40 
43 #define IKFAST_VERSION 0x10000048
44 
45 namespace ikfast {
46 
48 template <typename T>
50 {
51 public:
53  indices[0] = indices[1] = indices[2] = indices[3] = indices[4] = -1;
54  }
56  signed char freeind;
57  unsigned char jointtype;
58  unsigned char maxsolutions;
59  unsigned char indices[5];
60 };
61 
66 template <typename T>
68 {
69 public:
70  virtual ~IkSolutionBase() {
71  }
76  virtual void GetSolution(T* solution, const T* freevalues) const = 0;
77 
79  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const {
80  solution.resize(GetDOF());
81  GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : NULL);
82  }
83 
88  virtual const std::vector<int>& GetFree() const = 0;
89 
91  virtual const int GetDOF() const = 0;
92 };
93 
95 template <typename T>
97 {
98 public:
99  virtual ~IkSolutionListBase() {
100  }
101 
106  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) = 0;
107 
109  virtual const IkSolutionBase<T>& GetSolution(size_t index) const = 0;
110 
112  virtual size_t GetNumSolutions() const = 0;
113 
115  virtual void Clear() = 0;
116 };
117 
119 template <typename T>
121 {
122 public:
123  IkFastFunctions() : _ComputeIk(NULL), _ComputeIk2(NULL), _ComputeFk(NULL), _GetNumFreeParameters(NULL), _GetFreeParameters(NULL), _GetNumJoints(NULL), _GetIkRealSize(NULL), _GetIkFastVersion(NULL), _GetIkType(NULL), _GetKinematicsHash(NULL) {
124  }
125  virtual ~IkFastFunctions() {
126  }
127  typedef bool (*ComputeIkFn)(const T*, const T*, const T*, IkSolutionListBase<T>&);
128  ComputeIkFn _ComputeIk;
129  typedef bool (*ComputeIk2Fn)(const T*, const T*, const T*, IkSolutionListBase<T>&, void*);
130  ComputeIk2Fn _ComputeIk2;
131  typedef void (*ComputeFkFn)(const T*, T*, T*);
132  ComputeFkFn _ComputeFk;
133  typedef int (*GetNumFreeParametersFn)();
134  GetNumFreeParametersFn _GetNumFreeParameters;
135  typedef int* (*GetFreeParametersFn)();
136  GetFreeParametersFn _GetFreeParameters;
137  typedef int (*GetNumJointsFn)();
138  GetNumJointsFn _GetNumJoints;
139  typedef int (*GetIkRealSizeFn)();
140  GetIkRealSizeFn _GetIkRealSize;
141  typedef const char* (*GetIkFastVersionFn)();
142  GetIkFastVersionFn _GetIkFastVersion;
143  typedef int (*GetIkTypeFn)();
144  GetIkTypeFn _GetIkType;
145  typedef const char* (*GetKinematicsHashFn)();
146  GetKinematicsHashFn _GetKinematicsHash;
147 };
148 
149 // Implementations of the abstract classes, user doesn't need to use them
150 
152 template <typename T>
153 class IkSolution : public IkSolutionBase<T>
154 {
155 public:
156  IkSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) {
157  _vbasesol = vinfos;
158  _vfree = vfree;
159  }
160 
161  virtual void GetSolution(T* solution, const T* freevalues) const {
162  for(std::size_t i = 0; i < _vbasesol.size(); ++i) {
163  if( _vbasesol[i].freeind < 0 )
164  solution[i] = _vbasesol[i].foffset;
165  else {
166  solution[i] = freevalues[_vbasesol[i].freeind]*_vbasesol[i].fmul + _vbasesol[i].foffset;
167  if( solution[i] > T(3.14159265358979) ) {
168  solution[i] -= T(6.28318530717959);
169  }
170  else if( solution[i] < T(-3.14159265358979) ) {
171  solution[i] += T(6.28318530717959);
172  }
173  }
174  }
175  }
176 
177  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const {
178  solution.resize(GetDOF());
179  GetSolution(&solution.at(0), freevalues.size() > 0 ? &freevalues.at(0) : NULL);
180  }
181 
182  virtual const std::vector<int>& GetFree() const {
183  return _vfree;
184  }
185  virtual const int GetDOF() const {
186  return static_cast<int>(_vbasesol.size());
187  }
188 
189  virtual void Validate() const {
190  for(size_t i = 0; i < _vbasesol.size(); ++i) {
191  if( _vbasesol[i].maxsolutions == (unsigned char)-1) {
192  throw std::runtime_error("max solutions for joint not initialized");
193  }
194  if( _vbasesol[i].maxsolutions > 0 ) {
195  if( _vbasesol[i].indices[0] >= _vbasesol[i].maxsolutions ) {
196  throw std::runtime_error("index >= max solutions for joint");
197  }
198  if( _vbasesol[i].indices[1] != (unsigned char)-1 && _vbasesol[i].indices[1] >= _vbasesol[i].maxsolutions ) {
199  throw std::runtime_error("2nd index >= max solutions for joint");
200  }
201  }
202  if( !std::isfinite(_vbasesol[i].foffset) ) {
203  throw std::runtime_error("foffset was not finite");
204  }
205  }
206  }
207 
208  virtual void GetSolutionIndices(std::vector<unsigned int>& v) const {
209  v.resize(0);
210  v.push_back(0);
211  for(int i = (int)_vbasesol.size()-1; i >= 0; --i) {
212  if( _vbasesol[i].maxsolutions != (unsigned char)-1 && _vbasesol[i].maxsolutions > 1 ) {
213  for(size_t j = 0; j < v.size(); ++j) {
214  v[j] *= _vbasesol[i].maxsolutions;
215  }
216  size_t orgsize=v.size();
217  if( _vbasesol[i].indices[1] != (unsigned char)-1 ) {
218  for(size_t j = 0; j < orgsize; ++j) {
219  v.push_back(v[j]+_vbasesol[i].indices[1]);
220  }
221  }
222  if( _vbasesol[i].indices[0] != (unsigned char)-1 ) {
223  for(size_t j = 0; j < orgsize; ++j) {
224  v[j] += _vbasesol[i].indices[0];
225  }
226  }
227  }
228  }
229  }
230 
231  std::vector< IkSingleDOFSolutionBase<T> > _vbasesol;
232  std::vector<int> _vfree;
233 };
234 
236 template <typename T>
238 {
239 public:
240  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
241  {
242  size_t index = _listsolutions.size();
243  _listsolutions.push_back(IkSolution<T>(vinfos,vfree));
244  return index;
245  }
246 
247  virtual const IkSolutionBase<T>& GetSolution(size_t index) const
248  {
249  if( index >= _listsolutions.size() ) {
250  throw std::runtime_error("GetSolution index is invalid");
251  }
252  typename std::list< IkSolution<T> >::const_iterator it = _listsolutions.begin();
253  std::advance(it,index);
254  return *it;
255  }
256 
257  virtual size_t GetNumSolutions() const {
258  return _listsolutions.size();
259  }
260 
261  virtual void Clear() {
262  _listsolutions.clear();
263  }
264 
265 protected:
266  std::list< IkSolution<T> > _listsolutions;
267 };
268 
269 }
270 
271 #endif // OPENRAVE_IKFAST_HEADER
272 
273 // The following code is dependent on the C++ library linking with.
274 #ifdef IKFAST_HAS_LIBRARY
275 
276 // defined when creating a shared object/dll
277 #ifdef IKFAST_CLIBRARY
278 #ifdef _MSC_VER
279 #define IKFAST_API extern "C" __declspec(dllexport)
280 #else
281 #define IKFAST_API extern "C"
282 #endif
283 #else
284 #define IKFAST_API
285 #endif
286 
287 #ifdef IKFAST_NAMESPACE
288 namespace IKFAST_NAMESPACE {
289 #endif
290 
291 #ifdef IKFAST_REAL
292 typedef IKFAST_REAL IkReal;
293 #else
294 typedef double IkReal;
295 #endif
296 
306 IKFAST_API bool ComputeIk(const IkReal* eetrans, const IkReal* eerot, const IkReal* pfree, ikfast::IkSolutionListBase<IkReal>& solutions);
307 
310 IKFAST_API bool ComputeIk2(const IkReal* eetrans, const IkReal* eerot, const IkReal* pfree, ikfast::IkSolutionListBase<IkReal>& solutions, void* pOpenRAVEManip);
311 
313 IKFAST_API void ComputeFk(const IkReal* joints, IkReal* eetrans, IkReal* eerot);
314 
316 IKFAST_API int GetNumFreeParameters();
317 
319 IKFAST_API int* GetFreeParameters();
320 
322 IKFAST_API int GetNumJoints();
323 
325 IKFAST_API int GetIkRealSize();
326 
328 IKFAST_API const char* GetIkFastVersion();
329 
331 IKFAST_API int GetIkType();
332 
334 IKFAST_API const char* GetKinematicsHash();
335 
336 #ifdef IKFAST_NAMESPACE
337 }
338 #endif
339 
340 #endif // IKFAST_HAS_LIBRARY
Definition: ikfast.h:45
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
std::vector version of GetSolution
Definition: ikfast.h:79
IKFAST_API int GetNumFreeParameters()
GetFreeParametersFn _GetFreeParameters
Definition: ikfast.h:136
std::vector< int > _vfree
Definition: ikfast.h:232
unsigned char indices[5]
unique index of the solution used to keep track on what part it came from. sometimes a solution can b...
Definition: ikfast.h:59
virtual ~IkFastFunctions()
Definition: ikfast.h:125
holds function pointers for all the exported functions of ikfast
Definition: ikfast.h:120
unsigned char maxsolutions
max possible indices, 0 if controlled by free index or a free joint itself
Definition: ikfast.h:58
GetIkFastVersionFn _GetIkFastVersion
Definition: ikfast.h:142
IKFAST_API int GetIkType()
virtual const IkSolutionBase< T > & GetSolution(size_t index) const
returns the solution pointer
Definition: ikfast.h:247
virtual void GetSolution(T *solution, const T *freevalues) const
gets a concrete solution
Definition: ikfast.h:161
holds the solution for a single dof
Definition: ikfast.h:49
signed char freeind
if >= 0, mimics another joint
Definition: ikfast.h:56
The discrete solutions are returned in this structure.
Definition: ikfast.h:67
virtual void GetSolutionIndices(std::vector< unsigned int > &v) const
Definition: ikfast.h:208
virtual void Clear()
clears all current solutions, note that any memory addresses returned from GetSolution will be invali...
Definition: ikfast.h:261
#define IKFAST_NAMESPACE
std::vector< IkSingleDOFSolutionBase< T > > _vbasesol
solution and their offsets if joints are mimiced
Definition: ikfast.h:231
GetKinematicsHashFn _GetKinematicsHash
Definition: ikfast.h:146
IKFAST_API bool ComputeIk(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions)
ComputeFkFn _ComputeFk
Definition: ikfast.h:132
std::list< IkSolution< T > > _listsolutions
Definition: ikfast.h:266
GetNumFreeParametersFn _GetNumFreeParameters
Definition: ikfast.h:134
IKFAST_API const char * GetIkFastVersion()
GetNumJointsFn _GetNumJoints
Definition: ikfast.h:138
Default implementation of IkSolutionBase.
Definition: ikfast.h:153
ComputeIk2Fn _ComputeIk2
Definition: ikfast.h:130
for i
IKFAST_API int * GetFreeParameters()
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:182
IKFAST_API bool ComputeIk2(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions, void *pOpenRAVEManip)
virtual void Validate() const
Definition: ikfast.h:189
ComputeIkFn _ComputeIk
Definition: ikfast.h:128
virtual ~IkSolutionBase()
Definition: ikfast.h:70
IKFAST_API int GetNumJoints()
IKFAST_API int GetIkRealSize()
T fmul
Definition: ikfast.h:55
T foffset
joint value is fmul*sol[freeind]+foffset
Definition: ikfast.h:55
Default implementation of IkSolutionListBase.
Definition: ikfast.h:237
GetIkTypeFn _GetIkType
Definition: ikfast.h:144
virtual ~IkSolutionListBase()
Definition: ikfast.h:99
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
std::vector version of GetSolution
Definition: ikfast.h:177
GetIkRealSizeFn _GetIkRealSize
Definition: ikfast.h:140
IkSingleDOFSolutionBase()
Definition: ikfast.h:52
manages all the solutions
Definition: ikfast.h:96
virtual size_t GetNumSolutions() const
returns the number of solutions stored
Definition: ikfast.h:257
virtual const int GetDOF() const
the dof of the solution
Definition: ikfast.h:185
unsigned char jointtype
joint type, 0x01 is revolute, 0x11 is slider
Definition: ikfast.h:57
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:240
IKFAST_API const char * GetKinematicsHash()
IkSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
Definition: ikfast.h:156
IKFAST_API void ComputeFk(const IkReal *j, IkReal *eetrans, IkReal *eerot)
IkFastFunctions()
Definition: ikfast.h:123