Skip to content

Commit

Permalink
Merge pull request #325 from NoiseStudio/feature/324/mathematics-type…
Browse files Browse the repository at this point in the history
…s-shortcuts

Add mathematics types shortcuts
  • Loading branch information
Vixenka authored Aug 19, 2023
2 parents a5b3248 + a1a5ae0 commit 1a0801c
Show file tree
Hide file tree
Showing 48 changed files with 2,435 additions and 150 deletions.
3 changes: 1 addition & 2 deletions NoiseEngine.Tests/ApplicationTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NoiseEngine.DeveloperTools.Systems;
using NoiseEngine.Jobs;
using NoiseEngine.Mathematics;
using NoiseEngine.Tests.Environments;
using NoiseEngine.Tests.Fixtures;
using System.Threading;
Expand All @@ -23,7 +22,7 @@ public void SimpleScene() {
for (int x = -10; x < 10; x += 2) {
for (int y = -10; y < 10; y += 2) {
scene.Primitive.CreateCube(new Vector3<float>(x, 0, y));
scene.Primitive.CreateCube(new pos3(x, 0, y));
}
}
Expand Down
5 changes: 2 additions & 3 deletions NoiseEngine.Tests/Components/TransformComponentTest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using NoiseEngine.Components;
using NoiseEngine.Mathematics;

namespace NoiseEngine.Tests.Components;

public class TransformComponentTest {

[Fact]
public void EqualsTest() {
Assert.Equal(new TransformComponent(Vector3<float>.Zero), new TransformComponent(Vector3<float>.Zero));
Assert.NotEqual(new TransformComponent(Vector3<float>.Zero), new TransformComponent(Vector3<float>.One));
Assert.Equal(new TransformComponent(pos3.Zero), new TransformComponent(pos3.Zero));
Assert.NotEqual(new TransformComponent(pos3.Zero), new TransformComponent(pos3.One));
}

}
2 changes: 1 addition & 1 deletion NoiseEngine.Tests/Mathematics/NumberHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NoiseEngine.Mathematics;
using NoiseEngine.Mathematics.Helpers;

namespace NoiseEngine.Tests.Mathematics;

Expand Down
2 changes: 1 addition & 1 deletion NoiseEngine.Tests/Nesl/Functions/Texture2DTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ f32v4 Fragment(FragmentData data) {
new ushort[] {
0, 1, 2, 3, 2, 1
}
), material, new TransformComponent(new Vector3<float>(0, 0, 5)).Matrix);
), material, new TransformComponent(new pos3(0, 0, 5)).Matrix);
commandBuffer.DetachCameraUnchecked();

