Motive Animation System
An open source project by FPL.
 All Classes Functions Variables Typedefs Friends Pages
simple_processor_template.h
1 // Copyright 2017 Google Inc. All rights reserved.
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 "motive/engine.h"
16 
17 namespace motive {
18 
19 template <class T>
21  template <typename F>
22  void GatherFloats(MotiveIndex index, MotiveDimension dimensions, float* out,
23  F value_from_data_fn) const {
24  for (MotiveDimension i = 0; i < dimensions; ++i) {
25  const MotiveIndex data_idx = index + i;
26  const T& d = Data(data_idx);
27  out[i] = value_from_data_fn(d, values_[data_idx]);
28  }
29  }
30 
31  public:
32  virtual ~SimpleProcessorTemplate() {}
33 
34  // Accessors to allow the user to get and set simluation values.
35  virtual const float* Values(MotiveIndex index) const {
36  return &values_[index];
37  }
38 
39  virtual void Velocities(MotiveIndex index, MotiveDimension dimensions,
40  float* out) const {
41  GatherFloats(index, dimensions, out, [](const T& d, float value) {
42  return SimpleVelocity(d, value);
43  });
44  }
45 
46  virtual void TargetValues(MotiveIndex index, MotiveDimension dimensions,
47  float* out) const {
48  GatherFloats(index, dimensions, out, [](const T& d, float value) {
49  return SimpleTargetValue(d, value);
50  });
51  }
52 
53  virtual void TargetVelocities(MotiveIndex index, MotiveDimension dimensions,
54  float* out) const {
55  GatherFloats(index, dimensions, out, [](const T& d, float value) {
56  return SimpleTargetVelocity(d, value);
57  });
58  }
59 
60  virtual void Differences(MotiveIndex index, MotiveDimension dimensions,
61  float* out) const {
62  GatherFloats(index, dimensions, out, [](const T& d, float value) {
63  return SimpleDifference(d, value);
64  });
65  }
66 
67  virtual MotiveTime TargetTime(MotiveIndex index,
68  MotiveDimension dimensions) const {
69  MotiveTime greatest = std::numeric_limits<MotiveTime>::min();
70  for (MotiveDimension i = 0; i < dimensions; ++i) {
71  const T& d = Data(index + i);
72  greatest = std::max(greatest, SimpleTargetTime(d));
73  }
74  return greatest;
75  }
76 
77  protected:
78  virtual void InitializeIndices(const MotivatorInit& init, MotiveIndex index,
79  MotiveDimension dimensions,
80  MotiveEngine* /*engine*/) {
81  const SimpleInit& simple_init = static_cast<const SimpleInit&>(init);
82  for (MotiveDimension i = 0; i < dimensions; ++i) {
83  const MotiveIndex processor_index = i + index;
84  Data(processor_index) = T(simple_init, i);
85  values_[processor_index] = simple_init.start_values[i];
86  }
87  }
88 
89  virtual void RemoveIndices(MotiveIndex index, MotiveDimension dimensions) {
90  for (MotiveIndex i = index; i < index + dimensions; ++i) {
91  Data(i) = T();
92  values_[i] = 0.0f;
93  }
94  }
95 
96  virtual void MoveIndices(MotiveIndex old_index, MotiveIndex new_index,
97  MotiveDimension dimensions) {
98  MotiveIndex old_i = old_index;
99  MotiveIndex new_i = new_index;
100  for (MotiveDimension i = 0; i < dimensions; ++i, ++new_i, ++old_i) {
101  data_[new_i] = data_[old_i];
102  values_[new_i] = values_[old_i];
103  }
104  }
105 
106  virtual void SetNumIndices(MotiveIndex num_indices) {
107  data_.resize(num_indices);
108  values_.resize(num_indices);
109  }
110 
111  const T& Data(MotiveIndex index) const {
112  assert(ValidIndex(index));
113  return data_[index];
114  }
115 
116  T& Data(MotiveIndex index) {
117  assert(ValidIndex(index));
118  return data_[index];
119  }
120 
121  std::vector<T> data_;
122  std::vector<float> values_;
123 };
124 
125 } // namespace motive
Interface for motivator types that drive a single float value.
Definition: processor.h:264
virtual void InitializeIndices(const MotivatorInit &init, MotiveIndex index, MotiveDimension dimensions, MotiveEngine *)
Definition: simple_processor_template.h:78
bool ValidIndex(MotiveIndex index) const
Base class of Init classes for MotiveProcessors that derive from SimpleProcessorTemplate.
Definition: init.h:81
Hold and update all animation data.
Definition: engine.h:36
Definition: simple_processor_template.h:20
const float * start_values
Definition: init.h:97
virtual void SetNumIndices(MotiveIndex num_indices)
Definition: simple_processor_template.h:106
virtual void RemoveIndices(MotiveIndex index, MotiveDimension dimensions)
Definition: simple_processor_template.h:89
Definition: common.h:92
virtual void MoveIndices(MotiveIndex old_index, MotiveIndex new_index, MotiveDimension dimensions)
Definition: simple_processor_template.h:96