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