Files
test/source/blender/blenkernel/intern/anim_path.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

379 lines
10 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later */
2002-10-12 11:37:38 +00:00
/** \file
* \ingroup bke
2011-02-27 20:40:57 +00:00
*/
2002-10-12 11:37:38 +00:00
#include "MEM_guardedalloc.h"
#include <cfloat>
Code cleanup and structural improvements for dupli generation. This is a first step toward improving our dupli system. It implements a more generic way of treating the various methods of dupli generation by adding a few structs: * DupliContext holds a number of arguments commonly used in the recursive dupli functions and defines a recursion state for generating sub-duplis (nested groups). It also helps to prevent bloated argument lists. * DupliGenerator is a type struct that unifies the different dupli creation methods (groups, frames, verts, text chars, faces, particles). (As with context there should be no overhead from pointer indirection because everything can still be inlined inside anim.c) Beside making the code more easily understandable this implementation should also help to avoid weird side effects from custom matrix hacks by defining clearly what a generator does. The DupliContext is deliberately made const, so a generator can not simply add hidden matrix or flag modifications that are hard to track down. The result container for the generated duplis is stored in the context instead of being passed explicitly. This means the generators are oblivious to the storage of duplis, all they need to do is call the make_dupli function. This will allow us to implement more efficient ways of storing DupliObject instances, such as MemPools or batches. These can be implemented alongside the current ListBase so we can improve dupli bottlenecks without having to replace each and every dupli use case at once. Differential Revision: https://developer.blender.org/D189
2014-01-21 12:11:34 +01:00
#include "DNA_curve_types.h"
Orange branch: Revived hidden treasure, the Groups! Previous experiment (in 2000) didn't satisfy, it had even some primitive NLA option in groups... so, cleaned up the old code (removed most) and integrated it back in a more useful way. Usage: - CTRL+G gives menu to add group, add to existing group, or remove from groups. - In Object buttons, a new (should become first) Panel was added, showing not only Object "ID button" and Parent, but also the Groups the Object Belongs to. These buttons also allow rename, assigning or removing. - To indicate Objects are grouped, they're drawn in a (not theme yet, so temporal?) green wire color. - Use ALT+SHIFT mouse-select to (de)select an entire group But, the real power of groups is in the following features: -> Particle Force field and Guide control In the "Particle Motion" Panel, you can indicate a Group name, this then limits force fields or guides to members of that Group. (Note that layers still work on top of that... not sure about that). -> Light Groups In the Material "Shaders" Panel, you can indicate a Group name to limit lighting for the Material to lamps in this group. The Lights in a Group do need to be 'visible' for the Scene to be rendered (as usual). -> Group Duplicator In the Object "Anim" Panel, you can set any Object (use Empty!) to duplicate an entire Group. It will make copies of all Objects in that Group. Also works for animated Objects, but it will copy the current positions or deforms. Control over 'local timing' (so we can do Massive anims!) will be added later. (Note; this commit won't render Group duplicators yet, a fix in bf-blender will enable that, next commit will sync) -> Library Appending In the SHIFT-F1 or SHIFT+F4 browsers, you can also find the Groups listed. By appending or linking the Group itself, and use the Group Duplicator, you now can animate and position linked Objects. The nice thing is that the local saved file itself will only store the Group name that was linked, so on a next file read, the Group Objects will be re-read as stored (changed) in the Library file. (Note; current implementation also "gives a base" to linked Group Objects, to show them as Objects in the current Scene. Need that now for testing purposes, but probably will be removed later). -> Outliner Outliner now shows Groups as optio too, nice to organize your data a bit too! In General, Groups have a very good potential... for example, it could become default for MetaBall Objects too (jiri, I can help you later on how this works). All current 'layer relationships' in Blender should be dropped in time, I guess...
2005-12-06 10:55:30 +00:00
#include "DNA_key_types.h"
#include "DNA_object_types.h"
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
2002-10-12 11:37:38 +00:00
#include "BKE_anim_path.h"
#include "BKE_curve.hh"
2024-01-30 14:42:07 -05:00
#include "BKE_key.hh"
#include "BKE_object_types.hh"
#include "CLG_log.h"
static CLG_LogRef LOG = {"bke.anim"};
/* ******************************************************************** */
/* Curve Paths - for curve deforms and/or curve following */
static int get_bevlist_seg_array_size(const BevList *bl)
2002-10-12 11:37:38 +00:00
{
if (bl->poly >= 0) {
/* Cyclic curve. */
return bl->nr;
}
return bl->nr - 1;
2002-10-12 11:37:38 +00:00
}
int BKE_anim_path_get_array_size(const CurveCache *curve_cache)
{
BLI_assert(curve_cache != nullptr);
BevList *bl = static_cast<BevList *>(curve_cache->bev.first);
BLI_assert(bl != nullptr && bl->nr > 1);
return get_bevlist_seg_array_size(bl);
}
float BKE_anim_path_get_length(const CurveCache *curve_cache)
2002-10-12 11:37:38 +00:00
{
const int seg_size = BKE_anim_path_get_array_size(curve_cache);
return curve_cache->anim_path_accum_length[seg_size - 1];
}
void BKE_anim_path_calc_data(Object *ob)
{
if (ob == nullptr || ob->type != OB_CURVES_LEGACY) {
2012-09-03 22:53:34 +00:00
return;
}
if (ob->runtime->curve_cache == nullptr) {
CLOG_WARN(&LOG, "No curve cache!");
return;
}
/* We only use the first curve. */
BevList *bl = static_cast<BevList *>(ob->runtime->curve_cache->bev.first);
if (bl == nullptr || !bl->nr) {
CLOG_WARN(&LOG, "No bev list data!");
2012-09-03 22:53:34 +00:00
return;
}
/* Free old data. */
if (ob->runtime->curve_cache->anim_path_accum_length) {
MEM_freeN((void *)ob->runtime->curve_cache->anim_path_accum_length);
}
/* We assume that we have at least two points.
* If there is less than two points in the curve,
* no BevList should have been generated.
*/
BLI_assert(bl->nr > 1);
const int seg_size = get_bevlist_seg_array_size(bl);
float *len_data = (float *)MEM_mallocN(sizeof(float) * seg_size, "calcpathdist");
ob->runtime->curve_cache->anim_path_accum_length = len_data;
BevPoint *bp_arr = bl->bevpoints;
float prev_len = 0.0f;
for (int i = 0; i < bl->nr - 1; i++) {
prev_len += len_v3v3(bp_arr[i].vec, bp_arr[i + 1].vec);
len_data[i] = prev_len;
}
if (bl->poly >= 0) {
/* Cyclic curve. */
len_data[seg_size - 1] = prev_len + len_v3v3(bp_arr[0].vec, bp_arr[bl->nr - 1].vec);
2012-09-03 22:53:34 +00:00
}
}
static void get_curve_points_from_idx(const int idx,
const BevList *bl,
const bool is_cyclic,
BevPoint const **r_p0,
BevPoint const **r_p1,
BevPoint const **r_p2,
BevPoint const **r_p3)
{
BLI_assert(idx >= 0);
BLI_assert(idx < bl->nr - 1 || (is_cyclic && idx < bl->nr));
BLI_assert(bl->nr > 1);
const BevPoint *bp_arr = bl->bevpoints;
/* First segment. */
if (idx == 0) {
*r_p1 = &bp_arr[0];
if (is_cyclic) {
*r_p0 = &bp_arr[bl->nr - 1];
}
else {
*r_p0 = *r_p1;
}
*r_p2 = &bp_arr[1];
if (bl->nr > 2) {
*r_p3 = &bp_arr[2];
}
else {
*r_p3 = *r_p2;
2002-10-12 11:37:38 +00:00
}
return;
}
/* Last segment (or next to last in a cyclic curve). */
if (idx == bl->nr - 2) {
2021-04-11 13:09:27 +10:00
/* The case when the bl->nr == 2 falls in to the "first segment" check above.
* So here we can assume that bl->nr > 2.
*/
*r_p0 = &bp_arr[idx - 1];
*r_p1 = &bp_arr[idx];
*r_p2 = &bp_arr[idx + 1];
if (is_cyclic) {
*r_p3 = &bp_arr[0];
}
else {
*r_p3 = *r_p2;
}
return;
}
if (idx == bl->nr - 1) {
/* Last segment in a cyclic curve. This should only trigger if the curve is cyclic
* as it gets an extra segment between the end and the start point. */
*r_p0 = &bp_arr[idx - 1];
*r_p1 = &bp_arr[idx];
*r_p2 = &bp_arr[0];
*r_p3 = &bp_arr[1];
return;
2002-10-12 11:37:38 +00:00
}
/* To get here the curve has to have four curve points or more and idx can't
* be the first or the last segment.
* So we can assume that we can get four points without any special checks.
*/
*r_p0 = &bp_arr[idx - 1];
*r_p1 = &bp_arr[idx];
*r_p2 = &bp_arr[idx + 1];
*r_p3 = &bp_arr[idx + 2];
2002-10-12 11:37:38 +00:00
}
static bool binary_search_anim_path(const float *accum_len_arr,
const int seg_size,
const float goal_len,
int *r_idx,
float *r_frac)
2002-10-12 11:37:38 +00:00
{
float left_len, right_len;
int cur_idx = 0, cur_base = 0;
int cur_step = seg_size - 1;
while (true) {
cur_idx = cur_base + cur_step / 2;
left_len = accum_len_arr[cur_idx];
right_len = accum_len_arr[cur_idx + 1];
if (left_len <= goal_len && right_len > goal_len) {
*r_idx = cur_idx + 1;
*r_frac = (goal_len - left_len) / (right_len - left_len);
return true;
}
if (cur_idx == 0) {
2021-04-11 13:09:27 +10:00
/* We ended up at the first segment. The point must be in here. */
*r_idx = 0;
*r_frac = goal_len / accum_len_arr[0];
return true;
}
if (UNLIKELY(cur_step == 0)) {
/* This should never happen unless there is something horribly wrong. */
CLOG_ERROR(&LOG, "Couldn't find any valid point on the animation path!");
BLI_assert_msg(0, "Couldn't find any valid point on the animation path!");
return false;
}
if (left_len < goal_len) {
/* Go to the right. */
cur_base = cur_idx + 1;
cur_step--;
} /* Else, go to the left. */
cur_step /= 2;
2002-10-12 11:37:38 +00:00
}
}
bool BKE_where_on_path(const Object *ob,
float ctime,
float r_vec[4],
float r_dir[3],
float r_quat[4],
float *r_radius,
float *r_weight)
2002-10-12 11:37:38 +00:00
{
if (ob == nullptr || ob->type != OB_CURVES_LEGACY) {
return false;
}
Curve *cu = static_cast<Curve *>(ob->data);
if (ob->runtime->curve_cache == nullptr) {
CLOG_WARN(&LOG, "No curve cache!");
return false;
}
if (ob->runtime->curve_cache->anim_path_accum_length == nullptr) {
CLOG_WARN(&LOG, "No anim path!");
return false;
}
/* We only use the first curve. */
BevList *bl = static_cast<BevList *>(ob->runtime->curve_cache->bev.first);
if (bl == nullptr || !bl->nr) {
CLOG_WARN(&LOG, "No bev list data!");
return false;
}
/* Test for cyclic curve. */
const bool is_cyclic = bl->poly >= 0;
if (is_cyclic) {
/* Wrap the time into a 0.0 - 1.0 range. */
if (ctime < 0.0f || ctime > 1.0f) {
ctime -= floorf(ctime);
}
}
/* The curve points for this ctime value. */
const BevPoint *p0, *p1, *p2, *p3;
float frac;
const int seg_size = get_bevlist_seg_array_size(bl);
const float *accum_len_arr = ob->runtime->curve_cache->anim_path_accum_length;
const float goal_len = ctime * accum_len_arr[seg_size - 1];
/* Are we simply trying to get the start/end point? */
if (ctime <= 0.0f || ctime >= 1.0f) {
const float clamp_time = clamp_f(ctime, 0.0f, 1.0f);
const int idx = clamp_time * (seg_size - 1);
get_curve_points_from_idx(idx, bl, is_cyclic, &p0, &p1, &p2, &p3);
if (idx == 0) {
frac = goal_len / accum_len_arr[0];
}
else {
frac = (goal_len - accum_len_arr[idx - 1]) / (accum_len_arr[idx] - accum_len_arr[idx - 1]);
}
}
else {
/* Do binary search to get the correct segment. */
int idx;
const bool found_idx = binary_search_anim_path(accum_len_arr, seg_size, goal_len, &idx, &frac);
if (UNLIKELY(!found_idx)) {
return false;
}
get_curve_points_from_idx(idx, bl, is_cyclic, &p0, &p1, &p2, &p3);
}
/* NOTE: commented out for follow constraint
*
* If it's ever be uncommented watch out for BKE_curve_deform_coords()
* which used to temporary set CU_FOLLOW flag for the curve and no
* longer does it (because of threading issues of such a thing.
*/
// if (cu->flag & CU_FOLLOW) {
float w[4];
key_curve_tangent_weights(frac, w, KEY_BSPLINE);
if (r_dir) {
interp_v3_v3v3v3v3(r_dir, p0->vec, p1->vec, p2->vec, p3->vec, w);
/* Make compatible with #vec_to_quat. */
negate_v3(r_dir);
}
Result of 2 weeks of quiet coding work in Greece :) Aim was to get a total refresh of the animation system. This is needed because; - we need to upgrade it with 21st century features - current code is spaghetti/hack combo, and hides good design - it should become lag-free with using dependency graphs A full log, with complete code API/structure/design explanation will follow, that's a load of work... so here below the list with hot changes; - The entire object update system (matrices, geometry) is now centralized. Calls to where_is_object and makeDispList are forbidden, instead we tag objects 'changed' and let the depgraph code sort it out - Removed all old "Ika" code - Depgraph is aware of all relationships, including meta balls, constraints, bevelcurve, and so on. - Made depgraph aware of relation types and layers, to do smart flushing of 'changed' events. Nothing gets calculated too often! - Transform uses depgraph to detect changes - On frame-advance, depgraph flushes animated changes Armatures; Almost all armature related code has been fully built from scratch. It now reveils the original design much better, with a very clean implementation, lag free without even calculating each Bone more than once. Result is quite a speedup yes! Important to note is; 1) Armature is data containing the 'rest position' 2) Pose is the changes of rest position, and always on object level. That way more Objects can use same Pose. Also constraints are in Pose 3) Actions only contain the Ipos to change values in Poses. - Bones draw unrotated now - Drawing bones speedup enormously (10-20 times) - Bone selecting in EditMode, selection state is saved for PoseMode, and vice-versa - Undo in editmode - Bone renaming does vertexgroups, constraints, posechannels, actions, for all users of Armature in entire file - Added Bone renaming in NKey panel - Nkey PoseMode shows eulers now - EditMode and PoseMode now have 'active' bone too (last clicked) - Parenting in EditMode' CTRL+P, ALT+P, with nice options! - Pose is added in Outliner now, with showing that constraints are in the Pose, not Armature - Disconnected IK solving from constraints. It's a separate phase now, on top of the full Pose calculations - Pose itself has a dependency graph too, so evaluation order is lag free. TODO NOW; - Rotating in Posemode has incorrect inverse transform (Martin will fix) - Python Bone/Armature/Pose API disabled... needs full recode too (wait for my doc!) - Game engine will need upgrade too - Depgraph code needs revision, cleanup, can be much faster! (But, compliments for Jean-Luc, it works like a charm!) - IK changed, it now doesnt use previous position to advance to next position anymore. That system looks nice (no flips) but is not well suited for NLA and background render. TODO LATER; We now can do loadsa new nifty features as well; like: - Kill PoseMode (can be option for armatures itself) - Make B-Bones (Bezier, Bspline, like for spines) - Move all silly button level edit to 3d window (like CTRL+I = add IK) - Much better & informative drawing - Fix action/nla editors - Put all ipos in Actions (object, mesh key, lamp color) - Add hooks - Null bones - Much more advanced constraints... Bugfixes; - OGL render (view3d header) had wrong first frame on anim render - Ipo 'recording' mode had wrong playback speed - Vertex-key mode now sticks to show 'active key', until frame change -Ton-
2005-07-03 17:35:38 +00:00
//}
const ListBase *nurbs = BKE_curve_editNurbs_get(cu);
if (!nurbs) {
nurbs = &cu->nurb;
}
const Nurb *nu = static_cast<const Nurb *>(nurbs->first);
/* Make sure that first and last frame are included in the vectors here. */
if (ELEM(nu->type, CU_POLY, CU_BEZIER, CU_NURBS)) {
key_curve_position_weights(frac, w, KEY_LINEAR);
}
else if (p2 == p3) {
key_curve_position_weights(frac, w, KEY_CARDINAL);
}
else {
key_curve_position_weights(frac, w, KEY_BSPLINE);
}
if (r_vec) {
/* X, Y, Z axis. */
r_vec[0] = w[0] * p0->vec[0] + w[1] * p1->vec[0] + w[2] * p2->vec[0] + w[3] * p3->vec[0];
r_vec[1] = w[0] * p0->vec[1] + w[1] * p1->vec[1] + w[2] * p2->vec[1] + w[3] * p3->vec[1];
r_vec[2] = w[0] * p0->vec[2] + w[1] * p1->vec[2] + w[2] * p2->vec[2] + w[3] * p3->vec[2];
}
/* Clamp weights to 0-1 as we don't want to extrapolate other values than position. */
clamp_v4(w, 0.0f, 1.0f);
if (r_vec) {
/* Tilt, should not be needed since we have quat still used. */
r_vec[3] = w[0] * p0->tilt + w[1] * p1->tilt + w[2] * p2->tilt + w[3] * p3->tilt;
}
if (r_quat) {
float totfac, q1[4], q2[4];
totfac = w[0] + w[3];
if (totfac > FLT_EPSILON) {
interp_qt_qtqt(q1, p0->quat, p3->quat, w[3] / totfac);
}
else {
2012-05-06 17:22:54 +00:00
copy_qt_qt(q1, p1->quat);
}
totfac = w[1] + w[2];
if (totfac > FLT_EPSILON) {
interp_qt_qtqt(q2, p1->quat, p2->quat, w[2] / totfac);
}
else {
2012-05-06 17:22:54 +00:00
copy_qt_qt(q2, p3->quat);
}
totfac = w[0] + w[1] + w[2] + w[3];
if (totfac > FLT_EPSILON) {
interp_qt_qtqt(r_quat, q1, q2, (w[1] + w[2]) / totfac);
}
else {
copy_qt_qt(r_quat, q2);
}
2009-09-16 17:43:09 +00:00
}
if (r_radius) {
*r_radius = w[0] * p0->radius + w[1] * p1->radius + w[2] * p2->radius + w[3] * p3->radius;
}
if (r_weight) {
*r_weight = w[0] * p0->weight + w[1] * p1->weight + w[2] * p2->weight + w[3] * p3->weight;
}
return true;
2002-10-12 11:37:38 +00:00
}