Merge pull request #137 from Louka3000/main

Basic Interpolation and Extrapolation
This commit is contained in:
Eiren Rain authored and GitHub committed 2022-03-28 16:56:05 +03:00
commit 38b8e65d53
7 files changed
+320 -6

No files matched your search

@@ -0,0 +1,142 @@
package dev.slimevr.gui;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;
import com.jme3.math.FastMath;
import dev.slimevr.VRServer;
import dev.slimevr.gui.swing.EJBagNoStretch;
import dev.slimevr.vr.trackers.IMUTracker;
import dev.slimevr.vr.trackers.ReferenceAdjustedTracker;
import dev.slimevr.vr.trackers.Tracker;
import dev.slimevr.vr.trackers.TrackerFilters;
import io.eiren.util.StringUtils;
import io.eiren.util.collections.FastList;
public class TrackersFiltersGUI extends EJBagNoStretch {
private final VRServer server;
String filterType;
float filterAmount;
int filterTicks;
private List<Tracker> allTrackers = new FastList<>();
private JLabel amountLabel, ticksLabel;
public TrackersFiltersGUI(VRServer server, VRServerGUI gui) {
super(false, true);
this.server = server;
int row = 0;
setAlignmentY(TOP_ALIGNMENT);
add(Box.createVerticalStrut(10));
filterType = server.config.getString("filters.type");
if(filterType == null){
filterType = "NONE";
}
JComboBox<String> filterSelect;
add(filterSelect = new JComboBox<>(), s(c(0, row, 2), 4, 1));
for(TrackerFilters f : TrackerFilters.values()) {
filterSelect.addItem(f.name());
}
if(filterType != null) {
filterSelect.setSelectedItem(filterType);
} else {
filterSelect.setSelectedItem(TrackerFilters.NONE);
}
filterSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
filterType = filterSelect.getSelectedItem().toString();
server.config.setProperty("filters.type", filterType);
server.saveConfig();
updateTrackersFilters();
}
});
add(Box.createVerticalStrut(40));
row++;
filterAmount = (Float) FastMath.clamp(server.config.getFloat("filters.amount", 0.3f), 0, 1);
add(new JLabel("Intensity"), c(0, row, 2));
add(new AdjButton("+", 0, false), c(1, row, 2));
add(amountLabel = new JLabel(StringUtils.prettyNumber(filterAmount * 100f) + "%"), c(2, row, 2));
add(new AdjButton("-", 0, true), c(3, row, 2));
row++;
filterTicks = (int) FastMath.clamp(server.config.getInt("filters.tickCount", 2), 0, 80);
add(new JLabel("Ticks"), c(0, row, 2));
add(new AdjButton("+", 1, false), c(1, row, 2));
add(ticksLabel = new JLabel(StringUtils.prettyNumber(filterTicks)), c(2, row, 2));
add(new AdjButton("-", 1, true), c(3, row, 2));
}
void updateTrackersFilters(){
allTrackers = server.getAllTrackers();
IMUTracker imu;
for (Tracker t : allTrackers) {
Tracker realTracker = t;
if(t instanceof ReferenceAdjustedTracker)
realTracker = ((ReferenceAdjustedTracker<? extends Tracker>) t).getTracker();
if(realTracker instanceof IMUTracker){
imu = (IMUTracker)realTracker;
imu.setFilter(filterType, filterAmount, filterTicks);
}
}
}
void adjustValues(int cat, boolean neg){
if(cat == 0){
if(neg){
filterAmount = (Float) FastMath.clamp(filterAmount - 0.1f, 0, 1);
}
else{
filterAmount = (Float) FastMath.clamp(filterAmount + 0.1f, 0, 1);
}
amountLabel.setText((StringUtils.prettyNumber(filterAmount * 100f)) + "%");
server.config.setProperty("filters.amount", filterAmount);
}
else if(cat == 1){
if(neg){
filterTicks = (int) FastMath.clamp(filterTicks - 1, 0, 80);
}
else{
filterTicks = (int) FastMath.clamp(filterTicks + 1, 0, 80);
}
ticksLabel.setText((StringUtils.prettyNumber(filterTicks)));
server.config.setProperty("filters.tickCount", filterTicks);
}
server.saveConfig();
updateTrackersFilters();
}
private class AdjButton extends JButton {
public AdjButton(String text, int category, boolean neg) {
super(text);
addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
adjustValues(category, neg);
}
});
}
}
}
@@ -47,6 +47,7 @@ public class VRServerGUI extends JFrame {
public final VRServer server;
private final TrackersList trackersList;
private final TrackersFiltersGUI trackersFiltersGUI;
private final SkeletonList skeletonList;
private JButton resetButton;
private EJBox pane;
@@ -95,6 +96,7 @@ public class VRServerGUI extends JFrame {
getContentPane().setLayout(new BoxLayout(getContentPane(), PAGE_AXIS));
this.trackersList = new TrackersList(server, this);
trackersFiltersGUI = new TrackersFiltersGUI(server, this);
this.skeletonList = new SkeletonList(server, this);
this.poseStreamer = new ServerPoseStreamer(server);
@@ -353,6 +355,14 @@ public class VRServerGUI extends JFrame {
add(Box.createVerticalStrut(10));
}
add(l = new JLabel("Trackers filtering"));
l.setFont(l.getFont().deriveFont(Font.BOLD));
l.setAlignmentX(0.5f);
add(trackersFiltersGUI);
add(Box.createVerticalStrut(10));
add(new JLabel("Skeleton data"));
add(skeletonList);
add(Box.createVerticalGlue());
@@ -0,0 +1,107 @@
package dev.slimevr.vr.trackers;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.RandomAccess;
/**
* If you use this code, please consider notifying isak at du-preez dot com
* with a brief description of your application.
*
* This is free and unencumbered software released into the public domain.
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*/
public class CircularArrayList<E>
extends AbstractList<E> implements RandomAccess {
private final int n; // buffer length
private final List<E> buf; // a List implementing RandomAccess
private int head = 0;
private int tail = 0;
public CircularArrayList(int capacity) {
n = capacity + 1;
buf = new ArrayList<E>(Collections.nCopies(n, (E) null));
}
public int capacity() {
return n - 1;
}
private int wrapIndex(int i) {
int m = i % n;
if (m < 0) { // java modulus can be negative
m += n;
}
return m;
}
// This method is O(n) but will never be called if the
// CircularArrayList is used in its typical/intended role.
private void shiftBlock(int startIndex, int endIndex) {
assert (endIndex > startIndex);
for (int i = endIndex - 1; i >= startIndex; i--) {
set(i + 1, get(i));
}
}
@Override
public int size() {
return tail - head + (tail < head ? n : 0);
}
@Override
public E get(int i) {
if (i < 0 || i >= size()) {
throw new IndexOutOfBoundsException();
}
return buf.get(wrapIndex(head + i));
}
@Override
public E set(int i, E e) {
if (i < 0 || i >= size()) {
throw new IndexOutOfBoundsException();
}
return buf.set(wrapIndex(head + i), e);
}
@Override
public void add(int i, E e) {
int s = size();
if (s == n - 1) {
throw new IllegalStateException(
"CircularArrayList is filled to capacity. "
+ "(You may want to remove from front"
+ " before adding more to back.)");
}
if (i < 0 || i > s) {
throw new IndexOutOfBoundsException();
}
tail = wrapIndex(tail + 1);
if (i < s) {
shiftBlock(i, s);
}
set(i, e);
}
@Override
public E remove(int i) {
int s = size();
if (i < 0 || i >= s) {
throw new IndexOutOfBoundsException();
}
E e = get(i);
if (i > 0) {
shiftBlock(0, i);
}
head = wrapIndex(head + 1);
return e;
}
}
@@ -4,6 +4,7 @@ import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import dev.slimevr.VRServer;
import dev.slimevr.vr.trackers.udp.TrackersUDPServer;
import io.eiren.util.BufferedTimer;
@@ -18,6 +19,10 @@ public class IMUTracker implements Tracker, TrackerWithTPS, TrackerWithBattery {
public final Quaternion rotMagQuaternion = new Quaternion();
public final Quaternion rotAdjust = new Quaternion();
protected final Quaternion correction = new Quaternion();
protected CircularArrayList<Quaternion> previousRots;
private final Quaternion buffQuat = new Quaternion();
public int movementFilterTickCount = 0;
public float movementFilterAmount = 1f;
protected TrackerMountingRotation mounting = null;
protected TrackerStatus status = TrackerStatus.OK;
protected final int trackerId;
@@ -25,6 +30,7 @@ public class IMUTracker implements Tracker, TrackerWithTPS, TrackerWithBattery {
protected final String name;
protected final String descriptiveName;
protected final TrackersUDPServer server;
protected final VRServer vrserver;
protected float confidence = 0;
protected float batteryVoltage = 0;
protected float batteryLevel = 0;
@@ -41,11 +47,12 @@ public class IMUTracker implements Tracker, TrackerWithTPS, TrackerWithBattery {
public TrackerPosition bodyPosition = null;
public IMUTracker(int trackerId, String name, String descriptiveName, TrackersUDPServer server) {
public IMUTracker(int trackerId, String name, String descriptiveName, TrackersUDPServer server, VRServer vrserver) {
this.name = name;
this.server = server;
this.trackerId = trackerId;
this.descriptiveName = descriptiveName;
this.vrserver = vrserver;
}
@Override
@@ -75,9 +82,35 @@ public class IMUTracker implements Tracker, TrackerWithTPS, TrackerWithBattery {
rotAdjust.loadIdentity();
}
bodyPosition = TrackerPosition.getByDesignation(config.designation);
setFilter(vrserver.config.getString("filters.type"), vrserver.config.getFloat("filters.amount", 0.3f), vrserver.config.getInt("filters.tickCount", 2));
}
}
public void setFilter(String type, float amount, int ticks){
amount = FastMath.clamp(amount, 0, 1f);
ticks = (int) FastMath.clamp(ticks, 0, 80);
if(type != null){
switch(type){
case "INTERPOLATION":
movementFilterAmount = 1f - (amount / 1.75f);
movementFilterTickCount = ticks;
break;
case "EXTRAPOLATION":
movementFilterAmount = 1f + (amount * 1.1f);
movementFilterTickCount = ticks;
break;
case "NONE":
default:
movementFilterAmount = 1f;
movementFilterTickCount = 0;
break;
}
}
else{
movementFilterAmount = 1f;
movementFilterTickCount = 0;
}
previousRots = new CircularArrayList<Quaternion>(movementFilterTickCount + 1);
}
public TrackerMountingRotation getMountingRotation() {
return mounting;
}
@@ -101,6 +134,12 @@ public class IMUTracker implements Tracker, TrackerWithTPS, TrackerWithBattery {
calculateLiveMagnetometerCorrection();
}
}
if(movementFilterTickCount != 0){
previousRots.add(rotQuaternion.clone());
if(previousRots.size() > movementFilterTickCount){
previousRots.remove(0);
}
}
}
@Override
@@ -116,7 +155,14 @@ public class IMUTracker implements Tracker, TrackerWithTPS, TrackerWithBattery {
@Override
public boolean getRotation(Quaternion store) {
store.set(rotQuaternion);
if(movementFilterTickCount > 0 && movementFilterAmount != 1 && previousRots.size() > 0){
buffQuat.set(previousRots.get(0));
buffQuat.slerp(rotQuaternion, movementFilterAmount);
store.set(buffQuat);
}
else{
store.set(rotQuaternion);
}
//correction.mult(store, store); // Correction is not used now to prevent accidental errors while debugging other things
store.multLocal(rotAdjust);
return true;
@@ -2,14 +2,15 @@ package dev.slimevr.vr.trackers;
import java.nio.ByteBuffer;
import dev.slimevr.VRServer;
import dev.slimevr.vr.trackers.udp.TrackersUDPServer;
public class MPUTracker extends IMUTracker {
public ConfigurationData newCalibrationData;
public MPUTracker(int trackerId, String name, String descriptiveName, TrackersUDPServer server) {
super(trackerId, name, descriptiveName, server);
public MPUTracker(int trackerId, String name, String descriptiveName, TrackersUDPServer server, VRServer vrserver) {
super(trackerId, name, descriptiveName, server, vrserver);
}
public static class ConfigurationData {
@@ -0,0 +1,7 @@
package dev.slimevr.vr.trackers;
public enum TrackerFilters {
NONE,
INTERPOLATION,
EXTRAPOLATION;
}
@@ -27,6 +27,7 @@ import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import dev.slimevr.Main;
import dev.slimevr.NetworkProtocol;
import dev.slimevr.vr.trackers.IMUTracker;
import dev.slimevr.vr.trackers.ReferenceAdjustedTracker;
@@ -146,7 +147,7 @@ public class TrackersUDPServer extends Thread {
LogManager.log.info("[TrackerServer] Sensor " + trackerId + " for " + connection.name + " status: " + sensorStatus);
IMUTracker imu = connection.sensors.get(trackerId);
if(imu == null) {
imu = new IMUTracker(Tracker.getNextLocalTrackerId(), connection.name + "/" + trackerId, connection.descriptiveName + "/" + trackerId, this);
imu = new IMUTracker(Tracker.getNextLocalTrackerId(), connection.name + "/" + trackerId, connection.descriptiveName + "/" + trackerId, this, Main.vrServer);
connection.sensors.put(trackerId, imu);
ReferenceAdjustedTracker<IMUTracker> adjustedTracker = new ReferenceAdjustedTracker<>(imu);
trackersConsumer.accept(adjustedTracker);