aboutsummaryrefslogtreecommitdiff
path: root/ink_stroke_modeler/internal/wobble_smoother_test.cc
blob: f31ac9d83fab2fed0231afd83ff1ff7bfcb4287e (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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ink_stroke_modeler/internal/wobble_smoother.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "ink_stroke_modeler/internal/type_matchers.h"
#include "ink_stroke_modeler/params.h"
#include "ink_stroke_modeler/types.h"

namespace ink {
namespace stroke_model {
namespace {

const WobbleSmootherParams kDefaultParams{
    .timeout = Duration(.04), .speed_floor = 1.31, .speed_ceiling = 1.44};

TEST(WobbleSmootherTest, SlowStraightLine) {
  // The line moves at 1 cm/s, which is below the floor of 1.31 cm/s.
  WobbleSmoother filter;
  filter.Reset(kDefaultParams, {3, 4}, Time{1});
  EXPECT_THAT(filter.Update({3.016, 4}, Time{1.016}), Vec2Eq({3.008, 4}));
  EXPECT_THAT(filter.Update({3.032, 4}, Time{1.032}), Vec2Eq({3.016, 4}));
  EXPECT_THAT(filter.Update({3.048, 4}, Time{1.048}), Vec2Eq({3.032, 4}));
  EXPECT_THAT(filter.Update({3.064, 4}, Time{1.064}), Vec2Eq({3.048, 4}));
}

TEST(WobbleSmootherTest, FastStraightLine) {
  // The line moves at 1.5 cm/s, which is above the ceiling of 1.44 cm/s.
  WobbleSmoother filter;
  filter.Reset(kDefaultParams, {-1, 0}, Time{0});
  EXPECT_THAT(filter.Update({-1, .024}, Time{.016}), Vec2Eq({-1, .024}));
  EXPECT_THAT(filter.Update({-1, .048}, Time{.032}), Vec2Eq({-1, .048}));
  EXPECT_THAT(filter.Update({-1, .072}, Time{.048}), Vec2Eq({-1, .072}));
}

TEST(WobbleSmootherTest, SlowZigZag) {
  // The line moves at 1 cm/s, which is below the floor of 1.31 cm/s.
  WobbleSmoother filter;
  filter.Reset(kDefaultParams, {1, 2}, Time{5});
  EXPECT_THAT(filter.Update({1.016, 2}, Time{5.016}), Vec2Eq({1.008, 2}));
  EXPECT_THAT(filter.Update({1.016, 2.016}, Time{5.032}),
              Vec2Eq({1.0106667, 2.0053333}));
  EXPECT_THAT(filter.Update({1.032, 2.016}, Time{5.048}),
              Vec2Eq({1.0213333, 2.0106667}));
  EXPECT_THAT(filter.Update({1.032, 2.032}, Time{5.064}),
              Vec2Eq({1.0266667, 2.0213333}));
  EXPECT_THAT(filter.Update({1.048, 2.032}, Time{5.080}),
              Vec2Eq({1.0373333, 2.0266667}));
  EXPECT_THAT(filter.Update({1.048, 2.048}, Time{5.096}),
              Vec2Eq({1.0426667, 2.0373333}));
}

TEST(WobbleSmootherTest, FastZigZag) {
  // The line moves at 1.5 cm/s, which is above the ceiling of 1.44 cm/s.
  WobbleSmoother filter;
  filter.Reset(kDefaultParams, {7, 3}, Time{8});
  EXPECT_THAT(filter.Update({7, 3.024}, Time{8.016}), Vec2Eq({7, 3.024}));
  EXPECT_THAT(filter.Update({7.024, 3.024}, Time{8.032}),
              Vec2Eq({7.024, 3.024}));
  EXPECT_THAT(filter.Update({7.024, 3.048}, Time{8.048}),
              Vec2Eq({7.024, 3.048}));
  EXPECT_THAT(filter.Update({7.048, 3.048}, Time{8.064}),
              Vec2Eq({7.048, 3.048}));
}

}  // namespace
}  // namespace stroke_model
}  // namespace ink