Tanoda
TransformHistory.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 UnityEngine;
10namespace Leap.Unity {
14 public class TransformHistory {
16 public TransformHistory(int capacity = 32) {
17 history = new RingBuffer<TransformData>(capacity);
18 }
19
20 //Store current Transform in History
21 public void UpdateDelay(Pose curPose, long timestamp) {
22 TransformData currentTransform =
23 new TransformData() {
24 time = timestamp,
25 position = curPose.position,
26 rotation = curPose.rotation,
27 };
28
29 history.Add(currentTransform);
30 }
31
32 //Calculate delayed Transform
33 public void SampleTransform(long timestamp, out Vector3 delayedPos, out Quaternion delayedRot) {
34 TransformData desiredTransform = TransformData.GetTransformAtTime(history, timestamp);
35 delayedPos = desiredTransform.position;
36 delayedRot = desiredTransform.rotation;
37 }
38
39 public struct TransformData {
40 public long time; // microseconds
41 public Vector3 position; //meters
42 public Quaternion rotation; //magic
43
44 public static TransformData Lerp(TransformData from, TransformData to, long time) {
45 if (from.time == to.time) {
46 return from;
47 }
48 float fraction = (float)(((double)(time - from.time)) / ((double)(to.time - from.time)));
49 return new TransformData() {
50 time = time,
51 position = Vector3.Lerp(from.position, to.position, fraction),
52 rotation = Quaternion.Slerp(from.rotation, to.rotation, fraction)
53 };
54 }
55
57 for (int i = history.Count - 1; i > 0; i--) {
58 if (history.Get(i).time >= desiredTime && history.Get(i - 1).time < desiredTime) {
59 return Lerp(history.Get(i - 1), history.Get(i), desiredTime);
60 }
61 }
62
63 if (history.Count > 0) {
64 return history.GetLatest();
65 }
66 else {
67 // No history data available.
68 return new TransformData() {
69 time = desiredTime,
70 position = Vector3.zero,
71 rotation = Quaternion.identity
72 };
73 }
74 }
75 }
76 }
77}
Implements a resample-able transform history.
void SampleTransform(long timestamp, out Vector3 delayedPos, out Quaternion delayedRot)
RingBuffer< TransformData > history
void UpdateDelay(Pose curPose, long timestamp)
TransformHistory(int capacity=32)
A position and rotation. You can multiply two poses; this acts like Matrix4x4 multiplication,...
Definition: Pose.cs:21
Quaternion rotation
Definition: Pose.cs:24
Vector3 position
Definition: Pose.cs:23
static TransformData Lerp(TransformData from, TransformData to, long time)
static TransformData GetTransformAtTime(RingBuffer< TransformData > history, long desiredTime)