Fix Unity references

This commit is contained in:
Pascal Serrarens 2025-02-19 08:53:07 +01:00
parent 2428583110
commit 5059dfbe6e
4 changed files with 65 additions and 51 deletions

View File

@ -1,7 +1,11 @@
using System;
class Angle
{
namespace Passer.LinearAlgebra {
public class Angle {
public const float pi = 3.1415927410125732421875F;
public static float Rad2Deg = 360.0f / ((float)Math.PI * 2);
public static float Deg2Rad = ((float)Math.PI * 2) / 360.0f;
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using Vector3 = UnityEngine.Vector3;
using Vector2 = UnityEngine.Vector2;
using Passer.LinearAlgebra;
public readonly struct Slice {
public int start { get; }
@ -50,7 +49,7 @@ public class Matrix2 {
return new Matrix2(resultData);
}
public static Matrix2 SkewMatrix(Vector3 v) {
public static Matrix2 SkewMatrix(Vector3Float v) {
float[,] result = new float[3, 3] {
{0, -v.z, v.y},
{v.z, 0, -v.x},
@ -137,12 +136,12 @@ public class Matrix2 {
return new Matrix1(result);
}
public static Vector3 operator *(Matrix2 A, Vector3 v) {
return new Vector3() {
x = A.data[0, 0] * v.x + A.data[0, 1] * v.y + A.data[0, 2] * v.z,
y = A.data[1, 0] * v.x + A.data[1, 1] * v.y + A.data[1, 2] * v.z,
z = A.data[2, 0] * v.x + A.data[2, 1] * v.y + A.data[2, 2] * v.z
};
public static Vector3Float operator *(Matrix2 A, Vector3Float v) {
return new Vector3Float(
A.data[0, 0] * v.x + A.data[0, 1] * v.y + A.data[0, 2] * v.z,
A.data[1, 0] * v.x + A.data[1, 1] * v.y + A.data[1, 2] * v.z,
A.data[2, 0] * v.x + A.data[2, 1] * v.y + A.data[2, 2] * v.z
);
}
@ -263,14 +262,14 @@ public class Matrix1 {
return new Matrix1(magnitude);
}
public static Matrix1 FromVector2(Vector2 v) {
public static Matrix1 FromVector2(Vector2Float v) {
float[] result = new float[2];
result[0] = v.x;
result[1] = v.y;
return new Matrix1(result);
}
public static Matrix1 FromVector3(Vector3 v) {
public static Matrix1 FromVector3(Vector3Float v) {
float[] result = new float[3];
result[0] = v.x;
result[1] = v.y;
@ -278,18 +277,18 @@ public class Matrix1 {
return new Matrix1(result);
}
public Vector2 vector2 {
public Vector2Float vector2 {
get {
if (this.magnitude != 2)
throw new System.ArgumentException("Matrix1 must be of size 2");
return new Vector2(this.data[0], this.data[1]);
return new Vector2Float(this.data[0], this.data[1]);
}
}
public Vector3 vector3 {
public Vector3Float vector3 {
get {
if (this.magnitude != 3)
throw new System.ArgumentException("Matrix1 must be of size 3");
return new Vector3(this.data[0], this.data[1], this.data[2]);
return new Vector3Float(this.data[0], this.data[1], this.data[2]);
}
}
@ -461,12 +460,12 @@ public class Matrix {
}
// Vector-matrix multiplication
public static Vector3 MultiplyMatrixVector3(float[,] A, Vector3 v) {
return new Vector3() {
x = A[0, 0] * v.x + A[0, 1] * v.y + A[0, 2] * v.z,
y = A[1, 0] * v.x + A[1, 1] * v.y + A[1, 2] * v.z,
z = A[2, 0] * v.x + A[2, 1] * v.y + A[2, 2] * v.z
};
public static Vector3Float MultiplyMatrixVector3(float[,] A, Vector3Float v) {
return new Vector3Float(
A[0, 0] * v.x + A[0, 1] * v.y + A[0, 2] * v.z,
A[1, 0] * v.x + A[1, 1] * v.y + A[1, 2] * v.z,
A[2, 0] * v.x + A[2, 1] * v.y + A[2, 2] * v.z
);
}
public static float[,] MultiplyMatrixScalar(float[,] A, float s) {
@ -492,16 +491,17 @@ public class Matrix {
return column;
}
public static Vector3 GetRow3(float[,] M, int rowIx) {
public static Vector3Float GetRow3(float[,] M, int rowIx) {
int cols = M.GetLength(1);
Vector3 row = new();
row.x = M[rowIx, 0];
row.y = M[rowIx, 1];
row.z = M[rowIx, 2];
Vector3Float row = new(
M[rowIx, 0],
M[rowIx, 1],
M[rowIx, 2]
);
return row;
}
public static void SetRow3(float[,] M, int rowIx, Vector3 v) {
public static void SetRow3(float[,] M, int rowIx, Vector3Float v) {
M[rowIx, 0] = v.x;
M[rowIx, 1] = v.y;
M[rowIx, 2] = v.z;

View File

@ -1,5 +1,3 @@
using UnityEngine;
namespace Passer.LinearAlgebra {
public class Spherical {
public float distance;
@ -17,30 +15,30 @@ namespace Passer.LinearAlgebra {
this.direction = direction;
}
public static Spherical FromVector3(Vector3 v) {
public static Spherical FromVector3(Vector3Float v) {
float distance = v.magnitude;
if (distance == 0.0f)
return new Spherical(distance, 0, 0);
else {
float verticalAngle = (Mathf.PI / 2 - Mathf.Acos(v.y / distance)) * Mathf.Rad2Deg;
float horizontalAngle = Mathf.Atan2(v.x, v.z) * Mathf.Rad2Deg;
float verticalAngle = (float)((Angle.pi / 2 - Math.Acos(v.y / distance)) * Angle.Rad2Deg);
float horizontalAngle = (float) Math.Atan2(v.x, v.z) * Angle.Rad2Deg;
return new Spherical(distance, horizontalAngle, verticalAngle);
}
}
public Vector3 ToVector3() {
float verticalRad = (UnityEngine.Mathf.PI / 2) - this.direction.vertical * UnityEngine.Mathf.Deg2Rad;
float horizontalRad = this.direction.horizontal * UnityEngine.Mathf.Deg2Rad;
float cosVertical = UnityEngine.Mathf.Cos(verticalRad);
float sinVertical = UnityEngine.Mathf.Sin(verticalRad);
float cosHorizontal = UnityEngine.Mathf.Cos(horizontalRad);
float sinHorizontal = UnityEngine.Mathf.Sin(horizontalRad);
public Vector3Float ToVector3() {
float verticalRad = (Angle.pi / 2) - this.direction.vertical * Angle.Deg2Rad;
float horizontalRad = this.direction.horizontal * Angle.Deg2Rad;
float cosVertical = (float)Math.Cos(verticalRad);
float sinVertical = (float)Math.Sin(verticalRad);
float cosHorizontal = (float)Math.Cos(horizontalRad);
float sinHorizontal = (float)Math.Sin(horizontalRad);
float x = this.distance * sinVertical * sinHorizontal;
float y = this.distance * cosVertical;
float z = this.distance * sinVertical * cosHorizontal;
Vector3 v = new Vector3(x, y, z);
Vector3Float v = new Vector3Float(x, y, z);
return v;
}
}

View File

@ -1,3 +1,7 @@
#if UNITY_5_3_OR_NEWER
using Passer.LinearAlgebra.Vector3Float = UnityEngine.Vector3
#else
namespace Passer.LinearAlgebra {
public class Vector3Of<T> {
@ -10,12 +14,20 @@ namespace Passer.LinearAlgebra {
this.y = y;
this.z = z;
}
// public uint magnitude {
// get => (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
// }
}
public class Vector3Int : Vector3Of<int> {
public Vector3Int(int x, int y, int z) : base(x, y, z) { }
public class Vector3Int(int x, int y, int z) : Vector3Of<int>(x, y, z) {
}
public class Vector3Float : Vector3Of<float> {
public Vector3Float(float x, float y, float z) : base(x, y, z) { }
public class Vector3Float(float x, float y, float z) : Vector3Of<float>(x, y, z) {
public float magnitude {
get => (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
}
#endif