IK_solver.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  17. * All rights reserved.
  18. * Original author: Laurence
  19. */
  20. /** \file
  21. * \ingroup iksolver
  22. */
  23. /**
  24. * \page IK - Blender inverse kinematics module.
  25. *
  26. * \section about About the IK module
  27. *
  28. * This module allows you to create segments and form them into
  29. * tree. You can then define a goal points that the end of a given
  30. * segment should attempt to reach - an inverse kinematic problem.
  31. * This module will then modify the segments in the tree in order
  32. * to get the as near as possible to the goal. This solver uses an
  33. * inverse jacobian method to find a solution.
  34. *
  35. * \section issues Known issues with this IK solver.
  36. *
  37. * - There is currently no support for joint constraints in the
  38. * solver. This is within the realms of possibility - please ask
  39. * if this functionality is required.
  40. * - The solver is slow, inverse jacobian methods in general give
  41. * 'smooth' solutions and the method is also very flexible, it
  42. * does not rely on specific angle parameterization and can be
  43. * extended to deal with different joint types and joint
  44. * constraints. However it is not suitable for real time use.
  45. * Other algorithms exist which are more suitable for real-time
  46. * applications, please ask if this functionality is required.
  47. *
  48. * \section dependencies Dependencies
  49. *
  50. * This module only depends on Moto.
  51. */
  52. #ifndef __IK_SOLVER_H__
  53. #define __IK_SOLVER_H__
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * Typical order of calls for solving an IK problem:
  59. *
  60. * - create number of IK_Segment's and set their parents and transforms
  61. * - create an IK_Solver
  62. * - set a number of goals for the IK_Solver to solve
  63. * - call IK_Solve
  64. * - free the IK_Solver
  65. * - get basis and translation changes from segments
  66. * - free all segments
  67. */
  68. /**
  69. * IK_Segment defines a single segment of an IK tree.
  70. * - Individual segments are always defined in local coordinates.
  71. * - The segment is assumed to be oriented in the local
  72. * y-direction.
  73. * - start is the start of the segment relative to the end
  74. * of the parent segment.
  75. * - rest_basis is a column major matrix defineding the rest
  76. * position (w.r.t. which the limits are defined), must
  77. * be a pure rotation
  78. * - basis is a column major matrix defining the current change
  79. * from the rest basis, must be a pure rotation
  80. * - length is the length of the bone.
  81. *
  82. * - basis_change and translation_change respectively define
  83. * the change in rotation or translation. basis_change is a
  84. * column major 3x3 matrix.
  85. *
  86. * The local transformation is then defined as:
  87. * start * rest_basis * basis * basis_change * translation_change * translate(0,length,0)
  88. */
  89. typedef void IK_Segment;
  90. enum IK_SegmentFlag {
  91. IK_XDOF = 1,
  92. IK_YDOF = 2,
  93. IK_ZDOF = 4,
  94. IK_TRANS_XDOF = 8,
  95. IK_TRANS_YDOF = 16,
  96. IK_TRANS_ZDOF = 32
  97. };
  98. typedef enum IK_SegmentAxis {
  99. IK_X = 0,
  100. IK_Y = 1,
  101. IK_Z = 2,
  102. IK_TRANS_X = 3,
  103. IK_TRANS_Y = 4,
  104. IK_TRANS_Z = 5
  105. } IK_SegmentAxis;
  106. extern IK_Segment *IK_CreateSegment(int flag);
  107. extern void IK_FreeSegment(IK_Segment *seg);
  108. extern void IK_SetParent(IK_Segment *seg, IK_Segment *parent);
  109. extern void IK_SetTransform(
  110. IK_Segment *seg, float start[3], float rest_basis[][3], float basis[][3], float length);
  111. extern void IK_SetLimit(IK_Segment *seg, IK_SegmentAxis axis, float lmin, float lmax);
  112. extern void IK_SetStiffness(IK_Segment *seg, IK_SegmentAxis axis, float stiffness);
  113. extern void IK_GetBasisChange(IK_Segment *seg, float basis_change[][3]);
  114. extern void IK_GetTranslationChange(IK_Segment *seg, float *translation_change);
  115. /**
  116. * An IK_Solver must be created to be able to execute the solver.
  117. *
  118. * An arbitray number of goals can be created, stating that a given
  119. * end effector must have a given position or rotation. If multiple
  120. * goals are specified, they can be weighted (range 0..1) to get
  121. * some control over their importance.
  122. *
  123. * IK_Solve will execute the solver, that will run until either the
  124. * system converges, or a maximum number of iterations is reached.
  125. * It returns 1 if the system converged, 0 otherwise.
  126. */
  127. typedef void IK_Solver;
  128. IK_Solver *IK_CreateSolver(IK_Segment *root);
  129. void IK_FreeSolver(IK_Solver *solver);
  130. void IK_SolverAddGoal(IK_Solver *solver, IK_Segment *tip, float goal[3], float weight);
  131. void IK_SolverAddGoalOrientation(IK_Solver *solver,
  132. IK_Segment *tip,
  133. float goal[][3],
  134. float weight);
  135. void IK_SolverSetPoleVectorConstraint(IK_Solver *solver,
  136. IK_Segment *tip,
  137. float goal[3],
  138. float polegoal[3],
  139. float poleangle,
  140. int getangle);
  141. float IK_SolverGetPoleAngle(IK_Solver *solver);
  142. int IK_Solve(IK_Solver *solver, float tolerance, int max_iterations);
  143. #define IK_STRETCH_STIFF_EPS 0.01f
  144. #define IK_STRETCH_STIFF_MIN 0.001f
  145. #define IK_STRETCH_STIFF_MAX 1e10
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif // __IK_SOLVER_H__