gr2: align curve sampling with Granny 2.11 (loop window, cubic, degree 0)

- Curve::eval rebuilds the B-spline window like ConstructBSplineBuffers:
  clamped ends repeat the endpoint; underflow/overflow looping takes knots
  from the other end (+-duration, skipping the duplicated last knot); equal
  trailing knots get +1e-5.
- Degree 3 (CubicCoefficients) supported; degrees 0..3 share one path.
- Degree 0 takes the control of the first knot > t (SampleBSpline0 = pi[0]);
  DaKeyframes32f curves are marked keyframed and keep frame trunc(t/dt).
- Loop flags threaded through BoneTrack::sample, Animation::sample_local and
  sample_pose; control_loop_state() mirrors GetControlLoopState.
- Gr2ModelInstance::SetAnimationLoop(); animated attachments (fmod) loop.
- Quaternion continuity stays on for every span (Granny: buffer path only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 13:39:33 +09:00
co-authored by Claude Opus 5.5
parent 86759bb726
commit de8fa40098
7 changed files with 252 additions and 82 deletions
+76 -2
View File
@@ -26,14 +26,15 @@ int main() {
}
}
// Degree 0 with several knots (AVA2 DaKeyframes32f) holds the last knot <= t.
// Degree 0 with several knots: granny SampleBSpline0 takes pi[0], the control of
// the first knot > t (CurveFindKnot, clamped to n-1).
gr2::Curve steps;
steps.degree = 0;
steps.dim = 1;
steps.knots = {0.0f, 0.5f, 1.0f};
steps.controls = {10.0f, 20.0f, 30.0f};
const float step_t[] = {-1.0f, 0.0f, 0.49f, 0.5f, 0.99f, 1.0f, 2.0f};
const float step_v[] = {10.0f, 10.0f, 10.0f, 20.0f, 20.0f, 30.0f, 30.0f};
const float step_v[] = {10.0f, 20.0f, 20.0f, 30.0f, 30.0f, 30.0f, 30.0f};
for (size_t i = 0; i < sizeof step_t / sizeof step_t[0]; ++i) {
float v = 0.0f;
steps.eval(step_t[i], &v);
@@ -42,6 +43,79 @@ int main() {
return 1;
}
}
// DaKeyframes32f (keyframed) picks frame trunc(t/dt), i.e. the last knot <= t.
steps.keyframed = true;
const float key_v[] = {10.0f, 10.0f, 10.0f, 20.0f, 20.0f, 30.0f, 30.0f};
for (size_t i = 0; i < sizeof step_t / sizeof step_t[0]; ++i) {
float v = 0.0f;
steps.eval(step_t[i], &v);
if (v != key_v[i]) {
std::fprintf(stderr, "keyframed t=%f got %f want %f\n", step_t[i], v, key_v[i]);
return 1;
}
}
auto near = [](float a, float b) { return std::fabs(a - b) < 1e-5f; };
// Quadratic loop window (ConstructBSplineBuffers with Prev/Next = the curve itself).
// The last knot repeats the first one, as exported looping curves do.
gr2::Curve loop;
loop.degree = 2;
loop.dim = 1;
loop.knots = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f};
loop.controls = {0.0f, 1.0f, 2.0f, 1.0f, 0.0f};
struct LoopCase { float t; bool ul, ol; float want; };
const LoopCase loop_cases[] = {
{0.5f, false, false, 0.125f}, // clamped: the missing knot -1 repeats knot 0
{0.5f, true, false, 0.25f}, // underflow: knot 3 - 4 with control 1
{3.5f, false, true, 1.0f}, // overflow: knots 0+4, 1+4
{0.0f, true, true, 0.5f}, // seam is continuous when looping ...
{3.99999f, true, true, 0.5f},
{0.0f, false, false, 0.0f}, // ... and not when clamped
};
for (const auto& c : loop_cases) {
float v = -1.0f;
loop.eval(c.t, &v, c.ul, c.ol, 4.0f);
if (std::fabs(v - c.want) > 1e-4f) {
std::fprintf(stderr, "loop t=%f ul=%d ol=%d got %f want %f\n", c.t, c.ul, c.ol, v, c.want);
return 1;
}
}
// Cubic (granny CubicCoefficients). Uniform knots at t == knot: 1/6, 4/6, 1/6, 0.
gr2::Curve cubic;
cubic.degree = 3;
cubic.dim = 1;
for (int i = 0; i < 8; ++i) {
cubic.knots.push_back(float(i));
cubic.controls.push_back(float(i * i));
}
float cv = 0.0f;
cubic.eval(3.0f, &cv);
if (!near(cv, 26.0f / 6.0f)) {
std::fprintf(stderr, "cubic t=3 got %f want %f\n", cv, 26.0f / 6.0f);
return 1;
}
for (float& c : cubic.controls) c = 5.0f; // partition of unity, incl. clamped ends
const float cubic_t[] = {0.0f, 0.3f, 3.3f, 6.7f, 7.0f};
for (float t : cubic_t) {
cubic.eval(t, &cv);
if (!near(cv, 5.0f)) {
std::fprintf(stderr, "cubic constant t=%f got %f\n", t, cv);
return 1;
}
}
// granny GetControlLoopState.
bool ul = false, ol = false;
gr2::control_loop_state(0, 5, false, ul, ol);
if (!ul || !ol) { std::fprintf(stderr, "loop state infinite\n"); return 1; }
gr2::control_loop_state(3, 0, false, ul, ol);
if (ul || !ol) { std::fprintf(stderr, "loop state first\n"); return 1; }
gr2::control_loop_state(3, 2, false, ul, ol);
if (!ul || ol) { std::fprintf(stderr, "loop state last\n"); return 1; }
gr2::control_loop_state(0, 0, true, ul, ol);
if (ul || ol) { std::fprintf(stderr, "loop state clamped\n"); return 1; }
// BoneTrack::sample fills SRT components and flags; sample_local = compose(sample).
gr2::BoneTrack track;