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


motoman_mh5_ikfast_manipulator_plugin
Author(s):
autogenerated on Sat Sep 26 2020 03:53:08