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

keyframe_animation.h « animation « draco « src « draco « draco « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7afb2b81be4a3854f75fe515bca5364e68d4c83 (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
// Copyright 2017 The Draco Authors.
//
// 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.
//
#ifndef DRACO_ANIMATION_KEYFRAME_ANIMATION_H_
#define DRACO_ANIMATION_KEYFRAME_ANIMATION_H_

#include <vector>

#include "draco/point_cloud/point_cloud.h"

namespace draco {

// Class for holding keyframe animation data. It will have two or more
// attributes as a point cloud. The first attribute is always the timestamp
// of the animation. Each KeyframeAnimation could have multiple animations with
// the same number of frames. Each animation will be treated as a point
// attribute.
class KeyframeAnimation : public PointCloud {
 public:
  // Force time stamp to be float type.
  using TimestampType = float;

  KeyframeAnimation();

  // Animation must have only one timestamp attribute.
  // This function must be called before adding any animation data.
  // Returns false if timestamp already exists.
  bool SetTimestamps(const std::vector<TimestampType> &timestamp);

  // Returns an id for the added animation data. This id will be used to
  // identify this animation.
  // Returns -1 if error, e.g. number of frames is not consistent.
  // Type |T| should be consistent with |DataType|, e.g:
  //    float - DT_FLOAT32,
  //    int32_t - DT_INT32, ...
  template <typename T>
  int32_t AddKeyframes(DataType data_type, uint32_t num_components,
                       const std::vector<T> &data);

  const PointAttribute *timestamps() const {
    return GetAttributeByUniqueId(kTimestampId);
  }
  const PointAttribute *keyframes(int32_t animation_id) const {
    return GetAttributeByUniqueId(animation_id);
  }

  // Number of frames should be equal to number points in the point cloud.
  void set_num_frames(int32_t num_frames) { set_num_points(num_frames); }
  int32_t num_frames() const { return static_cast<int32_t>(num_points()); }

  int32_t num_animations() const { return num_attributes() - 1; }

 private:
  // Attribute id of timestamp is fixed to 0.
  static constexpr int32_t kTimestampId = 0;
};

template <typename T>
int32_t KeyframeAnimation::AddKeyframes(DataType data_type,
                                        uint32_t num_components,
                                        const std::vector<T> &data) {
  // TODO(draco-eng): Verify T is consistent with |data_type|.
  if (num_components == 0) {
    return -1;
  }
  // If timestamps is not added yet, then reserve attribute 0 for timestamps.
  if (!num_attributes()) {
    // Add a temporary attribute with 0 points to fill attribute id 0.
    std::unique_ptr<PointAttribute> temp_att =
        std::unique_ptr<PointAttribute>(new PointAttribute());
    temp_att->Init(GeometryAttribute::GENERIC, num_components, data_type, false,
                   0);
    this->AddAttribute(std::move(temp_att));

    set_num_frames(data.size() / num_components);
  }

  if (data.size() != num_components * num_frames()) {
    return -1;
  }

  std::unique_ptr<PointAttribute> keyframe_att =
      std::unique_ptr<PointAttribute>(new PointAttribute());
  keyframe_att->Init(GeometryAttribute::GENERIC, num_components, data_type,
                     false, num_frames());
  const size_t stride = num_components;
  for (PointIndex i(0); i < num_frames(); ++i) {
    keyframe_att->SetAttributeValue(keyframe_att->mapped_index(i),
                                    &data[i.value() * stride]);
  }
  return this->AddAttribute(std::move(keyframe_att));
}

}  // namespace draco

#endif  // DRACO_ANIMATION_KEYFRAME_ANIMATION_H_