Updated namespace
This commit is contained in:
parent
9ad1ec24d0
commit
efa9ba8522
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
DoxyGen/DoxyWarnLogfile.txt
|
||||
.vscode/settings.json
|
||||
**bin
|
||||
**obj
|
||||
**.meta
|
||||
*.sln
|
21
src/Angle.cs
21
src/Angle.cs
@ -1,11 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// %Angle utilities
|
||||
/// </summary>
|
||||
public static class Angle {
|
||||
public static 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;
|
||||
@ -21,7 +23,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="max">The maximum angle</param>
|
||||
/// <returns>The clamped angle</returns>
|
||||
/// Angles are normalized
|
||||
public static float Clamp(float angle, float min, float max) {
|
||||
public static float Clamp(float angle, float min, float max)
|
||||
{
|
||||
float normalizedAngle = Normalize(angle);
|
||||
return Float.Clamp(normalizedAngle, min, max);
|
||||
}
|
||||
@ -33,7 +36,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="b">The second angle</param>
|
||||
/// <returns>the angle between the two angles</returns>
|
||||
/// Angle values should be degrees
|
||||
public static float Difference(float a, float b) {
|
||||
public static float Difference(float a, float b)
|
||||
{
|
||||
float r = Normalize(b - a);
|
||||
return r;
|
||||
}
|
||||
@ -44,7 +48,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="angle">The angle to normalize</param>
|
||||
/// <returns>The normalized angle in interval (-180..180] </returns>
|
||||
/// Angle values should be in degrees
|
||||
public static float Normalize(float angle) {
|
||||
public static float Normalize(float angle)
|
||||
{
|
||||
if (float.IsInfinity(angle))
|
||||
return angle;
|
||||
|
||||
@ -61,7 +66,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="maxAngle">Maximum angle to rotate</param>
|
||||
/// <returns>The resulting angle</returns>
|
||||
/// This function is compatible with radian and degrees angles
|
||||
public static float MoveTowards(float fromAngle, float toAngle, float maxAngle) {
|
||||
public static float MoveTowards(float fromAngle, float toAngle, float maxAngle)
|
||||
{
|
||||
float d = toAngle - fromAngle;
|
||||
d = Normalize(d);
|
||||
d = Math.Sign(d) * Float.Clamp(Math.Abs(d), 0, maxAngle);
|
||||
@ -77,7 +83,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// Vectors a and b must be normalized
|
||||
/// \deprecated Please use Vector2.ToFactor instead.
|
||||
[Obsolete("Please use Vector2.ToFactor instead.")]
|
||||
public static float ToFactor(Vector2 v1, Vector2 v2) {
|
||||
public static float ToFactor(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
return (1 - Vector2.Dot(v1, v2)) / 2;
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,18 @@
|
||||
namespace Passer.LinearAlgebra {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
public class Direction {
|
||||
public class Direction
|
||||
{
|
||||
public float horizontal;
|
||||
public float vertical;
|
||||
|
||||
public Direction() {
|
||||
public Direction()
|
||||
{
|
||||
horizontal = 0;
|
||||
vertical = 0;
|
||||
}
|
||||
public Direction(float horizontal, float vertical) {
|
||||
public Direction(float horizontal, float vertical)
|
||||
{
|
||||
this.horizontal = horizontal;
|
||||
this.vertical = vertical;
|
||||
//Normalize();
|
||||
@ -21,8 +25,10 @@ namespace Passer.LinearAlgebra {
|
||||
public readonly static Direction left = new Direction(-90, 0);
|
||||
public readonly static Direction right = new Direction(90, 0);
|
||||
|
||||
public void Normalize() {
|
||||
if (this.vertical > 90 || this.vertical < -90) {
|
||||
public void Normalize()
|
||||
{
|
||||
if (this.vertical > 90 || this.vertical < -90)
|
||||
{
|
||||
this.horizontal += 180;
|
||||
this.vertical = 180 - this.vertical;
|
||||
}
|
||||
|
12
src/Float.cs
12
src/Float.cs
@ -1,9 +1,11 @@
|
||||
namespace Passer.LinearAlgebra {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Float number utilities
|
||||
/// </summary>
|
||||
public class Float {
|
||||
public class Float
|
||||
{
|
||||
/// <summary>
|
||||
/// The precision of float numbers
|
||||
/// </summary>
|
||||
@ -20,7 +22,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns>The clamped value</returns>
|
||||
public static float Clamp(float f, float min, float max) {
|
||||
public static float Clamp(float f, float min, float max)
|
||||
{
|
||||
if (f < min)
|
||||
return min;
|
||||
if (f > max)
|
||||
@ -33,7 +36,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// </summary>
|
||||
/// <param name="f">The value to clamp</param>
|
||||
/// <returns>The clamped value</returns>
|
||||
public static float Clamp01(float f) {
|
||||
public static float Clamp01(float f)
|
||||
{
|
||||
return Clamp(f, 0, 1);
|
||||
}
|
||||
}
|
||||
|
1014
src/Matrix.cs
1014
src/Matrix.cs
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
public class Quat32
|
||||
{
|
||||
|
@ -2,15 +2,19 @@ using System;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
using Quaternion = UnityEngine.Quaternion;
|
||||
#endif
|
||||
namespace Passer.LinearAlgebra {
|
||||
|
||||
public class QuaternionOf<T> {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
public class QuaternionOf<T>
|
||||
{
|
||||
public T x;
|
||||
public T y;
|
||||
public T z;
|
||||
public T w;
|
||||
|
||||
public QuaternionOf(T x, T y, T z, T w) {
|
||||
public QuaternionOf(T x, T y, T z, T w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
|
@ -3,35 +3,42 @@ using System;
|
||||
using Vector3Float = UnityEngine.Vector3;
|
||||
#endif
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
public class Spherical {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
public class Spherical
|
||||
{
|
||||
public float distance;
|
||||
public Direction direction;
|
||||
|
||||
public static Spherical zero = new Spherical(0, 0, 0);
|
||||
public static Spherical forward = new Spherical(1, 0, 0);
|
||||
|
||||
public Spherical(float distance, float horizontal, float vertical) {
|
||||
public Spherical(float distance, float horizontal, float vertical)
|
||||
{
|
||||
this.distance = distance;
|
||||
this.direction = new Direction(horizontal, vertical);
|
||||
}
|
||||
public Spherical(float distance, Direction direction) {
|
||||
public Spherical(float distance, Direction direction)
|
||||
{
|
||||
this.distance = distance;
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
public static Spherical FromVector3(Vector3Float v) {
|
||||
public static Spherical FromVector3(Vector3Float v)
|
||||
{
|
||||
float distance = v.magnitude;
|
||||
if (distance == 0.0f)
|
||||
return new Spherical(distance, 0, 0);
|
||||
else {
|
||||
else
|
||||
{
|
||||
float verticalAngle = (float)((Angle.pi / 2 - Math.Acos(v.y / distance)) * Angle.Rad2Deg);
|
||||
float horizontalAngle = (float) Math.Atan2(v.x, v.z) * Angle.Rad2Deg;
|
||||
float horizontalAngle = (float)Math.Atan2(v.x, v.z) * Angle.Rad2Deg;
|
||||
return new Spherical(distance, horizontalAngle, verticalAngle);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3Float ToVector3() {
|
||||
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);
|
||||
|
@ -1,4 +1,4 @@
|
||||
namespace Passer.LinearAlgebra
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
public class SwingTwist
|
||||
|
114
src/Vector2.cs
114
src/Vector2.cs
@ -1,50 +1,63 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
public class Vector2Of<T> where T : IComparable<T> {
|
||||
public class Vector2Of<T> where T : IComparable<T>
|
||||
{
|
||||
public T x;
|
||||
public T y;
|
||||
|
||||
public Vector2Of(T x, T y) {
|
||||
public Vector2Of(T x, T y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2Int : Vector2Of<int> {
|
||||
public class Vector2Int : Vector2Of<int>
|
||||
{
|
||||
public Vector2Int(int x, int y) : base(x, y) { }
|
||||
|
||||
public static Vector2Int operator -(Vector2Int v1, Vector2Int v2) {
|
||||
public static Vector2Int operator -(Vector2Int v1, Vector2Int v2)
|
||||
{
|
||||
return new Vector2Int(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
public float magnitude
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)Math.Sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
}
|
||||
|
||||
public static float Distance(Vector2Int v1, Vector2Int v2) {
|
||||
public static float Distance(Vector2Int v1, Vector2Int v2)
|
||||
{
|
||||
return (v1 - v2).magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2Float : Vector2Of<float> {
|
||||
public class Vector2Float : Vector2Of<float>
|
||||
{
|
||||
public Vector2Float(float x, float y) : base(x, y) { }
|
||||
|
||||
public static Vector2Float operator -(Vector2Float v1, Vector2Float v2) {
|
||||
public static Vector2Float operator -(Vector2Float v1, Vector2Float v2)
|
||||
{
|
||||
return new Vector2Float(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
public float magnitude
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)Math.Sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
}
|
||||
|
||||
public static float Distance(Vector2Float v1, Vector2Float v2) {
|
||||
public static float Distance(Vector2Float v1, Vector2Float v2)
|
||||
{
|
||||
return (v1 - v2).magnitude;
|
||||
}
|
||||
}
|
||||
@ -52,7 +65,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <summary>
|
||||
/// 2-dimensional vectors
|
||||
/// </summary>
|
||||
public struct Vector2 : IEquatable<Vector2> {
|
||||
public struct Vector2 : IEquatable<Vector2>
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// The right axis of the vector
|
||||
@ -69,7 +83,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// </summary>
|
||||
/// <param name="x">x axis value</param>
|
||||
/// <param name="y">y axis value</param>
|
||||
public Vector2(float x, float y) {
|
||||
public Vector2(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
@ -114,8 +129,10 @@ namespace Passer.LinearAlgebra {
|
||||
/// The squared length is computationally simpler than the real length.
|
||||
/// Think of Pythagoras A^2 + B^2 = C^2.
|
||||
/// This leaves out the calculation of the squared root of C.
|
||||
public float sqrMagnitude {
|
||||
get {
|
||||
public float sqrMagnitude
|
||||
{
|
||||
get
|
||||
{
|
||||
float d = x * x + y * y;
|
||||
return d;
|
||||
}
|
||||
@ -125,8 +142,10 @@ namespace Passer.LinearAlgebra {
|
||||
/// The length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The length of this vector</returns>
|
||||
public float magnitude {
|
||||
get {
|
||||
public float magnitude
|
||||
{
|
||||
get
|
||||
{
|
||||
float d = (float)Math.Sqrt(x * x + y * y);
|
||||
return d;
|
||||
}
|
||||
@ -136,8 +155,10 @@ namespace Passer.LinearAlgebra {
|
||||
/// Convert the vector to a length of a 1
|
||||
/// </summary>
|
||||
/// <returns>The vector with length 1</returns>
|
||||
public Vector2 normalized {
|
||||
get {
|
||||
public Vector2 normalized
|
||||
{
|
||||
get
|
||||
{
|
||||
float l = magnitude;
|
||||
Vector2 v = zero;
|
||||
if (l > Float.epsilon)
|
||||
@ -152,7 +173,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The result of adding the two vectors</returns>
|
||||
public static Vector2 operator +(Vector2 v1, Vector2 v2) {
|
||||
public static Vector2 operator +(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
Vector2 v = new Vector2(v1.x + v2.x, v1.y + v2.y);
|
||||
return v;
|
||||
}
|
||||
@ -163,7 +185,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The result of adding the two vectors</returns>
|
||||
public static Vector2 operator -(Vector2 v1, Vector2 v2) {
|
||||
public static Vector2 operator -(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
Vector2 v = new Vector2(v1.x - v2.x, v1.y - v2.y);
|
||||
return v;
|
||||
}
|
||||
@ -174,7 +197,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The vector to negate</param>
|
||||
/// <returns>The negated vector</returns>
|
||||
/// This will result in a vector pointing in the opposite direction
|
||||
public static Vector2 operator -(Vector2 v1) {
|
||||
public static Vector2 operator -(Vector2 v1)
|
||||
{
|
||||
Vector2 v = new Vector2(-v1.x, -v1.y);
|
||||
return v;
|
||||
}
|
||||
@ -186,7 +210,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="f">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be multipled with the same factor.
|
||||
public static Vector2 operator *(Vector2 v1, float f) {
|
||||
public static Vector2 operator *(Vector2 v1, float f)
|
||||
{
|
||||
Vector2 v = new Vector2(v1.x * f, v1.y * f);
|
||||
return v;
|
||||
}
|
||||
@ -198,7 +223,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The vector to scale</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be multipled with the same factor.
|
||||
public static Vector2 operator *(float f, Vector2 v1) {
|
||||
public static Vector2 operator *(float f, Vector2 v1)
|
||||
{
|
||||
Vector2 v = new Vector2(f * v1.x, f * v1.y);
|
||||
return v;
|
||||
}
|
||||
@ -210,7 +236,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="f">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be devided by the same factor.
|
||||
public static Vector2 operator /(Vector2 v1, float f) {
|
||||
public static Vector2 operator /(Vector2 v1, float f)
|
||||
{
|
||||
Vector2 v = new Vector2(v1.x / f, v1.y / f);
|
||||
return v;
|
||||
}
|
||||
@ -227,7 +254,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare to</param>
|
||||
/// <returns><em>false</em> when the object is not a Vector2 or does not have equal values</returns>
|
||||
public override bool Equals(object obj) {
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is Vector2 v))
|
||||
return false;
|
||||
|
||||
@ -243,7 +271,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// Note that this uses a Float equality check which cannot be not exact in all cases.
|
||||
/// In most cases it is better to check if the Vector2.Distance between the vectors is smaller than Float.epsilon
|
||||
/// Or more efficient: (v1 - v2).sqrMagnitude < Float.sqrEpsilon
|
||||
public static bool operator ==(Vector2 v1, Vector2 v2) {
|
||||
public static bool operator ==(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
return (v1.x == v2.x && v1.y == v2.y);
|
||||
}
|
||||
|
||||
@ -256,7 +285,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// Note that this uses a Float equality check which cannot be not exact in all case.
|
||||
/// In most cases it is better to check if the Vector2.Distance between the vectors is smaller than Float.epsilon.
|
||||
/// Or more efficient: (v1 - v2).sqrMagnitude < Float.sqrEpsilon
|
||||
public static bool operator !=(Vector2 v1, Vector2 v2) {
|
||||
public static bool operator !=(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
return (v1.x != v2.x || v1.y != v2.y);
|
||||
}
|
||||
|
||||
@ -264,7 +294,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// Get an hash code for the vector
|
||||
/// </summary>
|
||||
/// <returns>The hash code</returns>
|
||||
public override int GetHashCode() {
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (x, y).GetHashCode();
|
||||
}
|
||||
|
||||
@ -274,7 +305,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The distance between the two vectors</returns>
|
||||
public static float Distance(Vector2 v1, Vector2 v2) {
|
||||
public static float Distance(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
float x = v1.x - v2.x;
|
||||
float y = v1.y - v2.y;
|
||||
float d = (float)Math.Sqrt(x * x + y * y);
|
||||
@ -287,7 +319,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The dot product of the two vectors</returns>
|
||||
public static float Dot(Vector2 v1, Vector2 v2) {
|
||||
public static float Dot(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
return v1.x * v2.x + v1.y * v2.y;
|
||||
}
|
||||
|
||||
@ -301,7 +334,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// The factor f is unclamped. Value 0 matches the *v1* vector, Value 1
|
||||
/// matches the *v2* vector Value -1 is *v1* vector minus the difference
|
||||
/// between *v1* and *v2* etc.
|
||||
public static Vector2 Lerp(Vector2 v1, Vector2 v2, float f) {
|
||||
public static Vector2 Lerp(Vector2 v1, Vector2 v2, float f)
|
||||
{
|
||||
Vector2 v = v1 + (v2 - v1) * f;
|
||||
return v;
|
||||
}
|
||||
@ -313,7 +347,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="to">The ending vector</param>
|
||||
/// <param name="axis">The axis to rotate around</param>
|
||||
/// <returns>The signed angle in degrees</returns>
|
||||
public static float SignedAngle(Vector2 from, Vector2 to) {
|
||||
public static float SignedAngle(Vector2 from, Vector2 to)
|
||||
{
|
||||
//float sign = Math.Sign(v1.y * v2.x - v1.x * v2.y);
|
||||
//return Vector2.Angle(v1, v2) * sign;
|
||||
|
||||
@ -336,13 +371,15 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v1">The vector to rotate</param>
|
||||
/// <param name="angle">The angle in degrees</param>
|
||||
/// <returns></returns>
|
||||
public static Vector2 Rotate(Vector2 v1, float angle) {
|
||||
public static Vector2 Rotate(Vector2 v1, float angle)
|
||||
{
|
||||
float sin = (float)Math.Sin(angle * Angle.Deg2Rad);
|
||||
float cos = (float)Math.Cos(angle * Angle.Deg2Rad);
|
||||
|
||||
float tx = v1.x;
|
||||
float ty = v1.y;
|
||||
Vector2 v = new Vector2() {
|
||||
Vector2 v = new Vector2()
|
||||
{
|
||||
x = (cos * tx) - (sin * ty),
|
||||
y = (sin * tx) + (cos * ty)
|
||||
};
|
||||
@ -356,7 +393,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The resulting factor in interval [0..1]</returns>
|
||||
/// Vectors a and b must be normalized
|
||||
public static float ToFactor(Vector2 v1, Vector2 v2) {
|
||||
public static float ToFactor(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
return (1 - Vector2.Dot(v1, v2)) / 2;
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,16 @@
|
||||
#if !UNITY_5_3_OR_NEWER
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
public class Vector3Of<T> {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
public class Vector3Of<T>
|
||||
{
|
||||
public T x;
|
||||
public T y;
|
||||
public T z;
|
||||
|
||||
public Vector3Of(T x, T y, T z) {
|
||||
public Vector3Of(T x, T y, T z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
@ -18,13 +21,16 @@ namespace Passer.LinearAlgebra {
|
||||
// }
|
||||
}
|
||||
|
||||
public class Vector3Int : Vector3Of<int> {
|
||||
public class Vector3Int : Vector3Of<int>
|
||||
{
|
||||
public Vector3Int(int x, int y, int z) : base(x, y, z) { }
|
||||
}
|
||||
public class Vector3Float : Vector3Of<float> {
|
||||
public class Vector3Float : Vector3Of<float>
|
||||
{
|
||||
public Vector3Float(float x, float y, float z) : base(x, y, z) { }
|
||||
|
||||
public float magnitude {
|
||||
public float magnitude
|
||||
{
|
||||
get => (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
||||
}
|
||||
}
|
||||
@ -33,7 +39,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// 3-dimensional vectors
|
||||
/// </summary>
|
||||
/// This uses the right-handed coordinate system.
|
||||
public struct Vector3 : IEquatable<Vector3> {
|
||||
public struct Vector3 : IEquatable<Vector3>
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// The right axis of the vector
|
||||
@ -54,7 +61,8 @@ namespace Passer.LinearAlgebra {
|
||||
/// <param name="x">x axis value</param>
|
||||
/// <param name="y">y axis value</param>
|
||||
/// <param name="z">z axis value</param>
|
||||
public Vector3(float x, float y, float z) {
|
||||
public Vector3(float x, float y, float z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
@ -93,15 +101,19 @@ namespace Passer.LinearAlgebra {
|
||||
/// </summary>
|
||||
public static readonly Vector3 forward = new Vector3(0, 1, 0);
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
public float magnitude
|
||||
{
|
||||
get
|
||||
{
|
||||
float d = (float)Math.Sqrt(x * x + y * y);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 normalized {
|
||||
get {
|
||||
public Vector3 normalized
|
||||
{
|
||||
get
|
||||
{
|
||||
float l = magnitude;
|
||||
Vector3 v = zero;
|
||||
if (l > Float.epsilon)
|
||||
@ -110,66 +122,79 @@ namespace Passer.LinearAlgebra {
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 operator +(Vector3 v1, Vector3 v2) {
|
||||
public static Vector3 operator +(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
Vector3 v = new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector3 operator -(Vector3 v1, Vector3 v2) {
|
||||
public static Vector3 operator -(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
Vector3 v = new Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector3 operator -(Vector3 v1) {
|
||||
public static Vector3 operator -(Vector3 v1)
|
||||
{
|
||||
Vector3 v = new Vector3(-v1.x, -v1.y, -v1.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector3 operator *(Vector3 v1, float d) {
|
||||
public static Vector3 operator *(Vector3 v1, float d)
|
||||
{
|
||||
Vector3 v = new Vector3(v1.x * d, v1.y * d, v1.z * d);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector3 operator *(float d, Vector3 v1) {
|
||||
public static Vector3 operator *(float d, Vector3 v1)
|
||||
{
|
||||
Vector3 v = new Vector3(d * v1.x, d * v1.y, d * v1.z);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static Vector3 operator /(Vector3 v1, float d) {
|
||||
public static Vector3 operator /(Vector3 v1, float d)
|
||||
{
|
||||
Vector3 v = new Vector3(v1.x / d, v1.y / d, v1.z / d);
|
||||
return v;
|
||||
}
|
||||
|
||||
public bool Equals(Vector3 v) => (x == v.x && y == v.y && z == v.z);
|
||||
|
||||
public override bool Equals(object obj) {
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is Vector3 v))
|
||||
return false;
|
||||
|
||||
return (x == v.x && y == v.y && z == v.z);
|
||||
}
|
||||
|
||||
public static bool operator ==(Vector3 v1, Vector3 v2) {
|
||||
public static bool operator ==(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z);
|
||||
}
|
||||
|
||||
public static bool operator !=(Vector3 v1, Vector3 v2) {
|
||||
public static bool operator !=(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return (v1.x != v2.x || v1.y != v2.y || v1.z != v2.z);
|
||||
}
|
||||
|
||||
public override int GetHashCode() {
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (x, y, z).GetHashCode();
|
||||
}
|
||||
|
||||
public static float Distance(Vector3 v1, Vector3 v2) {
|
||||
public static float Distance(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return (v2 - v1).magnitude;
|
||||
}
|
||||
|
||||
public static float Dot(Vector3 v1, Vector3 v2) {
|
||||
public static float Dot(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
}
|
||||
|
||||
public static Vector3 Lerp(Vector3 v1, Vector3 v2, float f) {
|
||||
public static Vector3 Lerp(Vector3 v1, Vector3 v2, float f)
|
||||
{
|
||||
Vector3 v = v1 + (v2 - v1) * f;
|
||||
return v;
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
namespace LinearAlgebra
|
||||
{
|
||||
|
||||
public class float16 {
|
||||
public class float16
|
||||
{
|
||||
//
|
||||
// FILE: float16.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
@ -14,12 +16,14 @@ namespace Passer.LinearAlgebra {
|
||||
|
||||
public float16() { _value = 0; }
|
||||
|
||||
public float16(float f) {
|
||||
public float16(float f)
|
||||
{
|
||||
//_value = f32tof16(f);
|
||||
_value = F32ToF16__(f);
|
||||
}
|
||||
|
||||
public float toFloat() {
|
||||
public float toFloat()
|
||||
{
|
||||
return f16tof32(_value);
|
||||
}
|
||||
|
||||
@ -153,7 +157,8 @@ namespace Passer.LinearAlgebra {
|
||||
//
|
||||
// CORE CONVERSION
|
||||
//
|
||||
float f16tof32(ushort _value) {
|
||||
float f16tof32(ushort _value)
|
||||
{
|
||||
//ushort sgn;
|
||||
ushort man;
|
||||
int exp;
|
||||
@ -168,11 +173,13 @@ namespace Passer.LinearAlgebra {
|
||||
//Debug.Log($"{sgn} {exp} {man}");
|
||||
|
||||
// ZERO
|
||||
if ((_value & 0x7FFF) == 0) {
|
||||
if ((_value & 0x7FFF) == 0)
|
||||
{
|
||||
return sgn ? -0 : 0;
|
||||
}
|
||||
// NAN & INF
|
||||
if (exp == 0x001F) {
|
||||
if (exp == 0x001F)
|
||||
{
|
||||
if (man == 0)
|
||||
return sgn ? float.NegativeInfinity : float.PositiveInfinity; //-INFINITY : INFINITY;
|
||||
else
|
||||
@ -186,43 +193,50 @@ namespace Passer.LinearAlgebra {
|
||||
f = 1;
|
||||
|
||||
// PROCESS MANTISSE
|
||||
for (int i = 9; i >= 0; i--) {
|
||||
for (int i = 9; i >= 0; i--)
|
||||
{
|
||||
f *= 2;
|
||||
if ((man & (1 << i)) != 0)
|
||||
f = f + 1;
|
||||
}
|
||||
//Debug.Log($"{f}");
|
||||
f = f * (float)Math.Pow(2.0f, exp - 25);
|
||||
if (exp == 0) {
|
||||
if (exp == 0)
|
||||
{
|
||||
f = f * (float)Math.Pow(2.0f, -13); // 5.96046447754e-8;
|
||||
}
|
||||
//Debug.Log($"{f}");
|
||||
return sgn ? -f : f;
|
||||
}
|
||||
|
||||
public static uint SingleToInt32Bits(float value) {
|
||||
public static uint SingleToInt32Bits(float value)
|
||||
{
|
||||
byte[] bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
Array.Reverse(bytes); // If the system is little-endian, reverse the byte order
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public ushort F32ToF16__(float f) {
|
||||
public ushort F32ToF16__(float f)
|
||||
{
|
||||
uint t = BitConverter.ToUInt32(BitConverter.GetBytes(f), 0);
|
||||
ushort man = (ushort)((t & 0x007FFFFF) >> 12);
|
||||
int exp = (int)((t & 0x7F800000) >> 23);
|
||||
bool sgn = (t & 0x80000000) != 0;
|
||||
|
||||
// handle 0
|
||||
if ((t & 0x7FFFFFFF) == 0) {
|
||||
if ((t & 0x7FFFFFFF) == 0)
|
||||
{
|
||||
return sgn ? (ushort)0x8000 : (ushort)0x0000;
|
||||
}
|
||||
// denormalized float32 does not fit in float16
|
||||
if (exp == 0x00) {
|
||||
if (exp == 0x00)
|
||||
{
|
||||
return sgn ? (ushort)0x8000 : (ushort)0x0000;
|
||||
}
|
||||
// handle infinity & NAN
|
||||
if (exp == 0x00FF) {
|
||||
if (exp == 0x00FF)
|
||||
{
|
||||
if (man != 0)
|
||||
return 0xFE00; // NAN
|
||||
return sgn ? (ushort)0xFC00 : (ushort)0x7C00; // -INF : INF
|
||||
@ -231,11 +245,13 @@ namespace Passer.LinearAlgebra {
|
||||
// normal numbers
|
||||
exp = exp - 127 + 15;
|
||||
// overflow does not fit => INF
|
||||
if (exp > 30) {
|
||||
if (exp > 30)
|
||||
{
|
||||
return sgn ? (ushort)0xFC00 : (ushort)0x7C00; // -INF : INF
|
||||
}
|
||||
// subnormal numbers
|
||||
if (exp < -38) {
|
||||
if (exp < -38)
|
||||
{
|
||||
return sgn ? (ushort)0x8000 : (ushort)0x0000; // -0 or 0 ? just 0 ?
|
||||
}
|
||||
if (exp <= 0) // subnormal
|
||||
@ -260,7 +276,8 @@ namespace Passer.LinearAlgebra {
|
||||
}
|
||||
|
||||
//This function is faulty!!!!
|
||||
ushort f32tof16(float f) {
|
||||
ushort f32tof16(float f)
|
||||
{
|
||||
//uint t = *(uint*)&f;
|
||||
//uint t = (uint)BitConverter.SingleToInt32Bits(f);
|
||||
uint t = SingleToInt32Bits(f);
|
||||
@ -270,15 +287,18 @@ namespace Passer.LinearAlgebra {
|
||||
bool sgn = (t & 0x80000000) != 0;
|
||||
|
||||
// handle 0
|
||||
if ((t & 0x7FFFFFFF) == 0) {
|
||||
if ((t & 0x7FFFFFFF) == 0)
|
||||
{
|
||||
return sgn ? (ushort)0x8000 : (ushort)0x0000;
|
||||
}
|
||||
// denormalized float32 does not fit in float16
|
||||
if (exp == 0x00) {
|
||||
if (exp == 0x00)
|
||||
{
|
||||
return sgn ? (ushort)0x8000 : (ushort)0x0000;
|
||||
}
|
||||
// handle infinity & NAN
|
||||
if (exp == 0x00FF) {
|
||||
if (exp == 0x00FF)
|
||||
{
|
||||
if (man != 0)
|
||||
return 0xFE00; // NAN
|
||||
return sgn ? (ushort)0xFC00 : (ushort)0x7C00; // -INF : INF
|
||||
@ -287,11 +307,13 @@ namespace Passer.LinearAlgebra {
|
||||
// normal numbers
|
||||
exp = (short)(exp - 127 + 15);
|
||||
// overflow does not fit => INF
|
||||
if (exp > 30) {
|
||||
if (exp > 30)
|
||||
{
|
||||
return sgn ? (ushort)0xFC00 : (ushort)0x7C00; // -INF : INF
|
||||
}
|
||||
// subnormal numbers
|
||||
if (exp < -38) {
|
||||
if (exp < -38)
|
||||
{
|
||||
return sgn ? (ushort)0x8000 : (ushort)0x0000; // -0 or 0 ? just 0 ?
|
||||
}
|
||||
if (exp <= 0) // subnormal
|
||||
|
@ -1,12 +1,17 @@
|
||||
#if NUNIT
|
||||
using NUnit.Framework;
|
||||
using Passer.LinearAlgebra;
|
||||
|
||||
namespace LinearAlgebraTest {
|
||||
public class AngleTest {
|
||||
namespace LinearAlgebra.Test
|
||||
{
|
||||
public class Tests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Normalize() {
|
||||
public void Test_Normalize()
|
||||
{
|
||||
float r = 0;
|
||||
|
||||
r = Angle.Normalize(90);
|
||||
@ -38,7 +43,8 @@ namespace LinearAlgebraTest {
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Clamp() {
|
||||
public void Clamp()
|
||||
{
|
||||
float r = 0;
|
||||
|
||||
r = Angle.Clamp(1, 0, 2);
|
||||
@ -63,11 +69,12 @@ namespace LinearAlgebraTest {
|
||||
Assert.AreEqual(r, 1, "Clamp 1 0 INFINITY");
|
||||
|
||||
r = Angle.Clamp(1, float.NegativeInfinity, 1);
|
||||
Assert.AreEqual(r, 1, "Clamp 1 -INFINITY 1");
|
||||
Assert.AreEqual(r, 1, "Clamp 1 -INFINITY 1");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Difference() {
|
||||
public void Difference()
|
||||
{
|
||||
float r = 0;
|
||||
|
||||
r = Angle.Difference(0, 90);
|
||||
@ -105,7 +112,8 @@ namespace LinearAlgebraTest {
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MoveTowards() {
|
||||
public void MoveTowards()
|
||||
{
|
||||
float r = 0;
|
||||
|
||||
r = Angle.MoveTowards(0, 90, 30);
|
||||
@ -159,4 +167,3 @@ namespace LinearAlgebraTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user