Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ StyleCopReport.xml
*.ilk
*.meta
*.obj
!ControlApp/Resources/Models/MotionController.obj
*.iobj
*.pch
*.pdb
Expand Down
44 changes: 44 additions & 0 deletions ControlApp.Tests/MotionControllerModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Numerics;

using HelixToolkit.SharpDX;

using Nefarius.DsHidMini.ControlApp.Models.Motion;

using Xunit;

namespace Nefarius.DsHidMini.ControlApp.Tests;

public class MotionControllerModelTests
{
[Fact]
public void EmbeddedModel_LoadsWithNormalsAndExpectedOrientation()
{
// Exercise the embedded resource through the runtime OBJ reader, so a
// missing publish asset or incompatible export fails without a GPU.
IReadOnlyList<Object3D> parts = MotionControllerModel.Load();
Assert.Contains(parts, p => p.Name == "ShellUpper");
Assert.Contains(parts, p => p.Name == "Triangle");
Assert.Contains(parts, p => p.Name == "Trigger");
foreach (Object3D part in parts)
{
MeshGeometry3D mesh = Assert.IsType<MeshGeometry3D>(part.Geometry);
Assert.NotNull(mesh.Indices);
Assert.NotNull(mesh.Positions);
Assert.NotNull(mesh.Normals);
Assert.NotEmpty(mesh.Indices);
Assert.Equal(0, mesh.Indices.Count % 3);
Assert.Equal(mesh.Positions.Count, mesh.Normals.Count);
Assert.All(mesh.Indices, index => Assert.InRange(index, 0, mesh.Positions.Count - 1));
Assert.All(mesh.Positions, v => Assert.True(float.IsFinite(v.X) && float.IsFinite(v.Y) && float.IsFinite(v.Z)));
Assert.All(mesh.Normals, n => Assert.InRange(n.Length(), 0.99f, 1.01f));
}

Vector3[] shell = parts.Where(p => p.Name == "ShellUpper")
.SelectMany(p => p.Geometry!.Positions!).ToArray();
Assert.InRange(shell.Max(p => p.X) - shell.Min(p => p.X), 4f, 4.5f);
Assert.InRange(shell.Min(p => p.Y), -1.8f, -1.5f); // Grips toward -Y.
Assert.InRange(shell.Max(p => p.Y), 0.8f, 1.0f); // Nose toward +Y.
Assert.All(parts.Where(p => p.Name == "StickRubber").SelectMany(p => p.Geometry!.Positions!),
p => Assert.True(p.Z > 0.5f && p.Y < 0)); // Controls face +Z.
}
}
49 changes: 42 additions & 7 deletions ControlApp.Tests/MotionViewerRotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nefarius.DsHidMini.ControlApp.Models.Motion;
using Nefarius.DsHidMini.IPC.Models.Public;

using Xunit;

Expand All @@ -23,25 +24,25 @@ public void TiltFromUp_Flat_IsIdentity()
}

[Fact]
public void TiltFromUp_UsbUp_EqualsRxNeg90()
public void TiltFromUp_UsbUp_EqualsRxPos90()
{
MotionViewerQuaternion q = MotionViewerRotation.TiltFromUp(0, 1, 0);
MotionViewerQuaternion expected = MotionViewerRotation.FromAxisAngle(1, 0, 0, -90);
MotionViewerQuaternion expected = MotionViewerRotation.FromAxisAngle(1, 0, 0, 90);

Assert.True(MotionViewerRotation.AlmostEqual(q, expected));
}

[Fact]
public void TiltFromUp_GripDown_EqualsRyNeg90()
public void TiltFromUp_RightGripDown_EqualsRyPos90()
{
MotionViewerQuaternion q = MotionViewerRotation.TiltFromUp(1, 0, 0);
MotionViewerQuaternion expected = MotionViewerRotation.FromAxisAngle(0, 1, 0, -90);
MotionViewerQuaternion expected = MotionViewerRotation.FromAxisAngle(0, 1, 0, 90);

Assert.True(MotionViewerRotation.AlmostEqual(q, expected));
}

