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