solver.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef IK_SOLVER_H
  2. #define IK_SOLVER_H
  3. #include "ik/config.h"
  4. #include "ik/chain_tree.h"
  5. #include "ik/ordered_vector.h"
  6. #include "ik/quat.h"
  7. #include "ik/vec3.h"
  8. C_HEADER_BEGIN
  9. typedef void (*ik_solver_destruct_func)(ik_solver_t*);
  10. typedef int (*ik_solver_post_chain_build_func)(ik_solver_t*);
  11. typedef int (*ik_solver_solve_func)(ik_solver_t*);
  12. typedef void (*ik_solver_iterate_node_cb_func)(ik_node_t*);
  13. typedef enum solver_algorithm_e
  14. {
  15. SOLVER_ONE_BONE,
  16. SOLVER_TWO_BONE,
  17. SOLVER_FABRIK,
  18. SOLVER_MSD
  19. /* TODO Not implemented
  20. SOLVER_JACOBIAN_INVERSE,
  21. SOLVER_JACOBIAN_TRANSPOSE */
  22. } solver_algorithm_e;
  23. typedef enum solver_flags_e
  24. {
  25. /*!
  26. * @brief Causes the root node in the tree to be excluded from the list of
  27. * nodes to solve for. It won't be affected by the solver, but it may still
  28. * be passed through to the result callback function.
  29. */
  30. SOLVER_EXCLUDE_ROOT = 0x01,
  31. SOLVER_ENABLE_CONSTRAINTS = 0x02,
  32. SOLVER_CALCULATE_TARGET_ROTATIONS = 0x04
  33. } solver_flags_e;
  34. /*!
  35. * @brief This is a base for all solvers.
  36. */
  37. #define SOLVER_DATA_HEAD \
  38. int32_t max_iterations; \
  39. float tolerance; \
  40. uint8_t flags; \
  41. \
  42. /* Derived structure callbacks */ \
  43. ik_solver_destruct_func destruct; \
  44. ik_solver_post_chain_build_func post_chain_build; \
  45. ik_solver_solve_func solve; \
  46. \
  47. ordered_vector_t effector_nodes_list; \
  48. ik_node_t* tree; \
  49. /* list of ik_chain_tree_t objects (allocated in-place) */ \
  50. chain_tree_t chain_tree;
  51. struct ik_solver_t
  52. {
  53. SOLVER_DATA_HEAD
  54. };
  55. /*!
  56. * @brief Allocates a new solver object according to the specified algorithm.
  57. *
  58. * Once the solver is created, you can configure the solver to enable/disable
  59. * various features depending on your needs.
  60. *
  61. * The following attributes can be changed at any point.
  62. * + solver->apply_result
  63. * This is the main mechanism with which to obtain the solved data.
  64. * Assign a callback function here and it will be called for every node
  65. * in the tree when a new target position/rotation has been calculated.
  66. * You can use the node->user_data attribute to store external node
  67. * specific data, which can be accessed again the in callback function.
  68. * + solver->max_iterations
  69. * Specifies the maximum number of iterations. The more iterations, the
  70. * more exact the result will be. The default value for the FABRIK solver
  71. * is 20, but you can get away with values as low as 5.
  72. * + solver->tolerance
  73. * This value can be changed at any point. Specifies the acceptable
  74. * distance each effector needs to be to its target position. The solver
  75. * will stop iterating if the effectors are within this distance. The
  76. * default value is 1e-3. Recommended values are 100th of your world
  77. * unit.
  78. * + solver->flags
  79. * Changes the behaviour of the solver. See the enum solver_flags_e for
  80. * more information.
  81. *
  82. * The following attributes can be accessed (read from) but should not be
  83. * modified.
  84. * + solver->tree
  85. * The tree to be solved. You may modify the nodes in the tree.
  86. * @note If you add/remove nodes or if you add/remove effectors, you
  87. * must call ik_solver_rebuild_data() so the internal solver structures
  88. * are updated. Failing to do so may cause segfaults. If you're just
  89. * updating positions/rotations or any of the other public data then
  90. * there is no need to rebuild data.
  91. * + solver->effector_nodes_list
  92. * A vector containing pointers to nodes in the tree which have an
  93. * effector attached to them. You may not modify this list, but you may
  94. * iterate it.
  95. * @param[in] algorithm The algorithm to use. Currently, only FABRIK is
  96. * supported.
  97. */
  98. IK_PUBLIC_API ik_solver_t*
  99. ik_solver_create(solver_algorithm_e algorithm);
  100. /*!
  101. * @brief Destroys the solver and all nodes/effectors that are part of the
  102. * solver. Any pointers to tree nodes are invalid after this function returns.
  103. */
  104. IK_PUBLIC_API void
  105. ik_solver_destroy(ik_solver_t* solver);
  106. /*!
  107. * @brief Sets the tree to solve. The solver takes ownership of the tree, so
  108. * destroying the solver will destroy all nodes in the tree. Note that you will
  109. * have to call ik_solver_rebuild_data() before being able to solve it. If the
  110. * solver already has a tree, then said tree will be destroyed.
  111. */
  112. IK_PUBLIC_API void
  113. ik_solver_set_tree(ik_solver_t* solver, ik_node_t* root);
  114. /*!
  115. * @brief The solver releases any references to a previously set tree and
  116. * returns the root node of said tree. Any proceeding calls that involve the
  117. * tree (e.g. solve or rebuild) will have no effect until a new tree is set.
  118. * @return If the solver has no tree then NULL is returned.
  119. */
  120. IK_PUBLIC_API ik_node_t*
  121. ik_solver_unlink_tree(ik_solver_t* solver);
  122. /*!
  123. * @brief The solver releases any references to a previously set tree and
  124. * destroys it.
  125. */
  126. IK_PUBLIC_API void
  127. ik_solver_destroy_tree(ik_solver_t* solver);
  128. /*!
  129. * @brief Causes the set tree to be processed into more optimal data structures
  130. * for solving. Must be called before ik_solver_solve().
  131. * @note Needs to be called whenever the tree changes in any way. I.e. if you
  132. * remove nodes or add nodes, or if you remove effectors or add effectors,
  133. * you must call this again before calling the solver.
  134. * @return Returns non-zero if any of the chain trees are invalid for any
  135. * reason. If this happens, check the log for error messages.
  136. * @warning If this functions fails, the internal structures are in an
  137. * undefined state. You cannot solve the tree in this state.
  138. */
  139. IK_PUBLIC_API int
  140. ik_solver_rebuild_chain_trees(ik_solver_t* solver);
  141. /*!
  142. * @brief Unusual, but if you have a tree with translational motions such that
  143. * the distances between nodes changes (perhaps a slider?), you can call this
  144. * to re-calculate the segment lengths after assigning new positions to the
  145. * nodes.
  146. * @note This function gets called by ik_solver_rebuild_data().
  147. */
  148. IK_PUBLIC_API void
  149. ik_solver_recalculate_segment_lengths(ik_solver_t* solver);
  150. /*!
  151. * @brief Solves the IK problem. The node solutions will be provided via a
  152. * callback function, which can be registered to the solver by assigning it to
  153. * solver->apply_result.
  154. */
  155. IK_PUBLIC_API int
  156. ik_solver_solve(ik_solver_t* solver);
  157. IK_PUBLIC_API void
  158. ik_solver_calculate_joint_rotations(ik_solver_t* solver);
  159. /*!
  160. * @brief Iterates all nodes in the internal tree, breadth first, and passes
  161. * each node to the specified callback function.
  162. */
  163. IK_PUBLIC_API void
  164. ik_solver_iterate_tree(ik_solver_t* solver,
  165. ik_solver_iterate_node_cb_func callback);
  166. /*!
  167. * @brief Iterates just the nodes that are being affected by the solver. Useful
  168. * for a more optimized write-back of the solution data.
  169. */
  170. IK_PUBLIC_API void
  171. ik_solver_iterate_chain_tree(ik_solver_t* solver,
  172. ik_solver_iterate_node_cb_func callback);
  173. /*!
  174. * @brief Sets the solved positions and rotations equal to the original
  175. * positions and rotations for every node in the tree.
  176. */
  177. IK_PUBLIC_API void
  178. ik_solver_reset_to_original_pose(ik_solver_t* solver);
  179. C_HEADER_END
  180. #endif /* IK_SOLVER_H */