[Fact]
public void TiltFromUp_OnGripNoise_StaysNearRyNeg90()
public void TiltFromUp_OnGripNoise_StaysNearRyPos90()
{
MotionViewerQuaternion nominal = MotionViewerRotation.TiltFromUp(1, 0, 0);
MotionViewerQuaternion noisy = MotionViewerRotation.TiltFromUp(0.997, 0.05, -0.06);
Expand Down Expand Up @@ -73,14 +74,48 @@ public void ComposeYawThenTilt_FlatYawMatchesZ()
}

[Fact]
public void ComposeYawThenTilt_GripDownKeepsWorldUp()
public void ComposeYawThenTilt_GripDownLowersRightGrip()
{
MotionViewerQuaternion q = MotionViewerRotation.ComposeYawThenTilt(25, 1, 0, 0);
(double x, double y, double z) = Rotate(q, 1, 0, 0);

Assert.InRange(x, -0.02, 0.02);
Assert.InRange(y, -0.02, 0.02);
Assert.InRange(z, 0.98, 1.02);
Assert.InRange(z, -1.02, -0.98);
}

[Theory]
[InlineData(0, 500, -866, 0, 1, 0.5)] // USB edge up.
[InlineData(0, -500, -866, 0, 1, -0.5)] // USB edge down.
[InlineData(500, 0, -866, 1, 0, -0.5)] // Right grip down.
[InlineData(-500, 0, -866, 1, 0, 0.5)] // Right grip up.
[InlineData(0, 1000, 0, 0, 1, 1.0)]
[InlineData(0, -1000, 0, 0, 1, -1.0)]
[InlineData(1000, 0, 0, 1, 0, -1.0)]
[InlineData(-1000, 0, 0, 1, 0, 1.0)]
public void SensorTilt_MovesModelLandmarksInPhysicalDirection(
int ax, int ay, int az, double modelX, double modelY, double expectedHeight)
{
MotionOrientationEstimator estimator = new(1_000_000, smoothing: 1.0);
estimator.Update(new DsMotionSnapshot
{
Flags = DsMotionSnapshotFlags.Available,
AccelMilliGX = ax,
AccelMilliGY = ay,
AccelMilliGZ = az,
SampleIndex = 1,
TimestampQpc = 1_000_000
});

// Check physical model points, rather than just quaternion signs.
// Turning the heading must not reverse which edge is raised.
foreach (double yaw in new[] { 0.0, 45.0, 90.0 })
{
MotionViewerQuaternion q = MotionViewerRotation.ComposeYawThenTilt(
yaw, estimator.UpX, estimator.UpY, estimator.UpZ);
(_, _, double height) = Rotate(q, modelX, modelY, 0);
Assert.InRange(height, expectedHeight - 0.001, expectedHeight + 0.001);
}
}

private static (double X, double Y, double Z) Rotate(MotionViewerQuaternion q, double x, double y, double z)
Expand Down
1 change: 1 addition & 0 deletions ControlApp/ControlApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Models\MotionController.obj" LogicalName="MotionController.obj" />
<EmbeddedResource Update="Resources\test.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>test.Designer.cs</LastGenOutput>
Expand Down
63 changes: 63 additions & 0 deletions ControlApp/Models/Motion/MotionControllerModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.IO;

using HelixToolkit.Maths;
using HelixToolkit.SharpDX;
using HelixToolkit.Wpf.SharpDX;

namespace Nefarius.DsHidMini.ControlApp.Models.Motion;

