Tanoda
VectorHandRecording.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 System;
11using System.Collections;
12using System.Collections.Generic;
13using UnityEngine;
14
15namespace Leap.Unity.Recording {
16
18
19 [SerializeField]
20 private VectorFrameCurves _frameCurves;
21
22 private Hand _leftHand = new Hand();
23 private Hand _rightHand = new Hand();
24
25 public override float length {
26 get {
27 return _maxKeyframeTime - _minKeyframeTime;
28 }
29 }
30
31 [SerializeField]
32 private float _minKeyframeTime = 0f;
33 public float minKeyframeTime {
34 get { return _minKeyframeTime; }
35 }
36
37 [SerializeField]
38 private float _maxKeyframeTime = 0f;
39 public float maxKeyframeTime {
40 get { return _maxKeyframeTime; }
41 }
42
48 public override void LoadFrames(List<Frame> frames) {
49 _frameCurves = new VectorFrameCurves();
50
51 Debug.Log("Loading " + frames.Count + " frames.");
52
53 _minKeyframeTime = (float)(frames[0] .Timestamp * NS_TO_S);
54 _maxKeyframeTime = (float)(frames[frames.Count - 1].Timestamp * NS_TO_S);
55
56 Debug.Log("Total recording length is " + length);
57
58 // Add each frame as a keyframe naively.
59 foreach (var frame in frames) {
60 float keyframeTime = (float)(frame.Timestamp * NS_TO_S - _minKeyframeTime);
61
62 _frameCurves.AddKeyframes(keyframeTime, frame);
63 }
64
65#if UNITY_EDITOR
66 // Compress these keyframes.
67 _frameCurves.CompressCurves();
68#endif
69
70 Debug.Log("Finished loading frames.");
71 }
72
73 public override bool Sample(float time, Frame toFill, bool clampTimeToValid = true) {
74 float timeToSample = time + minKeyframeTime;
75
76 if (!clampTimeToValid && outsideDataBounds(timeToSample)) {
77 return false;
78 }
79 else {
80 _frameCurves.Sample(timeToSample, toFill, _leftHand, _rightHand);
81 return true;
82 }
83 }
84
85 private bool outsideDataBounds(float time) {
86 return time < minKeyframeTime || time > maxKeyframeTime;
87 }
88
89 }
90
91}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
The Frame class represents a set of hand and finger tracking data detected in a single frame.
Definition: Frame.cs:24
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
const double NS_TO_S
Converts nanoseconds to seconds. Leap frames store timestamps in nanoseconds.
override bool Sample(float time, Frame toFill, bool clampTimeToValid=true)
Samples the recording at the given time. Caller must provide a frame object that will be filled with ...
override void LoadFrames(List< Frame > frames)
Loads the provided sequential frame data into this recording representation.
VectorHand Animation Curve data struct for a Leap frame.
void AddKeyframes(float keyframeTime, Frame frame)
Adds keyframe data from the provided frame to this animation data at the time specified in seconds.
void Sample(float time, Frame intoFrame, Hand intoLeftHand, Hand intoRightHand)
Samples the data in these VectorFrameCurves at the specified time into the provided frame.