anim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file blender/blenkernel/intern/anim.c
  28. * \ingroup bke
  29. */
  30. #include "MEM_guardedalloc.h"
  31. #include <stdlib.h>
  32. #include "BLI_listbase.h"
  33. #include "BLI_math.h"
  34. #include "BLT_translation.h"
  35. #include "DNA_anim_types.h"
  36. #include "DNA_armature_types.h"
  37. #include "DNA_key_types.h"
  38. #include "DNA_scene_types.h"
  39. #include "BKE_curve.h"
  40. #include "BKE_depsgraph.h"
  41. #include "BKE_global.h"
  42. #include "BKE_key.h"
  43. #include "BKE_main.h"
  44. #include "BKE_object.h"
  45. #include "BKE_particle.h"
  46. #include "BKE_scene.h"
  47. #include "BKE_anim.h"
  48. #include "BKE_report.h"
  49. // XXX bad level call...
  50. /* --------------------- */
  51. /* forward declarations */
  52. /* ******************************************************************** */
  53. /* Animation Visualization */
  54. /* Initialize the default settings for animation visualization */
  55. void animviz_settings_init(bAnimVizSettings *avs)
  56. {
  57. /* sanity check */
  58. if (avs == NULL)
  59. return;
  60. /* ghosting settings */
  61. avs->ghost_bc = avs->ghost_ac = 10;
  62. avs->ghost_sf = 1; /* xxx - take from scene instead? */
  63. avs->ghost_ef = 250; /* xxx - take from scene instead? */
  64. avs->ghost_step = 1;
  65. /* path settings */
  66. avs->path_bc = avs->path_ac = 10;
  67. avs->path_sf = 1; /* xxx - take from scene instead? */
  68. avs->path_ef = 250; /* xxx - take from scene instead? */
  69. avs->path_viewflag = (MOTIONPATH_VIEW_KFRAS | MOTIONPATH_VIEW_KFNOS);
  70. avs->path_step = 1;
  71. }
  72. /* ------------------- */
  73. /* Free the given motion path's cache */
  74. void animviz_free_motionpath_cache(bMotionPath *mpath)
  75. {
  76. /* sanity check */
  77. if (mpath == NULL)
  78. return;
  79. /* free the path if necessary */
  80. if (mpath->points)
  81. MEM_freeN(mpath->points);
  82. /* reset the relevant parameters */
  83. mpath->points = NULL;
  84. mpath->length = 0;
  85. }
  86. /* Free the given motion path instance and its data
  87. * NOTE: this frees the motion path given!
  88. */
  89. void animviz_free_motionpath(bMotionPath *mpath)
  90. {
  91. /* sanity check */
  92. if (mpath == NULL)
  93. return;
  94. /* free the cache first */
  95. animviz_free_motionpath_cache(mpath);
  96. /* now the instance itself */
  97. MEM_freeN(mpath);
  98. }
  99. /* ------------------- */
  100. /**
  101. * Setup motion paths for the given data.
  102. * \note Only used when explicitly calculating paths on bones which may/may not be consider already
  103. *
  104. * \param scene Current scene (for frame ranges, etc.)
  105. * \param ob Object to add paths for (must be provided)
  106. * \param pchan Posechannel to add paths for (optional; if not provided, object-paths are assumed)
  107. */
  108. bMotionPath *animviz_verify_motionpaths(ReportList *reports, Scene *scene, Object *ob, bPoseChannel *pchan)
  109. {
  110. bAnimVizSettings *avs;
  111. bMotionPath *mpath, **dst;
  112. /* sanity checks */
  113. if (ELEM(NULL, scene, ob))
  114. return NULL;
  115. /* get destination data */
  116. if (pchan) {
  117. /* paths for posechannel - assume that posechannel belongs to the object */
  118. avs = &ob->pose->avs;
  119. dst = &pchan->mpath;
  120. }
  121. else {
  122. /* paths for object */
  123. avs = &ob->avs;
  124. dst = &ob->mpath;
  125. }
  126. /* avoid 0 size allocs */
  127. if (avs->path_sf >= avs->path_ef) {
  128. BKE_reportf(reports, RPT_ERROR,
  129. "Motion path frame extents invalid for %s (%d to %d)%s",
  130. (pchan) ? pchan->name : ob->id.name,
  131. avs->path_sf, avs->path_ef,
  132. (avs->path_sf == avs->path_ef) ? TIP_(", cannot have single-frame paths") : "");
  133. return NULL;
  134. }
  135. /* if there is already a motionpath, just return that,
  136. * provided it's settings are ok (saves extra free+alloc)
  137. */
  138. if (*dst != NULL) {
  139. int expected_length = avs->path_ef - avs->path_sf;
  140. mpath = *dst;
  141. /* path is "valid" if length is valid, but must also be of the same length as is being requested */
  142. if ((mpath->start_frame != mpath->end_frame) && (mpath->length > 0)) {
  143. /* outer check ensures that we have some curve data for this path */
  144. if (mpath->length == expected_length) {
  145. /* return/use this as it is already valid length */
  146. return mpath;
  147. }
  148. else {
  149. /* clear the existing path (as the range has changed), and reallocate below */
  150. animviz_free_motionpath_cache(mpath);
  151. }
  152. }
  153. }
  154. else {
  155. /* create a new motionpath, and assign it */
  156. mpath = MEM_callocN(sizeof(bMotionPath), "bMotionPath");
  157. *dst = mpath;
  158. }
  159. /* set settings from the viz settings */
  160. mpath->start_frame = avs->path_sf;
  161. mpath->end_frame = avs->path_ef;
  162. mpath->length = mpath->end_frame - mpath->start_frame;
  163. if (avs->path_bakeflag & MOTIONPATH_BAKE_HEADS)
  164. mpath->flag |= MOTIONPATH_FLAG_BHEAD;
  165. else
  166. mpath->flag &= ~MOTIONPATH_FLAG_BHEAD;
  167. /* set default custom values */
  168. mpath->color[0] = 1.0; /* Red */
  169. mpath->color[1] = 0.0;
  170. mpath->color[2] = 0.0;
  171. mpath->line_thickness = 1;
  172. mpath->flag |= MOTIONPATH_FLAG_LINES; /* draw lines by default */
  173. /* allocate a cache */
  174. mpath->points = MEM_callocN(sizeof(bMotionPathVert) * mpath->length, "bMotionPathVerts");
  175. /* tag viz settings as currently having some path(s) which use it */
  176. avs->path_bakeflag |= MOTIONPATH_BAKE_HAS_PATHS;
  177. /* return it */
  178. return mpath;
  179. }
  180. /* ------------------- */
  181. /* Motion path needing to be baked (mpt) */
  182. typedef struct MPathTarget {
  183. struct MPathTarget *next, *prev;
  184. bMotionPath *mpath; /* motion path in question */
  185. Object *ob; /* source object */
  186. bPoseChannel *pchan; /* source posechannel (if applicable) */
  187. } MPathTarget;
  188. /* ........ */
  189. /* get list of motion paths to be baked for the given object
  190. * - assumes the given list is ready to be used
  191. */
  192. /* TODO: it would be nice in future to be able to update objects dependent on these bones too? */
  193. void animviz_get_object_motionpaths(Object *ob, ListBase *targets)
  194. {
  195. MPathTarget *mpt;
  196. /* object itself first */
  197. if ((ob->avs.recalc & ANIMVIZ_RECALC_PATHS) && (ob->mpath)) {
  198. /* new target for object */
  199. mpt = MEM_callocN(sizeof(MPathTarget), "MPathTarget Ob");
  200. BLI_addtail(targets, mpt);
  201. mpt->mpath = ob->mpath;
  202. mpt->ob = ob;
  203. }
  204. /* bones */
  205. if ((ob->pose) && (ob->pose->avs.recalc & ANIMVIZ_RECALC_PATHS)) {
  206. bArmature *arm = ob->data;
  207. bPoseChannel *pchan;
  208. for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
  209. if ((pchan->bone) && (arm->layer & pchan->bone->layer) && (pchan->mpath)) {
  210. /* new target for bone */
  211. mpt = MEM_callocN(sizeof(MPathTarget), "MPathTarget PoseBone");
  212. BLI_addtail(targets, mpt);
  213. mpt->mpath = pchan->mpath;
  214. mpt->ob = ob;
  215. mpt->pchan = pchan;
  216. }
  217. }
  218. }
  219. }
  220. /* ........ */
  221. /* Note on evaluation optimizations:
  222. * Optimization's currently used here play tricks with the depsgraph in order to try and
  223. * evaluate as few objects as strictly necessary to get nicer performance under standard
  224. * production conditions. For those people who really need the accurate version,
  225. * disable the ifdef (i.e. 1 -> 0) and comment out the call to motionpaths_calc_optimise_depsgraph()
  226. */
  227. /* tweak the object ordering to trick depsgraph into making MotionPath calculations run faster */
  228. static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets)
  229. {
  230. Base *base, *baseNext;
  231. MPathTarget *mpt;
  232. /* make sure our temp-tag isn't already in use */
  233. for (base = scene->base.first; base; base = base->next)
  234. base->object->flag &= ~BA_TEMP_TAG;
  235. /* for each target, dump its object to the start of the list if it wasn't moved already */
  236. for (mpt = targets->first; mpt; mpt = mpt->next) {
  237. for (base = scene->base.first; base; base = baseNext) {
  238. baseNext = base->next;
  239. if ((base->object == mpt->ob) && !(mpt->ob->flag & BA_TEMP_TAG)) {
  240. BLI_remlink(&scene->base, base);
  241. BLI_addhead(&scene->base, base);
  242. mpt->ob->flag |= BA_TEMP_TAG;
  243. /* we really don't need to continue anymore once this happens, but this line might really 'break' */
  244. break;
  245. }
  246. }
  247. }
  248. /* "brew me a list that's sorted a bit faster now depsy" */
  249. DAG_scene_relations_rebuild(G.main, scene);
  250. }
  251. /* update scene for current frame */
  252. static void motionpaths_calc_update_scene(Scene *scene)
  253. {
  254. #if 1 // 'production' optimizations always on
  255. /* rigid body simulation needs complete update to work correctly for now */
  256. /* RB_TODO investigate if we could avoid updating everything */
  257. if (BKE_scene_check_rigidbody_active(scene)) {
  258. BKE_scene_update_for_newframe(G.main->eval_ctx, G.main, scene, scene->lay);
  259. }
  260. else { /* otherwise we can optimize by restricting updates */
  261. Base *base, *last = NULL;
  262. /* only stuff that moves or needs display still */
  263. DAG_scene_update_flags(G.main, scene, scene->lay, true, false);
  264. /* find the last object with the tag
  265. * - all those afterwards are assumed to not be relevant for our calculations
  266. */
  267. /* optimize further by moving out... */
  268. for (base = scene->base.first; base; base = base->next) {
  269. if (base->object->flag & BA_TEMP_TAG)
  270. last = base;
  271. }
  272. /* perform updates for tagged objects */
  273. /* XXX: this will break if rigs depend on scene or other data that
  274. * is animated but not attached to/updatable from objects */
  275. for (base = scene->base.first; base; base = base->next) {
  276. /* update this object */
  277. BKE_object_handle_update(G.main->eval_ctx, scene, base->object);
  278. /* if this is the last one we need to update, let's stop to save some time */
  279. if (base == last)
  280. break;
  281. }
  282. }
  283. #else // original, 'always correct' version
  284. /* do all updates
  285. * - if this is too slow, resort to using a more efficient way
  286. * that doesn't force complete update, but for now, this is the
  287. * most accurate way!
  288. */
  289. BKE_scene_update_for_newframe(G.main->eval_ctx, G.main, scene, scene->lay); /* XXX this is the best way we can get anything moving */
  290. #endif
  291. }
  292. /* ........ */
  293. /* perform baking for the targets on the current frame */
  294. static void motionpaths_calc_bake_targets(Scene *scene, ListBase *targets)
  295. {
  296. MPathTarget *mpt;
  297. /* for each target, check if it can be baked on the current frame */
  298. for (mpt = targets->first; mpt; mpt = mpt->next) {
  299. bMotionPath *mpath = mpt->mpath;
  300. bMotionPathVert *mpv;
  301. /* current frame must be within the range the cache works for
  302. * - is inclusive of the first frame, but not the last otherwise we get buffer overruns
  303. */
  304. if ((CFRA < mpath->start_frame) || (CFRA >= mpath->end_frame))
  305. continue;
  306. /* get the relevant cache vert to write to */
  307. mpv = mpath->points + (CFRA - mpath->start_frame);
  308. /* pose-channel or object path baking? */
  309. if (mpt->pchan) {
  310. /* heads or tails */
  311. if (mpath->flag & MOTIONPATH_FLAG_BHEAD) {
  312. copy_v3_v3(mpv->co, mpt->pchan->pose_head);
  313. }
  314. else {
  315. copy_v3_v3(mpv->co, mpt->pchan->pose_tail);
  316. }
  317. /* result must be in worldspace */
  318. mul_m4_v3(mpt->ob->obmat, mpv->co);
  319. }
  320. else {
  321. /* worldspace object location */
  322. copy_v3_v3(mpv->co, mpt->ob->obmat[3]);
  323. }
  324. }
  325. }
  326. /* Perform baking of the given object's and/or its bones' transforms to motion paths
  327. * - scene: current scene
  328. * - ob: object whose flagged motionpaths should get calculated
  329. * - recalc: whether we need to
  330. */
  331. /* TODO: include reports pointer? */
  332. void animviz_calc_motionpaths(Scene *scene, ListBase *targets)
  333. {
  334. MPathTarget *mpt;
  335. int sfra, efra;
  336. int cfra;
  337. /* sanity check */
  338. if (ELEM(NULL, targets, targets->first))
  339. return;
  340. /* set frame values */
  341. cfra = CFRA;
  342. sfra = efra = cfra;
  343. /* TODO: this method could be improved...
  344. * 1) max range for standard baking
  345. * 2) minimum range for recalc baking (i.e. between keyframes, but how?) */
  346. for (mpt = targets->first; mpt; mpt = mpt->next) {
  347. /* try to increase area to do (only as much as needed) */
  348. sfra = MIN2(sfra, mpt->mpath->start_frame);
  349. efra = MAX2(efra, mpt->mpath->end_frame);
  350. }
  351. if (efra <= sfra) return;
  352. /* optimize the depsgraph for faster updates */
  353. /* TODO: whether this is used should depend on some setting for the level of optimizations used */
  354. motionpaths_calc_optimise_depsgraph(scene, targets);
  355. /* calculate path over requested range */
  356. for (CFRA = sfra; CFRA <= efra; CFRA++) {
  357. /* update relevant data for new frame */
  358. motionpaths_calc_update_scene(scene);
  359. /* perform baking for targets */
  360. motionpaths_calc_bake_targets(scene, targets);
  361. }
  362. /* reset original environment */
  363. CFRA = cfra;
  364. motionpaths_calc_update_scene(scene);
  365. /* clear recalc flags from targets */
  366. for (mpt = targets->first; mpt; mpt = mpt->next) {
  367. bAnimVizSettings *avs;
  368. /* get pointer to animviz settings for each target */
  369. if (mpt->pchan)
  370. avs = &mpt->ob->pose->avs;
  371. else
  372. avs = &mpt->ob->avs;
  373. /* clear the flag requesting recalculation of targets */
  374. avs->recalc &= ~ANIMVIZ_RECALC_PATHS;
  375. }
  376. }
  377. /* ******************************************************************** */
  378. /* Curve Paths - for curve deforms and/or curve following */
  379. /* free curve path data
  380. * NOTE: frees the path itself!
  381. * NOTE: this is increasingly inaccurate with non-uniform BevPoint subdivisions [#24633]
  382. */
  383. void free_path(Path *path)
  384. {
  385. if (path->data) MEM_freeN(path->data);
  386. MEM_freeN(path);
  387. }
  388. /* calculate a curve-deform path for a curve
  389. * - only called from displist.c -> do_makeDispListCurveTypes
  390. */
  391. void calc_curvepath(Object *ob, ListBase *nurbs)
  392. {
  393. BevList *bl;
  394. BevPoint *bevp, *bevpn, *bevpfirst, *bevplast;
  395. PathPoint *pp;
  396. Nurb *nu;
  397. Path *path;
  398. float *fp, *dist, *maxdist, xyz[3];
  399. float fac, d = 0, fac1, fac2;
  400. int a, tot, cycl = 0;
  401. /* in a path vertices are with equal differences: path->len = number of verts */
  402. /* NOW WITH BEVELCURVE!!! */
  403. if (ob == NULL || ob->type != OB_CURVE) {
  404. return;
  405. }
  406. if (ob->curve_cache->path) free_path(ob->curve_cache->path);
  407. ob->curve_cache->path = NULL;
  408. /* weak! can only use first curve */
  409. bl = ob->curve_cache->bev.first;
  410. if (bl == NULL || !bl->nr) {
  411. return;
  412. }
  413. nu = nurbs->first;
  414. ob->curve_cache->path = path = MEM_callocN(sizeof(Path), "calc_curvepath");
  415. /* if POLY: last vertice != first vertice */
  416. cycl = (bl->poly != -1);
  417. tot = cycl ? bl->nr : bl->nr - 1;
  418. path->len = tot + 1;
  419. /* exception: vector handle paths and polygon paths should be subdivided at least a factor resolu */
  420. if (path->len < nu->resolu * SEGMENTSU(nu)) {
  421. path->len = nu->resolu * SEGMENTSU(nu);
  422. }
  423. dist = (float *)MEM_mallocN(sizeof(float) * (tot + 1), "calcpathdist");
  424. /* all lengths in *dist */
  425. bevp = bevpfirst = bl->bevpoints;
  426. fp = dist;
  427. *fp = 0.0f;
  428. for (a = 0; a < tot; a++) {
  429. fp++;
  430. if (cycl && a == tot - 1)
  431. sub_v3_v3v3(xyz, bevpfirst->vec, bevp->vec);
  432. else
  433. sub_v3_v3v3(xyz, (bevp + 1)->vec, bevp->vec);
  434. *fp = *(fp - 1) + len_v3(xyz);
  435. bevp++;
  436. }
  437. path->totdist = *fp;
  438. /* the path verts in path->data */
  439. /* now also with TILT value */
  440. pp = path->data = (PathPoint *)MEM_callocN(sizeof(PathPoint) * path->len, "pathdata");
  441. bevp = bevpfirst;
  442. bevpn = bevp + 1;
  443. bevplast = bevpfirst + (bl->nr - 1);
  444. if (UNLIKELY(bevpn > bevplast)) {
  445. bevpn = cycl ? bevpfirst : bevplast;
  446. }
  447. fp = dist + 1;
  448. maxdist = dist + tot;
  449. fac = 1.0f / ((float)path->len - 1.0f);
  450. fac = fac * path->totdist;
  451. for (a = 0; a < path->len; a++) {
  452. d = ((float)a) * fac;
  453. /* we're looking for location (distance) 'd' in the array */
  454. if (LIKELY(tot > 0)) {
  455. while ((fp < maxdist) && (d >= *fp)) {
  456. fp++;
  457. if (bevp < bevplast) bevp++;
  458. bevpn = bevp + 1;
  459. if (UNLIKELY(bevpn > bevplast)) {
  460. bevpn = cycl ? bevpfirst : bevplast;
  461. }
  462. }
  463. fac1 = (*(fp) - d) / (*(fp) - *(fp - 1));
  464. fac2 = 1.0f - fac1;
  465. }
  466. else {
  467. fac1 = 1.0f;
  468. fac2 = 0.0f;
  469. }
  470. interp_v3_v3v3(pp->vec, bevp->vec, bevpn->vec, fac2);
  471. pp->vec[3] = fac1 * bevp->alfa + fac2 * bevpn->alfa;
  472. pp->radius = fac1 * bevp->radius + fac2 * bevpn->radius;
  473. pp->weight = fac1 * bevp->weight + fac2 * bevpn->weight;
  474. interp_qt_qtqt(pp->quat, bevp->quat, bevpn->quat, fac2);
  475. normalize_qt(pp->quat);
  476. pp++;
  477. }
  478. MEM_freeN(dist);
  479. }
  480. static int interval_test(const int min, const int max, int p1, const int cycl)
  481. {
  482. if (cycl) {
  483. p1 = mod_i(p1 - min, (max - min + 1)) + min;
  484. }
  485. else {
  486. if (p1 < min) p1 = min;
  487. else if (p1 > max) p1 = max;
  488. }
  489. return p1;
  490. }
  491. /* calculate the deformation implied by the curve path at a given parametric position,
  492. * and returns whether this operation succeeded.
  493. *
  494. * note: ctime is normalized range <0-1>
  495. *
  496. * returns OK: 1/0
  497. */
  498. int where_on_path(Object *ob, float ctime, float vec[4], float dir[3], float quat[4], float *radius, float *weight)
  499. {
  500. Curve *cu;
  501. Nurb *nu;
  502. BevList *bl;
  503. Path *path;
  504. PathPoint *pp, *p0, *p1, *p2, *p3;
  505. float fac;
  506. float data[4];
  507. int cycl = 0, s0, s1, s2, s3;
  508. ListBase *nurbs;
  509. if (ob == NULL || ob->type != OB_CURVE) return 0;
  510. cu = ob->data;
  511. if (ob->curve_cache == NULL || ob->curve_cache->path == NULL || ob->curve_cache->path->data == NULL) {
  512. printf("no path!\n");
  513. return 0;
  514. }
  515. path = ob->curve_cache->path;
  516. pp = path->data;
  517. /* test for cyclic */
  518. bl = ob->curve_cache->bev.first;
  519. if (!bl) return 0;
  520. if (!bl->nr) return 0;
  521. if (bl->poly > -1) cycl = 1;
  522. /* values below zero for non-cyclic curves give strange results */
  523. BLI_assert(cycl || ctime >= 0.0f);
  524. ctime *= (path->len - 1);
  525. s1 = (int)floor(ctime);
  526. fac = (float)(s1 + 1) - ctime;
  527. /* path->len is corected for cyclic */
  528. s0 = interval_test(0, path->len - 1 - cycl, s1 - 1, cycl);
  529. s1 = interval_test(0, path->len - 1 - cycl, s1, cycl);
  530. s2 = interval_test(0, path->len - 1 - cycl, s1 + 1, cycl);
  531. s3 = interval_test(0, path->len - 1 - cycl, s1 + 2, cycl);
  532. p0 = pp + s0;
  533. p1 = pp + s1;
  534. p2 = pp + s2;
  535. p3 = pp + s3;
  536. /* NOTE: commented out for follow constraint
  537. *
  538. * If it's ever be uncommented watch out for curve_deform_verts()
  539. * which used to temporary set CU_FOLLOW flag for the curve and no
  540. * longer does it (because of threading issues of such a thing.
  541. */
  542. //if (cu->flag & CU_FOLLOW) {
  543. key_curve_tangent_weights(1.0f - fac, data, KEY_BSPLINE);
  544. interp_v3_v3v3v3v3(dir, p0->vec, p1->vec, p2->vec, p3->vec, data);
  545. /* make compatible with vectoquat */
  546. negate_v3(dir);
  547. //}
  548. nurbs = BKE_curve_editNurbs_get(cu);
  549. if (!nurbs)
  550. nurbs = &cu->nurb;
  551. nu = nurbs->first;
  552. /* make sure that first and last frame are included in the vectors here */
  553. if (nu->type == CU_POLY) key_curve_position_weights(1.0f - fac, data, KEY_LINEAR);
  554. else if (nu->type == CU_BEZIER) key_curve_position_weights(1.0f - fac, data, KEY_LINEAR);
  555. else if (s0 == s1 || p2 == p3) key_curve_position_weights(1.0f - fac, data, KEY_CARDINAL);
  556. else key_curve_position_weights(1.0f - fac, data, KEY_BSPLINE);
  557. vec[0] = data[0] * p0->vec[0] + data[1] * p1->vec[0] + data[2] * p2->vec[0] + data[3] * p3->vec[0]; /* X */
  558. vec[1] = data[0] * p0->vec[1] + data[1] * p1->vec[1] + data[2] * p2->vec[1] + data[3] * p3->vec[1]; /* Y */
  559. vec[2] = data[0] * p0->vec[2] + data[1] * p1->vec[2] + data[2] * p2->vec[2] + data[3] * p3->vec[2]; /* Z */
  560. vec[3] = data[0] * p0->vec[3] + data[1] * p1->vec[3] + data[2] * p2->vec[3] + data[3] * p3->vec[3]; /* Tilt, should not be needed since we have quat still used */
  561. if (quat) {
  562. float totfac, q1[4], q2[4];
  563. totfac = data[0] + data[3];
  564. if (totfac > FLT_EPSILON) interp_qt_qtqt(q1, p0->quat, p3->quat, data[3] / totfac);
  565. else copy_qt_qt(q1, p1->quat);
  566. totfac = data[1] + data[2];
  567. if (totfac > FLT_EPSILON) interp_qt_qtqt(q2, p1->quat, p2->quat, data[2] / totfac);
  568. else copy_qt_qt(q2, p3->quat);
  569. totfac = data[0] + data[1] + data[2] + data[3];
  570. if (totfac > FLT_EPSILON) interp_qt_qtqt(quat, q1, q2, (data[1] + data[2]) / totfac);
  571. else copy_qt_qt(quat, q2);
  572. }
  573. if (radius)
  574. *radius = data[0] * p0->radius + data[1] * p1->radius + data[2] * p2->radius + data[3] * p3->radius;
  575. if (weight)
  576. *weight = data[0] * p0->weight + data[1] * p1->weight + data[2] * p2->weight + data[3] * p3->weight;
  577. return 1;
  578. }