/// <summary>
/// Original, texture-free controller mesh. X points right, +Y toward the
/// USB port and +Z out of the face, matching the viewer's existing pose map.
/// </summary>
internal static class MotionControllerModel
{
internal static IReadOnlyList<Object3D> Load()
{
using Stream stream = typeof(MotionControllerModel).Assembly
.GetManifestResourceStream("MotionController.obj")
?? throw new InvalidOperationException("The motion controller model is missing.");
// This bundled, geometry-only OBJ needs no native Assimp dependency.
#pragma warning disable CS0618 // Use the existing managed reader for this fixed asset.
ObjReader reader = new() { IgnoreErrors = false };
#pragma warning restore CS0618
reader.Read(stream);
// Helix 3.1.2 does not copy OBJ group names into Object3D.Name.
// Retain them explicitly because they identify the part materials.
return reader.Groups.SelectMany(group => group.CreateModels(reader.ModelInfo)
.Select(model =>
{
model.Name = group.Name;
return model;
})).ToArray();
}

internal static PhongMaterial MaterialFor(string part)
{
// Linear colours: keep the shell readable under the diagnostic scene's
// lighting, with enough contrast to distinguish the rubber and controls.
(float r, float g, float b, float shine) = part switch
{
"ShellUpper" => (0.16f, 0.19f, 0.24f, 64f),
"ShellLower" => (0.075f, 0.09f, 0.12f, 40f),
"Seam" or "ControlWell" or "UsbRim" => (0.018f, 0.023f, 0.032f, 12f),
"StickRubber" or "StickRim" => (0.045f, 0.05f, 0.06f, 8f),
"StickSocket" or "HomeBezel" => (0.10f, 0.12f, 0.15f, 80f),
"Triangle" => (0.10f, 0.85f, 0.55f, 24f),
"Circle" => (0.95f, 0.20f, 0.28f, 24f),
"Cross" => (0.25f, 0.55f, 1.0f, 24f),
"Square" => (0.90f, 0.35f, 0.65f, 24f),
"Home" => (0.35f, 0.42f, 0.52f, 80f),
"LedOn" => (0.95f, 0.12f, 0.08f, 24f),
"LedOff" => (0.13f, 0.035f, 0.03f, 24f),
_ => (0.035f, 0.043f, 0.058f, 40f)
};
return new PhongMaterial
{
DiffuseColor = new Color4(r, g, b, 1),
AmbientColor = new Color4(r * 0.5f, g * 0.5f, b * 0.5f, 1),
SpecularColor = new Color4(0.22f, 0.22f, 0.22f, 1),
SpecularShininess = shine
};
}
}
17 changes: 9 additions & 8 deletions ControlApp/Models/Motion/MotionViewerRotation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Nefarius.DsHidMini.ControlApp.Models.Motion;

/// <summary>
/// One Helix axis-angle applied to the face-on diagnostic pad
/// (X right, Y up, Z toward the camera).
/// One Helix axis-angle applied to the controller model
/// (X right, Y toward the USB port, Z out of the button face).
/// </summary>
internal readonly record struct MotionViewerAxisAngle(
double AxisX,
Expand All @@ -23,7 +23,7 @@ internal readonly record struct MotionViewerQuaternion(
}

/// <summary>
/// Maps the Sony gravity-up vector and integrated yaw onto the face-on
/// Maps the Sony gravity-up vector and integrated yaw onto the controller
/// Helix pad without an Euler singularity at roll ±90°.
/// </summary>
internal static class MotionViewerRotation
Expand All @@ -38,14 +38,15 @@ public static MotionViewerAxisAngle Yaw(double yawDegrees)
/// <summary>
/// Shortest-arc tilt that takes the mapped pad-up vector onto Helix
/// world-up <c>(0, 0, 1)</c>. Pad frame to Helix is
/// <c>M = diag(1, -1, -1)</c>, matching the hardware-verified Euler
/// map (flat = identity, USB-up = <c>R_X(-90)</c>, grip-down =
/// <c>R_Y(-90)</c>). Face-down uses axis X, 180°.
/// <c>M = diag(-1, 1, -1)</c>: the model's +Y points toward the USB
/// port and +X toward the right grip. USB-up therefore uses
/// <c>R_X(+90)</c>, and right-grip-down uses <c>R_Y(+90)</c>.
/// Flat is identity; face-down uses axis X, 180°.
/// </summary>
public static MotionViewerQuaternion TiltFromUp(double upX, double upY, double upZ)
{
double vx = upX;
double vy = -upY;
double vx = -upX;
double vy = upY;
double vz = -upZ;
double mag = Math.Sqrt((vx * vx) + (vy * vy) + (vz * vz));
if (mag < 1e-9)
Expand Down
Loading
Loading