1 // Copyright 2017 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #include "draco/animation/keyframe_animation.h"
16 
17 #include "draco/core/draco_test_base.h"
18 
19 namespace {
20 
21 class KeyframeAnimationTest : public ::testing::Test {
22  protected:
KeyframeAnimationTest()23   KeyframeAnimationTest() {}
24 
CreateAndAddTimestamps(int32_t num_frames)25   bool CreateAndAddTimestamps(int32_t num_frames) {
26     timestamps_.resize(num_frames);
27     for (int i = 0; i < timestamps_.size(); ++i)
28       timestamps_[i] = static_cast<draco::KeyframeAnimation::TimestampType>(i);
29     return keyframe_animation_.SetTimestamps(timestamps_);
30   }
31 
CreateAndAddAnimationData(int32_t num_frames,uint32_t num_components)32   int32_t CreateAndAddAnimationData(int32_t num_frames,
33                                     uint32_t num_components) {
34     // Create and add animation data with.
35     animation_data_.resize(num_frames * num_components);
36     for (int i = 0; i < animation_data_.size(); ++i)
37       animation_data_[i] = static_cast<float>(i);
38     return keyframe_animation_.AddKeyframes(draco::DT_FLOAT32, num_components,
39                                             animation_data_);
40   }
41 
42   template <int num_components_t>
CompareAnimationData()43   void CompareAnimationData() {
44     // Compare time stamp.
45     const auto timestamp_att = keyframe_animation_.timestamps();
46     for (int i = 0; i < timestamps_.size(); ++i) {
47       std::array<float, 1> att_value;
48       ASSERT_TRUE((timestamp_att->GetValue<float, 1>(
49           draco::AttributeValueIndex(i), &att_value)));
50       ASSERT_FLOAT_EQ(att_value[0], i);
51     }
52 
53     // Compare keyframe data.
54     const auto keyframe_att = keyframe_animation_.keyframes(1);
55     for (int i = 0; i < animation_data_.size() / num_components_t; ++i) {
56       std::array<float, num_components_t> att_value;
57       ASSERT_TRUE((keyframe_att->GetValue<float, num_components_t>(
58           draco::AttributeValueIndex(i), &att_value)));
59       for (int j = 0; j < num_components_t; ++j) {
60         ASSERT_FLOAT_EQ(att_value[j], i * num_components_t + j);
61       }
62     }
63   }
64 
65   template <int num_components_t>
TestKeyframeAnimation(int32_t num_frames)66   void TestKeyframeAnimation(int32_t num_frames) {
67     ASSERT_TRUE(CreateAndAddTimestamps(num_frames));
68     ASSERT_EQ(CreateAndAddAnimationData(num_frames, num_components_t), 1);
69     CompareAnimationData<num_components_t>();
70   }
71 
72   draco::KeyframeAnimation keyframe_animation_;
73   std::vector<draco::KeyframeAnimation::TimestampType> timestamps_;
74   std::vector<float> animation_data_;
75 };
76 
77 // Test animation with 1 component and 10 frames.
TEST_F(KeyframeAnimationTest,OneComponent)78 TEST_F(KeyframeAnimationTest, OneComponent) { TestKeyframeAnimation<1>(10); }
79 
80 // Test animation with 4 component and 10 frames.
TEST_F(KeyframeAnimationTest,FourComponent)81 TEST_F(KeyframeAnimationTest, FourComponent) { TestKeyframeAnimation<4>(10); }
82 
83 // Test adding animation data before timestamp.
TEST_F(KeyframeAnimationTest,AddingAnimationFirst)84 TEST_F(KeyframeAnimationTest, AddingAnimationFirst) {
85   ASSERT_EQ(CreateAndAddAnimationData(5, 1), 1);
86   ASSERT_TRUE(CreateAndAddTimestamps(5));
87 }
88 
89 // Test adding timestamp more than once.
TEST_F(KeyframeAnimationTest,ErrorAddingTimestampsTwice)90 TEST_F(KeyframeAnimationTest, ErrorAddingTimestampsTwice) {
91   ASSERT_TRUE(CreateAndAddTimestamps(5));
92   ASSERT_FALSE(CreateAndAddTimestamps(5));
93 }
94 // Test animation with multiple animation data.
TEST_F(KeyframeAnimationTest,MultipleAnimationData)95 TEST_F(KeyframeAnimationTest, MultipleAnimationData) {
96   const int num_frames = 5;
97   ASSERT_TRUE(CreateAndAddTimestamps(num_frames));
98   ASSERT_EQ(CreateAndAddAnimationData(num_frames, 1), 1);
99   ASSERT_EQ(CreateAndAddAnimationData(num_frames, 2), 2);
100 }
101 
102 }  // namespace
103