commandBuffer.Execute();
Expand Down
4 changes: 2 additions & 2 deletions NoiseEngine.Tests/Nesl/NeslCompilerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ internal static void ExecuteVector3PositionVector3Color(
GraphicsCommandBuffer commandBuffer = new GraphicsCommandBuffer(device, false);
commandBuffer.AttachCameraUnchecked(camera);
commandBuffer.DrawMeshUnchecked(
new Mesh<(Vector3<float>, Vector3<float>), ushort>(device, vertices, triangles), new Material(shader),
new TransformComponent(new Vector3<float>(0, 0, 5)).Matrix
new Mesh<(float3, float3), ushort>(device, vertices, triangles), new Material(shader),
new TransformComponent(new pos3(0, 0, 5)).Matrix
);
commandBuffer.DetachCameraUnchecked();

Expand Down
4 changes: 2 additions & 2 deletions NoiseEngine.Tests/Physics/PhysicsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void SimpleScene() {
scene.Spawn(
new TransformComponent(
new Vector3<float>(0, -105, 0), Quaternion<float>.Identity, new Vector3<float>(200, 200, 200)
new pos3(0, -105, 0), Quaternion<float>.Identity, new float3(200, 200, 200)
),
new MeshRendererComponent(scene.Primitive.GetSphereMesh(), scene.Primitive.DefaultMaterial),
new ColliderComponent(new SphereCollider())
Expand All @@ -35,7 +35,7 @@ public void SimpleScene() {
for (int y = 0; y < 40; y += 2) {
for (int z = 0; z < 1; z += 2) {
scene.Spawn(
new TransformComponent(new Vector3<float>(x, y * 3 + 4.5f, z)),
new TransformComponent(new pos3(x, y * 3 + 4.5f, z)),
new MeshRendererComponent(scene.Primitive.GetSphereMesh(), scene.Primitive.DefaultMaterial),
new RigidBodyComponent(),
new ColliderComponent(new SphereCollider())
Expand Down
6 changes: 3 additions & 3 deletions NoiseEngine.Tests/Physics/PhysicsTestActivatorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ protected override void OnUpdate() {
Enabled = false;

scene.Primitive.CreateCube(
new Vector3<float>(0, 0, 15),
Quaternion.EulerDegrees(new Vector3<float>(0, 90, 0)),
new Vector3<float>(10, 10, 10)
new pos3(0, 0, 15),
Quaternion.EulerDegrees<float>(0, 90, 0),
new float3(10, 10, 10)
);

scene.AddFrameDependentSystem(new RigidBodyFrameSmoothingSystem());
Expand Down
10 changes: 5 additions & 5 deletions NoiseEngine.Tests/Rendering/MeshT2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void Figure2D() {
GraphicsCommandBuffer commandBuffer = new GraphicsCommandBuffer(device, false);
commandBuffer.AttachCameraUnchecked(camera);
commandBuffer.DrawMeshUnchecked(
new Mesh<(Vector4<float>, Color), ushort>(device, vertices, triangles), new Material(shader),
new Matrix4x4<float>()
new Mesh<(float4, Color), ushort>(device, vertices, triangles), new Material(shader),
new Matrix4x4<pos>()
);
commandBuffer.DetachCameraUnchecked();

Expand Down Expand Up @@ -225,9 +225,9 @@ public void Figure3D() {
OrthographicSize = 0.5f
};

Mesh mesh = new Mesh<(Vector3<float>, Vector3<float>), ushort>(device, vertices, triangles);
Mesh mesh = new Mesh<(float3, float3), ushort>(device, vertices, triangles);
Material material = new Material(shader);
TransformComponent transform = new TransformComponent(new Vector3<float>(0, 0, 5));
TransformComponent transform = new TransformComponent(new pos3(0, 0, 5));

GraphicsCommandBuffer commandBuffer = new GraphicsCommandBuffer(device, false);

Expand All @@ -246,7 +246,7 @@ public void Figure3D() {

commandBuffer.AttachCameraUnchecked(camera);
commandBuffer.DrawMeshUnchecked(
mesh, material, (transform with { Position = new Vector3<float>(0, 0, -100) }
mesh, material, (transform with { Position = new pos3(0, 0, -100) }
).Matrix);
commandBuffer.DetachCameraUnchecked();

Expand Down
4 changes: 2 additions & 2 deletions NoiseEngine.Tests/Rendering/ShaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void Triangle() {
GraphicsCommandBuffer commandBuffer = new GraphicsCommandBuffer(device, false);
commandBuffer.AttachCameraUnchecked(camera);
commandBuffer.DrawMeshUnchecked(
new Mesh<Vector4<float>, ushort>(device, new Vector4<float>[4], new ushort[] { 0, 1, 2 }),
new Material(shader), new Matrix4x4<float>()
new Mesh<float4, ushort>(device, new float4[4], new ushort[] { 0, 1, 2 }),
new Material(shader), new Matrix4x4<pos>()
);
commandBuffer.DetachCameraUnchecked();

Expand Down
4 changes: 2 additions & 2 deletions NoiseEngine.Tests/Rendering/SimpleCameraTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public void DepthTesting() {

commandBuffer.DrawMeshUnchecked(
new Mesh<VertexPosition3Color3, ushort>(device, verticesA, indices), shared.DefaultMaterial,
new TransformComponent(new Vector3<float>(0, 0, 5)).Matrix
new TransformComponent(new pos3(0, 0, 5)).Matrix
);
commandBuffer.DrawMeshUnchecked(
new Mesh<VertexPosition3Color3, ushort>(device, verticesB, indices), shared.DefaultMaterial,
new TransformComponent(new Vector3<float>(0, 0, 10)).Matrix
new TransformComponent(new pos3(0, 0, 10)).Matrix
);

commandBuffer.DetachCameraUnchecked();
Expand Down
59 changes: 59 additions & 0 deletions NoiseEngine.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
global using Xunit;

global using byte2 = NoiseEngine.Mathematics.Vector2<byte>;
global using byte3 = NoiseEngine.Mathematics.Vector3<byte>;
global using byte4 = NoiseEngine.Mathematics.Vector4<byte>;

global using sbyte2 = NoiseEngine.Mathematics.Vector2<sbyte>;
global using sbyte3 = NoiseEngine.Mathematics.Vector3<sbyte>;
global using sbyte4 = NoiseEngine.Mathematics.Vector4<sbyte>;

global using decimal2 = NoiseEngine.Mathematics.Vector2<decimal>;
global using decimal3 = NoiseEngine.Mathematics.Vector3<decimal>;
global using decimal4 = NoiseEngine.Mathematics.Vector4<decimal>;

global using float2 = NoiseEngine.Mathematics.Vector2<float>;
global using float3 = NoiseEngine.Mathematics.Vector3<float>;
global using float4 = NoiseEngine.Mathematics.Vector4<float>;

global using double2 = NoiseEngine.Mathematics.Vector2<double>;
global using double3 = NoiseEngine.Mathematics.Vector3<double>;
global using double4 = NoiseEngine.Mathematics.Vector4<double>;

global using int2 = NoiseEngine.Mathematics.Vector2<int>;
global using int3 = NoiseEngine.Mathematics.Vector3<int>;
global using int4 = NoiseEngine.Mathematics.Vector4<int>;

global using uint2 = NoiseEngine.Mathematics.Vector2<uint>;
global using uint3 = NoiseEngine.Mathematics.Vector3<uint>;
global using uint4 = NoiseEngine.Mathematics.Vector4<uint>;

global using nint2 = NoiseEngine.Mathematics.Vector2<nint>;
global using nint3 = NoiseEngine.Mathematics.Vector3<nint>;
global using nint4 = NoiseEngine.Mathematics.Vector4<nint>;

global using nuint2 = NoiseEngine.Mathematics.Vector2<nuint>;
global using nuint3 = NoiseEngine.Mathematics.Vector3<nuint>;
global using nuint4 = NoiseEngine.Mathematics.Vector4<nuint>;

global using long2 = NoiseEngine.Mathematics.Vector2<long>;
global using long3 = NoiseEngine.Mathematics.Vector3<long>;
global using long4 = NoiseEngine.Mathematics.Vector4<long>;

global using ulong2 = NoiseEngine.Mathematics.Vector2<ulong>;
global using ulong3 = NoiseEngine.Mathematics.Vector3<ulong>;
global using ulong4 = NoiseEngine.Mathematics.Vector4<ulong>;

global using short2 = NoiseEngine.Mathematics.Vector2<short>;
global using short3 = NoiseEngine.Mathematics.Vector3<short>;
global using short4 = NoiseEngine.Mathematics.Vector4<short>;

global using ushort2 = NoiseEngine.Mathematics.Vector2<ushort>;
global using ushort3 = NoiseEngine.Mathematics.Vector3<ushort>;
global using ushort4 = NoiseEngine.Mathematics.Vector4<ushort>;

#pragma warning disable CS8981
global using pos = NoiseEngine.Mathematics.Position;
#pragma warning restore CS8981
global using pos2 = NoiseEngine.Mathematics.Vector2<NoiseEngine.Mathematics.Position>;
global using pos3 = NoiseEngine.Mathematics.Vector3<NoiseEngine.Mathematics.Position>;
global using pos4 = NoiseEngine.Mathematics.Vector4<NoiseEngine.Mathematics.Position>;
6 changes: 3 additions & 3 deletions NoiseEngine/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public RenderLoop? RenderLoop {
}

public Camera(
ApplicationScene scene, Vector3<float> position, Quaternion<float> rotation
ApplicationScene scene, pos3 position, Quaternion<float> rotation
) : base(scene.GraphicsDevice) {
Scene = scene;
Entity = scene.Spawn(
Expand All @@ -55,10 +55,10 @@ public Camera(
Scene.AddCameraToScene(this);
}

public Camera(ApplicationScene scene, Vector3<float> position) : this(scene, position, Quaternion<float>.Identity) {
public Camera(ApplicationScene scene, pos3 position) : this(scene, position, Quaternion<float>.Identity) {
}

public Camera(ApplicationScene scene) : this(scene, new Vector3<float>(0, 0, -5)) {
public Camera(ApplicationScene scene) : this(scene, new pos3(0, 0, -5)) {
}

/// <summary>
Expand Down
25 changes: 13 additions & 12 deletions NoiseEngine/Components/TransformComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace NoiseEngine.Components;

public record struct TransformComponent : IComponent {

private readonly Vector3<float> position;
private readonly pos3 position;
private readonly Quaternion<float> rotation;
private readonly Vector3<float> scale;
private readonly float3 scale;

public static TransformComponent Default => new TransformComponent();

public Vector3<float> Position {
public pos3 Position {
get => position;
init {
position = value;
Expand All @@ -28,15 +28,15 @@ public Quaternion<float> Rotation {
}
}

public Vector3<float> Scale {
public float3 Scale {
get => scale;
init {
scale = value;
Matrix = CalculateMatrix();
}
}

public Matrix4x4<float> Matrix { get; private init; }
public Matrix4x4<pos> Matrix { get; private init; }

public Vector3<float> Left => Rotation * Vector3<float>.Left;
public Vector3<float> Right => Rotation * Vector3<float>.Right;
Expand All @@ -45,22 +45,22 @@ public Vector3<float> Scale {
public Vector3<float> Front => Rotation * Vector3<float>.Front;
public Vector3<float> Back => Rotation * Vector3<float>.Back;

public TransformComponent(Vector3<float> position, Quaternion<float> rotation, Vector3<float> scale) {
public TransformComponent(pos3 position, Quaternion<float> rotation, float3 scale) {
this.position = position;
this.rotation = rotation;
this.scale = scale;

Matrix = CalculateMatrix();
}

public TransformComponent(Vector3<float> position, Quaternion<float> rotation)
: this(position, rotation, Vector3<float>.One) {
public TransformComponent(pos3 position, Quaternion<float> rotation)
: this(position, rotation, float3.One) {
}

public TransformComponent(Vector3<float> position) : this(position, Quaternion<float>.Identity) {
public TransformComponent(pos3 position) : this(position, Quaternion<float>.Identity) {
}

public TransformComponent() : this(Vector3<float>.Zero) {
public TransformComponent() : this(pos3.Zero) {
}

/// <summary>
Expand All @@ -81,8 +81,9 @@ public override int GetHashCode() {
return Matrix.GetHashCode();
}

private Matrix4x4<float> CalculateMatrix() {
return Matrix4x4<float>.Translate(Position) * Matrix4x4<float>.Rotate(Rotation) * Matrix4x4<float>.Scale(Scale);
private Matrix4x4<pos> CalculateMatrix() {
return Matrix4x4<pos>.Translate(Position) * Matrix4x4<pos>.Rotate(Rotation.ToPos()) *
Matrix4x4<pos>.Scale(Scale.ToPos());
}

private bool PrintMembers(StringBuilder builder) {
Expand Down
17 changes: 8 additions & 9 deletions NoiseEngine/DeveloperTools/Systems/DebugMovementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NoiseEngine.Inputs;
using NoiseEngine.Jobs;
using NoiseEngine.Mathematics;
using NoiseEngine.Physics;
using System;

namespace NoiseEngine.DeveloperTools.Systems;
Expand Down Expand Up @@ -60,7 +59,7 @@ public static DebugMovementSystem InitializeTo(Camera camera) {
private void OnUpdateEntity(ref TransformComponent transform, ref DebugMovementComponent movement) {
WindowInput input = Window.Input;

Vector3<float> position = transform.Position;
pos3 position = transform.Position;
bool changePosition = false;

if (input.ScrollDelta.Y != 0) {
Expand All @@ -73,26 +72,26 @@ private void OnUpdateEntity(ref TransformComponent transform, ref DebugMovementC

// Move
if (input.Pressed(Key.W)) {
position += transform.Front * (currentSpeed * SpeedMultipler * DeltaTimeF);
position += (transform.Front * (currentSpeed * SpeedMultipler * DeltaTimeF)).ToPos();
changePosition = true;
}
if (input.Pressed(Key.S)) {
position += transform.Back * (currentSpeed * SpeedMultipler * DeltaTimeF);
position += (transform.Back * (currentSpeed * SpeedMultipler * DeltaTimeF)).ToPos();
changePosition = true;
}

if (input.Pressed(Key.A)) {
position += transform.Left * (currentSpeed * SpeedMultipler * DeltaTimeF);
position += (transform.Left * (currentSpeed * SpeedMultipler * DeltaTimeF)).ToPos();
changePosition = true;
}
if (input.Pressed(Key.D)) {
position += transform.Right * (currentSpeed * SpeedMultipler * DeltaTimeF);
position += (transform.Right * (currentSpeed * SpeedMultipler * DeltaTimeF)).ToPos();
changePosition = true;
}

// Rotation
movement = movement with {
MouseRotation = new Vector2<float>(
MouseRotation = new float2(
Math.Clamp(
(float)input.CursorPositionDelta.Y * Sensitivity + movement.MouseRotation.X,
-MouseDownLookLimiter,
Expand All @@ -101,11 +100,11 @@ private void OnUpdateEntity(ref TransformComponent transform, ref DebugMovementC
(float)input.CursorPositionDelta.X * Sensitivity + movement.MouseRotation.Y
)
};
Quaternion<float> rotation = Quaternion.EulerRadians(new Vector3<float>(
Quaternion<float> rotation = Quaternion.EulerRadians(
movement.MouseRotation.X,
movement.MouseRotation.Y,
0
));
);

if (changePosition) {
movement = movement with { TimeUntilLastChangedPosition = 0 };
Expand Down
18 changes: 0 additions & 18 deletions NoiseEngine/Mathematics/FloatingPointIeee754Helper.cs

This file was deleted.

9 changes: 9 additions & 0 deletions NoiseEngine/Mathematics/Helpers/AdditiveIdentityHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Numerics;

namespace NoiseEngine.Mathematics.Helpers;

internal static class AdditiveIdentityHelper<TSelf, TResult> where TSelf : IAdditiveIdentity<TSelf, TResult> {

public static TResult AdditiveIdentity => TSelf.AdditiveIdentity;

}
Loading

0 comments on commit 1a0801c

Please sign in to comment.