Skip to content

Commit

Permalink
wip hand tracking stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzo committed Nov 25, 2024
1 parent 7915963 commit 9491d91
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 162 deletions.
86 changes: 86 additions & 0 deletions common/Utilities/KeyedHeap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;

namespace Sst.Utilities;

public class KeyedHeap<K, V> {
public List<(K Key, V Value)> Heap = new();
public Dictionary<K, int> KeyToIndexInHeap = new();
public Func<V, V, bool> Comparator;

public KeyedHeap(Func<V, V, bool> comparator) { Comparator = comparator; }

// O(1)
public int Size() => Heap.Count;

// O(1)
public (K Key, V Value) Peek() => Heap[0];

// O(log n)
public void Push(K key, V value) {
Heap.Add((key, value));
KeyToIndexInHeap.Add(key, Heap.Count - 1);
BubbleUp(Heap.Count - 1);
}

// O(log n)
public (K Key, V Value) Pop() {
var first = Heap[0];
KeyToIndexInHeap.Remove(first.Key);
var last = Heap[Heap.Count - 1];
Heap.RemoveAt(Heap.Count - 1);
Heap[0] = last;
KeyToIndexInHeap[last.Key] = 0;
SiftDown(0);
return first;
}

// O(log n)
public void Delete(K key) {
var index = KeyToIndexInHeap[key];
KeyToIndexInHeap.Remove(key);
var end = Heap[Heap.Count - 1];
Heap.RemoveAt(Heap.Count - 1);
if (index == Heap.Count)
return;
Heap[index] = end;
KeyToIndexInHeap[end.Key] = index;
BubbleUp(index);
SiftDown(index);
}

private void BubbleUp(int index) {
var parentIndex = (index - 1) / 2;
if (index <= 0 || Comparator(Heap[parentIndex].Value, Heap[index].Value))
return;
Swap(index, parentIndex);
BubbleUp(parentIndex);
}

private void SiftDown(int index) {
var leftIndex = index * 2 + 1;
var rightIndex = index * 2 + 2;
var smallest = index;
if (leftIndex < Heap.Count &&
!Comparator(Heap[smallest].Value, Heap[leftIndex].Value)) {
smallest = leftIndex;
}
if (rightIndex < Heap.Count &&
!Comparator(Heap[smallest].Value, Heap[rightIndex].Value)) {
smallest = rightIndex;
}
if (smallest != index) {
Swap(index, smallest);
SiftDown(smallest);
}
}

private void Swap(int i, int j) {
var a = Heap[i];
var b = Heap[j];
Heap[i] = b;
Heap[j] = a;
KeyToIndexInHeap[a.Key] = j;
KeyToIndexInHeap[b.Key] = i;
}
}
2 changes: 2 additions & 0 deletions projects/Bonelab/HandTracking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Hand tracking's accuracy has several limitations and can be a frustrating experi
- Don't hold them too close to your face while aiming (hand will lose tracking if too close to headset)
- **While throwing things, climbing or any other action:**
- Try not to bring your hands too close to the headset or out of your eye sight
- Sometimes hand tracking doesn't work when the game starts
- Just open and close the Oculus menu and it should start working

## Fun facts

Expand Down
2 changes: 2 additions & 0 deletions projects/Bonelab/HandTracking/scripts/vid.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- Hand mapping
- Different bones in Oculus hand tracking API vs Bonelab
- Movement
- Examples
- [Pinch and pull](https://x.com/haltor/status/1671163104175194113)
- Hand running
- Nimsony head bobbing
- Jumping
Expand Down
Loading

0 comments on commit 9491d91

Please sign in to comment.