Tanoda
DeltaBuffer.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity.Query;
10using UnityEngine;
11
12namespace Leap.Unity {
13
24 public abstract class DeltaBuffer<SampleType, DerivativeType> : IIndexable<SampleType> {
25
26 protected struct ValueTimePair {
27 public SampleType value;
28 public float time;
29 }
30
31 public DeltaBuffer(int bufferSize) {
32 _buffer = new RingBuffer<ValueTimePair>(bufferSize);
33 }
34
36
37 public int Count { get { return _buffer.Count; } }
38
39 public bool IsFull { get { return _buffer.IsFull; } }
40
41 public bool IsEmpty { get { return _buffer.IsEmpty; } }
42
43 public int Capacity { get { return _buffer.Capacity; } }
44
45 public SampleType this[int idx] {
46 get { return _buffer[idx].value; }
47 }
48
49 public void Clear() { _buffer.Clear(); }
50
51 public void Add(SampleType sample, float sampleTime) {
52 if (!IsEmpty && sampleTime == GetLatestTime()) {
53 SetLatest(sample, sampleTime);
54 return;
55 }
56
57 _buffer.Add(new ValueTimePair { value = sample, time = sampleTime });
58 }
59
60 public SampleType Get(int idx) {
61 return _buffer.Get(idx).value;
62 }
63
64 public SampleType GetLatest() {
65 return Get(Count - 1);
66 }
67
68 public void Set(int idx, SampleType sample, float sampleTime) {
69 _buffer.Set(idx, new ValueTimePair { value = sample, time = sampleTime });
70 }
71
72 public void SetLatest(SampleType sample, float sampleTime) {
73 if (Count == 0) Set(0, sample, sampleTime);
74 else Set(Count - 1, sample, sampleTime);
75 }
76
77 public float GetTime(int idx) {
78 return _buffer.Get(idx).time;
79 }
80
81 public float GetLatestTime() {
82 return _buffer.Get(Count - 1).time;
83 }
84
90 public abstract DerivativeType Delta();
91
92 #region foreach Support
93
95 return new IndexableEnumerator<SampleType>(this);
96 }
97
98 #endregion
99
100 }
101
109 public class DeltaBuffer : DeltaBuffer<Vector3, Vector3> {
110
111 public DeltaBuffer(int bufferSize) : base(bufferSize) { }
112
120 public override Vector3 Delta() {
121 if (Count <= 1) { return Vector3.zero; }
122
123 Vector3 deltaPerTimeSum = Vector3.zero;
124 for (int i = 0; i < Count - 1; i++) {
125 deltaPerTimeSum += (Get(i + 1) - Get(i)) / (GetTime(i + 1) - GetTime(i));
126 }
127 return deltaPerTimeSum / (Count - 1);
128 }
129
130 }
131
140 public class DeltaFloatBuffer : DeltaBuffer<float, float> {
141
142 public DeltaFloatBuffer(int bufferSize) : base(bufferSize) { }
143
148 public override float Delta() {
149 if (Count <= 1) { return 0f; }
150
151 float deltaPerTimeSum = 0f;
152 for (int i = 0; i < Count - 1; i++) {
153 deltaPerTimeSum += (Get(i + 1) - Get(i)) / (GetTime(i + 1) - GetTime(i));
154 }
155 return deltaPerTimeSum / (Count - 1);
156 }
157
158 }
159
168 public class DeltaQuaternionBuffer : DeltaBuffer<Quaternion, Vector3> {
169
170 public DeltaQuaternionBuffer(int bufferSize) : base(bufferSize) { }
171
176 public override Vector3 Delta() {
177 if (Count <= 1) return Vector3.zero;
178
179 var deltaSum = Vector3.zero;
180 for (int i = 0; i < Count - 1; i++) {
181 var sample0 = _buffer.Get(i);
182 var sample1 = _buffer.Get(i + 1);
183 var r0 = sample0.value;
184 var t0 = sample0.time;
185 var r1 = sample1.value;
186 var t1 = sample1.time;
187
188 var delta = (r1.From(r0)).ToAngleAxisVector();
189 var deltaTime = t1.From(t0);
190
191 deltaSum += delta / deltaTime;
192 }
193
194 return deltaSum / (Count - 1);
195 }
196
197 }
198
199}
Allows you to add to a capped-size ring buffer of Ts and, when full, compute the buffer's average cha...
Definition: DeltaBuffer.cs:24
DeltaBuffer(int bufferSize)
Definition: DeltaBuffer.cs:31
SampleType Get(int idx)
Definition: DeltaBuffer.cs:60
void Add(SampleType sample, float sampleTime)
Definition: DeltaBuffer.cs:51
override Vector3 Delta()
Returns the average change between each sample per unit time, or zero if the buffer contains one or f...
Definition: DeltaBuffer.cs:120
RingBuffer< ValueTimePair > _buffer
Definition: DeltaBuffer.cs:35
SampleType GetLatest()
Definition: DeltaBuffer.cs:64
abstract DerivativeType Delta()
Returns the average change between each sample per unit time.
void SetLatest(SampleType sample, float sampleTime)
Definition: DeltaBuffer.cs:72
IndexableEnumerator< SampleType > GetEnumerator()
Definition: DeltaBuffer.cs:94
void Set(int idx, SampleType sample, float sampleTime)
Definition: DeltaBuffer.cs:68
float GetTime(int idx)
Definition: DeltaBuffer.cs:77
A ring buffer of floats with a Delta() function that computes the buffer's average change over time....
Definition: DeltaBuffer.cs:140
DeltaFloatBuffer(int bufferSize)
Definition: DeltaBuffer.cs:142
override float Delta()
Returns the average change between each sample per unit time, or zero if the buffer is empty.
Definition: DeltaBuffer.cs:148
A ring buffer of Quaternions with a Delta() function that computes the buffer's average change over t...
Definition: DeltaBuffer.cs:168
override Vector3 Delta()
Returns the average angular velocity of Quaternions in the buffer as an angle-axis vector,...
Definition: DeltaBuffer.cs:176
DeltaQuaternionBuffer(int bufferSize)
Definition: DeltaBuffer.cs:170
This easy-to-implement interface represents the ability to index into a collection of elements of typ...
Definition: IIndexable.cs:21