Skip to content

Commit

Permalink
Merge pull request #49 from ButterscotchVanilla/autobone-positions
Browse files Browse the repository at this point in the history
AutoBone PoseFrame file format rework and other related fixes
  • Loading branch information
Eirenliel authored Sep 18, 2021
2 parents d5c0486 + 6d103d4 commit 18cea30
Show file tree
Hide file tree
Showing 21 changed files with 1,203 additions and 513 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.eiren.gui.autobone;
package dev.slimevr.gui;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
Expand All @@ -20,25 +20,31 @@
import io.eiren.util.collections.FastList;
import io.eiren.util.logging.LogManager;
import io.eiren.vr.VRServer;
import dev.slimevr.vr.autobone.AutoBone;

import javax.swing.event.MouseInputAdapter;

import org.apache.commons.lang3.tuple.Pair;

import dev.slimevr.vr.poserecorder.PoseFrame;
import dev.slimevr.vr.poserecorder.PoseFrameIO;
import dev.slimevr.vr.poserecorder.PoseRecorder;

public class AutoBoneWindow extends JFrame {

private static File saveDir = new File("Recordings");
private static File loadDir = new File("LoadRecordings");

private EJBox pane;

private final VRServer server;
private final SkeletonConfig skeletonConfig;
private final PoseRecorder poseRecorder;
private final AutoBone autoBone;
private final transient VRServer server;
private final transient SkeletonConfig skeletonConfig;
private final transient PoseRecorder poseRecorder;
private final transient AutoBone autoBone;

private Thread recordingThread = null;
private Thread saveRecordingThread = null;
private Thread autoBoneThread = null;
private transient Thread recordingThread = null;
private transient Thread saveRecordingThread = null;
private transient Thread autoBoneThread = null;

private JButton saveRecordingButton;
private JButton adjustButton;
Expand Down Expand Up @@ -77,12 +83,12 @@ private String getLengthsString() {
return configInfo.toString();
}

private void saveRecording(PoseFrame[] frames) {
private void saveRecording(PoseFrame frames) {
if (saveDir.isDirectory() || saveDir.mkdirs()) {
File saveRecording;
int recordingIndex = 1;
do {
saveRecording = new File(saveDir, "ABRecording" + recordingIndex++ + ".abf");
saveRecording = new File(saveDir, "ABRecording" + recordingIndex++ + ".pfr");
} while (saveRecording.exists());

LogManager.log.info("[AutoBone] Exporting frames to \"" + saveRecording.getPath() + "\"...");
Expand All @@ -96,15 +102,15 @@ private void saveRecording(PoseFrame[] frames) {
}
}

private List<Pair<String, PoseFrame[]>> loadRecordings() {
List<Pair<String, PoseFrame[]>> recordings = new FastList<Pair<String, PoseFrame[]>>();
private List<Pair<String, PoseFrame>> loadRecordings() {
List<Pair<String, PoseFrame>> recordings = new FastList<Pair<String, PoseFrame>>();
if (loadDir.isDirectory()) {
File[] files = loadDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && org.apache.commons.lang3.StringUtils.endsWithIgnoreCase(file.getName(), ".abf")) {
if (file.isFile() && org.apache.commons.lang3.StringUtils.endsWithIgnoreCase(file.getName(), ".pfr")) {
LogManager.log.info("[AutoBone] Detected recording at \"" + file.getPath() + "\", loading frames...");
PoseFrame[] frames = PoseFrameIO.readFromFile(file);
PoseFrame frames = PoseFrameIO.readFromFile(file);

if (frames == null) {
LogManager.log.severe("Reading frames from \"" + file.getPath() + "\" failed...");
Expand All @@ -119,9 +125,7 @@ private List<Pair<String, PoseFrame[]>> loadRecordings() {
return recordings;
}

private float processFrames(PoseFrame[] frames) {
autoBone.reloadConfigValues();

private float processFrames(PoseFrame frames) {
autoBone.minDataDistance = server.config.getInt("autobone.minimumDataDistance", autoBone.minDataDistance);
autoBone.maxDataDistance = server.config.getInt("autobone.maximumDataDistance", autoBone.maxDataDistance);

Expand Down Expand Up @@ -167,8 +171,8 @@ public void run() {
// 1000 samples at 20 ms per sample is 20 seconds
int sampleCount = server.config.getInt("autobone.sampleCount", 1000);
long sampleRate = server.config.getLong("autobone.sampleRateMs", 20L);
Future<PoseFrame[]> framesFuture = poseRecorder.startFrameRecording(sampleCount, sampleRate);
PoseFrame[] frames = framesFuture.get();
Future<PoseFrame> framesFuture = poseRecorder.startFrameRecording(sampleCount, sampleRate);
PoseFrame frames = framesFuture.get();
LogManager.log.info("[AutoBone] Done recording!");

saveRecordingButton.setEnabled(true);
Expand Down Expand Up @@ -219,17 +223,28 @@ public void mouseClicked(MouseEvent e) {
@Override
public void run() {
try {
Future<PoseFrame[]> framesFuture = poseRecorder.getFramesAsync();
Future<PoseFrame> framesFuture = poseRecorder.getFramesAsync();
if (framesFuture != null) {
setText("Waiting for Recording...");
PoseFrame[] frames = framesFuture.get();
PoseFrame frames = framesFuture.get();

if (frames.length <= 0) {
if (frames.getTrackerCount() <= 0) {
throw new IllegalStateException("Recording has no trackers");
}

if (frames.getMaxFrameCount() <= 0) {
throw new IllegalStateException("Recording has no frames");
}

setText("Saving...");
saveRecording(frames);

setText("Recording Saved!");
try {
Thread.sleep(3000); // Wait for 3 seconds
} catch (Exception e1) {
// Ignore
}
} else {
setText("No Recording...");
LogManager.log.severe("[AutoBone] Unable to save, no recording was done...");
Expand Down Expand Up @@ -277,17 +292,21 @@ public void mouseClicked(MouseEvent e) {
public void run() {
try {
setText("Load...");
List<Pair<String, PoseFrame[]>> frameRecordings = loadRecordings();
List<Pair<String, PoseFrame>> frameRecordings = loadRecordings();

if (frameRecordings.size() > 0) {
if (!frameRecordings.isEmpty()) {
LogManager.log.info("[AutoBone] Done loading frames!");
} else {
Future<PoseFrame[]> framesFuture = poseRecorder.getFramesAsync();
Future<PoseFrame> framesFuture = poseRecorder.getFramesAsync();
if (framesFuture != null) {
setText("Waiting for Recording...");
PoseFrame[] frames = framesFuture.get();
PoseFrame frames = framesFuture.get();

if (frames.getTrackerCount() <= 0) {
throw new IllegalStateException("Recording has no trackers");
}

if (frames.length <= 0) {
if (frames.getMaxFrameCount() <= 0) {
throw new IllegalStateException("Recording has no frames");
}

Expand All @@ -307,7 +326,7 @@ public void run() {
setText("Processing...");
LogManager.log.info("[AutoBone] Processing frames...");
FastList<Float> heightPercentError = new FastList<Float>(frameRecordings.size());
for (Pair<String, PoseFrame[]> recording : frameRecordings) {
for (Pair<String, PoseFrame> recording : frameRecordings) {
LogManager.log.info("[AutoBone] Processing frames from \"" + recording.getKey() + "\"...");

heightPercentError.add(processFrames(recording.getValue()));
Expand Down Expand Up @@ -341,7 +360,7 @@ public void run() {
lengthsLabel.setText(lengthsString);
}

if (heightPercentError.size() > 0) {
if (!heightPercentError.isEmpty()) {
float mean = 0f;
for (float val : heightPercentError) {
mean += val;
Expand Down
Loading

0 comments on commit 18cea30

Please sign in to comment.