forked from lukas-walker/4d-holographic-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputRecordingBuffer.cs
More file actions
356 lines (308 loc) · 11.5 KB
/
InputRecordingBuffer.cs
File metadata and controls
356 lines (308 loc) · 11.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;
namespace Tutorials
{
/// <summary>
/// Simple class that is used to serialize and store the position and rotation of idividual joints or other objects.
/// Note: This class takes the LOCAL position and rotation, as these are the relevant values for joints, since joints' positions and rotations always depend on their respective parent joints.
/// </summary>
public class TransformData
{
public float posx;
public float posy;
public float posz;
public float rotx;
public float roty;
public float rotz;
public float rotw;
public float scalex;
public float scaley;
public float scalez;
public float globPosx;
public float globPosy;
public float globPosz;
public float globRotx;
public float globRoty;
public float globRotz;
public float globRotw;
public static TransformData ZeroIdentity()
{
return new TransformData(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1);
}
public TransformData(Transform transform)
{
posx = transform.localPosition.x;
posy = transform.localPosition.y;
posz = transform.localPosition.z;
rotx = transform.localRotation.x;
roty = transform.localRotation.y;
rotz = transform.localRotation.z;
rotw = transform.localRotation.w;
scalex = transform.localScale.x;
scaley = transform.localScale.y;
scalez = transform.localScale.z;
globPosx = transform.position.x;
globPosy = transform.position.y;
globPosz = transform.position.z;
globRotx = transform.rotation.x;
globRoty = transform.rotation.y;
globRotz = transform.rotation.z;
globRotw = transform.rotation.w;
}
//public TransformData(float posx,
// float posy,
// float posz,
// float rotx,
// float roty,
// float rotz,
// float rotw,
// float scalex,
// float scaley,
// float scalez)
//{
// this.posx = posx;
// this.posy = posy;
// this.posz = posz;
// this.rotx = rotx;
// this.roty = roty;
// this.rotz = rotz;
// this.rotw = rotw;
// this.scalex = scalex;
// this.scaley = scaley;
// this.scalez = scalez;
//}
public TransformData(float posx,
float posy,
float posz,
float rotx,
float roty,
float rotz,
float rotw,
float scalex,
float scaley,
float scalez,
float globPosx,
float globPosy,
float globPosz,
float globRotx,
float globRoty,
float globRotz,
float globRotw)
{
this.posx = posx;
this.posy = posy;
this.posz = posz;
this.rotx = rotx;
this.roty = roty;
this.rotz = rotz;
this.rotw = rotw;
this.scalex = scalex;
this.scaley = scaley;
this.scalez = scalez;
this.globPosx = globPosx;
this.globPosy = globPosy;
this.globPosz = globPosz;
this.globRotx = globRotx;
this.globRoty = globRoty;
this.globRotz = globRotz;
this.globRotw = globRotw;
}
/// <summary>
/// Returns the position as a Vector3 object
/// </summary>
public Vector3 GetPosition()
{
return new Vector3(posx, posy, posz);
}
public Vector3 GetGlobalPosition()
{
return new Vector3(globPosx, globPosy, globPosz);
}
/// <summary>
/// Gets the rotation as a Quaternion
/// </summary>
public Quaternion GetRotation()
{
return new Quaternion(rotx, roty, rotz, rotw);
}
public Quaternion GetGlobalRotation()
{
return new Quaternion(globRotx, globRoty, globRotz, globRotw);
}
/// <summary>
/// Gets the scale as a Vector3 object
/// </summary>
public Vector3 GetScale()
{
return new Vector3(scalex, scaley, scalez);
}
/// <summary>
/// Converts the TransForm to a human readable string.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return string.Format("[{0},{1},{2}][{3},{4},{5},{6}]", posx, posy, posz, rotx, roty, rotz, rotw);
}
}
/// <summary>
/// Container used to efficiently store a sequence of input animation keyframes while recording
/// </summary>
public class InputRecordingBuffer : IEnumerable<InputRecordingBuffer.Keyframe>
{
/// <summary>
/// The input state for a single frame
/// </summary>
public class Keyframe
{
public float Time { get; set; }
public bool LeftTracked { get; set; }
public bool RightTracked { get; set; }
public bool LeftPinch { get; set; }
public bool RightPinch { get; set; }
public MixedRealityPose CameraPose { get; set; }
public MixedRealityPose GazePose { get; set; }
public Dictionary<TrackedHandJoint, MixedRealityPose> LeftJoints { get; set; }
public Dictionary<TrackedHandJoint, MixedRealityPose> RightJoints { get; set; }
public Dictionary<TrackedHandJoint, TransformData> LeftJointsTransformData { get; set; }
public Dictionary<TrackedHandJoint, TransformData> RightJointsTransformData { get; set; }
/// <summary>
/// Holds transformation data for each object recorded
/// </summary>
public Dictionary<string, TransformData> ObjectsTransformData { get; set; }
/// <summary>
/// Initialize the data structures to hold the joint/object transformations for a time-specific keyframe
/// </summary>
/// <param name="time"></param>
public Keyframe(float time)
{
Time = time;
LeftJoints = new Dictionary<TrackedHandJoint, MixedRealityPose>();
RightJoints = new Dictionary<TrackedHandJoint, MixedRealityPose>();
LeftJointsTransformData = new Dictionary<TrackedHandJoint, TransformData>();
RightJointsTransformData = new Dictionary<TrackedHandJoint, TransformData>();
ObjectsTransformData = new Dictionary<string, TransformData>();
}
}
/// <summary>
/// The time of the first keyframe in the buffer
/// </summary>
public float StartTime
{
get
{
return keyframes.Peek().Time;
}
}
private Keyframe currentKeyframe;
private Queue<Keyframe> keyframes;
/// <summary>
/// Default constructor
/// </summary>
public InputRecordingBuffer() => keyframes = new Queue<Keyframe>();
public bool Empty()
{
return keyframes.Count == 0;
}
/// <summary>
/// Removes all keyframes from the buffer
/// </summary>
public void Clear()
{
keyframes.Clear();
currentKeyframe = null;
}
/// <summary>
/// Removes all keyframes before a given time
/// </summary>
public void RemoveBeforeTime(float time)
{
while (keyframes.Count > 0 && keyframes.Peek().Time < time)
{
keyframes.Dequeue();
}
}
/// <summary>
/// Sets the camera pose to be stored in the newest keyframe
/// </summary>
public void SetCameraPose(MixedRealityPose pose) => currentKeyframe.CameraPose = pose;
/// <summary>
/// Sets the eye gaze pose to be stored in the newest keyframe
/// </summary>
public void SetGazePose(MixedRealityPose pose) => currentKeyframe.GazePose = pose;
/// <summary>
/// Sets the state of a given hand to be stored in the newest keyframe
/// </summary>
public void SetHandState(Handedness handedness, bool tracked, bool pinching)
{
if (handedness == Handedness.Left)
{
currentKeyframe.LeftTracked = tracked;
currentKeyframe.LeftPinch = pinching;
}
else
{
currentKeyframe.RightTracked = tracked;
currentKeyframe.RightPinch = pinching;
}
}
/// <summary>
/// Sets the pose of a given joint to be stored in the newest keyframe
/// </summary>
public void SetJointPose(Handedness handedness, TrackedHandJoint joint, MixedRealityPose pose)
{
if (handedness == Handedness.Left)
{
currentKeyframe.LeftJoints.Add(joint, pose);
}
else
{
currentKeyframe.RightJoints.Add(joint, pose);
}
}
/// <summary>
/// Sets the transform data of a given joint to be stored in the newest keyframe
/// </summary>
public void SetJointTransform(Handedness handedness, TrackedHandJoint joint, Transform transform)
{
if (handedness == Handedness.Left)
{
currentKeyframe.LeftJointsTransformData.Add(joint, new TransformData(transform));
}
else
{
currentKeyframe.RightJointsTransformData.Add(joint, new TransformData(transform));
}
}
public void SetObjectState(Transform objectTransform)
{
currentKeyframe.ObjectsTransformData.Add(objectTransform.name, new TransformData(objectTransform));
}
/// <summary>
/// Creates a new, empty keyframe with a given time at the end of the buffer
/// </summary>
/// <returns>The index of the new keyframe</returns>
public int NewKeyframe(float time)
{
latestValidKeyframe = currentKeyframe;
currentKeyframe = new Keyframe(time);
keyframes.Enqueue(currentKeyframe);
return keyframes.Count - 1;
}
private Keyframe latestValidKeyframe;
public Keyframe GetLatestKeyframe()
{
return latestValidKeyframe;
}
/// <summary>
/// Returns a sequence of all keyframes in the buffer
/// </summary>
public IEnumerator<Keyframe> GetEnumerator() => keyframes.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}