Tesseract
Motion Planning Environment
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.
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
47namespace ikfast
48{
50template <typename T>
52{
53public:
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
70template <typename T>
71class IkSolutionBase // NOLINT
72{
73public:
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
101template <typename T>
102class IkSolutionListBase // NOLINT
103{
104public:
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
126template <typename T>
127class IkFastFunctions // NOLINT
128{
129public:
130 IkFastFunctions() = default;
131 virtual ~IkFastFunctions() = default;
132 using ComputeIkFn = bool (*)(const T*, const T*, const T*, IkSolutionListBase<T>&);
134 using ComputeFkFn = void (*)(const T*, T*, T*);
136 using GetNumFreeParametersFn = int (*)();
138 using GetFreeParametersFn = int* (*)();
140 using GetNumJointsFn = int (*)();
142 using GetIkRealSizeFn = int (*)();
144 using GetIkFastVersionFn = const char* (*)();
146 using GetIkTypeFn = int (*)();
148 using GetKinematicsHashFn = const char* (*)();
150};
151
152// Implementations of the abstract classes, user doesn't need to use them
153
155template <typename T>
156class IkSolution : public IkSolutionBase<T>
157{
158public:
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
257template <typename T>
259{
260public:
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
283protected:
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
305namespace IKFAST_NAMESPACE
306{
307#endif
308
309#ifdef IKFAST_REAL
310typedef IKFAST_REAL IkReal;
311#else
312using IkReal = double;
313#endif
314
326IKFAST_API bool ComputeIk(const IkReal* eetrans,
327 const IkReal* eerot,
328 const IkReal* pfree,
330
332IKFAST_API void ComputeFk(const IkReal* joints, IkReal* eetrans, IkReal* eerot);
333
335IKFAST_API int GetNumFreeParameters();
336
338IKFAST_API int* GetFreeParameters();
339
341IKFAST_API int GetNumJoints();
342
344IKFAST_API int GetIkRealSize();
345
347IKFAST_API const char* GetIkFastVersion();
348
350IKFAST_API int GetIkType();
351
353IKFAST_API const char* GetKinematicsHash();
354
355#ifdef IKFAST_NAMESPACE
356}
357#endif
358
359#endif // IKFAST_HAS_LIBRARY
360// LCOV_EXCL_STOP
IKFAST_API int * GetFreeParameters()
Definition: abb_irb2400_ikfast_solver.hpp:380
IKFAST_API int GetNumJoints()
Definition: abb_irb2400_ikfast_solver.hpp:381
IKFAST_API const char * GetIkFastVersion()
Definition: abb_irb2400_ikfast_solver.hpp:3221
IKFAST_API int GetIkRealSize()
Definition: abb_irb2400_ikfast_solver.hpp:383
IKFAST_API const char * GetKinematicsHash()
Definition: abb_irb2400_ikfast_solver.hpp:3216
IKFAST_API int GetIkType()
Definition: abb_irb2400_ikfast_solver.hpp:385
IKFAST_API void ComputeFk(const IkReal *j, IkReal *eetrans, IkReal *eerot)
Definition: abb_irb2400_ikfast_solver.hpp:284
IKFAST_API bool ComputeIk(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions)
Definition: abb_irb2400_ikfast_solver.hpp:3210
IKFAST_API int GetNumFreeParameters()
Definition: abb_irb2400_ikfast_solver.hpp:379
holds function pointers for all the exported functions of ikfast
Definition: ikfast.h:128
GetIkFastVersionFn _GetIkFastVersion
Definition: ikfast.h:145
GetIkTypeFn _GetIkType
Definition: ikfast.h:147
virtual ~IkFastFunctions()=default
int(*)() GetNumJointsFn
Definition: ikfast.h:140
GetFreeParametersFn _GetFreeParameters
Definition: ikfast.h:139
int(*)() GetNumFreeParametersFn
Definition: ikfast.h:136
const char *(*)() GetIkFastVersionFn
Definition: ikfast.h:144
ComputeFkFn _ComputeFk
Definition: ikfast.h:135
GetIkRealSizeFn _GetIkRealSize
Definition: ikfast.h:143
ComputeIkFn _ComputeIk
Definition: ikfast.h:133
GetKinematicsHashFn _GetKinematicsHash
Definition: ikfast.h:149
const char *(*)() GetKinematicsHashFn
Definition: ikfast.h:148
void(*)(const T *, T *, T *) ComputeFkFn
Definition: ikfast.h:134
int(*)() GetIkRealSizeFn
Definition: ikfast.h:142
GetNumJointsFn _GetNumJoints
Definition: ikfast.h:141
int(*)() GetIkTypeFn
Definition: ikfast.h:146
int *(*)() GetFreeParametersFn
Definition: ikfast.h:138
GetNumFreeParametersFn _GetNumFreeParameters
Definition: ikfast.h:137
bool(*)(const T *, const T *, const T *, IkSolutionListBase< T > &) ComputeIkFn
Definition: ikfast.h:132
holds the solution for a single dof
Definition: ikfast.h:52
T foffset
joint value is fmul*sol[freeind]+foffset
Definition: ikfast.h:56
unsigned char jointtype
joint type, 0x01 is revolute, 0x11 is slider
Definition: ikfast.h:58
unsigned char maxsolutions
max possible indices, 0 if controlled by free index or a free joint itself
Definition: ikfast.h:59
unsigned char indices[5]
Definition: ikfast.h:60
IkSingleDOFSolutionBase()
Definition: ikfast.h:54
T fmul
joint value is fmul*sol[freeind]+foffset
Definition: ikfast.h:55
signed char freeind
if >= 0, mimics another joint
Definition: ikfast.h:57
The discrete solutions are returned in this structure.
Definition: ikfast.h:72
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:82
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()=default
manages all the solutions
Definition: ikfast.h:103
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
virtual ~IkSolutionListBase()=default
Default implementation of IkSolutionListBase.
Definition: ikfast.h:259
std::list< IkSolution< T > > _listsolutions
Definition: ikfast.h:284
void Clear() override
clears all current solutions, note that any memory addresses returned from GetSolution will be invali...
Definition: ikfast.h:281
size_t AddSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree) override
add one solution and return its index for later retrieval
Definition: ikfast.h:261
const IkSolutionBase< T > & GetSolution(size_t index) const override
returns the solution pointer
Definition: ikfast.h:268
size_t GetNumSolutions() const override
returns the number of solutions stored
Definition: ikfast.h:279
Default implementation of IkSolutionBase.
Definition: ikfast.h:157
virtual void Validate() const
Definition: ikfast.h:199
void GetSolution(std::vector< T > &solution, const std::vector< T > &freevalues) const override
std::vector version of GetSolution
Definition: ikfast.h:187
std::vector< int > _vfree
Definition: ikfast.h:253
virtual void GetSolutionIndices(std::vector< unsigned int > &v) const
Definition: ikfast.h:221
IkSolution(const std::vector< IkSingleDOFSolutionBase< T > > &vinfos, const std::vector< int > &vfree)
Definition: ikfast.h:159
int GetDOF() const override
the dof of the solution
Definition: ikfast.h:197
std::vector< IkSingleDOFSolutionBase< T > > _vbasesol
solution and their offsets if joints are mimiced
Definition: ikfast.h:252
const std::vector< int > & GetFree() const override
Gets the indices of the configuration space that have to be preset before a full solution can be retu...
Definition: ikfast.h:196
void GetSolution(T *solution, const T *freevalues) const override
gets a concrete solution
Definition: ikfast.h:165
Definition: polygon_mesh.h:46
auto it
Definition: collision_core_unit.cpp:208
Definition: ikfast.h:48
v
Definition: tesseract_common_unit.cpp:369
tesseract_geometry::PolygonMesh T
Definition: tesseract_geometry_unit.cpp:140
JointDynamics j
Definition: tesseract_scene_graph_joint_unit.cpp:15
object joints["j_1"]
Definition: tesseract_scene_graph_serialization_unit.cpp:224