solver_FABRIK.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #include "ik/bst_vector.h"
  2. #include "ik/constraint.h"
  3. #include "ik/effector.h"
  4. #include "ik/log.h"
  5. #include "ik/memory.h"
  6. #include "ik/node.h"
  7. #include "ik/solver_FABRIK.h"
  8. #include <assert.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12. typedef struct position_direction_t
  13. {
  14. vec3_t position;
  15. vec3_t direction;
  16. } position_direction_t;
  17. typedef struct transform_t
  18. {
  19. vec3_t position;
  20. quat_t rotation;
  21. } transform_t;
  22. /* ------------------------------------------------------------------------- */
  23. int
  24. solver_FABRIK_construct(ik_solver_t* solver)
  25. {
  26. fabrik_t* fabrik = (fabrik_t*)solver;
  27. /* set up derived functions */
  28. fabrik->destruct = solver_FABRIK_destruct;
  29. fabrik->solve = solver_FABRIK_solve;
  30. /* typical default values */
  31. fabrik->max_iterations = 20;
  32. fabrik->tolerance = 1e-3;
  33. return 0;
  34. }
  35. /* ------------------------------------------------------------------------- */
  36. void
  37. solver_FABRIK_destruct(ik_solver_t* solver)
  38. {
  39. }
  40. /* ------------------------------------------------------------------------- */
  41. static void
  42. determine_target_data_from_effector(chain_t* chain, vec3_t* target_position)
  43. {
  44. /* Extract effector node and get its effector object */
  45. ik_node_t* effector_node;
  46. ik_effector_t* effector;
  47. assert(ordered_vector_count(&chain->nodes) > 1);
  48. effector_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, 0);
  49. assert(effector_node->effector != NULL);
  50. effector = effector_node->effector;
  51. /* lerp using effector weight to get actual target position */
  52. *target_position = effector->target_position;
  53. vec3_sub_vec3(target_position->f, effector_node->original_position.f);
  54. vec3_mul_scalar(target_position->f, effector->weight);
  55. vec3_add_vec3(target_position->f, effector_node->original_position.f);
  56. /* Fancy algorithm using nlerp, makes transitions look more natural */
  57. if (effector->flags & EFFECTOR_WEIGHT_NLERP && effector->weight < 1.0)
  58. {
  59. ik_real distance_to_target;
  60. vec3_t base_to_effector;
  61. vec3_t base_to_target;
  62. ik_node_t* base_node;
  63. /* Need distance from base node to target and base to effector node */
  64. base_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes,
  65. ordered_vector_count(&chain->nodes) - 1);
  66. base_to_effector = effector_node->original_position;
  67. base_to_target = effector->target_position;
  68. vec3_sub_vec3(base_to_effector.f, base_node->original_position.f);
  69. vec3_sub_vec3(base_to_target.f, base_node->original_position.f);
  70. /* The effective distance is a lerp between these two distances */
  71. distance_to_target = vec3_length(base_to_target.f) * effector->weight;
  72. distance_to_target += vec3_length(base_to_effector.f) * (1.0 - effector->weight);
  73. /* nlerp the target position by pinning it to the base node */
  74. vec3_sub_vec3(target_position->f, base_node->original_position.f);
  75. vec3_normalise(target_position->f);
  76. vec3_mul_scalar(target_position->f, distance_to_target);
  77. vec3_add_vec3(target_position->f, base_node->original_position.f);
  78. }
  79. }
  80. /* ------------------------------------------------------------------------- */
  81. static position_direction_t
  82. solve_chain_forwards_with_target_rotation(chain_t* chain)
  83. {
  84. int node_count, node_idx;
  85. int average_count;
  86. position_direction_t target;
  87. vec3_set_zero(target.position.f);
  88. /*
  89. * Target position is the average of all solved child chain base positions.
  90. */
  91. average_count = 0;
  92. ORDERED_VECTOR_FOR_EACH(&chain->children, chain_t, child)
  93. position_direction_t child_posdir = solve_chain_forwards_with_target_rotation(child);
  94. vec3_add_vec3(target.position.f, child_posdir.position.f);
  95. vec3_add_vec3(target.direction.f, child_posdir.direction.f);
  96. ++average_count;
  97. ORDERED_VECTOR_END_EACH
  98. /*
  99. * If there are no child chains, then the first node in the chain must
  100. * contain an effector. The target position is the effector's target
  101. * position. Otherwise, average the data we've been accumulating from the
  102. * child chains.
  103. */
  104. if (average_count == 0)
  105. {
  106. ik_node_t* effector_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, 0);
  107. ik_effector_t* effector = effector_node->effector;
  108. determine_target_data_from_effector(chain, &target.position);
  109. /* TODO This "global direction" could be made configurable if (needed */
  110. target.direction.v.x = 0.0;
  111. target.direction.v.y = 0.0;
  112. target.direction.v.z = 1.0;
  113. quat_rotate_vec(target.direction.f, effector->target_rotation.f);
  114. }
  115. else
  116. {
  117. ik_real div = 1.0 / average_count;
  118. vec3_mul_scalar(target.position.f, div);
  119. vec3_mul_scalar(target.direction.f, div);
  120. }
  121. /*
  122. * Iterate through each segment and apply the FABRIK algorithm.
  123. */
  124. node_count = ordered_vector_count(&chain->nodes);
  125. for (node_idx = 0; node_idx < node_count - 1; ++node_idx)
  126. {
  127. ik_node_t* child_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 0);
  128. ik_node_t* parent_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 1);
  129. /* move node to target */
  130. child_node->position = target.position;
  131. /* lerp direction vector and segment vector */
  132. vec3_sub_vec3(target.position.f, target.direction.f);
  133. vec3_sub_vec3(target.position.f, parent_node->position.f);
  134. vec3_mul_scalar(target.position.f, parent_node->rotation_weight);
  135. vec3_add_vec3(target.position.f, parent_node->position.f);
  136. vec3_sub_vec3(target.position.f, child_node->position.f);
  137. /* point segment to previous node and set target position to its end */
  138. vec3_normalise(target.position.f);
  139. vec3_mul_scalar(target.position.f, child_node->segment_length);
  140. vec3_add_vec3(target.position.f, child_node->position.f);
  141. }
  142. return target;
  143. }
  144. /* ------------------------------------------------------------------------- */
  145. vec3_t
  146. solve_chain_forwards_with_constraints(chain_t* chain)
  147. {
  148. int node_count, node_idx;
  149. int average_count;
  150. vec3_t target_position = {{0, 0, 0}};
  151. /*
  152. * Target position is the average of all solved child chain base positions.
  153. */
  154. average_count = 0;
  155. ORDERED_VECTOR_FOR_EACH(&chain->children, chain_t, child)
  156. vec3_t child_base_position = solve_chain_forwards_with_constraints(child);
  157. vec3_add_vec3(target_position.f, child_base_position.f);
  158. ++average_count;
  159. ORDERED_VECTOR_END_EACH
  160. /*
  161. * If there are no child chains, then the first node in the chain must
  162. * contain an effector. The target position is the effector's target
  163. * position. Otherwise, average the data we've been accumulating from the
  164. * child chains.
  165. */
  166. if (average_count == 0)
  167. determine_target_data_from_effector(chain, &target_position);
  168. else
  169. vec3_div_scalar(target_position.f, average_count);
  170. /*
  171. * Iterate through each segment and apply the FABRIK algorithm.
  172. */
  173. node_count = ordered_vector_count(&chain->nodes);
  174. for (node_idx = 0; node_idx < node_count - 1; ++node_idx)
  175. {
  176. /*vec3_t segment_original, segment_current;*/
  177. ik_node_t* child_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 0);
  178. ik_node_t* parent_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 1);
  179. /* move node to target */
  180. child_node->position = target_position;
  181. /* point segment to previous node and set target position to its end */
  182. vec3_sub_vec3(target_position.f, parent_node->position.f); /* parent points to child */
  183. vec3_normalise(target_position.f); /* normalise */
  184. vec3_mul_scalar(target_position.f, -child_node->segment_length); /* child points to parent */
  185. vec3_add_vec3(target_position.f, child_node->position.f); /* attach to child -- this is the new target */
  186. /* Calculate global rotation of parent node *
  187. segment_original = child_node->initial_position;
  188. segment_current = child_node->position;
  189. vec3_sub_vec3(segment_original.f, parent_node->initial_position.f);
  190. vec3_sub_vec3(segment_current.f, target_position.f);
  191. vec3_angle(parent_node->rotation.f, segment_original.f, segment_current.f);
  192. quat_mul_quat(parent_node->rotation.f, parent_node->initial_rotation.f);
  193. * Convert global transform to local *
  194. inv_rotation = accumulated.rotation;
  195. quat_conj(inv_rotation.f);
  196. quat_mul_quat(parent_node->rotation.f, inv_rotation.f);
  197. vec3_sub_vec3(parent_node->position.f, accumulated.position.f);
  198. quat_rotate_vec(parent_node->position.f, inv_rotation.f);
  199. if (child_node->constraint != NULL)
  200. child_node->constraint->apply(parent_node);
  201. * Accumulate local rotation and translation for deeper nodes *after*
  202. * constraint was applied *
  203. accumulated_previous = accumulated;
  204. quat_mul_quat(accumulated.rotation.f, parent_node->rotation.f);
  205. vec3_add_vec3(accumulated.position.f, parent_node->position.f);
  206. * Convert local transform back to global *
  207. quat_rotate_vec(parent_node->position.f, accumulated_previous.rotation.f);
  208. vec3_add_vec3(parent_node->position.f, accumulated_previous.position.f);
  209. quat_mul_quat(parent_node->rotation.f, accumulated_previous.rotation.f);
  210. if (child_node->constraint != NULL)
  211. {
  212. * XXX combine this? *
  213. inv_rotation = parent_node->initial_rotation;
  214. quat_conj(inv_rotation.f);
  215. quat_mul_quat(parent_node->rotation.f, inv_rotation.f);
  216. target_position = parent_node->position;
  217. quat_rotate_vec(segment_original.f, parent_node->rotation.f);
  218. vec3_add_vec3(target_position.f, segment_original.f);
  219. }*/
  220. }
  221. return target_position;
  222. }
  223. /* ------------------------------------------------------------------------- */
  224. vec3_t
  225. solve_chain_forwards(chain_t* chain)
  226. {
  227. int node_count, node_idx;
  228. int average_count;
  229. vec3_t target_position = {{0, 0, 0}};
  230. /*
  231. * Target position is the average of all solved child chain base positions.
  232. */
  233. average_count = 0;
  234. ORDERED_VECTOR_FOR_EACH(&chain->children, chain_t, child)
  235. vec3_t child_base_position = solve_chain_forwards(child);
  236. vec3_add_vec3(target_position.f, child_base_position.f);
  237. ++average_count;
  238. ORDERED_VECTOR_END_EACH
  239. /*
  240. * If there are no child chains, then the first node in the chain must
  241. * contain an effector. The target position is the effector's target
  242. * position. Otherwise, average the data we've been accumulating from the
  243. * child chains.
  244. */
  245. if (average_count == 0)
  246. determine_target_data_from_effector(chain, &target_position);
  247. else
  248. vec3_div_scalar(target_position.f, average_count);
  249. /*
  250. * Iterate through each segment and apply the FABRIK algorithm.
  251. */
  252. node_count = ordered_vector_count(&chain->nodes);
  253. for (node_idx = 0; node_idx < node_count - 1; ++node_idx)
  254. {
  255. ik_node_t* child_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 0);
  256. ik_node_t* parent_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 1);
  257. /* move node to target */
  258. child_node->position = target_position;
  259. /* point segment to previous node and set target position to its end */
  260. vec3_sub_vec3(target_position.f, parent_node->position.f); /* parent points to child */
  261. vec3_normalise(target_position.f); /* normalise */
  262. vec3_mul_scalar(target_position.f, -child_node->segment_length); /* child points to parent */
  263. vec3_add_vec3(target_position.f, child_node->position.f); /* attach to child -- this is the new target */
  264. }
  265. return target_position;
  266. }
  267. /* ------------------------------------------------------------------------- */
  268. static void
  269. solve_chain_backwards_with_constraints(chain_t* chain,
  270. vec3_t target_position,
  271. vec3_t accumulated_positions)
  272. {
  273. int node_idx = ordered_vector_count(&chain->nodes) - 1;
  274. /*
  275. * The base node must be set to the target position before iterating.
  276. */
  277. if (node_idx > 1)
  278. {
  279. ik_node_t* base_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx);
  280. base_node->position = target_position;
  281. }
  282. /*
  283. * Iterate through each segment the other way around and apply the FABRIK
  284. * algorithm.
  285. */
  286. while (node_idx-- > 0)
  287. {
  288. ik_node_t* child_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 0);
  289. ik_node_t* parent_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 1);
  290. /* point segment to child node and set target position to its beginning */
  291. vec3_sub_vec3(target_position.f, child_node->position.f); /* child points to parent */
  292. vec3_normalise(target_position.f); /* normalise */
  293. vec3_mul_scalar(target_position.f, -child_node->segment_length); /* parent points to child */
  294. vec3_add_vec3(target_position.f, parent_node->position.f); /* attach to parent -- this is the new target */
  295. /* target_position is now where the position of child_node should be. */
  296. /* Calculate delta rotation of parent node *
  297. segment_original = child_node->initial_position;
  298. segment_current = target_position;
  299. vec3_sub_vec3(segment_original.f, parent_node->initial_position.f);
  300. vec3_sub_vec3(segment_current.f, parent_node->position.f);
  301. vec3_angle(parent_node->rotation.f, segment_original.f, segment_current.f);
  302. *
  303. * Since the initial rotation is in local space temporarily (see
  304. * solve() entry point on why), we now have the rotation in local space
  305. *
  306. quat_mul_quat(parent_node->rotation.f, parent_node->initial_rotation.f);
  307. * Convert global translation to local *
  308. inv_rotation = accumulated_positions.rotation;
  309. quat_conj(inv_rotation.f);
  310. vec3_sub_vec3(parent_node->position.f, accumulated_positions.position.f);
  311. quat_rotate_vec(parent_node->position.f, inv_rotation.f);
  312. if (child_node->constraint != NULL)
  313. child_node->constraint->apply(parent_node);
  314. * Accumulate local rotation and translation for deeper nodes *after*
  315. * constraint was applied *
  316. accumulated_previous = accumulated_positions;
  317. vec3_add_vec3(accumulated_positions.position.f, parent_node->position.f);
  318. * Convert local transform back to global *
  319. quat_rotate_vec(parent_node->position.f, accumulated_previous.rotation.f);
  320. vec3_add_vec3(parent_node->position.f, accumulated_previous.position.f);
  321. quat_mul_quat(parent_node->rotation.f, accumulated_previous.rotation.f);
  322. if (child_node->constraint != NULL)
  323. {
  324. * XXX combine this? *
  325. inv_rotation = parent_node->initial_rotation;
  326. quat_conj(inv_rotation.f);
  327. quat_mul_quat(parent_node->rotation.f, inv_rotation.f);
  328. target_position = parent_node->position;
  329. quat_rotate_vec(segment_original.f, parent_node->rotation.f);
  330. vec3_add_vec3(target_position.f, segment_original.f);
  331. }*/
  332. /* move node to target */
  333. child_node->position = target_position;
  334. }
  335. ORDERED_VECTOR_FOR_EACH(&chain->children, chain_t, child)
  336. solve_chain_backwards_with_constraints(child, target_position, accumulated_positions);
  337. ORDERED_VECTOR_END_EACH
  338. }
  339. /* ------------------------------------------------------------------------- */
  340. void
  341. solve_chain_backwards(chain_t* chain, vec3_t target_position)
  342. {
  343. int node_idx = ordered_vector_count(&chain->nodes) - 1;
  344. /*
  345. * The base node must be set to the target position before iterating.
  346. */
  347. if (node_idx > 1)
  348. {
  349. ik_node_t* base_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx);
  350. base_node->position = target_position;
  351. }
  352. /*
  353. * Iterate through each segment the other way around and apply the FABRIK
  354. * algorithm.
  355. */
  356. while (node_idx-- > 0)
  357. {
  358. ik_node_t* child_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 0);
  359. ik_node_t* parent_node = *(ik_node_t**)ordered_vector_get_element(&chain->nodes, node_idx + 1);
  360. /* point segment to child node and set target position to its beginning */
  361. vec3_sub_vec3(target_position.f, child_node->position.f); /* child points to parent */
  362. vec3_normalise(target_position.f); /* normalise */
  363. vec3_mul_scalar(target_position.f, -child_node->segment_length); /* parent points to child */
  364. vec3_add_vec3(target_position.f, parent_node->position.f); /* attach to parent -- this is the new target */
  365. /* move node to target */
  366. child_node->position = target_position;
  367. }
  368. ORDERED_VECTOR_FOR_EACH(&chain->children, chain_t, child)
  369. solve_chain_backwards(child, target_position);
  370. ORDERED_VECTOR_END_EACH
  371. }
  372. /* ------------------------------------------------------------------------- */
  373. static void
  374. initial_to_global_recursive(ik_node_t* node, quat_t acc_rot)
  375. {
  376. quat_t rotation = node->original_rotation;
  377. quat_mul_quat(node->original_rotation.f, acc_rot.f);
  378. quat_mul_quat(acc_rot.f, rotation.f);
  379. BSTV_FOR_EACH(&node->children, ik_node_t, guid, child)
  380. initial_to_global_recursive(child, acc_rot);
  381. BSTV_END_EACH
  382. }
  383. void
  384. initial_rotation_to_global(ik_node_t* node)
  385. {
  386. quat_t acc_rot = {{0, 0, 0, 1}};
  387. initial_to_global_recursive(node, acc_rot);
  388. }
  389. /* ------------------------------------------------------------------------- */
  390. static void
  391. initial_to_local_recursive(ik_node_t* node, quat_t acc_rot)
  392. {
  393. quat_t inv_rotation = acc_rot;
  394. quat_conj(inv_rotation.f);
  395. quat_mul_quat(node->original_rotation.f, inv_rotation.f);
  396. quat_mul_quat(acc_rot.f, node->original_rotation.f);
  397. BSTV_FOR_EACH(&node->children, ik_node_t, guid, child)
  398. initial_to_local_recursive(child, acc_rot);
  399. BSTV_END_EACH
  400. }
  401. void
  402. initial_rotation_to_local(ik_node_t* node)
  403. {
  404. quat_t acc_rot = {{0, 0, 0, 1}};
  405. initial_to_local_recursive(node, acc_rot);
  406. }
  407. /* ------------------------------------------------------------------------- */
  408. int
  409. solver_FABRIK_solve(ik_solver_t* solver)
  410. {
  411. int result = 0;
  412. fabrik_t* fabrik = (fabrik_t*)solver;
  413. int iteration = solver->max_iterations;
  414. ik_real tolerance_squared = solver->tolerance * solver->tolerance;
  415. /*
  416. * NOTE: Kind of a hack. Initial rotations are in local space during
  417. * iteration.
  418. *
  419. * FABRIK works entirely in global space, so when constraints come into
  420. * play, it is necessary to calculate joint angles and convert global
  421. * positions into local positions. The constrained angles are then
  422. * converted back again into global space.
  423. *
  424. * As you can imagine, this process is costly. We can actually cut down on
  425. * a significant number of operations if the initial rotations are in local
  426. * space. The algorithm doesn't need initial rotations, so this should have
  427. * no side effects. We just need to make sure to convert the rotations back
  428. * into global space after the algorithm has completed.
  429. */
  430. if (solver->flags & SOLVER_ENABLE_CONSTRAINTS)
  431. initial_rotation_to_local(solver->tree);
  432. while (iteration-- > 0)
  433. {
  434. vec3_t root_position;
  435. /* Actual algorithm here */
  436. ORDERED_VECTOR_FOR_EACH(&fabrik->chain_tree.islands, chain_island_t, island)
  437. chain_t* root_chain = &island->root_chain;
  438. /* The algorithm assumes chains have at least one bone. This should
  439. * be asserted while building the chain trees, but it can't hurt
  440. * to double check */
  441. assert(ordered_vector_count(&root_chain->nodes) > 1);
  442. root_position = (*(ik_node_t**)ordered_vector_get_element(&root_chain->nodes,
  443. ordered_vector_count(&root_chain->nodes) - 1))->position;
  444. if (solver->flags & SOLVER_CALCULATE_TARGET_ROTATIONS)
  445. solve_chain_forwards_with_target_rotation(root_chain);
  446. else
  447. solve_chain_forwards(root_chain);
  448. if (solver->flags & SOLVER_ENABLE_CONSTRAINTS)
  449. solve_chain_backwards_with_constraints(root_chain, root_position, root_position);
  450. else
  451. solve_chain_backwards(root_chain, root_position);
  452. ORDERED_VECTOR_END_EACH
  453. /* Check if all effectors are within range */
  454. ORDERED_VECTOR_FOR_EACH(&fabrik->effector_nodes_list, ik_node_t*, pnode)
  455. vec3_t diff = (*pnode)->position;
  456. vec3_sub_vec3(diff.f, (*pnode)->effector->target_position.f);
  457. if (vec3_length_squared(diff.f) > tolerance_squared)
  458. {
  459. result = 1; /* converged */
  460. break;
  461. }
  462. ORDERED_VECTOR_END_EACH
  463. }
  464. /* Restore initial rotations to global space again. See above as to why. */
  465. if (solver->flags & SOLVER_ENABLE_CONSTRAINTS)
  466. initial_rotation_to_global(solver->tree);
  467. return result;
  468. }