Welcome to mirror list, hosted at ThFree Co, Russian Federation.

keyframes_keylist_test.cc « animation « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17a21be5ae8cd26917b67fa938779c2f576eee0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Apache License, Version 2.0 */

#include "testing/testing.h"

#include "BLI_utildefines.h"

#include "ED_keyframes_keylist.h"

#include "DNA_anim_types.h"
#include "DNA_curve_types.h"

#include "MEM_guardedalloc.h"

#include "BKE_fcurve.h"

#include <functional>
#include <optional>

namespace blender::editor::animation::tests {

const float KEYLIST_NEAR_ERROR = 0.1;
const float FRAME_STEP = 0.005;

static void build_fcurve(FCurve &fcurve)
{
  fcurve.totvert = 3;
  fcurve.bezt = static_cast<BezTriple *>(
      MEM_callocN(sizeof(BezTriple) * fcurve.totvert, "BezTriples"));
  fcurve.bezt[0].vec[1][0] = 10.f;
  fcurve.bezt[0].vec[1][1] = 1.f;
  fcurve.bezt[1].vec[1][0] = 20.f;
  fcurve.bezt[1].vec[1][1] = 2.f;
  fcurve.bezt[2].vec[1][0] = 30.f;
  fcurve.bezt[2].vec[1][1] = 1.f;
}

static AnimKeylist *create_test_keylist()
{
  FCurve *fcurve = BKE_fcurve_create();
  build_fcurve(*fcurve);

  AnimKeylist *keylist = ED_keylist_create();
  fcurve_to_keylist(nullptr, fcurve, keylist, 0);
  BKE_fcurve_free(fcurve);

  ED_keylist_prepare_for_direct_access(keylist);
  return keylist;
}

static void assert_act_key_column(const ActKeyColumn *column,
                                  const std::optional<float> expected_frame)
{
  if (expected_frame.has_value()) {
    EXPECT_NE(column, nullptr);
    EXPECT_NEAR(column->cfra, *expected_frame, KEYLIST_NEAR_ERROR);
  }
  else {
    EXPECT_EQ(column, nullptr);
  }
}

using KeylistFindFunction = std::function<const ActKeyColumn *(const AnimKeylist *, float)>;

static float check_keylist_find_range(const AnimKeylist *keylist,
                                      KeylistFindFunction keylist_find_func,
                                      const float frame_from,
                                      const float frame_to,
                                      const std::optional<float> expected_frame)
{
  float cfra = frame_from;
  for (; cfra < frame_to; cfra += FRAME_STEP) {
    const ActKeyColumn *found = keylist_find_func(keylist, cfra);
    assert_act_key_column(found, expected_frame);
  }
  return cfra;
}

static float check_keylist_find_next_range(const AnimKeylist *keylist,
                                           const float frame_from,
                                           const float frame_to,
                                           const std::optional<float> expected_frame)
{
  return check_keylist_find_range(
      keylist, ED_keylist_find_next, frame_from, frame_to, expected_frame);
}

TEST(keylist, find_next)
{
  AnimKeylist *keylist = create_test_keylist();

  float cfra = check_keylist_find_next_range(keylist, 0.0f, 9.99f, 10.0f);
  cfra = check_keylist_find_next_range(keylist, cfra, 19.99f, 20.0f);
  cfra = check_keylist_find_next_range(keylist, cfra, 29.99f, 30.0f);
  cfra = check_keylist_find_next_range(keylist, cfra, 39.99f, std::nullopt);

  ED_keylist_free(keylist);
}

static float check_keylist_find_prev_range(const AnimKeylist *keylist,
                                           const float frame_from,
                                           const float frame_to,
                                           const std::optional<float> expected_frame)
{
  return check_keylist_find_range(
      keylist, ED_keylist_find_prev, frame_from, frame_to, expected_frame);
}

TEST(keylist, find_prev)
{
  AnimKeylist *keylist = create_test_keylist();

  float cfra = check_keylist_find_prev_range(keylist, 0.0f, 10.01f, std::nullopt);
  cfra = check_keylist_find_prev_range(keylist, cfra, 20.01f, 10.0f);
  cfra = check_keylist_find_prev_range(keylist, cfra, 30.01f, 20.0f);
  cfra = check_keylist_find_prev_range(keylist, cfra, 49.99f, 30.0f);

  ED_keylist_free(keylist);
}

static float check_keylist_find_exact_range(const AnimKeylist *keylist,
                                            const float frame_from,
                                            const float frame_to,
                                            const std::optional<float> expected_frame)
{
  return check_keylist_find_range(
      keylist, ED_keylist_find_exact, frame_from, frame_to, expected_frame);
}

TEST(keylist, find_exact)
{
  AnimKeylist *keylist = create_test_keylist();

  float cfra = check_keylist_find_exact_range(keylist, 0.0f, 9.99f, std::nullopt);
  cfra = check_keylist_find_exact_range(keylist, cfra, 10.01f, 10.0f);
  cfra = check_keylist_find_exact_range(keylist, cfra, 19.99f, std::nullopt);
  cfra = check_keylist_find_exact_range(keylist, cfra, 20.01f, 20.0f);
  cfra = check_keylist_find_exact_range(keylist, cfra, 29.99f, std::nullopt);
  cfra = check_keylist_find_exact_range(keylist, cfra, 30.01f, 30.0f);
  cfra = check_keylist_find_exact_range(keylist, cfra, 49.99f, std::nullopt);

  ED_keylist_free(keylist);
}

}  // namespace blender::editor::animation::tests