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 // LCOV_EXCL_START
36 #include <vector>
37 #include <list>
38 #include <stdexcept>
39 #include <cassert>
40 
41 #ifndef IKFAST_HEADER_COMMON
42 #define IKFAST_HEADER_COMMON
43 
45 #define IKFAST_VERSION 61
46 
47 namespace ikfast
48 {
50 template <typename T>
51 class IkSingleDOFSolutionBase
52 {
53 public:
54  IkSingleDOFSolutionBase() { indices[0] = indices[1] = indices[2] = indices[3] = indices[4] = -1; } // NOLINT
55  T fmul{ 0 };
56  T foffset{ 0 };
57  signed char freeind{ -1 };
58  unsigned char jointtype{ 1 };
59  unsigned char maxsolutions{ 1 };
60  unsigned char indices[5]; // NOLINT ///< unique index of the solution used to keep track on what part it came from.
61  // sometimes a
63 };
64 
70 template <typename T>
71 class IkSolutionBase // NOLINT
72 {
73 public:
74  virtual ~IkSolutionBase() = default;
79  virtual void GetSolution(T* solution, const T* freevalues) const = 0;
80 
82  virtual void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const
83  {
84  solution.resize(GetDOF());
85  if (freevalues.empty())
86  GetSolution(&solution.at(0), nullptr);
87  else
88  GetSolution(&solution.at(0), &freevalues.at(0));
89  }
90 
94  virtual const std::vector<int>& GetFree() const = 0;
95 
97  virtual int GetDOF() const = 0;
98 };
99 
101 template <typename T>
102 class IkSolutionListBase // NOLINT
103 {
104 public:
105  virtual ~IkSolutionListBase() = default;
106 
112  virtual size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) = 0;
113 
115  virtual const IkSolutionBase<T>& GetSolution(size_t index) const = 0;
116 
118  virtual size_t GetNumSolutions() const = 0;
119 
122  virtual void Clear() = 0;
123 };
124 
126 template <typename T>
127 class IkFastFunctions // NOLINT
128 {
129 public:
130  IkFastFunctions() = default;
131  virtual ~IkFastFunctions() = default;
132  using ComputeIkFn = bool (*)(const T*, const T*, const T*, IkSolutionListBase<T>&);
133  ComputeIkFn _ComputeIk{ nullptr };
134  using ComputeFkFn = void (*)(const T*, T*, T*);
135  ComputeFkFn _ComputeFk{ nullptr };
136  using GetNumFreeParametersFn = int (*)();
138  using GetFreeParametersFn = int* (*)();
140  using GetNumJointsFn = int (*)();
141  GetNumJointsFn _GetNumJoints{ nullptr };
142  using GetIkRealSizeFn = int (*)();
143  GetIkRealSizeFn _GetIkRealSize{ nullptr };
144  using GetIkFastVersionFn = const char* (*)();
146  using GetIkTypeFn = int (*)();
147  GetIkTypeFn _GetIkType{ nullptr };
148  using GetKinematicsHashFn = const char* (*)();
150 };
151 
152 // Implementations of the abstract classes, user doesn't need to use them
153 
155 template <typename T>
156 class IkSolution : public IkSolutionBase<T>
157 {
158 public:
159  IkSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree)
160  {
161  _vbasesol = vinfos;
162  _vfree = vfree;
163  }
164 
165  void GetSolution(T* solution, const T* freevalues) const override
166  {
167  for (std::size_t i = 0; i < _vbasesol.size(); ++i)
168  {
169  if (_vbasesol[i].freeind < 0)
170  solution[i] = _vbasesol[i].foffset;
171  else
172  {
173  assert(freevalues != nullptr);
174  solution[i] = freevalues[_vbasesol[i].freeind] * _vbasesol[i].fmul + _vbasesol[i].foffset; // NOLINT
175  if (solution[i] > T(3.14159265358979))
176  {
177  solution[i] -= T(6.28318530717959);
178  }
179  else if (solution[i] < T(-3.14159265358979))
180  {
181  solution[i] += T(6.28318530717959);
182  }
183  }
184  }
185  }
186 
187  void GetSolution(std::vector<T>& solution, const std::vector<T>& freevalues) const override
188  {
189  solution.resize(GetDOF());
190  if (freevalues.empty())
191  GetSolution(&solution.at(0), nullptr);
192  else
193  GetSolution(&solution.at(0), &freevalues.at(0));
194  }
195 
196  const std::vector<int>& GetFree() const override { return _vfree; }
197  int GetDOF() const override { return static_cast<int>(_vbasesol.size()); }
198 
199  virtual void Validate() const
200  {
201  for (size_t i = 0; i < _vbasesol.size(); ++i)
202  {
203  if (_vbasesol[i].maxsolutions == (unsigned char)-1)
204  {
205  throw std::runtime_error("max solutions for joint not initialized");
206  }
207  if (_vbasesol[i].maxsolutions > 0)
208  {
209  if (_vbasesol[i].indices[0] >= _vbasesol[i].maxsolutions)
210  {
211  throw std::runtime_error("index >= max solutions for joint");
212  }
213  if (_vbasesol[i].indices[1] != (unsigned char)-1 && _vbasesol[i].indices[1] >= _vbasesol[i].maxsolutions)
214  {
215  throw std::runtime_error("2nd index >= max solutions for joint");
216  }
217  }
218  }
219  }
220 
221  virtual void GetSolutionIndices(std::vector<unsigned int>& v) const
222  {
223  v.resize(0);
224  v.push_back(0);
225  for (int i = (int)_vbasesol.size() - 1; i >= 0; --i)
226  {
227  if (_vbasesol[i].maxsolutions != (unsigned char)-1 && _vbasesol[i].maxsolutions > 1)
228  {
229  for (size_t j = 0; j < v.size(); ++j) // NOLINT
230  {
231  v[j] *= _vbasesol[i].maxsolutions;
232  }
233  size_t orgsize = v.size();
234  if (_vbasesol[i].indices[1] != (unsigned char)-1)
235  {
236  for (size_t j = 0; j < orgsize; ++j)
237  {
238  v.push_back(v[j] + _vbasesol[i].indices[1]);
239  }
240  }
241  if (_vbasesol[i].indices[0] != (unsigned char)-1)
242  {
243  for (size_t j = 0; j < orgsize; ++j)
244  {
245  v[j] += _vbasesol[i].indices[0];
246  }
247  }
248  }
249  }
250  }
251 
252  std::vector<IkSingleDOFSolutionBase<T> > _vbasesol;
253  std::vector<int> _vfree;
254 };
255 
257 template <typename T>
258 class IkSolutionList : public IkSolutionListBase<T>
259 {
260 public:
261  size_t AddSolution(const std::vector<IkSingleDOFSolutionBase<T> >& vinfos, const std::vector<int>& vfree) override
262  {
263  size_t index = _listsolutions.size();
264  _listsolutions.push_back(IkSolution<T>(vinfos, vfree));
265  return index;
266  }
267 
268  const IkSolutionBase<T>& GetSolution(size_t index) const override
269  {
270  if (index >= _listsolutions.size())
271  {
272  throw std::runtime_error("GetSolution index is invalid");
273  }
274  auto it = _listsolutions.begin();
275  std::advance(it, index);
276  return *it;
277  }
278 
279  size_t GetNumSolutions() const override { return _listsolutions.size(); }
280 
281  void Clear() override { _listsolutions.clear(); }
282 
283 protected:
284  std::list<IkSolution<T> > _listsolutions;
285 };
286 } // namespace ikfast
287 
288 #endif // OPENRAVE_IKFAST_HEADER
289 
290 // The following code is dependent on the C++ library linking with.
291 #ifdef IKFAST_HAS_LIBRARY
292 
293 // defined when creating a shared object/dll
294 #ifdef IKFAST_CLIBRARY
295 #ifdef _MSC_VER
296 #define IKFAST_API extern "C" __declspec(dllexport)
297 #else
298 #define IKFAST_API extern "C" __attribute__((visibility("default")))
299 #endif
300 #else
301 #define IKFAST_API
302 #endif
303 
304 #ifdef IKFAST_NAMESPACE
305 namespace IKFAST_NAMESPACE
306 {
307 #endif
308 
309 #ifdef IKFAST_REAL
310 typedef IKFAST_REAL IkReal;
311 #else
312 using IkReal = double;
313 #endif
314 
326 IKFAST_API bool ComputeIk(const IkReal* eetrans,
327  const IkReal* eerot,
328  const IkReal* pfree,
330 
332 IKFAST_API void ComputeFk(const IkReal* joints, IkReal* eetrans, IkReal* eerot);
333 
335 IKFAST_API int GetNumFreeParameters();
336 
338 IKFAST_API int* GetFreeParameters();
339 
341 IKFAST_API int GetNumJoints();
342 
344 IKFAST_API int GetIkRealSize();
345 
347 IKFAST_API const char* GetIkFastVersion();
348 
350 IKFAST_API int GetIkType();
351 
353 IKFAST_API const char* GetKinematicsHash();
354 
355 #ifdef IKFAST_NAMESPACE
356 }
357 #endif
358 
359 #endif // IKFAST_HAS_LIBRARY
360 // LCOV_EXCL_STOP
ikfast::IkSingleDOFSolutionBase::jointtype
unsigned char jointtype
joint type, 0x01 is revolute, 0x11 is slider
Definition: ikfast.h:58
GetNumJoints
IKFAST_API int GetNumJoints()
Definition: abb_irb2400_ikfast_solver.hpp:380
ikfast::IkSolutionListBase::GetSolution
virtual const IkSolutionBase< T > & GetSolution(size_t index) const=0
ikfast::IkFastFunctions::_GetIkFastVersion
GetIkFastVersionFn _GetIkFastVersion
Definition: ikfast.h:145
ikfast::IkSingleDOFSolutionBase::IkSingleDOFSolutionBase
IkSingleDOFSolutionBase()
ikfast::IkFastFunctions::ComputeFkFn
void(* ComputeFkFn)(const T *, T *, T *)
ikfast::IkFastFunctions::_GetFreeParameters
GetFreeParametersFn _GetFreeParameters
Definition: ikfast.h:139
ikfast::IkFastFunctions::IkFastFunctions
IkFastFunctions()
GetIkFastVersion
const IKFAST_API char * GetIkFastVersion()
Definition: abb_irb2400_ikfast_solver.hpp:3220
ikfast::IkSolutionList::GetNumSolutions
virtual size_t GetNumSolutions() const
ikfast::IkSolution::_vfree
std::vector< int > _vfree
Definition: ikfast.h:253
ikfast::IkSolution::GetSolutionIndices
virtual void GetSolutionIndices(std::vector< unsigned int > &v) const
ikfast::IkSingleDOFSolutionBase::fmul
T fmul
joint value is fmul*sol[freeind]+foffset
Definition: ikfast.h:55
ikfast
GetIkRealSize
IKFAST_API int GetIkRealSize()
Definition: abb_irb2400_ikfast_solver.hpp:382
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:102
ikfast::IkSolutionList::AddSolution
virtual size_t AddSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
ikfast::IkFastFunctions::GetIkTypeFn
int(* GetIkTypeFn)()
ikfast::IkFastFunctions::_GetKinematicsHash
GetKinematicsHashFn _GetKinematicsHash
Definition: ikfast.h:149
ikfast::IkSolutionListBase::GetNumSolutions
virtual size_t GetNumSolutions() const=0
GetIkType
IKFAST_API int GetIkType()
Definition: abb_irb2400_ikfast_solver.hpp:384
ikfast::IkFastFunctions::GetIkRealSizeFn
int(* GetIkRealSizeFn)()
ikfast::IkFastFunctions::GetNumFreeParametersFn
int(* GetNumFreeParametersFn)()
GetFreeParameters
IKFAST_API int * GetFreeParameters()
Definition: abb_irb2400_ikfast_solver.hpp:379
ikfast::IkFastFunctions::_GetNumFreeParameters
GetNumFreeParametersFn _GetNumFreeParameters
Definition: ikfast.h:137
ikfast::IkSolution::GetFree
virtual const std::vector< int > & GetFree() const
ikfast::IkSolutionBase::GetDOF
virtual const int GetDOF() const=0
ikfast::IkSolutionBase::~IkSolutionBase
virtual ~IkSolutionBase()
ikfast::IkSolutionListBase::AddSolution
virtual size_t AddSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)=0
ikfast::IkFastFunctions::_GetIkType
GetIkTypeFn _GetIkType
Definition: ikfast.h:147
ikfast::IkSolution::Validate
virtual void Validate() const
ikfast::IkFastFunctions::_ComputeIk
ComputeIkFn _ComputeIk
Definition: ikfast.h:133
ikfast::IkSolutionList::GetSolution
virtual const IkSolutionBase< T > & GetSolution(size_t index) const
ikfast::IkFastFunctions::_ComputeFk
ComputeFkFn _ComputeFk
Definition: ikfast.h:135
ComputeFk
IKFAST_API void ComputeFk(const IkReal *j, IkReal *eetrans, IkReal *eerot)
Definition: abb_irb2400_ikfast_solver.hpp:283
ikfast::IkFastFunctions::GetIkFastVersionFn
const typedef char *(* GetIkFastVersionFn)()
ikfast::IkFastFunctions::_GetNumJoints
GetNumJointsFn _GetNumJoints
Definition: ikfast.h:141
ikfast::IkSolutionListBase::~IkSolutionListBase
virtual ~IkSolutionListBase()
ikfast::IkSolutionList::_listsolutions
std::list< IkSolution< T > > _listsolutions
Definition: ikfast.h:284
ikfast::IkSolutionListBase::Clear
virtual void Clear()=0
GetNumFreeParameters
IKFAST_API int GetNumFreeParameters()
Definition: abb_irb2400_ikfast_solver.hpp:378
ikfast::IkFastFunctions::_GetIkRealSize
GetIkRealSizeFn _GetIkRealSize
Definition: ikfast.h:143
ikfast::IkFastFunctions::GetNumJointsFn
int(* GetNumJointsFn)()
GetKinematicsHash
const IKFAST_API char * GetKinematicsHash()
Definition: abb_irb2400_ikfast_solver.hpp:3215
ikfast::IkFastFunctions::~IkFastFunctions
virtual ~IkFastFunctions()
ikfast::IkFastFunctions::GetKinematicsHashFn
const typedef char *(* GetKinematicsHashFn)()
ikfast::IkSolution::GetDOF
virtual const int GetDOF() const
ikfast::IkSolutionBase::GetFree
virtual const std::vector< int > & GetFree() const=0
ikfast::IkSolution::GetSolution
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
ikfast::IkSolution::_vbasesol
std::vector< IkSingleDOFSolutionBase< T > > _vbasesol
solution and their offsets if joints are mimiced
Definition: ikfast.h:252
ikfast::IkSolution::IkSolution
IkSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
ikfast::IkSingleDOFSolutionBase::freeind
signed char freeind
if >= 0, mimics another joint
Definition: ikfast.h:57
ComputeIk
IKFAST_API bool ComputeIk(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions)
Definition: abb_irb2400_ikfast_solver.hpp:3209
ikfast::IkFastFunctions::ComputeIkFn
bool(* ComputeIkFn)(const T *, const T *, const T *, IkSolutionListBase< T > &)
ikfast::IkFastFunctions::GetFreeParametersFn
int *(* GetFreeParametersFn)()
ikfast::IkSolutionBase::GetSolution
virtual void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const
ikfast::IkSolutionList::Clear
virtual void Clear()
ikfast::IkSingleDOFSolutionBase::indices
unsigned char indices[5]
Definition: ikfast.h:60


tesseract_kinematics
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:14