DeltaBuffer.cs
3.77 KB
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
108
109
110
111
112
113
114
115
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using System.Collections;
/// <summary>
/// Allows you to add to a capped-size ring buffer of Ts and,
/// when full, compute the buffer's average change over time.
/// DeltaBuffer without type parameters supports Vector3s;
/// DeltaFloatBuffer supports floats. To support other types,
/// subclass DeltaBuffer with your type as the generic type
/// argument and implement its Delta() averaging function.
/// </summary>
public abstract class DeltaBuffer<T> {
protected struct ValueTimePair {
public T value;
public float time;
}
public DeltaBuffer(int bufferSize) {
_buffer = new RingBuffer<ValueTimePair>(bufferSize);
}
protected RingBuffer<ValueTimePair> _buffer;
public int Length { get { return _buffer.Length; } }
public bool IsFull { get { return _buffer.IsFull; } }
public void Clear() { _buffer.Clear(); }
private float _previousSampleTime = 0F;
public void Add(T sample, float sampleTime) {
if (sampleTime == _previousSampleTime) {
SetLatest(sample, sampleTime);
return;
}
_buffer.Add(new ValueTimePair { value = sample, time = sampleTime });
}
public T Get(int idx) {
return _buffer.Get(idx).value;
}
public T GetLatest() {
return Get(Length - 1);
}
public void Set(int idx, T sample, float sampleTime) {
_buffer.Set(idx, new ValueTimePair { value = sample, time = sampleTime });
}
public void SetLatest(T sample, float sampleTime) {
if (Length == 0) Set(0, sample, sampleTime);
else Set(Length - 1, sample, sampleTime);
}
public float GetTime(int idx) {
return _buffer.Get(idx).time;
}
/// <summary> Returns the average change between each sample per unit time, or zero if the buffer is not full. </summary>
public abstract T Delta();
}
/// <summary> Allows you to add to a capped-size ring buffer of Vector3s and,
/// when full, compute the buffer's average change over time. </summary>
public class DeltaBuffer : DeltaBuffer<Vector3> {
public DeltaBuffer(int bufferSize) : base(bufferSize) { }
/// <summary> Returns the average change between each sample per unit time, or zero if the buffer is not full. </summary>
public override Vector3 Delta() {
if (!IsFull) {
return Vector3.zero;
}
Vector3 deltaPerTimeSum = Vector3.zero;
int length = Length;
for (int i = 0; i < length - 1; i++) {
deltaPerTimeSum += (Get(i + 1) - Get(i)) / (GetTime(i + 1) - GetTime(i));
}
return deltaPerTimeSum / (length - 1);
}
}
/// <summary> Allows you to add to a capped-size ring buffer of floats and,
/// when full, compute the buffer's average change over time. </summary>
public class DeltaFloatBuffer : DeltaBuffer<float> {
public DeltaFloatBuffer(int bufferSize) : base(bufferSize) { }
/// <summary>Returns the average change between each sample per unit time, or zero if the buffer is not full.</summary>
public override float Delta() {
if (!IsFull) {
return 0F;
}
float deltaPerTimeSum = 0F;
int length = Length;
for (int i = 0; i < length - 1; i++) {
deltaPerTimeSum += (Get(i + 1) - Get(i)) / (GetTime(i + 1) - GetTime(i));
}
return deltaPerTimeSum / (length - 1);
}
}